创建deployment
[root@master deploy]# kubectl create deploy nginx-deploy --image=nginx:1.7.9
deployment.apps/nginx-deploy created
// kubectl create 资源类型 资源名称(deployment的名称) 指定镜像
// 都是在动态的更新pod副本的数量 deployment和replicaSet是嵌套关系,
[root@master deploy]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deploy 1/1 1 1 66s
[root@master deploy]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deploy-68c658776b 1 1 1 75s
[root@master deploy]# kubectl get replicaset
NAME DESIRED CURRENT READY AGE
nginx-deploy-68c658776b 1 1 1 4m5s
[root@master deploy]# kubectl get po
NAME READY STATUS RESTARTS AGE
my-pod 1/1 Running 1 (19m ago) 4d19h
nginx-demo 1/1 Running 2 (19m ago) 4d23h
nginx-deploy-68c658776b-z9rc6 1/1 Running 0 4m48s
nginx-po 1/1 Running 2 (19m ago) 4d23h
[root@master deploy]#
//先是有deployment,然后有replicaSet,然后有pod
//vim :set paste 粘贴模式
[root@master deploy]# vim nginx-deploy.yaml
piVersion: apps/v1 #deployment api 版本
kind: Deployment # 资源类型为deployment
metadata: # 元数据
labels: # 标签
app: nginx-deploy # 具体的 k:y配置形式
name: nginx-deploy # deployment的名字
namespace: default # 所在的命名空间
spec:
replicas: 1 # 期望的副本数
revisionHistoryLimit: 10 # 进行滚动更新后,保留的历史版本数
selector: # 选择器,用于找到匹配的rs
matchLabels: # 按照标签匹配
app: nginx-deploy # 匹配的标签k:y
strategy: # 更新策略
rollingUpdate: # 滚动更新配置
maxSurge: 25% # 进行滚动更新时候,更新的个数最多可以超过预期副本数的个数/比
例
maxUnavailable: 25% #进行滚动更新时,最大不可用更新比例,表述在所有副本数中,最>多可以有多少个不更新成功
type: RollingUpdate # 更新类型,采用滚动更新
template: # pod模版
metadata: # pod原信息
labels: # pod标签
app: nginx-deploy
spec: # pod的期望信息
containers: # pod 的容器
- image: nginx:1.7.9 # 镜像
imagePullPolicy: IfNotPresent # 拉取策略
name: nginx # 容器名称
restartPolicy: Always # 重启策略
terminationGracePeriodSeconds: 30 #删除操作最多宽限多久
滚动更新
[root@master deploy]# kubectl edit deploy nginx-deploy
deployment.apps/nginx-deploy edited
[root@master deploy]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deploy 3/3 3 3 28m
[root@master deploy]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deploy-68c658776b 3 3 3 29m
[root@master deploy]# kubectl get po
NAME READY STATUS RESTARTS AGE
my-pod 1/1 Running 1 (44m ago) 4d19h
nginx-demo 1/1 Running 2 (44m ago) 4d23h
nginx-deploy-68c658776b-hgppq 1/1 Running 0 23s
nginx-deploy-68c658776b-vld7l 1/1 Running 0 23s
nginx-deploy-68c658776b-z9rc6 1/1 Running 0 29m
nginx-po 1/1 Running 2 (44m ago) 4d23h
[root@master deploy]#
//开两个终端
[root@master deploy]# kubectl set image deployment/nginx-deploy nginx=nginx:1.7.9
deployment.apps/nginx-deploy image updated
[root@master deploy]#
[root@master deploy]# kubectl rollout status deplloy nginx-deploy
Waiting for deployment "nginx-deploy" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deploy" rollout to finish: 1 old replicas are pending termination...
deployment "nginx-deploy" successfully rolled out
[root@master deploy]#
# 滚动更新过程
1.创建一个空的rs2
2.在空的rs2中创建pod1,在rs1中关闭原pod1
3. 在rs2中创建pod2,在rs1中关闭原pod2
//整个过程中保持2个pod的存在
# 查看rs可以看到2个rs
[root@master deploy]# kubectl get rs --show-labels
NAME DESIRED CURRENT READY AGE LABELS
nginx-deploy-68c658776b 3 3 3 40m app=nginx-deploy,pod-template-hash=68c658776b
nginx-deploy-7c97fc8644 0 0 0 9m21s app=nginx-deploy,pod-template-hash=7c97fc8644
[root@master deploy]#
回滚
// 当你的更新存在问题,可以回滚
//可以使用如下命令查看更新的历史版本
[root@master deploy]# kubectl rollout history deployment/nginx-deploy
deployment.apps/nginx-deploy
REVISION CHANGE-CAUSE
2 <none>
3 <none>
// 查看历史版本
[root@master deploy]# kubectl rollout history deployment/nginx-deploy --revision=2
deployment.apps/nginx-deploy with revision #2
Pod Template:
Labels: app=nginx-deploy
pod-template-hash=7c97fc8644
Containers:
nginx:
Image: nginx:1.9.1
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
[root@master deploy]# kubectl rollout history deployment/nginx-deploy --revision=3
deployment.apps/nginx-deploy with revision #3
Pod Template:
Labels: app=nginx-deploy
pod-template-hash=68c658776b
Containers:
nginx:
Image: nginx:1.7.9
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
// 会记录修改的值
// 回退操作
[root@master deploy]# kubectl rollout undo deployment/nginx-deploy --to-revision=2
deployment.apps/nginx-deploy rolled back
[root@master deploy]# kubectl rollout status deployment/nginx-deploy
deployment "nginx-deploy" successfully rolled out
[root@master deploy]#
// 回退操作 在.spec.revisionHistoryLimit 中指定deploment保留多少revision,如果设置0,则不允许回滚
扩容和缩容
扩容
[root@master deploy]# kubectl scale --replicas=6 deploy nginx-deploy
deployment.apps/nginx-deploy scaled
[root@master deploy]# kubectl get po
NAME READY STATUS RESTARTS AGE
my-pod 1/1 Running 1 (81m ago) 4d20h
nginx-demo 1/1 Running 2 (81m ago) 5d
nginx-deploy-7c97fc8644-62dpp 1/1 Running 0 13m
nginx-deploy-7c97fc8644-g48zt 1/1 Running 0 10s
nginx-deploy-7c97fc8644-jlhfn 1/1 Running 0 13m
nginx-deploy-7c97fc8644-lqpxj 1/1 Running 0 13m
nginx-deploy-7c97fc8644-v69f4 1/1 Running 0 10s
nginx-deploy-7c97fc8644-zslj4 1/1 Running 0 10s
nginx-po 1/1 Running 2 (81m ago) 5d
[root@master deploy]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deploy-68c658776b 0 0 0 66m
nginx-deploy-7c97fc8644 6 6 6 36m
[root@master deploy]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deploy 6/6 6 6 67m
[root@master deploy]#
缩容
[root@master deploy]# kubectl scale --replicas=3 deploy nginx-deploy
deployment.apps/nginx-deploy scaled
[root@master deploy]# kubectl get po
NAME READY STATUS RESTARTS AGE
my-pod 1/1 Running 1 (83m ago) 4d20h
nginx-demo 1/1 Running 2 (83m ago) 5d
nginx-deploy-7c97fc8644-62dpp 1/1 Running 0 14m
nginx-deploy-7c97fc8644-jlhfn 1/1 Running 0 14m
nginx-deploy-7c97fc8644-lqpxj 1/1 Running 0 14m
nginx-po 1/1 Running 2 (83m ago) 5d
[root@master deploy]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deploy 3/3 3 3 68m
[root@master deploy]#
暂停与恢复
暂停
// 由于每次对pod template的修改都会触发更新,所以可以通过暂停更新来避免频繁更新。我们可以先暂停更新,然后修改pod template,最后恢复更新。
[root@master deploy]# kubectl rollout pause deployment nginx-deploy
deployment.apps/nginx-deploy paused
[root@master deploy]# kubectl edit deploy nginx-deployment
Error from server (NotFound): deployments.apps "nginx-deployment" not found
[root@master deploy]# kubectl edit deploy nginx-deploy
deployment.apps/nginx-deploy edited
[root@master deploy]# kubectl rollout history deployment nginx-deploy
deployment.apps/nginx-deploy
REVISION CHANGE-CAUSE
3 <none>
4 <none>
[root@master deploy]# kubectl rollout history deployment nginx-deploy --revision=4
deployment.apps/nginx-deploy with revision #4
Pod Template:
Labels: app=nginx-deploy
pod-template-hash=7c97fc8644
Containers:
nginx:
Image: nginx:1.9.1
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
[root@master deploy]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deploy-68c658776b 0 0 0 80m
nginx-deploy-7c97fc8644 3 3 3 49m
[root@master deploy]#
恢复
[root@master deploy]#
[root@master deploy]# kubectl rollout resume deployment nginx-deploy
deployment.apps/nginx-deploy resumed
[root@master deploy]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deploy-68c658776b 0 0 0 82m
nginx-deploy-6cbbdbbf8b 3 3 3 10s
nginx-deploy-7c97fc8644 0 0 0 51m
[root@master deploy]# kubectl rollout history
daemonset deployment statefulset
[root@master deploy]# kubectl rollout history deployment nginx-deploy
deployment.apps/nginx-deploy
REVISION CHANGE-CAUSE
3 <none>
4 <none>
5 <none>
[root@master deploy]# kubectl rollout history deployment nginx-deploy --revision=5
deployment.apps/nginx-deploy with revision #5
Pod Template:
Labels: app=nginx-deploy
pod-template-hash=6cbbdbbf8b
Containers:
nginx:
Image: nginx:1.9.1
Port: <none>
Host Port: <none>
Limits:
cpu: 500m
memory: 512Mi
Requests:
cpu: 100m
memory: 128Mi
Environment: <none>
Mounts: <none>
Volumes: <none>
[root@master deploy]#
标签:kubectl,deploy,nginx,master,deployment,k8s,root
From: https://www.cnblogs.com/humlogs/p/18203358