Kubernetes Services
Published On: 2020/01/17
Services in kubernetes is a logical unit through which the external or internal applications access the targeted application functionalities.
Analogy
Consider you own a Notebook of XYZ company which has 10 service unit in your state. You know only one of them which is near to your area. One day you went to this service unit to fix something with the notebook but found the unit is closed for 1 week. What do you do now? At this point one of your friends told you that there is a customer service unit nearby and they will send the laptop to a service unit and get it fixed. You give the laptop to the customer service unit and they will send it to a functioning service unit and get it fixed.
In Kubernetes
Kubernetes service (similar to the customer service unit which keeps a record of the functional service units) keeps a record, Endpoint, of its targeted Pods. The client calls the service which in turn forward the result to one of the actual pods to get the request processed.
In the below example we have a Deployment object of nginx server and it is connected to a service nginx.
nginx-server.yaml
#// kubectl run nginx-server --image=nginx apiVersion: apps/v1 kind: Deployment metadata: annotations: deployment.kubernetes.io/revision: "1" creationTimestamp: null generation: 1 labels: tier: frontend name: nginx selfLink: /apis/apps/v1/namespaces/default/deployments/nginx spec: progressDeadlineSeconds: 600 replicas: 1 revisionHistoryLimit: 10 selector: matchLabels: tier: frontend strategy: rollingUpdate: maxSurge: 25% maxUnavailable: 25% type: RollingUpdate template: metadata: creationTimestamp: null labels: tier: frontend spec: containers: - image: nginx imagePullPolicy: Always name: nginx ports: - containerPort: 80 protocol: TCP resources: {} terminationMessagePath: /dev/termination-log terminationMessagePolicy: File dnsPolicy: ClusterFirst restartPolicy: Always
nginx-service.yaml
#// kubectl expose nginx-server --name=nginx-service --port=80 apiVersion: v1 kind: Service metadata: creationTimestamp: null labels: tier: frontend name: nginx selfLink: /api/v1/namespaces/default/services/nginx spec: ports: - port: 80 protocol: TCP targetPort: 80 selector: tier: frontend sessionAffinity: None type: ClusterIP
How to check if the service is working
The access to the service can be checked by passing the name of the service or using the ip address of the pod to the nslookup utility .
- Using the service name
nslookup nginx.default.svc.cluster.local 10.244.0.92
The result is as given below
Server: 10.244.0.92
Address: 10.244.0.92#53
Name: nginx.default.svc.cluster.local
Address: 10.96.50.250
- Using the pod ip address
Find the ip address of the pods in the deployment. Replace the dot in the ip address with dash and pass it to the nslookup.
nslookup 10-244-0-91.nginx.default.svc.cluster.local 10.244.0.92 The result is as given below
Server: 10.244.0.92
Address: 10.244.0.92#53
Name: 10-244-0-91.nginx.default.svc.cluster.local
Address: 10.244.0.91
Conclusion
There are three ways to expose services in kubernetes, in this blog post we have taken the example of ClusterIP to explain the concept of service and access it.