理解statefulset工作原理
1 - yaml文件
kind: PersistentVolume == 同上一篇,定义PV和PVC
apiVersion: v1
metadata:
name: task-pv-volume
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 2Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/usr/local/"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: task-pv-claim
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
---
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None == 定义一个headless service
selector:
app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
selector:
matchLabels:
app: nginx # has to match .spec.template.metadata.labels
serviceName: "nginx"
replicas: 3 # by default is 1
template:
metadata:
labels:
app: nginx # has to match .spec.selector.matchLabels
spec:
terminationGracePeriodSeconds: 10
containers:
- name: nginx
image: k8s.gcr.io/nginx-slim:0.8
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumes:
- name: www
persistentVolumeClaim:
claimName: task-pv-claim
执行完毕验证效果:
pod名字是statefulset的name + 一个从0开始的数字组成的。
PS C:\e\temp> kubectl get pods
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 3h
web-1 1/1 Running 0 3h
web-2 1/1 Running 0 3h
从下面的结果开一看出,这个service没有clusterIP
PS C:\e\temp> kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 14d
nginx ClusterIP None <none> 80/TCP 3h
登录其中一个pod
apt-get update
apt-get install dnsutils ==安装nslookup
apt-get install iputils-ping ==安装ping
service name = nginx
namespace = default
#nslookup nginx
...
...
Name: nginx.default.svc.cluster.local == $(service name).$(namespace).svc.cluster.local
Address: 10.1.0.201
Name: nginx.default.svc.cluster.local
Address: 10.1.0.203
Name: nginx.default.svc.cluster.local
Address: 10.1.0.204
对于每个pod,是上面DNS的subdomain. $(podname).$(governing service domain)
governing service domain = nginx.default.svc.cluster.local
对于pod-1来说,他的DNS就是web-1 + nginx.default.svc.cluster.local,以此类推。
# ping web-1.nginx.default.svc.cluster.local
PING web-1.nginx.default.svc.cluster.local (10.1.0.204) 56(84) bytes of data.
64 bytes from web-1.nginx.default.svc.cluster.local (10.1.0.204): icmp_seq=1 ttl=64 time=0.113 ms
64 bytes from web-1.nginx.default.svc.cluster.local (10.1.0.204): icmp_seq=2 ttl=64 time=0.079 ms
64 bytes from web-1.nginx.default.svc.cluster.local (10.1.0.204): icmp_seq=3 ttl=64 time=0.128 ms
# ping web-2.nginx.default.svc.cluster.local
PING web-2.nginx.default.svc.cluster.local (10.1.0.203) 56(84) bytes of data.
64 bytes from web-2.nginx.default.svc.cluster.local (10.1.0.203): icmp_seq=1 ttl=64 time=0.170 ms
64 bytes from web-2.nginx.default.svc.cluster.local (10.1.0.203): icmp_seq=2 ttl=64 time=0.080 ms
https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/