0%

K8s学习笔记——StatefulSet之拓扑状态

学习极客时间上的《深入剖析Kubernetes》

秉持眼过千遍不如手过一遍的原则。动手实践并记录结果

对应章节:18 | 深入理解StatefulSet(一):拓扑状态

Headless Service

创建service

1
2
3
4
5
6
7
8
9
10
11
12
13
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
1
2
3
$ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx ClusterIP None <none> 80/TCP 129m

创建StatefulSet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
name: web
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ kubectl apply -f statefulset.yaml
statefulset.apps/web created
$ kubectl get pods -w -l app=nginx
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 2s
web-1 0/1 ContainerCreating 0 1s
web-1 1/1 Running 0 2s
$ kubectl get statefulset -o wide
NAME READY AGE CONTAINERS IMAGES
web 2/2 52s nginx nginx
$ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web-0 1/1 Running 0 85s 172.1.1.199 node2 <none> <none>
web-1 1/1 Running 0 81s 172.1.2.180 bqi-k8s-node3 <none> <none>

可以看出,statefulset创建出来的pod,名字是固定的,而不再像deployment中的,pod名字会变化

验证

1
2
3
4
$ kubectl exec web-0 -- sh -c 'hostname'
web-0
$ kubectl exec web-1 -- sh -c 'hostname'
web-1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ kubectl run -i --tty --image busybox:1.28.4 web-test --restart=Never --rm -- /bin/sh
If you don't see a command prompt, try pressing enter.
/ # nslookup web-0.nginx
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name: web-0.nginx
Address 1: 172.1.1.199 web-0.nginx.default.svc.cluster.local
/ # nslookup web-1.nginx
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name: web-1.nginx
Address 1: 172.1.2.180 web-1.nginx.default.svc.cluster.local
/ #

重建pod

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ kubectl delete pod -l app=nginx
pod "web-0" deleted
pod "web-1" deleted
# 快速执行
$ kubectl get pod -w -l app=nginx
NAME READY STATUS RESTARTS AGE
web-0 0/1 ContainerCreating 0 0s
web-0 1/1 Running 0 1s
web-1 0/1 Pending 0 0s
web-1 0/1 Pending 0 0s
web-1 0/1 ContainerCreating 0 0s
web-1 1/1 Running 0 3s
$ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web-0 1/1 Running 0 85s 172.1.1.201 node2 <none> <none>
web-1 1/1 Running 0 81s 172.1.2.181 bqi-k8s-node3 <none> <none>

可以看到,当删除了相关的pod后,kubernetes会以同样的名字重建pod

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ kubectl run -i --tty --image busybox:1.28.4 connect-test --restart=Never --rm -- /bin/sh
If you don't see a command prompt, try pressing enter.
/ # nslookup web-0.nginx
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name: web-0.nginx
Address 1: 172.1.1.201 web-0.nginx.default.svc.cluster.local
/ # nslookup web-1.nginx
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name: web-1.nginx
Address 1: 172.1.2.181 web-1.nginx.default.svc.cluster.local
/ #

重建后,依然可以访问web-0.nginx,但IP地址发生了变化

小结

本节课程主要学习了StatefulSet这个控制器,K8s在按照pod模版进行创建时:

  1. 对pod进行编号并逐一完成创建工作
  2. 当重建发生时,也会按照编号对pod逐一进行操作
  3. 通过headless service,StatefulSet 为每个 Pod 创建了一个固定并且稳定的 DNS 记录