【四】K8S_Deployment
Deployment作用
Kubernetes(K8S)中的 Deployment 是用于定义和管理应用程序部署的重要资源对象。Deployment 在 K8S 中具有以下用途和相关概念:
- 应用程序部署和更新:Deployment 用于指定应用程序的部署配置,包括容器镜像、副本数、环境变量、存储卷等。通过 Deployment,您可以方便地定义和管理应用程序的初始部署,并进行后续的版本更新和升级。
- ReplicaSet:Deployment 通过与 ReplicaSet 关联来管理 Pod 的副本。ReplicaSet 定义了创建和维护一组相同的 Pod 的规则。Deployment 使用 ReplicaSet 来确保指定数量的 Pod 副本在运行,并负责升级或回滚这些副本。
- 滚动更新和回滚:Deployment 提供了无缝的应用程序版本升级和回滚机制。您可以通过更新 Deployment 的配置或容器镜像来触发滚动更新。Deployment 将逐步替换旧版的 Pod,以确保在升级过程中始终有足够的可用副本。如果需要回滚到先前的版本,也可以轻松地执行回滚操作。
- 健康检查和自动修复:Deployment 负责监测 Pod 的健康状态,并自动修复故障的副本。您可以配置 Deployment 以使用健康检查来确定是否要替换失败的 Pod 实例。这确保了应用程序的高可用性和稳定性。
- 扩展和缩减:Deployment 允许您根据流量需求动态调整 Pod 的副本数。通过修改 Deployment 的副本数,在需要时进行水平扩展以应对高流量,或者在需要时进行缩减以减少资源消耗。K8S 会自动管理 Pod 的创建和删除过程。
- 持久化存储:Deployment 可以指定需要挂载到 Pod 中的持久化存储卷。这允许应用程序在多个 Pod 之间共享数据,并在容器重启或重新调度时保留数据。
通过使用 Deployment,您可以轻松地管理和控制应用程序的部署、更新和扩展。它提供了易于使用和强大的功能,使得应用程序在 K8S 集群中能够高效、可靠地运行。
deployment的冗余能力
创建一个deployment的pod用delete删除不了,下面案例两种创建pod的不通
[root@master ~]# kubectl run nginx --image=nginx
pod/nginx created
[root@master ~]# kubectl create deployment myginx01 --image=nginx
deployment.apps/myginx01 created
[root@master ~]# kubectl delete pod nginx
pod "nginx" deleted
[root@master ~]# kubectl delete pod myginx01-5c9d6dbf77-k75d5
pod "myginx01-5c9d6dbf77-k75d5" deleted
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myginx01-5c9d6dbf77-k8f8x 1/1 Running 0 2m45s
也可以在开一个窗口看一下效果
[root@master ~]# watch -n 1 kubectl get pod
只有把delopment删除以后才可以删除pod
[root@master ~]# kubectl delete deploy myginx01
deployment.apps "myginx01" deleted
[root@master ~]# kubectl get pod
No resources found in default namespace.
deployment的多副本部署
[root@master ~]# kubectl create deploy mynginx --image=nginx --replicas=3
deployment.apps/mynginx created
[root@master ~]# kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mynginx-5b686ccd46-4w2nm 1/1 Running 0 36s 192.168.196.146 node01 <none> <none>
mynginx-5b686ccd46-cxtjn 1/1 Running 0 36s 192.168.196.144 node01 <none> <none>
mynginx-5b686ccd46-j5xpq 1/1 Running 0 36s 192.168.196.145 node01 <none> <none>
[root@master ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
mynginx 3/3 3 3 92s
也可以在bashboard上部署
用yaml部署
先创建一个yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: my-dep
name: my-dep
spec:
replicas: 3
selector:
matchLabels:
app: my-dep
template:
metadata:
labels:
app: my-dep
spec:
containers:
- image: nginx
name: nginx
[root@master ~]# kubectl apply -f pod.yaml
deployment.apps/my-dep created
[root@master ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
my-dep 2/3 3 2 30s
mynginx 5/5 5 5 16m
[root@master ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
my-dep 3/3 3 3 35s
mynginx 5/5 5 5 16m
[root@master ~]#
deployment的扩缩容
kubectl scale --replicas=5 deployment/my-dep
kubectl edit deploy my-dep
案例走起:
[root@master ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
my-dep 3/3 3 3 7m17s
[root@master ~]# kubectl scale deploy/my-dep --replicas=5
deployment.apps/my-dep scaled
[root@master ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
my-dep 5/5 5 5 8m10s
[root@master ~]# kubectl scale deploy/my-dep --replicas=2
deployment.apps/my-dep scaled
[root@master ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
my-dep 2/2 2 2 8m42s
[root@master ~]# kubectl edit deploy my-dep
deployment.apps/my-dep edited
[root@master ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
my-dep 2/3 3 2 11m
deployment的自愈能力
案例走起:
在node01节点上停止一个容器看效果
[root@node01 ~]# docker ps |grep my-dep-5b7868d854-dgrt4
14744aac665a nginx "/docker-entrypoint.…" 13 minut8s_nginx_my-dep-5b7868d854-dgrt4_default_bc8be424-2226-44f3-8dc5-49b08d1ef3fe_0
96beef3e2dba registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 13 minut8s_POD_my-dep-5b7868d854-dgrt4_default_bc8be424-2226-44f3-8dc5-49b08d1ef3fe_0
[root@node01 ~]# docker ps |grep my-dep-5b7868d854-dgrt4
14744aac665a nginx "/docker-entrypoint.…" 14 minutdgrt4_default_bc8be424-2226-44f3-8dc5-49b08d1ef3fe_0
96beef3e2dba registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 14 minutrt4_default_bc8be424-2226-44f3-8dc5-49b08d1ef3fe_0
[root@node01 ~]# docker stop 14744aac665a
14744aac665a
[root@node01 ~]#
发现node01节点的容器重启了一次
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
my-dep-5b7868d854-dgrt4 1/1 Running 1 16m
my-dep-5b7868d854-lq6mz 1/1 Running 0 27m
my-dep-5b7868d854-pgp9n 1/1 Running 0 27m
现在我们把node01服务器关机看效果
[root@master ~]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
my-dep-5b7868d854-dgrt4 1/1 Running 1 20m
my-dep-5b7868d854-lq6mz 1/1 Running 0 31m
my-dep-5b7868d854-pgp9n 1/1 Running 0 31m
my-dep-5b7868d854-dgrt4 1/1 Terminating 1 25m
my-dep-5b7868d854-6qzt6 0/1 Pending 0 0s
my-dep-5b7868d854-6qzt6 0/1 Pending 0 0s
my-dep-5b7868d854-6qzt6 0/1 ContainerCreating 0 0s
my-dep-5b7868d854-6qzt6 0/1 ContainerCreating 0 1s
my-dep-5b7868d854-6qzt6 1/1 Running 0 2s
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
my-dep-5b7868d854-6qzt6 1/1 Running 0 2m18s 192.168.140.81 node02 <none> <none>
my-dep-5b7868d854-dgrt4 1/1 Terminating 1 27m 192.168.196.154 node01 <none> <none>
my-dep-5b7868d854-lq6mz 1/1 Running 0 38m 192.168.140.80 node02 <none> <none>
my-dep-5b7868d854-pgp9n 1/1 Running 0 38m 192.168.140.79 node02 <none> <none>
滚动更新
以yaml的方式查看pod
[root@master ~]# kubectl get deploy my-dep -o yaml
...
spec:
containers:
- image: nginx
imagePullPolicy: Always
name: nginx
...
现在我们要变更镜像的版本
[root@master ~]# kubectl set image deploy/my-dep nginx=nginx:1.16.1 --record
deployment.apps/my-dep image updated
查看效果:
[root@master ~]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
my-dep-5b7868d854-6qzt6 1/1 Running 0 10m
my-dep-5b7868d854-lq6mz 1/1 Running 0 46m
my-dep-5b7868d854-pgp9n 1/1 Running 0 46m
my-dep-6b48cbf4f9-x25jn 0/1 ContainerCreating 0 23s
my-dep-6b48cbf4f9-x25jn 1/1 Running 0 50s
my-dep-5b7868d854-6qzt6 1/1 Terminating 0 10m
my-dep-6b48cbf4f9-gfhnf 0/1 Pending 0 0s
my-dep-6b48cbf4f9-gfhnf 0/1 Pending 0 0s
my-dep-6b48cbf4f9-gfhnf 0/1 ContainerCreating 0 0s
my-dep-5b7868d854-6qzt6 1/1 Terminating 0 10m
my-dep-6b48cbf4f9-gfhnf 0/1 ContainerCreating 0 1s
my-dep-5b7868d854-6qzt6 0/1 Terminating 0 10m
my-dep-6b48cbf4f9-gfhnf 1/1 Running 0 2s
my-dep-5b7868d854-lq6mz 1/1 Terminating 0 46m
my-dep-6b48cbf4f9-ztzzw 0/1 Pending 0 0s
my-dep-6b48cbf4f9-ztzzw 0/1 Pending 0 0s
my-dep-6b48cbf4f9-ztzzw 0/1 ContainerCreating 0 0s
my-dep-5b7868d854-lq6mz 1/1 Terminating 0 46m
my-dep-6b48cbf4f9-ztzzw 0/1 ContainerCreating 0 1s
my-dep-5b7868d854-lq6mz 0/1 Terminating 0 46m
my-dep-5b7868d854-6qzt6 0/1 Terminating 0 10m
my-dep-5b7868d854-6qzt6 0/1 Terminating 0 10m
my-dep-5b7868d854-lq6mz 0/1 Terminating 0 46m
my-dep-5b7868d854-lq6mz 0/1 Terminating 0 46m
my-dep-6b48cbf4f9-ztzzw 1/1 Running 0 17s
my-dep-5b7868d854-pgp9n 1/1 Terminating 0 46m
my-dep-5b7868d854-pgp9n 1/1 Terminating 0 46m
my-dep-5b7868d854-pgp9n 0/1 Terminating 0 46m
my-dep-5b7868d854-pgp9n 0/1 Terminating 0 47m
my-dep-5b7868d854-pgp9n 0/1 Terminating 0 47m
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
my-dep-6b48cbf4f9-gfhnf 1/1 Running 0 2m41s 192.168.196.158 node01 <none> <none>
my-dep-6b48cbf4f9-x25jn 1/1 Running 0 3m31s 192.168.196.157 node01 <none> <none>
my-dep-6b48cbf4f9-ztzzw 1/1 Running 0 2m39s 192.168.196.159 node01 <none> <none>
[root@master ~]#
[root@master ~]# kubectl get deploy -o yaml
...
spec:
containers:
- image: nginx:1.16.1
imagePullPolicy: Always
name: nginx
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
版本回退
查看历史命令
[root@master ~]# kubectl rollout history deployment/my-dep
deployment.apps/my-dep
REVISION CHANGE-CAUSE
1 <none>
2 kubectl set image deploy/my-dep nginx=nginx:1.16.1 --record=true
设置回退到1的时候:
[root@master ~]# kubectl rollout undo deploy/my-dep --to-revision=1
deployment.apps/my-dep rolled back
参考一下镜像版本:
[root@master ~]# kubectl get deploy/my-dep -o yaml |grep image
{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app":"my-dep"},"name":"my-dep","namespace":"default"},"spec":{"replicas":3,"selector":{"matchLabels":{"app":"my-dep"}},"template":{"metadata":{"labels":{"app":"my-dep"}},"spec":{"containers":[{"image":"nginx","name":"nginx"}]}}}}
f:imagePullPolicy: {}
f:image: {}
- image: nginx
imagePullPolicy: Always
Kubernetes-核心概念-其他工作负载
Deployment: 无状态应用部署,比如微服务,提供多副本等功能
StatefulSet:有状态应用部署,比如redis,提供稳定的存储、网络等功能
DaemonSet:守护型应用部署,比如日志收集组件,在每个机器都运行一份
Job/CronJob:定时任务部署,比如垃圾清理组件,可以在指定时间运行
阅读剩余
本站代码模板仅供学习交流使用请勿商业运营,严禁从事违法,侵权等任何非法活动,否则后果自负!
THE END