用kubectl管理应用升级,服务发布与回滚,扩缩容

Kubernetes概述
编辑
Kubernetes是Google开源的一个容器编排引擎,它支持自动化部署、大规模可伸缩、应用容器化管理。在生产环境中部署一个应用程序时,通常要部署该应用的多个实例以便对应用请求进行负载均衡。
在Kubernetes中,我们可以创建多个容器,每个容器里面运行一个应用实例,然后通过内置的负载均衡策略,实现对这一组应用实例的管理、发现、访问,而这些细节都不需要运维人员去进行复杂的手工配置和处理。

应用升级

Kubectl set image --help 有案例指定新版本,并添加标签,以便于回滚
[root@k8s-master ~]# kubectl set image deployment/nginx nginx=nginx:1.11  --record

升级之后他会将所有版本进行替换,用describe来查看版本


[root@k8s-master ~]# kubectl get pods
NAME                       READY     STATUS    RESTARTS   AGE
busybox-5d4f595646-dzjv4   1/1       Running   0          2d
nginx-76c4c6d6d8-5w825     1/1       Running   0          2m
nginx-76c4c6d6d8-bh2sm     1/1       Running   0          2m
nginx-76c4c6d6d8-cwhw5     1/1       Running   0          1m

用describe来查看版本,看到版本已经升级为1.11版本了,另外下面还有他的输出信息

[root@k8s-master ~]# kubectl describe po/nginx-76c4c6d6d8-cwhw5
 nginx:
    Container ID:   docker://ca046dd27d86bc10f330131be7ba5b91b14cb555c5d9677bb23f481e4e69aa67
    Image:          nginx:1.11

第二种修改应用版本===可以使用edit

直接进行编辑就行
[root@k8s-master ~]# kubectl edit deploy/nginx

找到image直接修改进行保存,升级之后他会将所有版本进行替换,用describe来查看版本

等启动之后查看pod重启新的镜像已经运行了

[root@k8s-master ~]# kubectl get pods
NAME                       READY     STATUS    RESTARTS   AGE
busybox-5d4f595646-dzjv4   1/1       Running   0          2d
nginx-7454cd89d8-fpncj     1/1       Running   0          5m
nginx-7454cd89d8-gdz8c     1/1       Running   0          4m
nginx-7454cd89d8-tlngj     1/1       Running   0          5m

用describe查看Pod镜像信息

[root@k8s-master ~]# kubectl describe po/nginx-7454cd89d8-tlngj
nginx:
    Container ID:   docker://6768df2391defe44097e3ce2a857529ec614d49962181c1cf541c1bbc6dbce08
Image:          nginx:1.15

用node端直接访问查看版本

[root@k8s-node1 ~]# curl -I 10.10.10.173:88
HTTP/1.1 200 OK
Server: nginx/1.15.12
Date: Mon, 08 Jul 2019 04:23:42 GMT

查看版本升级信息

[root@k8s-master ~]# kubectl rollout history deploy/nginx
deployments "nginx"
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
3         <none>

记录版本信息状态,便于回滚

[root@k8s-master ~]# kubectl set image deployment/nginx nginx=nginx:1.16 --record
[root@k8s-master ~]# kubectl rollout history deploy/nginx
deployments "nginx"
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
3         <none>
4         kubectl set image deployment/nginx nginx=nginx:1.16 --record=true

进行回滚用rollout undo,默认恢复到上一个版本也就是1.15
[root@k8s-master ~]# kubectl rollout undo deploy/nginx
默认少一个3,也就是回到上次第三次版本了

[root@k8s-master ~]# kubectl rollout history deploy/nginx
deployments "nginx"
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
4         kubectl set image deployment/nginx nginx=nginx:1.16 --record=true
5         <none>
[root@k8s-master ~]# kubectl describe po/nginx-7454cd89d8-txjpm
Containers:
  nginx:
    Container ID:   docker://f17b88c301d61b8c75a46cba84d97c285f5d61886c081b3cfc9c0668e5cbf4e3
    Image:          nginx:1.15

业务量突然增加,我们需要增加副本

[root@k8s-master ~]# kubectl scale deploy/nginx --replicas=10
deployment "nginx" scaled
[root@k8s-master ~]# kubectl get pod
NAME                       READY     STATUS    RESTARTS   AGE
busybox-5d4f595646-dzjv4   1/1       Running   0          2d
nginx-7454cd89d8-4cct8     1/1       Running   0          19s
nginx-7454cd89d8-75tvg     1/1       Running   0          7m
nginx-7454cd89d8-7v5qp     1/1       Running   0          1m
nginx-7454cd89d8-dtsgq     1/1       Running   0          19s
nginx-7454cd89d8-hjcpw     1/1       Running   0          7m
nginx-7454cd89d8-mhhww     1/1       Running   0          19s
nginx-7454cd89d8-txjpm     1/1       Running   0          7m
nginx-7454cd89d8-vkmjn     1/1       Running   0          19s
nginx-7454cd89d8-z8dbn     1/1       Running   0          19s
nginx-7454cd89d8-zn8pm     1/1       Running   0          1m

业务量下降我们进行缩容

[root@k8s-master ~]# kubectl scale deploy/nginx --replicas=5
[root@k8s-master ~]# kubectl get pod
NAME                       READY     STATUS    RESTARTS   AGE
busybox-5d4f595646-dzjv4   1/1       Running   0          2d
nginx-7454cd89d8-75tvg     1/1       Running   0          8m
nginx-7454cd89d8-7v5qp     1/1       Running   0          3m
nginx-7454cd89d8-hjcpw     1/1       Running   0          8m
nginx-7454cd89d8-txjpm     1/1       Running   0          8m
nginx-7454cd89d8-zn8pm     1/1       Running   0          3m

要是不需要这些资源或者换项目了
直接用delete=======删除deploy还有service

[root@k8s-master ~]# kubectl delete deploy/nginx
deployment "nginx" deleted
[root@k8s-master ~]# kubectl delete svc/nginx-service
查看资源已经删除
[root@k8s-master ~]# kubectl get all
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值