The above first

In the Kubernetes cluster, the service selects the corresponding pod through the tag selector, and then forwards the request

Pod, EndPoints, and Service relationships

1, for example, look at the deployment file first, mainly the comments section. Because the file is too long, this article removes some fields from the YAML file

---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: gogs
  labels:       # define the label of StatefulSet to provide the label for the following Service selector
    app: gogs
spec:
  serviceName: gogs
  replicas: 1
  selector:
    matchLabels:  # tag selector, with which StatefulSet selects pod
      app: gogs
  template:
    metadata:
      labels:    # define the pod tag
        app: gogs
    spec:
      terminationGracePeriodSeconds: 180
.
---
apiVersion: v1
kind: Service
metadata:
  name: gogs
  labels:
    app: gogs
spec:
  type: NodePort
  ports:
  - port: 3000
    targetPort: 3000
  selector: 	 The tag matcher, through which the Service matches the corresponding Pod
    app: gogs
Copy the code

2. View the running result

[root@k8s-node001 ~]# kubectl get po,ep,svcNAME READY STATUS RESTARTS AGE pod/ GOGs-0 1/1 Running 0 3D1H NAME ENDPOINTS AGE ENDPOINTS/GOgs 100.68.150.1970:3000 3D1H NAME TYPE cluster-ip external-ip PORT(S) AGE service/gogs NodePort 10.106.102.74 < None > 300:30526 /TCP 3D1HCopy the code

3, Use Kubectl describe to view pod, endpoints, service details4. The correlation information can be seen from the figure above:

1. The IP address of the Pod is the same as that of the Endpoint, which maps the Pod that can provide services externally. If the pod is not running, no Endpoint will be generated. 2. The relationship between StatefulSet and pod is associated with label-selector LoadBalancer, which provides load balancing for multiple pods. 4. A Service matches its load balancing Pod with the tag App: gogsCopy the code

Kubernetes supports pod, EndPoints, and Service types

ClusterIP

The service is exposed by the internal IP of the cluster. By choosing this value, the service can only be accessed within the cluster, which is also the default ServiceType disadvantage: the service can only be accessed within the cluster

apiVersion: v1
kind: Service
metadata:
  name: gogs-clusterip
spec:
  type: ClusterIP
  ports:
  - port: 3000
    targetPort: 3000
  selector:
    app: gogs
Copy the code

The results of

NAME             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
gogs-clusterip   ClusterIP   10.104.1.9      <none>        3000/TCP         2m11s
Copy the code

As you can see, 10.104.1.9 is an IP address inside the cluster and is not accessible outside the cluster

NodePort

If the type field is set to NodePort, Kubernetes will allocate ports in the specified range (default: 3000-32767). Each node proxies randomly assigned ports into the service. For chestnuts

apiVersion: v1
kind: Service
metadata:
  name: gogs
spec:
  type: NodePort
  ports:
  - port: 3000
    targetPort: 3000
  selector:
    app: gogs
Copy the code

The results of

NAME TYPE cluster-ip external-ip PORT(S) AGE gogs NodePort 10.106.102.74 < None > 300:30526 /TCP 3D2HCopy the code

As you can see here, port 30526 was randomly assigned and the service can now be accessed via IP+30526 of any node in the cluster

Ingress

Ingress exposes HTTP and HTTPS routes from outside the cluster to services within the cluster. Traffic routing is controlled by rules defined on the Ingress resource. Ingress can be configured for services to provide externally accessible urls, load balancing traffic, terminating SSL/TLS, and providing name-based virtual hosting capabilities. The Ingress controller is typically responsible for implementing the Ingress through a load balancer, although it can also configure an edge router or other front end to help handle the traffic. chestnuts

---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: gogs-gogs
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
  - host: gogs.test.cn
    http:
      paths:
      - path: /
        backend:
          serviceName: gogs
          servicePort: 3000
Copy the code

The results of

NAME CLASS HOSTS ADDRESS PORTS AGE gogs-gogs < None > gogs.test.cn 10.26.25.21 80 3D19hCopy the code

Gogs is now accessible via the domain name gogs.test.cn. Note that gogs.test.cn needs to add its own DNS resolution to the IP address of the machine where the ingress resides

LoadBalancer

When creating a service, you can choose to automatically create a cloud network load balancer. This provides an externally accessible IP address to assign traffic to the correct port on the cluster node. LoadBalancer is generally provided by a public cloud vendor and does not need to be built by itself.