環境
- kubernetes 1.20.4
- Spring Boot 2.5.0-M3
目標
pod-template-hash 標簽是 deploy 自動給它生成 rs 和 pod 加上的。
標簽的值是 rs 名稱的后綴,一是讓 rs 生成不重復,而是可以唯一識別 rs。
deploy 可以生成多個 rs,其中這個標簽的值也會跟着變化。
不要修改此標簽,否則 deploy 不能正常管理 rs 和 pod。
示例
Deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: busybox
spec:
selector:
matchLabels:
app: busybox
replicas: 4
template:
metadata:
labels:
app: busybox
spec:
terminationGracePeriodSeconds: 5
containers:
- name: busybox
image: busybox:1.30.0
command: ["/bin/sh", "-c", "sleep 3600"]
查看 rs
[root@master ~]# kubectl describe rs
Name: busybox-6bd65c67cf
Namespace: default
Selector: app=busybox,pod-template-hash=6bd65c67cf
Labels: app=busybox
pod-template-hash=6bd65c67cf
Annotations: deployment.kubernetes.io/desired-replicas: 4
deployment.kubernetes.io/max-replicas: 5
deployment.kubernetes.io/revision: 1
Controlled By: Deployment/busybox
Replicas: 4 current / 4 desired
Pods Status: 4 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: app=busybox
pod-template-hash=6bd65c67cf
Containers:
busybox:
Image: busybox:1.30.0
Port: <none>
Host Port: <none>
Command:
/bin/sh
-c
sleep 3600
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 7m33s replicaset-controller Created pod: busybox-6bd65c67cf-9hz9g
Normal SuccessfulCreate 7m33s replicaset-controller Created pod: busybox-6bd65c67cf-frbcn
Normal SuccessfulCreate 7m33s replicaset-controller Created pod: busybox-6bd65c67cf-98xb7
Normal SuccessfulCreate 7m33s replicaset-controller Created pod: busybox-6bd65c67cf-6xphf
pod-template-hash 標簽的值和 rs 名稱后綴相同。
查看 pod
[root@master ~]# kubectl describe pod busybox-6bd65c67cf-6xphf
Name: busybox-6bd65c67cf-6xphf
Namespace: default
Priority: 0
Node: node1/192.168.56.102
Start Time: Tue, 30 Mar 2021 22:05:36 +0800
Labels: app=busybox
pod-template-hash=6bd65c67cf
Annotations: <none>
Status: Running
IP: 10.244.1.248
IPs:
IP: 10.244.1.248
Controlled By: ReplicaSet/busybox-6bd65c67cf
Containers:
busybox:
...
具有和 rs 一樣的 pod-template-hash 標簽。
總結
介紹了 pod-template-hash 標簽的作用。