Kubernetes 应用版本号
在 Kubernetes 里,版本更新使用的不是 API 对象,而是两个命令:kubectl apply 和 kubectl rollout,当然它们也要搭配部署应用所需要的 Deployment、DaemonSet 等 YAML 文件。
在 Kubernetes 里应用都是以 Pod 的形式运行的,而 Pod 通常又会被 Deployment 等对象来管理,所以应用的“版本更新”实际上更新的是整个 Pod。
Pod 是由 YAML 描述文件来确定的,更准确地说,是 Deployment 等对象里的字段 template。
所以 Kubernetes 就使用了“摘要”功能,用摘要算法计算 template 的 Hash 值作为“版本号”。
#获取ngx的pod
kubectl get pod
#删除其中一个
kubectl delete pod ngx-dep-6796688696-9zwxh
#再获取ngx的pod,查看变化
kubectl get pod
可以看到,Pod 名字里的那串随机数“6796……”是没有变化的,变化的是数字后面的pod编号的随机字符。中间的随机数字就是 Pod 模板的 Hash 值,也就是 Pod 的“版本号”。
如果变动了 Pod YAML 描述,比如把镜像改成 nginx:stable-alpine会生成一个新的应用版本,kubectl apply 后就会重新创建 Pod。
命令操作
#删除pod
kubectl delete -f nginx-deploy.yml
#查看删除结果
kubectl get pod
# 不改yaml,再次生成pod
kubectl apply -f nginx-deploy.yml
#查看生成pod的版本号是否改变
kubectl get pod
#删除pod
kubectl delete -f nginx-deploy.yml
#查看删除结果
kubectl get pod
#编辑pod yaml,修改版本号
vim nginx-deploy.yml
# 重新生成pod
kubectl apply -f nginx-deploy.yml
#查看生成pod的版本号是否改变
kubectl get pod
更改后的yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: ngx-dep
spec:
replicas: 2
selector:
matchLabels:
app: ngx-dep
template:
metadata:
labels:
app: ngx-dep
spec:
volumes:
- name: ngx-conf-vol
configMap:
name: ngx-conf
containers:
- image: nginx:stable-alpine
name: nginx
ports:
- containerPort: 80
volumeMounts:
- mountPath: /etc/nginx/conf.d
name: ngx-conf-vol
~