1. Overview
Stateless applications focus more on groups, and any member can be replaced. Stateless applications focus on individuals. The nginx and myapp managed by deployment controller belong to stateless applications, such as mysql, redis, zookeeper and so on. They also have master-slave and sequential.
The stateful set controller can manage stateful applications, but it is also very troublesome to implement. It needs to script the operation and maintenance management process and inject it into stateful set to use. Although some people have done stateful script well on the Internet, it is recommended that we not easily redis, mysql and other stateful applications. Use migration to k8s.
In k8s, statefulset management has the following special effects:
(a) Each Pod is stable and has a unique network identifier; b) Stable and durable storage devices; c) Require orderly and smooth deployment and expansion; d) Require orderly and smooth termination and deletion; E. Orderly rolling updates should first update the slave node and then update the master node.
statefulset consists of three components:
Headless service (headless service, i.e. no name); B) statefulset controller; Volume ClaimTemplate
2. Create StatefulSet Controller
kubectl explain sts cat stateful-demo.yaml apiVersion: v1 kind: Service metadata: name: myapp-svc labels: app: myapp-svc spec: ports: - port: 80 name: web clusterIP: None selector: app: myapp-pod --- apiVersion: apps/v1 kind: StatefulSet metadata: name: myapp spec: serviceName: myapp-svc replicas: 2 selector: matchLabels: app: myapp-pod template: metadata: labels: app: myapp-pod spec: containers: - name: myapp image: ikubernetes/myapp:v1 ports: - containerPort: 80 name: web volumeMounts: - name: myappdata mountPath: /usr/share/nginx/html volumeClaimTemplates: - metadata: name: myappdata spec: accessModes: ["ReadWriteOnce"] #storageClassName: "gluster-dynamic" resources: requests: storage: 5Gi
Volume ClaimTemplates: Store volume application template, define volume for each pod; automatically create pvc for the namespace where the pod resides.
kubectl apply -f stateful-demo.yaml kubectl get pods NAME READY STATUS RESTARTS AGE myapp-0 1/1 Running 0 4m myapp-1 1/1 Running 0 4m # pod and service will be deleted, but pvc will not be deleted, so it can be restored. kubectl delete -f stateful-demo.yaml # Format for parsing pod: pod name. SVC name. namespace name. svc.cluster.local kubectl exec -it myapp-0 -- /bin/sh / # nslookup myapp-0.myapp-svc.default.svc.cluster.local nslookup: can't resolve '(null)': Name does not resolve Name: myapp-0.myapp-svc.default.svc.cluster.local Address 1: 10.244.1.110 myapp-0.myapp-svc.default.svc.cluster.local # Five pod s kubectl scale sts myapp --replicas=5 # patch patching can also be used to expand and shrink kubectl patch sts myapp -p '{"spec":{"replicas":2}}' # Update strategy kubectl explain sts.spec.updateStrategy.rollingUpdate //Suppose there are four pods (pod0, pod1, pod2, pod3). If the partition is set to 5, then it means that the four pods are not updated if the partition is greater than or equal to 5. //If partition is 4, it means that pod updates greater than or equal to 4, that is, POD 3 updates, and no other pod updates. //If partiton is 3, then the pod updates greater than or equal to 3 are POD 2 and POD 3 updates, and no other pod updates. kubectl patch sts myapp -p '{"spec":{"updateStrategy":{"rollingUpdate":{"partition":4}}}}' kubectl describe sts myapp Update Strategy: RollingUpdate Partition: 4 # Upgrade myapp to v2 version kubectl set image sts/myapp myapp=ikubernetes/myapp:v2 kubectl get pods myapp-4 -o yaml containerStatuses: image: ikubernetes/myapp:v2
You can refer to the stateful applications done by others on github: GitHub k8s stateful Set redis | MySQL
Reference Blog: http://blog.itpub.net/28916011/viewspace-2215046/