当前位置:网站首页>kubernetes资源对象介绍及常用命令(二)
kubernetes资源对象介绍及常用命令(二)
2022-07-01 02:31:00 【江小南】
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/
边栏推荐
- QML control type: tooltip
- Nacos configuration center tutorial
- Go import self built package
- js中的图片预加载
- Visual effects, picture to cartoon function
- robots.txt限制搜索引擎收录
- JS anti shake and throttling
- Pulsar 主题压缩
- SWT / anr problem - SWT caused by long execution time of native method
- Do you write API documents or code first?
猜你喜欢

The image variables in the Halcon variable window are not displayed, and it is useless to restart the software and the computer

产业互联网中,「小」程序有「大」作为

Youmeng (a good helper for real-time monitoring of software exceptions: crash) access tutorial (the easiest tutorial for Xiaobai with some foundation)

QML control type: tooltip

视觉特效,图片转成漫画功能

Rocketqa: cross batch negatives, de noised hard negative sampling and data augmentation
![Pytoch -- foundation refers to the north_ II. What high school students can understand [back propagation and gradient descent]](/img/6e/279dbb7a8d7a5ecd240de464c5b8b2.png)
Pytoch -- foundation refers to the north_ II. What high school students can understand [back propagation and gradient descent]

运算符重载的初识

如何在智汀中实现智能锁与灯、智能窗帘电机场景联动?

Restcloud ETL实践之数据行列转换
随机推荐
Ernie gram, an explicit and complete n-gram mask language model, implements explicit n-gram semantic unit knowledge modeling.
Pulsar Geo Replication/灾备/地域复制
(translation) reasons why real-time inline verification is easier for users to make mistakes
鼠标悬停效果十
Viewing JVM parameters
How to learn and read code
Leetcode (524) -- match the longest word in the dictionary by deleting letters
How do the top ten securities firms open accounts? Also, is it safe to open an account online?
The latest CSDN salary increase technology stack in 2022 overview of APP automated testing
最新微信ipad协议 CODE获取 公众号授权等
map数组函数
LabVIEW calculates the camera image sensor resolution and lens focal length
UE4渲染管线学习笔记
使用ipmitool配置X86服务器的BMC网络和用户信息
十大劵商如何开户?还有,在线开户安全么?
[multi source BFS] 934 Shortest Bridge
Pulsar 主题压缩
RestCloud ETL WebService数据同步到本地
How to add a condition for an associated table in an SQL statement [null value required or not required]
Evaluation of the entry-level models of 5 mainstream smart speakers: apple, Xiaomi, Huawei, tmall, Xiaodu, who is better?