K8S实践Ⅲ(Pod控制器)
作者:互联网
一、Deployment
Deployment的主要功能就是自动部署一个容器应用的多份副本,以及持续监控副本的数量,在集群内始终维持用户指定的副本数量
1.配置参数
Selector(选择器):
.spec.selector是可选字段,用来指定 label selector ,圈定Deployment管理的pod范围。如果被指定, .spec.selector 必须匹配 .spec.template.metadata.labels,否则它将被API拒绝。如果 .spec.selector 没有被指定, .spec.selector.matchLabels 默认是.spec.template.metadata.labels。
2.Deployment配置实例
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 58s
# kubectl get pods
NAME READY STATUS RESTARTS AGE
liveness-http 0/1 CrashLoopBackOff 473 28h
nginx-deployment-6dd86d77d-6hk7w 1/1 Running 0 71s
nginx-deployment-6dd86d77d-mxr8l 1/1 Running 0 71s
nginx-deployment-6dd86d77d-xxhpd 1/1 Running 0 71s
# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-deployment-6dd86d77d-6hk7w 1/1 Running 0 2m5s app=nginx,pod-template-hash=6dd86d77d
nginx-deployment-6dd86d77d-mxr8l 1/1 Running 0 2m5s app=nginx,pod-template-hash=6dd86d77d
nginx-deployment-6dd86d77d-xxhpd 1/1 Running 0 2m5s app=nginx,pod-template-hash=6dd86d77d
当 Deployment 创建或者接管 ReplicaSet 时,Deployment controller 会自动为 Pod 添加 pod-template-hash label。这样做的目的是防止 Deployment 的子ReplicaSet 的 pod 名字重复
3.Deployment更新升级
现将nginx镜像更新为1.9.1版本,可以通过kubectl set image命令为Deployment设置新的镜像版本
# kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1
deployment.extensions/nginx-deployment image updated
# kubectl rollout status deployment/nginx-deployment
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
deployment "nginx-deployment" successfully rolled out
# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deployment-6dd86d77d 0 0 0 70m
nginx-deployment-784b7cc96d 3 3 3 21m
# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-784b7cc96d-4575d 1/1 Running 0 2m13s
nginx-deployment-784b7cc96d-c98s8 1/1 Running 0 91s
nginx-deployment-784b7cc96d-r6sm9 1/1 Running 0 2m51s
#此处名称已经更新
查看Deployment详细信息
# kubectl describe deployment/nginx-deployment
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 17m deployment-controller Scaled up replica set nginx-deployment-784b7cc96d to 1
Normal ScalingReplicaSet 17m deployment-controller Scaled down replica set nginx-deployment-6dd86d77d to 2
Normal ScalingReplicaSet 17m deployment-controller Scaled up replica set nginx-deployment-784b7cc96d to 2
Normal ScalingReplicaSet 16m deployment-controller Scaled down replica set nginx-deployment-6dd86d77d to 1
Normal ScalingReplicaSet 16m deployment-controller Scaled up replica set nginx-deployment-784b7cc96d to 3
Normal ScalingReplicaSet 16m deployment-controller Scaled down replica set nginx-deployment-6dd86d77d to 0
另一种更新方法是使用kubectl edit命令修改 Deployment配置:
#kubectl edit deployment/nginx-deployment
4.Deployment版本回滚
查看deployment升级记录
# kubectl rollout history deployment/nginx-deployment
deployment.extensions/nginx-deployment
REVISION CHANGE-CAUSE
1 <none>
2 <none>
- 默认情况下,kubernetes 会在系统中保存前两次 Deployment 的 rollout 历史记录,可以修改revision history limit来更改保存的revision数。
- 这里在创建Deployment时没有增加--record参数,所以并不能看到revision的变化。在创建 Deployment 的时候加上--record参数就可以看到每个版本使用的命令。
- Deployment的更新操作是在进行rollout时触发的,这意味着当且仅当 Deployment 的 Pod template(如.spec.template)被更改时才会创建新的revision,其他的更新操作(增加副本数)将不会触发Deployment的更新操作,这意味着当回滚到之前的版本时,只有 Deployment 中的 Pod template 部分才会回退。
查看特点版本的详细信息
# kubectl rollout history deployment/nginx-deployment --revision=2
回滚至上一个版本
# kubectl rollout undo deployment/nginx-deployment
deployment.extensions/nginx-deployment rolled back
也可以使用--to-revision参数指定某个历史版本
# kubectl rollout undo deployment/nginx-deployment --to-revision=2
deployment.extensions/nginx-deployment rolled back
5.Deployment暂停和恢复
标签:kubectl,控制器,6dd86d77d,deployment,nginx,Deployment,rollout,Pod,K8S 来源: https://blog.51cto.com/lullaby/2406041