This paper mainly illustrates with examples:
As shown below, a Pod collection that provides web services is composed of two copies of Tomcat containers. The service port number provided by each container is 8080:
apiVersion: apps/v1 kind: Deployment metadata: name: webapp spec: replicas: 2 selector: matchLabels: app: webapp template: metadata: labels: app: webapp spec: containers: - name: webapp image: kubeguide/tomcat-app:v1 ports: - containerPort: 8080
Create this Deployment:
[root@k8s-master ~]# kubectl create -f webapp-deployment.yaml deployment.apps/webapp created
View the IP address of each Pod:
[root@k8s-master ~]# kubectl get pods -l app=webapp -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES webapp-8554f77548-cbs6r 1/1 Running 0 99s 10.244.140.119 k8s-node-2 <none> <none> webapp-8554f77548-mvn89 1/1 Running 0 99s 10.244.109.119 k8s-node-1 <none> <none>
The client application can access the web service through the IP address and port number 8080 of the two pods:
[root@k8s-master ~]# curl 10.244.140.119:8080 <!DOCTYPE html> <html lang="en"> .... [root@k8s-master ~]# curl 10.244.140.119:8080 <!DOCTYPE html> <html lang="en"> .....
However, container applications that provide services are usually distributed and provide services through multiple Pod replicas. While the number of Pod replicas changes dynamically (for example, horizontal capacity reduction is performed), the IP address of a single PO may also change (for example, failure recovery).
In response to the above changes, we can use Servcie to dynamically monitor the changes of Pod copies.
Create a Service:
[root@k8s-master ~]# kubectl expose deployment webapp service/webapp exposed
Viewing the newly created Service, you can see that the system assigns it a virtual IP address (ClusterIP address), and the port number of the Service is copied from containerPort in Pod:
[root@k8s-master ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE webapp ClusterIP 10.1.183.185 <none> 8080/TCP 104s
Next, you can access the Service through its IP address and port number:
[root@k8s-master ~]# curl 10.1.183.185:8080 <!DOCTYPE html> <html lang="en"> .....
The client application's access to the Service address 10.1.183.185:8080 is automatically distributed to one of the two back-end pods: 10.244.140.119:8080 or 10.244.140.119:8080.
Of course, in addition to creating services above, you can also create services using yaml files:
apiVersion: v1 kind: Service metadata: name: webapp spec: selector: app: webapp ports: - protocol: TCP port: 8080 targetPort: 8080
Using the create command, the following phenomena occur:
[root@k8s-master ~]# kubectl create -f webapp-service.yaml Error from server (AlreadyExists): error when creating "webapp-service.yaml": services "webapp" already exists
It means that you have created it successfully, but you have to delete it before you can create it again.
You can also use the following command to check which service s are running:
[root@k8s-master ~]# kubectl get svc -n default NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE webapp ClusterIP 10.1.183.185 <none> 8080/TCP 15m
You can also see that my webapp service has been created successfully. You can use the following command to delete it:
[root@k8s-master ~]# kubectl delete svc webapp -n default service "webapp" deleted
establish:
[root@k8s-master ~]# kubectl create -f webapp-service.yaml service/webapp created
see:
[root@k8s-master ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE webapp ClusterIP 10.1.50.201 <none> 8080/TCP 5m5s
visit:
[root@k8s-master ~]# curl 10.1.50.201:8080 <!DOCTYPE html> <html lang="en"> ....
During the running process of the replica of the pod providing the service, if the pod changes, the service controller of kubernetes will continuously monitor the change of the back-end pod list and update the back-end pod list corresponding to the service in real time.
The backend corresponding to a service consists of the ip of the pod and the port number of the container, that is, a complete IP:port access address, which is called endpoint in the Kubernetes system. By viewing the details of the service, you can see the list of its backend endpoints:
[root@k8s-master ~]# kubectl describe svc webapp Name: webapp Namespace: default Labels: <none> Annotations: <none> Selector: app=webapp Type: ClusterIP IP Family Policy: SingleStack IP Families: IPv4 IP: 10.1.50.201 IPs: 10.1.50.201 Port: <unset> 8080/TCP TargetPort: 8080/TCP Endpoints: 10.244.109.119:8080,10.244.140.119:8080 Session Affinity: None Events: <none>
In fact, you can view the EndPoint object using the following command:
[root@k8s-master ~]# kubectl get endpoints NAME ENDPOINTS AGE webapp 10.244.109.119:8080,10.244.140.119:8080 11m
The content behind is more wonderful.