Kubernetes provides a feature called service discovery. It gives containers their own IP addresses and a single DNS name and can load-balance across them.
Services are responsible for communication between pods. You can see in architecture where serviceIP is actually a key component for the service discovery. It consists of DNS mapping with Pod IP(iptables).
Example of Kubernetes services:
Below is an example of the services. This example is for mysql deployment.
apiVersion: v1 kind: Service metadata: name: mysqlauth-service namespace: production-cloud labels: label: mysqlauth-service spec: type: NodePort ports: - port: 3306 nodePort: 32001 targetPort: 3306 protocol: TCP name: http selector: name: mysqlauth-selector
mysql-deployment for this service:
apiVersion: extensions/v1beta1 kind: Deployment metadata: labels: service: mysqlauth-service #Name of service name: mysqlauth-deployment #Name of deployment namespace: production-cloud spec: template: metadata: labels: name: mysqlauth-selector #Name of selector spec: containers: - env: - name: MYSQL_ROOT_PASSWORD value: admin - name: MYSQL_DATABASE value: admin image: mysql:latest name: mysqlauth-container ports: - name: mysqlauth-port containerPort: 3306 protocol: TCP resources: limits: cpu: 500m memory: 256Mi requests: cpu: 250m memory: 128Mi
You can see in deployment labels
Which actually connects deployment with mysql service. Selectors are used to establish this relationship.
selector: name: mysqlauth-selector
Now we have a good understanding of the services. To communicate between pods using internal domain name system of Kubernetes:
Lets use above mysql in python app:
apiVersion: extensions/v1beta1 kind: Deployment metadata: labels: service: authenticate-service # Name of the service name: authenticate-deployment #Deployment Name namespace: production-cloud #Namespace Name spec: template: metadata: labels: name: authenticate-selector #Name of selector spec: containers: - command: ["/bin/sh","-c"] args: ["python run.py"] env: - name: DB_HOST value: "mysqlauth-service.production-cloud.svc.cluster.local:3306" - name: DB_USER value: admin - name: DB_PASS value: admin image: jakirpatel/authenticate name: authservice-container ports: - name: auth-port containerPort: 5010 protocol: TCP resources: limits: cpu: 500m memory: 256Mi requests: cpu: 250m memory: 128Mi workingDir: /build/authenticate_service
We established connection between auth and mysql deployments using environment variable.
env: - name: DB_HOST value: "mysqlauth-service.production-cloud.svc.cluster.local:3306"
Pods are mortal. A Kubernetes
Service is an abstraction which defines a logical set of
Pods and a policy by which to access them; sometimes called a micro-service. The set of
Pods targeted by a
Service is (usually) determined by a
Pod can access other Pods using fully classified DNS like:
It consists structure like servicename.namespace.svc.cluster.local. Using environment variables are a good choice if microservice architecture is followed.
This is the standard way to communicate between the pods. K8S namespaces also plays the important role in terms of the communication. In Microservice architecture, it is possible to pass the environment variable to containerized image of the service. It is possible to pass the environment variable from K8S manifests. Check out our other blogs to know more about containers and K8S.