Preface
Preceding text [from getting started to giving up - Kubernetes] getting started with Kubernetes - expanding and shrinking stateless applications In, we learned how to deploy a stateless application through yaml file and expand and shrink it.
For stateless applications, in case of failure or pod deletion, relevant resources will be released. If we want to keep the resources in the pod or deploy data related stateful applications such as MySQL, it is obviously unreasonable to delete resources. In this paper, we will study the deployment and expansion of stateful applications.
StatefulSet
Statefullset is similar to Deployment in that it is used to manage a set of pods defined based on the same container. The difference is that statefullset maintains a fixed ID for each pod. These pods are created based on the same declaration, but the defects cannot be replaced with each other. Each pod has a fixed ID identity.
Define the desired state in the statefullset object, and then the controller of statefullset will achieve the desired state through various updates.
Stateful sets are valuable for applications that need to meet one or more of the following requirements:
- Stable and unique network identifier.
- Stable, persistent storage.
- Orderly and elegant deployment and scaling.
- Orderly and automatic rolling update.
Establish
# web.yaml apiVersion: v1 kind: Service metadata: name: nginx labels: app: nginx spec: ports: - port: 80 name: web clusterIP: None selector: app: nginx --- 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: registry.cn-hangzhou.aliyuncs.com/larswang/nginx:1.0 ports: - containerPort: 80 name: web volumeMounts: - name: www mountPath: /usr/share/nginx/html imagePullSecrets: - name: registry-secret-aliyun volumeClaimTemplates: - metadata: name: www spec: accessModes: [ "ReadWriteOnce" ] resources: requests: storage: 1Gi
Establish
kubectl apply -f web.yaml
Image permission problem
If you execute the command creation directly, you may not be able to pull down the image due to the warehouse permission.
It can be solved in the following ways.
kubectl create secret docker-registry registry-secret-aliyun --docker-server=registry.cn-hangzhou.aliyuncs.com --docker-username=xxx@xx.com --docker-password=xxxx //Change to your own alicloud image warehouse account password. kubectl describe secret registry-secret-aliyun //View secret
In the yaml file, containers are added at the same level
imagePullSecrets: - name: registry-secret-aliyun
Sequential start
Run the create command again and use the following command on another terminal to view the operation.
//View statefullset kubectl get statefulset web //View pod kubectl get pods -l app=nginx
Each pod will be marked in order and started in order. After web-0 starts, web-1 starts again.
Network identity
Each Pod has a stable hostname based on its sequential index. Use kubectl exec to execute hostname in each Pod.
for i in 0 1; do kubectl exec web-$i -- sh -c 'hostname'; done
Use kubectl run to run a container that provides the nslookup command from the dnsutils package. By performing nslookup on the host name of the Pod, you can check their DNS address inside the cluster.
kubectl run -i --tty --image busybox:1.28 dns-test --restart=Never --rm
PersistentVolumeClaims
Persistent volumes are persistent volumes that belong to resources in the cluster. PVC is a request for these resources, as well as a check of the request for resources
This is the main reason why stateful applications can retain data after pod is deleted.
View pvc used by pod
kubectl get pvc -l app=nginx
By default, the NGINX web server loads the index file located at / usr/share/nginx/html/index.html. The volumeMounts field in StatefulSets spec ensures that the / usr/share/nginx/html folder is supported by a PersistentVolume.
write file
for i in 0 1; do kubectl exec web-$i -- sh -c 'echo $(hostname) > /usr/share/nginx/html/index.html'; done
see file
for i in 0 1; do kubectl exec -it web-$i -- cat /usr/share/nginx/html/index.html; done
Delete pod
kubectl delete pod -l app=nginx
After the pod is deleted, statefullset will automatically pull up
Another terminal can be used to observe pod
View again
for i in 0 1; do kubectl exec -it web-$i -- cat /usr/share/nginx/html/index.html; done
As you can see, after the pod is deleted, a new pod is created again, and the previously written files are not lost.
Expansion and contraction capacity
Capacity expansion
kubectl scale sts web --replicas=5
Shrinkage capacity
kubectl scale sts web --replicas=2
We can see that the expansion is in the order of pod, from small to large.
Shrinkage is in the order of pod, from large to small.
View pvc
View pvc of statefullset after shrink
It is found that pvc is still retained and will not be deleted.
Clean up the site
Clean statefullset
kubectl delete statefulset web
Clean up service
kubectl delete service nginx
Cleaning pvc
k delete pvc www-web-0 k delete pv pvc-d52ff063-937c-4246-864a-c6fab808e2ff
summary
In this paper, we learned how to create a stateful application, mainly using the pod sequential index of stateful set and the persistent storage feature of persistent volume claims.
Key points:
- Each pod has a sequence ID
- When starting & expanding, start from small to large
- When stopping & shrinking, stop from large to small according to the identification
- After the pod node is hung up, the storage resources will not be deleted. The newly created pod with the same name can continue to use the relevant resources
See my GitHub repository for yaml files used in this article AloofJr
More articles
See my blog: https://nc2era.com
written by AloofJr , reprint please indicate the source