5 k8s之deployment及Rs
一,rc 的局限性
1 如kubectl edit rc myweb 只有启动新的pod才会生效,或者删除老的pod,重新启动新的pod的才会生效,否则还是继续使用老的pod,无法自动生效
2 rc滚动升级之后,pod的标签也升级了,导致svc关联不上
[root@k8s-master ~]# kubectl rolling-update myweb -f nginx-rc1.15.yaml --update-period=1s [root@k8s-master ~]# kubectl get all -o wide
二 deployment出现,为了弥补Rc的局限性
1 测试前删除rc,即对应的pod都删除了
[root@k8s-master ~]# kubectl delete rc mywebv2 replicationcontroller "mywebv2" deleted
2 deployment的yml配置文件示例
[root@k8s-master ~]# vim nginx-deploy.yaml
apiVersion: extensions/v1beta1 #版本 kind: Deployment #资源类型 metadata: name: nginx-deployment #定义deployment的名字 spec: replicas: 3 #副本数设置 template: #模板 metadata: labels: app: nginx #标签设置为nginx spec: containers: - name: nginx # 镜像名称 image: 192.168.31.50:5000/nginx:1.13 #使用的镜像 ports: - containerPort: 80 # 镜像端口
3 创建deployment
[root@k8s-master ~]# vim nginx-deploy.yaml [root@k8s-master ~]# kubectl create -f nginx-deploy.yaml deployment "nginx-deployment" created [root@k8s-master ~]# kubectl get all -o wide
3-1 由于创建deployment使用的标签是nginx,修改svc关联的标签为nginx即可,否则无法访问资源
[root@k8s-master ~]# kubectl edit svc myweb
3 -2 测试通过deployment来实现升级
[root@k8s-master ~]# kubectl edit deployment nginx-deployment
升级成功
4 创建depllyment会自动创建rs, rs有rc的95%的功能,rs由deployment自动创建
5 通过rs实现回滚相关操作
5-1 查看版本
[root@k8s-master ~]# kubectl rollout history deployment nginx-deployment deployments "nginx-deployment" REVISION CHANGE-CAUSE 1 <none> 2 <none>
5-2 回滚到老的版本1版本
[root@k8s-master ~]# kubectl rollout undo deployment nginx-deployment --to-revision=1 deployment "nginx-deployment" rolled back
5-3 查看是否成功回滚到nginx1.13版本
6 通过命令创建deployment
[root@k8s-master ~]# kubectl run deploy-nginx2 --image=192.168.31.50:5000/nginx:1.13 --replicas=3 --record deployment "deploy-nginx2" created [root@k8s-master ~]#
6-1 查看历史版本
[root@k8s-master ~]# kubectl rollout history deployment deploy-nginx2 deployments "deploy-nginx2" REVISION CHANGE-CAUSE 1 kubectl run deploy-nginx2 --image=192.168.31.50:5000/nginx:1.13 --replicas=3 --record
6-2 通过命令更改镜像版本
[root@k8s-master ~]# kubectl set image deployment deploy-nginx2 deploy-nginx2=192.168.31.50:5000/nginx:1.15 deployment "deploy-nginx2" image updated
6-3 查看命令操作记录的版本
[root@k8s-master ~]# kubectl rollout history deployment deploy-nginx2 deployments "deploy-nginx2" REVISION CHANGE-CAUSE 1 kubectl run deploy-nginx2 --image=192.168.31.50:5000/nginx:1.13 --replicas=3 --record 2 kubectl set image deployment deploy-nginx2 deploy-nginx2=192.168.31.50:5000/nginx:1.15
6-3 创建开放端口
[root@k8s-master ~]# kubectl expose deployment deploy-nginx2 --port=80 --type=NodePort service "deploy-nginx2" exposed
7 查看pod的日志
kubectl logs pod名
[root@k8s-master ~]# kubectl logs nginx-deployment-3776102587-1h8br 172.16.96.0 - - [20/Sep/2019:05:40:42 +0000] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.29.0" "-"