K8S deploy stateful application through Statefulset

Keywords: Linux Nginx curl network DNS

1, Understanding statefuse

1. Statefuse ensures that pod s retain their identity (container name) and state after rescheduling.
2. Statefuse achieves that each pod corresponds to the corresponding PV volume, and each pod can support a set of independent data volumes.
3. Provide a stable network identity. Each pod created by a Statefulset has a zero based sequential index, which is reflected in the name and host name of the pod, as well as the fixed storage corresponding to the pod.
4. Let the pod have a predictable name and host name. Sometimes a stateful pod needs to be located by its host name, while a stateless pod does not. For the above reasons, a Statefulset usually requires you to create a headless Service to record each pod's network tag. Through this Service, each pod will have an independent DNS record. For example, a control Service belonging to the default namespace, named foo, has a pod name of A-0, Then you can access it through the full domain name: A-0 foo.default.svc . cluster.local .

2, Share mount permissions through NFS device

[root@test-operator nfs-volume]# pwd
/data/nfs-volume
[root@test-operator nfs-volume]# ll
//Total dosage 0
drwxr-xr-x 2 root root 24 5 20 / 22:49 web0
drwxr-xr-x 2 root root 24 5 20 / 22:49 web1
[root@test-operator nfs-volume]# cat /etc/exports
/data/nfs-volume/web0 10.3.153.0/24(rw,no_root_squash)
/data/nfs-volume/web1 10.3.153.0/24(rw,no_root_squash)
[root@test-operator nfs-volume]# cat web0/index.html
This is Web0!!!!!!!!!
[root@test-operator nfs-volume]# cat web1/index.html 
This is Web1!!!!!!!

3, Create two PV volumes from the NFS shared directory

#Web0 PV volume
[root@test-nodes1 statefulset]# vi web0-pv.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-web0
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    path: /data/nfs-volume/web0
    server: test-operator.cedarhd.com

#Web1 PV volume
[root@test-nodes1 statefulset]# vi web1-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-web1
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    path: /data/nfs-volume/web1
    server: test-operator.cedarhd.com

3, Create headless Service service

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None       #Must be None
  selector:
    app: nginx

4, Create Statefulset

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  serviceName: "nginx"
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: www
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi

5, Validate creation results

1. Check the binding of PV and PVC
[root@test-nodes1 statefulset]# kubectl get pv
NAME      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM               STORAGECLASS   REASON   AGE
pv-web0   1Gi        RWO            Recycle          Bound    default/www-web-0                           39m
pv-web1   1Gi        RWO            Recycle          Bound    default/www-web-1                           39m
 Note: pv-web0 binds the corresponding declaration volume: www-web-0 (default table space)
2,see pod Operation and maintenance
[root@test-nodes1 statefulset]# kubectl get pod -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP           NODE                      NOMINATED NODE   READINESS GATES
web-0   1/1     Running   0          43m   172.7.22.8   test-nodes2.cedarhd.com   <none>           <none>
web-1   1/1     Running   0          40m   172.7.21.8   test-nodes1.cedarhd.com   <none>           <none>
3,Verify two pod The relative mount of
[root@test-nodes1 statefulset]# curl 172.7.22.8
This is Web0!!!!!!!!!
[root@test-nodes1 statefulset]# curl 172.7.21.8
This is Web1!!!!!!!
4,see headless Service
[root@test-nodes1 statefulset]# kubectl get service -o wide
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE   SELECTOR
kubernetes   ClusterIP   192.168.0.1   <none>        443/TCP   54d   <none>
nginx        ClusterIP   None          <none>        80/TCP    47m   app=nginx
5,verification POD Between DNS Explain whether it is normal
[root@test-nodes1 statefulset]# kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
web-0   1/1     Running   0          48m
web-1   1/1     Running   0          45m
[root@test-nodes1 statefulset]# kubectl exec -ti web-0 /bin/sh
/ # ping web-1.nginx.default
PING web-1.nginx.default (172.7.21.8): 56 data bytes
64 bytes from 172.7.21.8: seq=0 ttl=62 time=0.289 ms
64 bytes from 172.7.21.8: seq=1 ttl=62 time=0.275 ms

//Note: visit the domain name web-1 completely nginx.default.svc . cluster.local
6,see statefulset information
[root@test-nodes1 statefulset]# kubectl get statefulset web -o wide
NAME   READY   AGE   CONTAINERS   IMAGES
web    2/2     51m   nginx        nginx:alpine
7,Delete one pod,Check whether the status is consistent after recovery
[root@test-nodes1 statefulset]# kubectl get pod -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP           NODE                      NOMINATED NODE   READINESS GATES
web-0   1/1     Running   0          53m   172.7.22.8   test-nodes2.cedarhd.com   <none>           <none>
web-1   1/1     Running   0          50m   172.7.21.8   test-nodes1.cedarhd.com   <none>           <none>
[root@test-nodes1 statefulset]# kubectl delete pod web-1
pod "web-1" deleted
[root@test-nodes1 statefulset]# kubectl get pod -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP           NODE                      NOMINATED NODE   READINESS GATES
web-0   1/1     Running   0          53m   172.7.22.8   test-nodes2.cedarhd.com   <none>           <none>
web-1   1/1     Running   0          5s    172.7.21.8   test-nodes1.cedarhd.com   <none>           <none>
[root@test-nodes1 statefulset]# curl 172.7.21.8
This is Web1!!!!!!!

6, Statefullset reduction and expansion

Available commands:
[root@test-nodes1 ~]# kubectl edit statefulset web
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: apps/v1
kind: StatefulSet
metadata:
  creationTimestamp: "2020-05-21T02:53:49Z"
  generation: 3
  name: web
  namespace: default
  resourceVersion: "13473136"
  selfLink: /apis/apps/v1/namespaces/default/statefulsets/web
  uid: f2336d24-8eea-41c6-8236-b45fba9cebf4
spec:
  podManagementPolicy: OrderedReady
  replicas: 2               #Number of expanding and shrinking copies
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: nginx
  serviceName: nginx
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:

Posted by kh411dz on Thu, 21 May 2020 08:38:01 -0700