(2022版)一套教程搞定k8s安装到实战 | Deployment
Posted COCOgsta
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(2022版)一套教程搞定k8s安装到实战 | Deployment相关的知识,希望对你有一定的参考价值。
视频来源:B站《(2022版)最新、最全、最详细的Kubernetes(K8s)教程,从K8s安装到实战一套搞定》
一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持!
附上汇总贴:(2022版)一套教程搞定k8s安装到实战 | 汇总_COCOgsta的博客-CSDN博客
Deployment概念
用于部署无状态的服务,这个是最常用的控制器。一般用于管理维护企业内部无状态的微服务,比如configserver、zuul、springboot。它可以管理多个副本的pod,实现无缝迁移、自动扩容缩容、自动灾难恢复、一键回滚等功能。
创建一个Deployment
手工创建
kubectl create deployment nginx --image=nginx:1.15.2
复制代码
从文件创建
# cat nginx-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
creationTimestamp: "2020-09-19T02:41:11Z"
generation: 1
labels:
app: nginx # 通过--show-labels看到的结果
name: nginx
namespace: default
spec:
progressDeadlineSeconds: 600
replicas: 2 # 副本数
revisionHistoryLimit: 10 # 历史记录保留的
selector:
matchLabels:
app: nginx # 与template中的labels要保持一致,且不能修改
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx:1.15.2
imagePullPolicy: IfNotPresent
name: nginx
resources:
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext:
terminationGracePeriodSeconds: 30
复制代码
状态解析:
[root@k8s-master-lb ~]# kubectl get deploy -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx 2/2 2 2 2m41s nginx nginx:1.15.2 app=nginx
复制代码
NAME:Deployment名称
READY:Pod的状态,已经Ready的个数
UP-TO-DATE:已经达到期望状态的被更新的副本数
AVAILABLE:已经可以用的副本数
AGE:显示应用程序运行的时间
CONTAINERS:容器名称
IMAGES:容器的镜像
SELECTOR:管理Pod的标签
更新与回滚
查看历史记录
kubectl rollout history deploy nginx
复制代码
回滚到上一个版本
kubectl rollout undo deploy nginx
复制代码
查看指定版本的详细信息
kubectl rollout history deploy nginx --revision=5
复制代码
回滚到指定的版本
kubectl rollout undo deploy nginx --to-revision=5
复制代码
扩缩容
kubectl scale --replicas=3 deploy nginx
复制代码
如果需要缩容,修改replicas即可。
暂停
# 暂停更新
kubectl rollout pause deployment nginx
# 进行一次变更
kubectl set image deploy nginx nginx=nginx:1.15.3 --record
# 进行二次变更
kubectl set resources deploy nginx -c nginx --limits=cpu=200m,memory=128Mi --requests=cpu=10m,memory=16Mi
# 开启更新
kubectl rollout resume deploy nginx
复制代码
Deployment注意事项
[root@k8s-master-lb ~]# kubectl get deploy nginx -oyaml
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
creationTimestamp: "2022-08-04T01:12:05Z"
generation: 2
labels:
app: nginx
spec:
progressDeadlineSeconds: 600
replicas: 2
revisionHistoryLimit: 10
selector:
matchLabels:
app: nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx:1.15.2
imagePullPolicy: IfNotPresent
name: nginx
resources:
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext:
terminationGracePeriodSeconds: 30
status:
availableReplicas: 2
conditions:
- lastTransitionTime: "2022-08-04T01:12:05Z"
lastUpdateTime: "2022-08-04T01:12:26Z"
message: ReplicaSet "nginx-66bbc9fdc5" has successfully progressed.
reason: NewReplicaSetAvailable
status: "True"
type: Progressing
- lastTransitionTime: "2022-08-04T01:13:19Z"
lastUpdateTime: "2022-08-04T01:13:19Z"
message: Deployment has minimum availability.
reason: MinimumReplicasAvailable
status: "True"
type: Available
observedGeneration: 2
readyReplicas: 2
replicas: 2
updatedReplicas: 2
[root@k8s-master-lb ~]#
复制代码
- .spec.revisionHistoryLimit:设置保留RS旧的revision的个数,设置为0的话,不保留历史数据
- .spec.minReadySeconds:可选参数,指定新创建的Pod在没有任何容器崩溃的情况下视为Ready最小的秒数,默认为0,即一旦被创建就视为可用。
-
滚动更新的策略:
-
.spec.strategy.type:更新deployment的方式,默认是RollingUpdate
-
RollingUpdate:滚动更新,可以指定maxSurge和maxUnavailable
- maxUnavailable:指定在回滚或更新时最大不可用的Pod的数量,可选字段,默认25%,可以设置成数字或百分比,如果该值为0,那么maxSurge就不能为0
- maxSurge:可以超过期望值的最大Pod数,可选字段,默认为25%,可以设置成数字或百分比,如果该值为0,那么maxUnavailable不能为0
-
Recreate:重建,先删除旧的Pod,再创建新的Pod(很少用)
-
-
以上是关于(2022版)一套教程搞定k8s安装到实战 | Deployment的主要内容,如果未能解决你的问题,请参考以下文章
(2022版)一套教程搞定k8s安装到实战 | InitContainer
(2022版)一套教程搞定k8s安装到实战 | DaemonSet
(2022版)一套教程搞定k8s安装到实战 | Volumes