Access Pod through Service - 5 minutes a day to play with Docker container technology (136)

This section begins to learn about Services.
Instead of expecting Kubernetes Pods to be robust, we should assume that containers in a Pod are likely to fail and die for various reasons. Controllers such as Deployment will ensure the overall robustness of the application by dynamically creating and destroying Pods. In other words, Pods are fragile, but applications are robust.

Each Pod has its own IP address. When the controller replaces the failed Pod with a new Pod, the new Pod is assigned a new IP address. This creates a problem:

If a group of Pods provide external services (such as HTTP), their IPs are likely to change, so how do clients find and access this service?

The solution given by Kubernetes is Service.

Create Service

Kubernetes Service logically represents a group of Pods, and which Pods are selected by label. Service has its own IP, and this IP is unchanged. The client only needs to access the IP of the Service, and Kubernetes is responsible for establishing and maintaining the mapping relationship between the Service and the Pod. No matter how the backend Pod changes, it will not have any effect on the client because the Service has not changed.

As an example, create the following Deployment:

We start three Pods, run the httpd image, and the label is  run: httpdthe label that the Service will use to pick the Pod.

Pods are assigned their own IPs that can only be accessed by containers and nodes in the Kubernetes Cluster.

Next, create a Service, and its configuration file is as follows:

①It  v1 is for Service  apiVersion.

② Indicates the type of the current resource is  Service.

③ The name of the Service is  httpd-svc.

④Specify  selector to select those Pods with the label as  run: httpd the backend of the Service.

⑤ Map the 8080 port of the Service to the 80 port of the Pod, and use the TCP protocol.

Execute  kubectl apply Create Service  httpd-svc.

httpd-svc Assigned to a CLUSTER-IP  10.99.229.179. The httpd pod on the backend can be accessed through this IP.

According to the previous port mapping, we need to use port 8080 here. In addition, in addition to the one we created  httpd-svc, there is also a Service  kubernetes, through which the Cluster accesses the kubernetes API Server internally.

Through  kubectl describe can view  httpd-svc the corresponding relationship with Pod.

Endpoints The IPs and ports of the three Pods are listed. We know that the IP of the Pod is configured in the container, so where is the Cluster IP of the Service configured? How does CLUSTER-IP map to Pod IP?

The answer is iptables, which we discuss in the next section.

Books:
1. "Play with Docker Container Technology in 5 Minutes a Day"
https://item.jd.com/16936307278.html

2. "Fun with OpenStack for 5 minutes a day"
https://item.jd.com/12086376.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325094347&siteId=291194637