linux operation and maintenance, architecture -K8s access road through the Service Pod

A visit by the Pod Service

         Each Pod has its own IP address, when the Controller Pod faulty substitution occurs with a new Pod, Pod newly assigned to the new IP address, for example: a group of Pod provides HTTP services, their IP is likely to change , then the client how to find and access the service does, service for us. Service on behalf of a group of logically Pod, which is specific to the selection by the label, Service has its own IP, and the IP is the same, the client only needs to access the Service IP, regardless of the back-end Pod change, the customer Client access will not have any impact, k8s responsible for the establishment and maintenance Service and Pod mappings.

1. Create a Service

① Create a sample file

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: httpd
spec:
  replicas: 3
  template:
    metadata:
      labels:
        App: httpd      #service will use this label to select Pod
    spec:
      containers:
      - name: httpd
        image: httpd
        ports:
        - containerPort: 80

② create Deployment application of a Httpd

[root@k8s-node1 ~]# kubectl apply -f httpd.yaml 
deployment.extensions "httpd" created

[root@k8s-node1 ~]# kubectl get pod -o wide
NAME                    READY     STATUS    RESTARTS   AGE       IP            NODE
httpd-fcdb8b4d8-bzsq8   1/1       Running   0          2m        10.2.72.180   192.168.56.12
httpd-fcdb8b4d8-k5ds9   1/1       Running   0          2m        10.2.72.182   192.168.56.12
httpd-fcdb8b4d8-ks4qq   1/1       Running   0          2m        10.2.72.181   192.168.56.12

FIG Pod assigned the respective IP, the IP can only be accessed and the container k8s cluster nodes

[root@k8s-node2 ~]# curl 10.2.72.180
<html><body><h1>It works!</h1></body></html>

③ Creating Service Files

apiVersion: v1       #service of apiVersion 
kind: Service        # resource type
metadata:
  name: httpd - svc    #service name
spec:
  selector: 
    app: httpd       # label selection, which label is selected app: httpd as a backend Service of the Pod
  ports:
  - Protocol: TCP    # 8080 port mapping Pod Service to port 80, use the TCP protocol 
    Port: 8080 
    TARGETPORT: 80

④ create and view Service

[root@k8s-node1 ~]# kubectl apply -f httpd-svc.yaml 
service "httpd-svc" created

[root@k8s-node1 ~]# kubectl get service
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)    AGE
httpd-svc    ClusterIP   10.1.168.37   <none>        8080/TCP   7s
kubernetes   ClusterIP   10.1.0.1      <none>        443/TCP    8d

httpd-svc assigned to a cluster IP, Pod can be accessed through the rear end of the IP

 

[root@k8s-node2 ~]# curl 10.1.168.37:8080
<html><body><h1>It works!</h1></body></html>

 

⑤ check the mapping of Pod and httpd-svc

[root@k8s-node1 ~]# kubectl describe service httpd-svc 
Name:              httpd-svc
Namespace:         default
Labels:            <none>
Annotations:       kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"httpd-svc","namespace":"default"},"spec":{"ports":[{"port":8080,"protocol":"TC...
Selector:          app=httpd
Type:              ClusterIP
IP:                10.1.168.37
Port:              <unset>  8080/TCP
TargetPort:        80/TCP
Endpoints:         10.2.72.180:80,10.2.72.181:80,10.2.72.182:80
Session Affinity:  None
Events:            <none>

Cluster IP Service is mapped to the Pod by iptables

 

 

Guess you like

Origin www.cnblogs.com/yanxinjiang/p/12030758.html