0%

K8s学习笔记——控制器模型

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

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

对应章节:16 | 编排其实很简单:谈谈“控制器”模型

Deployment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-dp
spec:
selector:
matchLabels:
app: nginx
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: Never
ports:
- containerPort: 80
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-dp 2/2 2 2 75s
$ kubectl describe deploy nginx-dp
Name: nginx-dp
Namespace: default
CreationTimestamp: Mon, 06 Jul 2020 15:56:10 +0800
Labels: <none>
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
...
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-dp-67f857c57f-kgmsq 1/1 Running 0 100s
nginx-dp-67f857c57f-qx7l7 1/1 Running 0 100s

这个 Deployment 定义的编排动作:确保携带了 app=nginx 标签的 Pod 的个数,永远等于 spec.replicas 指定的个数,即 2 个。

现在,可以手动删除一个pod看看

1
2
3
4
5
6
$ kubectl delete pod nginx-dp-67f857c57f-kgmsq
pod "nginx-dp-67f857c57f-kgmsq" deleted
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-dp-67f857c57f-nmrhh 1/1 Running 0 7s
nginx-dp-67f857c57f-qx7l7 1/1 Running 0 3m8s

可以看到,删除了nginx-dp-67f857c57f-kgmsq的pod,控制器自动拉起一个新的nginx-dp-67f857c57f-nmrhh的pod

扩展一下

如果我手工拉起一个匹配了label的pod会怎样呢

1
2
3
4
5
6
7
8
9
10
11
12
13
apiVersion: v1
kind: Pod
metadata:
name: new-nginx-pod
labels:
app: nginx
spec:
containers:
- name: new-nginx-contrainer
image: nginx
imagePullPolicy: Never
ports:
- containerPort: 80
1
2
3
4
5
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
new-nginx-pod 1/1 Running 0 7m20s
nginx-dp-67f857c57f-nmrhh 1/1 Running 0 14m
nginx-dp-67f857c57f-qx7l7 1/1 Running 0 17m

发现并没有因此而减少一个pod。那么手工删除一个

1
2
3
4
5
6
7
$ kubectl delete pod nginx-dp-67f857c57f-nmrhh
pod "nginx-dp-67f857c57f-nmrhh" deleted
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
new-nginx-pod 1/1 Running 0 7m50s
nginx-dp-67f857c57f-4wptz 1/1 Running 0 5s
nginx-dp-67f857c57f-qx7l7 1/1 Running 0 17m

依然还是创建了一个新的,为什么呢?

1
2
3
4
5
6
7
8
$ kubectl describe pod nginx-dp-67f857c57f-qx7l7
Name: nginx-dp-67f857c57f-qx7l7
Namespace: default
Priority: 0
Node: node2/10.160.18.181
Start Time: Mon, 06 Jul 2020 15:56:10 +0800
Labels: app=nginx
pod-template-hash=67f857c57f

可以看到,Labels中,k8s自动创建了一个pod-template-hash=67f857c57f,来区别于手工创建出来的pod

那如果在自定义的pod中也指定了呢?不妨试试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
apiVersion: v1
kind: Pod
metadata:
name: new-nginx-pod
labels:
app: nginx
pod-template-hash: 67f857c57f
spec:
containers:
- name: new-nginx-contrainer
image: nginx
imagePullPolicy: Never
ports:
- containerPort: 80
1
2
3
4
5
6
7
8
9
10
11
12
13
$ kubectl apply -f new-pod.yaml
pod/new-nginx-pod created
# 以极快的速度查看pod
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
new-nginx-pod 0/1 Terminating 0 1s
nginx-dp-67f857c57f-4wptz 1/1 Running 0 6m25s
nginx-dp-67f857c57f-qx7l7 1/1 Running 0 24m
# 稍等一下再试
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-dp-67f857c57f-4wptz 1/1 Running 0 7m34s
nginx-dp-67f857c57f-qx7l7 1/1 Running 0 25m

可以看到,这个new-nginx-pod一创建出来,就进入了Terminating的状态,再次查看时,已经不存在了。

小结

本章节主要学习了Deployment的基础用法。可以看到,Deployment这种控制器,对携带指定标签的Pod,会维持一个指定的份数。

而且也注意到,为了有别于非deployment创建出来的pod,会为pod添加一个pod-template-hash的标签。