What is kubernetes Service object?
A Service in Kubernetes is an abstraction which defines a logical set of Pods and a policy by which to access them. Services enable a loose coupling between dependent Pods. A Service is defined using YAML like all Kubernetes objects. The set of Pods targeted by a Service is usually determined by a LabelSelector. (We will see example in below section).we can expose Services in different ways by specifying a type in the Service Specification section.Let's discuss about them in short.
ClusterIP -ClusterIP is used to Exposes the Service on an internal IP in the cluster. This type makes the Service only reachable from within the cluster.Dependent applications can interact with other applications internally using the ClusterIP service.
How to create ClusterIP?
ClusterIP can be created using by specifying a type in
specs while creating Service object.
Using yaml definition file Example
Step-1 Create deploy-service.yaml using vim or any IDE and provide below definition.
Command to create
deploy-service yaml file using vim
apiVersion: v1 kind: Service metadata: name: spring-service spec: type: ClusterIP selector: app: spring-app ports: - protocol: TCP port: 8080 targetPort: 8080
kubectl apply command to create service.
kubeclt apply -f deploy-service.yaml
Service also can be created using
kubectl create command. let's see how we can create.
kubectl create command
kubectl create service clusterip spring-service --tcp=8081:8081
when you run this command it will create spring-service named service type spring-service which will have
As of now, we have discussed how to create ClusterIP type service. Now, we will discuss how to expose our application which is deployed using deployment in the last blog.
Here I, will use
deploy-service.yaml file to create definition for
--- in yaml is used to separate the documents.
apiVersion: v1 kind: Service metadata: creationTimestamp: null labels: app: spring-service name: spring-service spec: ports: - name: spring-service-port port: 8081 protocol: TCP targetPort: 8081 selector: app: spring-service type: ClusterIP --- apiVersion: apps/v1 kind: Deployment metadata: name: spring-deployment spec: replicas: 3 selector: matchLabels: app: spring-service template: metadata: labels: app: spring-service spec: containers: - name: demo-springapp image: jeewangautam02261995/springbootapp:v2
Let's apply this file using
kubectl apply command.
kubectl apply -f deploy-service.yaml
We can see, deployment is created and exposed with service object type
You can see the deployment and service using below command.
kubectl get deployment -o wide #to view the deployment kubectl get svc -o wide # to view the service
Let's access our application from one the pods using ClusterIP
10.96.89.35.To do so, we need to navigate to terminal of container inside the pod using below command first. First, let get pods.
kubectl get pods -o wide
Let, navigate to container's terminal of
kubectl exec -it spring-deployment-57f5dbb74d-t82v2 -- bash
Now, we are in terminal, run below curl request to access the application using ClusterIP and port.
you can see, we successfully access our application using ClusterIP and port. There are various ways to access our application. let's see each of them shortly.
Using service name and port
Here, we Access our application using service name and port number associated with it only because our application is deploy in default name space. let's see how to access application if this is deployed in different namespace in the same cluster. To do so we need to use DNS query.
curl <service name>.<namespace>:<portno>
curl spring-service.default:8081 #application is deployed in default namespace.
As of now, we deployed application using deployment and exposed to ClusterIP type service object and accessed from inside the cluster. But this application is not accessible from outside of the cluster. How we can make application accessible to external user? we will discuss this approach in next blog.