当前位置:网站首页>kubernetes资源对象介绍及常用命令(二)
kubernetes资源对象介绍及常用命令(二)
2022-07-01 07:24:00 【傻啦猫@[email protected]】
Deployment
Deployment顾名思义,就是部署的意思。Deploymen用于控制Pod,使Pod拥有多副本,自愈,扩缩容等能力。
多副本
副本,可以理解为Pod的数量。这些Pod会被随机分配到集群中的任意节点。
创建一次名叫my-app的部署,使用镜像为nginx。副本数为3。
命令行方式
kubectl create deploy my-app --image=nginx --replicas=3
yaml方式,my-app.yml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: my-app
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- image: nginx
name: nginx
kubectl apply -f my-app.yml

扩缩容
扩缩容是将原有的副本数进行扩大或缩小。流量大的时候可以扩容,流量高峰过后可以进行缩容,资源利用最大化。
命令行方式
kubectl scale --replicas=5 deployment/my-app
副本数由3个变成5个。
修改yaml方式
kubectl edit deployment my-app
将replicas修改成自己想要的数量,保存退出即可进行扩缩容。

自愈&故障转移
使用Deployment部署的应用始终和设定的副本数保持一致,当pod出现异常或者服务器宕机,亦或者删除pod,会在正常的节点重新拉起副本,以满足我们设定的副本数量。
我们模拟将k8s-worker1节点宕机。
发现k8s-worker1节点的两个pod状态为Terminating。在k8s-worker2多了两个Running状态的pod。实现了故障转移。
扩展:
查看集群内pod信息。
kubectl get pod -owide
监控集群内pod情况,打印详细信息。
kubectl get pod -w

阈值:pod出现了异常状况,k8s并不会立即杀死pod进行重启,而是超过了阈值才进行相应的重启操作,以防止因短暂网络问题等故障导致的频繁启动pod。
滚动更新
传统的更新升级需要将服务停止在做升级操作。k8s不用停止服务,会将pod依次替换成最新的,即启动一个新pod,杀死一个旧pod,直至全部更新完为止。
将nginx镜像更新为1.16.1版本。
命令行方式
kubectl set image deployment/my-app nginx=nginx:1.16.1 --record
修改yaml方式
kubectl edit deployment/my-app
将images修改为1.16.1中,保存退出即可。

扩展:
查看某次部署的yaml。
kubectl get deployment [my-app] -oyaml
版本回退
将版本回退到历史的某一次。
查看历史记录
[[email protected] ~]# kubectl rollout history deployment/my-app
deployment.apps/my-app
REVISION CHANGE-CAUSE
1 <none>
2 kubectl set image deployment/my-app nginx=nginx:1.16.1 --record=true
[[email protected] ~]#
查看某个历史详情
[[email protected] ~]# kubectl rollout history deployment/my-app --revision=2
deployment.apps/my-app with revision #2
Pod Template:
Labels: app=my-app
pod-template-hash=5ff664f457
Annotations: kubernetes.io/change-cause: kubectl set image deployment/my-app nginx=nginx:1.16.1 --record=true
Containers:
nginx:
Image: nginx:1.16.1
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
[[email protected] ~]#
回滚(回到上次)
[[email protected] ~]# kubectl rollout undo deployment/my-app
deployment.apps/my-app rolled back
[[email protected] ~]#
回滚(回到指定版本)
[[email protected] ~]# kubectl rollout undo deployment/my-app --to-revision=2
deployment.apps/my-app rolled back
[[email protected] ~]#
回滚的版本需要按照自己需要的填写。
小结:

除了Deployment,k8s还有 StatefulSet 、DaemonSet 、Job 等 类型资源。我们都称为 工作负载。 有状态应用使用 StatefulSet 部署,无状态应用使用 Deployment 部署 https://kubernetes.io/zh-cn/docs/concepts/workloads/controllers/
本文由 mdnice 多平台发布
版权声明
本文为[傻啦猫@[email protected]]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_45842494/article/details/125550761
边栏推荐
- 关于图灵测试和中文屋Chinese room的理解
- Huawei modelarts training alexnet model
- 2022年茶艺师(中级)复训题库及答案
- ctfshow-web351(SSRF)
- 【深圳IO】精确食品称(汇编语言的一些理解)
- 【Flutter 问题系列第 72 篇】在 Flutter 中使用 Camera 插件拍的图片被拉伸问题的解决方案
- Is it safe and reliable for Huatai Securities to open an account? How to open Huatai Securities Account
- atguigu----脚手架--02-使用脚手架(2)
- 未来互联网人才还稀缺吗?哪些技术方向热门?
- C language implementation [Sanzi chess game] (step analysis and implementation source code)
猜你喜欢

运维管理系统,人性化操作体验

Image style migration cyclegan principle
![Those high-frequency written tests and interview questions in [Jianzhi offer & Niuke 101] - linked list](/img/9a/44976b5df5567a7aff315e63569f6a.png)
Those high-frequency written tests and interview questions in [Jianzhi offer & Niuke 101] - linked list

Atguigu---- scaffold --02- use scaffold (2)

Redisson uses the full solution - redisson official document + comments (Part 2)

华为ModelArts训练Alexnet模型
![[chapter 72 of the flutter problem series] a solution to the problem that pictures taken in the flutter using the camera plug-in are stretched](/img/8d/cf259b9bb8574aa1842280c9661d1e.jpg)
[chapter 72 of the flutter problem series] a solution to the problem that pictures taken in the flutter using the camera plug-in are stretched

2022制冷与空调设备运行操作国家题库模拟考试平台操作

Understanding of Turing test and Chinese Room

赌上了绩效,赢了公司CTO,我要搭DevOps平台!
随机推荐
Stepsister becomes stepmother, son breaks off relationship with himself, and musk, the world's richest man, why is it so miserable?
Apple account password auto fill
电脑有网络,但所有浏览器网页都打不开,是怎么回事?
赌上了绩效,赢了公司CTO,我要搭DevOps平台!
redisson使用全解——redisson官方文档+注释(中篇)
[lingo] find the shortest path problem of undirected graph
Do securities account opening affect the security of account opening
C语言实现【扫雷游戏】完整版(实现源码)
【图像处理】图像直方图均衡化系统含GUI界面
The computer has a network, but all browser pages can't be opened. What's the matter?
Oracle创建自增id
1286_ Implementation analysis of task priority setting in FreeRTOS
【Flutter 问题系列第 72 篇】在 Flutter 中使用 Camera 插件拍的图片被拉伸问题的解决方案
【深圳IO】精确食品称(汇编语言的一些理解)
Summary of the concept and advantages of 5g massive MIMO
在长城证券上做基金定投安全吗?
Is the account opening of GF Securities safe and reliable? How to open GF Securities Account
2022茶艺师(初级)操作证考试题库及模拟考试
【系统分析师之路】第五章 复盘软件工程(逆向净室与模型驱动开发)
Is it safe and reliable for Huatai Securities to open an account? How to open Huatai Securities Account