statefulSet 为每一个pod提供一个稳定的、唯一的网络标识符,每个 Pod 通过 PersistentVolumeClaim (PVC) 获得自己的存储。即使 Pod 被重新调度到另一个节点,这个 PVC 也会被重新挂载到正确的 Pod 上,保证存储的稳定性。
statefulset创建的pod是有序的,比如statefulset叫web,那么创建的两个pods叫做web_0 web_1. web_0删掉以后,新创建的还叫web_0
新创建的statefulset必须要标记serviceName,这个name将是pod的名称前缀
[root@master stateful]# cat statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:name: web
spec:selector: matchLabels:app: nginxserviceName: "nginx"replicas: 2template:metadata:labels:app: nginxspec:containers:- name: nginximage: nginximagePullPolicy: IfNotPresentports:- containerPort: 80name: web
---
apiVersion: v1
kind: Service
metadata: name: nginxlabels:app: nginx
spec:ports:- port: 80name: webclusterIP: Noneselector:app: nginx
注意上面的service没有cluster IP
[root@master stateful]# kubectl get pods -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nfs-provisioner-5654cf4975-sqfpv 1/1 Running 0 166m 10.244.166.129 node1 <none> <none>
web-0 1/1 Running 0 5m43s 10.244.166.141 node1 <none> <none>
web-1 1/1 Running 0 5m41s 10.244.104.37 node2 <none> <none>
[root@master stateful]# kubectl delete pods web-1
pod "web-1" deleted
[root@master stateful]# kubectl get pods -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nfs-provisioner-5654cf4975-sqfpv 1/1 Running 0 167m 10.244.166.129 node1 <none> <none>
web-0 1/1 Running 0 6m16s 10.244.166.141 node1 <none> <none>
web-1 0/1 ContainerCreating 0 2s <none> node2 <none> <none>
可以看到删除web_1后,会重新创建一个新的(ip不一样)
然后通过nslookup解析服务nginx,由于没有clusterIP,会直接解析到两个pod的地址
直接解析web_0的名称,也可以直接解析到pod地址
[root@master storage_test]# kubectl run busybox --image=docker.io/library/busybox:1.28 --image-pull-policy=IfNotPresent --restart=Never --rm -it If you don't see a command prompt, try pressing enter./ # nslookup nginx
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.localName: nginx
Address 1: 10.244.104.38 web-1.nginx.default.svc.cluster.local
Address 2: 10.244.166.141 web-0.nginx.default.svc.cluster.local
/ # nslookup web-0.nginx.default.svc.cluster.local
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.localName: web-0.nginx.default.svc.cluster.local
Address 1: 10.244.166.141 web-0.nginx.default.svc.cluster.local
/ # exit