简介
1、 k8s1 master ---IP地址:172.31.100.20
2、 何为滚动发布Rolling update:一种高级发布策略,按批次依次替换老版本,逐步升级到新版本。发布过 程中,应用不中断,用户体验平滑 (发布版本v1到v2,有问题回滚)
1、创建目录
[root@k8s1 ~]# mkdir -p /root/yaml/petclinic-deployment
[root@k8s1 ~]# cd /root/yaml/petclinic-deployment
2、配置yml文件
[root@k8s1 petclinic-deployment]# cat deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: petclinic
spec:
selector:
matchLabels:
app: petclinic
minReadySeconds: 10 ##pod启动后等待10秒才就绪,认为添加,方便查看回滚效果
replicas: 3 ##发布3个pod
template:
metadata:
labels:
app: petclinic ##app:名称要与petclinic-service.yml 中的app一样
spec:
containers:
- name: petclinic
image: spring2go/spring-petclinic:1.0.0.RELEASE
[root@k8s1 petclinic-deployment]# cat
deployment.yml petclinic-service.yml
[root@k8s1 petclinic-deployment]# cat petclinic-service.yml
apiVersion: v1
kind: Service
metadata:
name: petclinic
spec:
ports:
- name: http
port: 8080
targetPort: 8080
nodePort: 32000 #创建暴露nodeport端口 32000,
selector:
app: petclinic
type: NodePort
3、查看默认所有pod
[root@k8s1 petclinic-deployment]# kubectl get all
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 22h
4、执行yml文件
[root@k8s1 petclinic-deployment]# kubectl apply -f .
service/petclinic created
deployment.apps/petclinic created
5、查看所有pod
##可以看到3个pod已经创建成功。
##replicaset.apps管理3个pod
##同样也创建暴露nodeport端口 32000,
[root@k8s1 petclinic-deployment]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/petclinic-58998486f8-4nvr5 1/1 Running 0 6m19s
pod/petclinic-58998486f8-626vl 1/1 Running 0 6m19s
pod/petclinic-58998486f8-bdhtn 1/1 Running 0 6m19s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 22h
service/petclinic NodePort 10.109.88.55 <none> 8080:32000/TCP 5m34s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/petclinic 3/3 3 3 6m19s
NAME DESIRED CURRENT READY AGE
replicaset.apps/petclinic-58998486f8 3 3 3 6m19s
6、访问–效果
###v1.0.0创建成功
http://172.31.100.20:32000/
数据显示也正常
7、升级版本deployment (滚动方式)
cat deployment.yml #修改image 版本1.0.1
8、执行yml文件
[root@k8s1 petclinic-deployment]# kubectl apply -f .
deployment.apps/petclinic configured
service/petclinic unchanged
9、查看pod状态
[root@k8s1 petclinic-deployment]# kubectl get all
##3个pod状态running 正常
##deployment 3个正常
## replicaset上面的pod为0 #说明老版本已经全部关闭
## replicaset下面的pod为3 #说明新版本已经全部替换老版本完毕!
10、查看版本升级成功
刷新浏览器页面
看看效果更新版本1.0.1成功
10、查看deployment的历史记录(版本)
[root@k8s1 petclinic-deployment]# kubectl rollout history deployment/petclinic
deployment.extensions/petclinic
REVISION CHANGE-CAUSE
1 <none>
2 <none>
12、降级版本deployment (滚动方式)
使用场景:升级完版本有故障,快速退回上一个版本
[root@k8s1 petclinic-deployment]# kubectl rollout undo deployment/petclinic
deployment.extensions/petclinic rolled back
13、查看升级或降级(回退)状态过程
[root@k8s1 petclinic-deployment]# kubectl rollout status deployment/petclinic
Waiting for deployment "petclinic" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "petclinic" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "petclinic" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "petclinic" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "petclinic" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "petclinic" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "petclinic" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "petclinic" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "petclinic" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "petclinic" rollout to finish: 1 old replicas are pending termination...
deployment "petclinic" successfully rolled out
14、再次查看deployment的历史记录(版本)
[root@k8s1 petclinic-deployment]# kubectl rollout history deployment/petclinic
deployment.extensions/petclinic
REVISION CHANGE-CAUSE
4 <none>
5 <none>
15、查看deployment详细记录
## 4 版本是 1.0.1
[root@k8s1 petclinic-deployment]# kubectl rollout history deployment/petclinic --revision=4
deployment.extensions/petclinic with revision #4
Pod Template:
Labels: app=petclinic
pod-template-hash=59dbcd78fc
Containers:
petclinic:
Image: spring2go/spring-petclinic:1.0.1.RELEASE
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
16、指定版本回退
##--to-revision=4 指定版本
[root@k8s1 petclinic-deployment]# kubectl rollout undo deployment/petclinic --to-revision=4
deployment.extensions/petclinic rolled back
##回退状态
[root@k8s1 petclinic-deployment]# kubectl rollout status deployment/petclinic
Waiting for deployment "petclinic" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "petclinic" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "petclinic" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "petclinic" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "petclinic" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "petclinic" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "petclinic" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "petclinic" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "petclinic" rollout to finish: 1 old replicas are pending termination...
deployment "petclinic" successfully rolled out
17、清理deployment
[root@k8s1 petclinic-deployment]# kubectl delete deploy petclinic
deployment.extensions "petclinic" deleted
##删除deployment会删除三个deployment、 replicaset、pod。 因为pod是关联在replicaset上的 ;而replicaset是关联在deployment上的
##已经清理成功 deployment、 replicaset、pod
[root@k8s1 petclinic-deployment]# kubectl get all
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 23h
service/petclinic NodePort 10.109.88.55 <none> 8080:32000/TCP 47m
18、还有service 咱们删除
[root@k8s1 petclinic-deployment]# kubectl delete svc petclinic
service "petclinic" deleted
[root@k8s1 petclinic-deployment]# kubectl get all
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 23h
总结:回滚发布是一种高级发布机制,支持按批次滚动发布,用户体验不中断,适用于版本兼容发布。蓝绿发布则适用于版本不兼容发布
k8s中的Deployment 是对RepcliaSet+滚动发布流程的一中包装
发布机制类似于Replicaset