1、Controller Manager(资源控制器) 由kube-controller-manager
和 cloud-controller-manager
组成, 是Kubernetes 的大脑,它通过 apiserver 监控整个集群的状态, 并确保集群处于预期的工作状态。
2、kube-controller-manager
kube-controller-manager运行管理控制器,它们是集群中处理常规任务的后台线程,逻辑上,每个控制器是一个单独的进程,但为了降低复杂性,它们都被编译成单个二进制文件,并在单个进程中运行。
这些控制器包括:
3、cloud-controller-manager
--cloud-provider
flag设置为external。kube-controller-manager
来禁用控制器循环。cloud-controller-manager 具体功能:
节点(Node Controller)控制器
路由(Route Controller)控制器
Service(Service Controller)控制器
卷(VolumeController)控制器
1、ReplicaSet:适合无状态的服务部署
用户创建指定数量的pod副本数量,确保pod副本数量符合预期状态,并且支持滚动式自动扩容和缩容功能。
ReplicaSet主要三个组件组成
注意点:
帮助用户管理无状态的pod资源,精确反应用户定义的目标数量,但是RelicaSet不是直接使用的控制器,而是使用Deployment。
2、deployment:适合无状态的服务部署,工作在ReplicaSet之上,用于管理无状态应用,目前来说最好的控制器。
支持滚动更新和回滚功能,还提供声明式配置。
3、StatefullSet:适合有状态的服务部署。需要学完存储卷后进行系统学习。
4、DaemonSet:一次部署,所有的node节点都会部署,一些典型的应用场景:
5、Job:一次性的执行任务,只要完成就立即退出,不需要重启或重建。
6、Cronjob:周期性的执行任务,周期性任务控制,不需要持续后台运行。
replication controller简称RC,是kubernetes系统中的核心概念之一,简单来说,它其实定义了一个期望的场景,即声明某种pod的副本数量在任意时刻都复合某个预期值,所以RC的定义包含以下部分
1、基本简介
ReplicationController
用来确保容器应用的副本数始终保持在用户定义的副本数,即如果有容器异常退出,会自动创建新的Pod来替代,而如果异常多出来的容器也会自动回收。2、环境所需镜像
docker pull nginx:1.17.10-alpine
docker pull nginx:1.18.0-alpine
docker pull nginx:1.19.2-alpine
3、ReplicaSet模板说明
apiVersion: apps/v1 #api版本定义
kind: ReplicaSet #定义资源类型为ReplicaSet
metadata: #元数据定义
name: guardwhy
namespace: default
spec: #ReplicaSet的规格定义
replicas: 2 #定义副本数量为2个
selector: #标签选择器,定义匹配pod的标签
matchLabels:
app: guardwhy
release: canary
template: #pod的模板定义
metadata: #pod的元数据定义
name: guardwhy-pod #自定义pod的名称
labels: #定义pod的标签,需要和上面定义的标签一致,也可以多出其他标签
app: guardwhy
release: canary
environment: qa
spec: #pod的规格定义
containers: #容器定义
- name: guardwhy-container #容器名称
image: nginx:1.17.10-alpine #容器镜像
ports: #暴露端口
- name: http
containerPort: 80
1、在k8sdemo01
工程创建replicasetdemo.yml
文件
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: replicasetdemo
labels:
app: replicasetdemo
spec:
replicas: 1
template:
metadata:
name: replicasetdemo
labels:
app: replicasetdemo
spec:
containers:
- name: replicasetdemo
image: nginx:1.17.10-alpine
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
restartPolicy: Always
selector:
matchLabels:
app: replicasetdemo
2、通过idea的Remote Host快速将yml文件上传k8s集群进行测试
[root@k8s-master01 controller]# ls
replicasetdemo.yml
# 1、运行ReplicaSet
[root@k8s-master01 controller]# kubectl apply -f replicasetdemo.yml
replicaset.apps/replicasetdemo created
# 2、查看rs控制器
[root@k8s-master01 controller]# kubectl get rs
NAME DESIRED CURRENT READY AGE
replicasetdemo 1 1 1 25s
# 3、查看pod信息
[root@k8s-master01 controller]# kubectl get pod
NAME READY STATUS RESTARTS AGE
replicasetdemo-2gllp 1/1 Running 0 34s
# 4、查看pod详细信息
[root@k8s-master01 controller]# kubectl describe pod replicasetdemo-2gllp
Name: replicasetdemo-2gllp
Namespace: default
Priority: 0
Node: k8s-node01/172.21.252.4
Start Time: Thu, 08 Jul 2021 22:43:10 +0800
Labels: app=replicasetdemo
Annotations: cni.projectcalico.org/podIP: 10.81.85.230/32
cni.projectcalico.org/podIPs: 10.81.85.230/32
Status: Running
IP: 10.81.85.230
IPs:
IP: 10.81.85.230
Controlled By: ReplicaSet/replicasetdemo
.....................
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled <unknown> default-scheduler Successfully assigned default/replicasetdemo-2gllp to k8s-node01
Normal Pulled 67s kubelet, k8s-node01 Container image "nginx:1.17.10-alpine" already present on machine
Normal Created 67s kubelet, k8s-node01 Created container replicasetdemo
Normal Started 67s kubelet, k8s-node01 Started container replicasetdemo
# 5、测试controller控制器下的pod删除、重新被controller控制器拉起
[root@k8s-master01 controller]# kubectl delete pod --all
pod "replicasetdemo-2gllp" deleted
[root@k8s-master01 controller]# kubectl get pod
NAME READY STATUS RESTARTS AGE
replicasetdemo-6kbdn 1/1 Running 0 110s
[root@k8s-master01 controller]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
replicasetdemo-6kbdn 1/1 Running 0 2m2s 10.81.58.214 k8s-node02 <none> <none>
[root@k8s-master01 controller]# curl 10.81.58.214
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
..............
</html>
# 6、修改pod的副本数量:通过命令行方式
[root@k8s-master01 controller]# kubectl scale replicaset replicasetdemo --replicas=3
replicaset.apps/replicasetdemo scaled
[root@k8s-master01 controller]# kubectl get pod
NAME READY STATUS RESTARTS AGE
replicasetdemo-6kbdn 1/1 Running 0 14m
replicasetdemo-g5664 1/1 Running 0 22s
replicasetdemo-t8ksq 1/1 Running 0 22s
# 7、修改pod的副本数量:通过资源清单方式
[root@k8s-master01 controller]# kubectl edit replicasets.apps replicasetdemo
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: apps/v1
kind: ReplicaSet
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"apps/v1","kind":"ReplicaSet","metadata":{"annotations":{},"labels":{"app":"replicasetdemo"},"name":"replicasetdemo","namespace":"default"},"spec":{"replicas":1,"selector":{"matchLabels":{"app":"replicasetdemo"}},"template":{"metadata":{"labels":{"app":"replicasetdemo"},"name":"replicasetdemo"},"spec":{"containers":[{"image":"nginx:1.17.10-alpine","imagePullPolicy":"IfNotPresent","name":"replicasetdemo","ports":[{"containerPort":80}]}],"restartPolicy":"Always"}}}}
creationTimestamp: "2021-07-08T14:43:10Z"
generation: 5
labels:
app: replicasetdemo
name: replicasetdemo
namespace: default
resourceVersion: "294582"
selfLink: /apis/apps/v1/namespaces/default/replicasets/replicasetdemo
"/tmp/kubectl-edit-6ttin.yaml" 52L, 1886C
[root@k8s-master01 controller]# kubectl get pod
NAME READY STATUS RESTARTS AGE
replicasetdemo-47672 1/1 Running 0 11s
replicasetdemo-6kbdn 1/1 Running 0 19m
replicasetdemo-g5664 1/1 Running 0 5m39s
replicasetdemo-smddx 1/1 Running 0 11s
replicasetdemo-t8ksq 1/1 Running 0 5m39s
[root@k8s-master01 controller]#
3、显示Pod标签
# 1、显示pod的标签
[root@k8s-master01 controller]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
replicasetdemo-47672 1/1 Running 0 13m app=replicasetdemo
replicasetdemo-6kbdn 1/1 Running 0 32m app=replicasetdemo
replicasetdemo-g5664 1/1 Running 0 18m app=replicasetdemo
replicasetdemo-smddx 1/1 Running 0 13m app=replicasetdemo
replicasetdemo-t8ksq 1/1 Running 0 18m app=replicasetdemo
# 2、修改pod标签(label)
[root@k8s-master01 controller]# kubectl label pod replicasetdemo-g5664 app=guardwhy --overwrite=True
pod/replicasetdemo-g5664 labeled
# 3、再次显示pod的标签
[root@k8s-master01 controller]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
replicasetdemo-47672 1/1 Running 0 15m app=replicasetdemo
replicasetdemo-6kbdn 1/1 Running 0 34m app=replicasetdemo
replicasetdemo-g5664 1/1 Running 0 21m app=guardwhy
replicasetdemo-hsbx5 1/1 Running 0 4s app=replicasetdemo
replicasetdemo-smddx 1/1 Running 0 15m app=replicasetdemo
replicasetdemo-t8ksq 1/1 Running 0 21m app=replicasetdemo
# 4、查看rs控制器
[root@k8s-master01 controller]# kubectl get rs
NAME DESIRED CURRENT READY AGE
replicasetdemo 5 5 5 40m
# 4、查看控制器
[root@k8s-master01 controller]# kubectl delete rs replicasetdemo
replicaset.apps "replicasetdemo" deleted
[root@k8s-master01 controller]# kubectl get rs
No resources found in default namespace.
[root@k8s-master01 controller]#
1、kubectl命令行工具适用于RC的绝大部分命令同样适用于ReplicaSet,当前很少单独适用ReplicaSet
,它主要被Deployment
这个更高层的资源对象所使用,从而形成一整套Pod创建,删除,更新的编排机制,在使用Deployment
时无需关心它是如何维护和创建ReplicaSet
的,这一切都是自动发生的。
2、总结一下RC(ReplicaSet)
特性和作用
1、Deployment是kubernetes在1.2版本中引入的新概念,用于更好的解决Pod的编排问题,keployment在内部使用了ReplicaSet来实现目的,可以把Deployment理解为ReplicaSet的一次升级。
2、Deployment的使用过程
Deployment
对象来生成对应的ReplicaSet
并完成Pod副本的创建。Deployment
的状态来看部署动作是否完成(Pod副本数量是否达到了预期的值)。Deployment
以创建新的Pod(比如镜像升级),如果当前Deployment
不稳定,可以回滚到一个早先的Deployment
版本。Deployment
以便于一次性修改多个PodTemplateSpec
的配置项,之后在恢复Deployment
,进行新的发布。Deployment
以应对高负载,查看Deployment
的状态,以此作为发布是否成功的标志,清理不在需要的旧版本ReplicaSet
。1、可以通过kubectl
命令行方式获取更加详细的信息
kubectl explain deploy
kubectl explain deploy.spec
2、在k8sdemo01
工程创建deploymentdemo.yml
文件
Deployment除了API生命与Kind类型有区别,Deployment的定义与Replica Set的定义很类似。
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploymentdemo
labels:
app: deploymentdemo
spec:
replicas: 3
template:
metadata:
name: deploymentdemo
labels:
app: deploymentdemo
spec:
containers:
- name: deploymentdemo
image: nginx:1.17.10-alpine
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
restartPolicy: Always
selector:
matchLabels:
app: deploymentdemo
3、通过idea的Remote Host快速将yml文件上传k8s集群进行测试
[root@k8s-master01 controller]# ls
deploymentdemo.yml replicasetdemo.yml
# 1、运行Deployment
[root@k8s-master01 controller]# kubectl apply -f deploymentdemo.yml
deployment.apps/deploymentdemo created
# 2、查看deployment
[root@k8s-master01 controller]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
deploymentdemo 3/3 3 3 22s
# 3、查看rs控制器
[root@k8s-master01 controller]# kubectl get rs
NAME DESIRED CURRENT READY AGE
deploymentdemo-cff9d5c4d 3 3 3 35s
# 4、查看pod
[root@k8s-master01 controller]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deploymentdemo-cff9d5c4d-csq4d 1/1 Running 0 42s
deploymentdemo-cff9d5c4d-kdl6w 1/1 Running 0 42s
deploymentdemo-cff9d5c4d-zjhn9 1/1 Running 0 42s
[root@k8s-master01 controller]# kubectl exec -it deploymentdemo-cff9d5c4d-csq4d sh
/ # nginx -v
nginx version: nginx/1.17.10
/ # exit
[root@k8s-master01 controller]#
1、命令行方式
# 1、升级nginx镜像版本为1.18.0
[root@k8s-master01 controller]# kubectl set image deployment deploymentdemo deploymentdemo=nginx:1.18.0-alpine
deployment.apps/deploymentdemo image updated
# 2、查看pod
[root@k8s-master01 controller]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deploymentdemo-795cbd9bbd-d4vml 1/1 Running 0 2m2s
deploymentdemo-795cbd9bbd-nzs8w 1/1 Running 0 62s
deploymentdemo-795cbd9bbd-qlcvc 1/1 Running 0 94s
# 3、进去某一个pod内部,查看nginx升级版本信息
[root@k8s-master01 controller]# kubectl exec -it deploymentdemo-795cbd9bbd-d4vml sh
/ # nginx -v
nginx version: nginx/1.18.0
/ # exit
[root@k8s-master01 controller]#
2、yml文件方式
[root@k8s-master01 controller]# ls
deploymentdemo.yml replicasetdemo.yml
# 1、升级nginx镜像版本为1.19.2-alpine
[root@k8s-master01 controller]# kubectl edit deployments.apps deploymentdemo
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "2"
kubectl.kubernetes.io/last-applied-configuration: |
creationTimestamp: "2021-07-09T02:04:24Z"
..........
spec:
containers:
- image: nginx:1.19.2-alpine
imagePullPolicy: IfNotPresent
deployment.apps/deploymentdemo edited
# 2、查看pod升级情况
[root@k8s-master01 controller]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deploymentdemo-56fb57bf59-7lxp9 1/1 Running 0 87s
deploymentdemo-56fb57bf59-9lcz7 1/1 Running 0 43s
deploymentdemo-56fb57bf59-cjzd2 1/1 Running 0 2s
deploymentdemo-795cbd9bbd-d4vml 0/1 Terminating 1 3h55m
deploymentdemo-795cbd9bbd-qlcvc 0/1 Terminating 1 3h54m
# 3、进去某一个pod内部,查看nginx升级版本信息
[root@k8s-master01 controller]# kubectl exec -it deploymentdemo-56fb57bf59-7lxp9 sh
/ # nginx -v
nginx version: nginx/1.19.2
/ # exit
[root@k8s-master01 controller]#
1、命令行方式
[root@k8s-master01 ~]# cd /home/data/controller/
[root@k8s-master01 controller]# ls
deploymentdemo.yml replicasetdemo.yml
[root@k8s-master01 controller]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deploymentdemo-56fb57bf59-7lxp9 1/1 Running 1 137m
deploymentdemo-56fb57bf59-9lcz7 1/1 Running 1 137m
deploymentdemo-56fb57bf59-cjzd2 1/1 Running 1 136m
[root@k8s-master01 controller]# kubectl scale deployment deploymentdemo --replicas=6
deployment.apps/deploymentdemo scaled
[root@k8s-master01 controller]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deploymentdemo-56fb57bf59-576x6 1/1 Running 0 3s
deploymentdemo-56fb57bf59-7lxp9 1/1 Running 1 138m
deploymentdemo-56fb57bf59-9lcz7 1/1 Running 1 137m
deploymentdemo-56fb57bf59-cjzd2 1/1 Running 1 136m
deploymentdemo-56fb57bf59-ptbxq 1/1 Running 0 3s
deploymentdemo-56fb57bf59-xb2zh 1/1 Running 0 3s
[root@k8s-master01 controller]#
2、yml文件方式
[root@k8s-master01 controller]# kubectl edit deployments.apps deploymentdemo
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "3"
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app":"deploymentdemo"},"name":"deploymentdemo","namespace":"default"},"spec":{"replicas":3,"selector":{"matchLabels":{"app":"deploymentdemo"}},"template":{"metadata":{"labels":{"app":"deploymentdemo"},"name":"deploymentdemo"},"spec":{"containers":[{"image":"nginx:1.17.10-alpine","imagePullPolicy":"IfNotPresent","name":"deploymentdemo","ports":[{"containerPort":80}]}],"restartPolicy":"Always"}}}}
creationTimestamp: "2021-07-09T02:04:24Z"
generation: 4
labels:
app: deploymentdemo
name: deploymentdemo
namespace: default
resourceVersion: "336158"
selfLink: /apis/apps/v1/namespaces/default/deployments/deploymentdemo
uid: 9d7ed144-738c-4e6f-a0e0-d1727a1e7c37
spec:
progressDeadlineSeconds: 600
replicas: 4
revisionHistoryLimit: 10
selector:
deployment.apps/deploymentdemo edited
[root@k8s-master01 controller]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deploymentdemo-56fb57bf59-576x6 1/1 Running 0 4m13s
deploymentdemo-56fb57bf59-7lxp9 1/1 Running 1 142m
deploymentdemo-56fb57bf59-9lcz7 1/1 Running 1 141m
deploymentdemo-56fb57bf59-cjzd2 1/1 Running 1 141m
[root@k8s-master01 controller]#
1、基本概述
微服务部署种类:蓝绿部署、滚动部署、灰度发布、金丝雀发布。
2、金丝雀发布
Deployment控制器支持自定义控制更新过程中的滚动节奏,如暂停(pause)
或继续(resume)
更新操作。比如等待第一批新的Pod资源创建完成后立即暂停更新过程,此时,仅存在一部分新版本的应用,主体部分还是旧的版本。再筛选一小部分的用户请求路由到新版本的Pod应用,继续观察能否稳定地按期望的方式运行,确定没问题之后再继续完成余下的Pod资源滚动更新,否则立即回滚更新操作这就是所谓的金丝雀发布(Canary Release
)
# 1、查看deployment
[root@k8s-master01 ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
deploymentdemo 4/4 4 4 10h
[root@k8s-master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deploymentdemo-56fb57bf59-576x6 1/1 Running 1 3h18m
deploymentdemo-56fb57bf59-7lxp9 1/1 Running 2 5h36m
deploymentdemo-56fb57bf59-9lcz7 1/1 Running 2 5h35m
deploymentdemo-56fb57bf59-cjzd2 1/1 Running 2 5h35m
[root@k8s-master01 ~]# kubectl exec -it deploymentdemo-56fb57bf59-576x6 sh
/ # nginx -v
nginx version: nginx/1.19.2
/ # exit
[root@k8s-master01 controller]# ls
deploymentdemo.yml replicasetdemo.yml
# 2、更新deployment的nginx:1.18.0-alpine版本,并配置暂停deployment
[root@k8s-master01 controller]# kubectl set image deployment deploymentdemo deploymentdemo=nginx:1.18.0-alpine && kubectl rollout pause deployment deploymentdemo
deployment.apps/deploymentdemo image updated
deployment.apps/deploymentdemo paused
[root@k8s-master01 controller]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
deploymentdemo-56fb57bf59-576x6 1/1 Running 1 3h31m
deploymentdemo-56fb57bf59-9lcz7 1/1 Running 2 5h49m
deploymentdemo-56fb57bf59-cjzd2 1/1 Running 2 5h48m
deploymentdemo-795cbd9bbd-rxn9c 1/1 Running 0 40s
deploymentdemo-795cbd9bbd-xdlh5 1/1 Running 0 40s
^C
# 3、观察更新状态
[root@k8s-master01 controller]# kubectl rollout resume deployment deploymentdemo
deployment.apps/deploymentdemo resumed
# 4、监控更新的过程,可以看到已经新增了一个资源,但是并未删除一个旧的资源,就是因为使用了pause暂停命令
[root@k8s-master01 controller]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
deploymentdemo-795cbd9bbd-fs75g 1/1 Running 0 21s
deploymentdemo-795cbd9bbd-rxn9c 1/1 Running 0 6m46s
deploymentdemo-795cbd9bbd-vwvn4 1/1 Running 0 21s
deploymentdemo-795cbd9bbd-xdlh5 1/1 Running 0 6m46s
^C[root@k8s-master01 controller]#
[root@k8s-master01 controller]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deploymentdemo-795cbd9bbd-fs75g 1/1 Running 0 43s
deploymentdemo-795cbd9bbd-rxn9c 1/1 Running 0 7m8s
deploymentdemo-795cbd9bbd-vwvn4 1/1 Running 0 43s
deploymentdemo-795cbd9bbd-xdlh5 1/1 Running 0 7m8s
# 5、进去某一个pod内部,查看nginx更新版本信息
[root@k8s-master01 controller]# kubectl exec -it deploymentdemo-795cbd9bbd-fs75g sh
/ # nginx -v
nginx version: nginx/1.18.0
/ # exit
[root@k8s-master01 controller]#
1、默认情况下,kubernetes 会在系统中保存前两次的 Deployment 的 rollout 历史记录,以便可以随时回退(可以修改 revision history limit
来更改保存的revision数)。
2、只要 Deployment 的 rollout
被触发就会创建一个revision
,当且仅当Deployment 的Pod template
被更改,例如更新template 中的 label 和容器镜像时,就会创建出一个新的 revision。
3、其他的更新,比如扩容 Deployment 不会创建 revision
。因此可以很方便的手动或者自动扩容。这意味着当回退到历史 revision 时,只有 Deployment 中的 Pod template
部分才会回退。
4、rollout常见命令
常见命令 | 功能具体说明 |
---|---|
history | 查看rollout操作历史。 |
pause | 将提供的资源设定为暂停状态。 |
restart | 重启某资源。 |
resume | 将某资源从暂停状态恢复正常。 |
status | 查看rollout操作状态。 |
undo | 回滚前一rollout。 |
# 1、history历史操作
[root@k8s-master01 controller]# kubectl rollout history deployment deploymentdemo
deployment.apps/deploymentdemo
REVISION CHANGE-CAUSE
1 <none>
3 <none>
4 <none>
# 2、回滚版本信息
[root@k8s-master01 controller]# kubectl rollout undo deployment deploymentdemo
deployment.apps/deploymentdemo rolled back
# 3、查看pod回滚情况
[root@k8s-master01 controller]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
deploymentdemo-56fb57bf59-729m2 1/1 Running 0 11s
deploymentdemo-56fb57bf59-fldk5 1/1 Running 0 12s
deploymentdemo-56fb57bf59-msl9r 1/1 Running 0 12s
deploymentdemo-56fb57bf59-wmrlw 1/1 Running 0 10s
deploymentdemo-795cbd9bbd-rxn9c 0/1 Terminating 0 43m
deploymentdemo-795cbd9bbd-rxn9c 0/1 Terminating 0 43m
deploymentdemo-795cbd9bbd-rxn9c 0/1 Terminating 0 43m
^C[root@k8s-master01 controller]#
[root@k8s-master01 controller]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deploymentdemo-56fb57bf59-729m2 1/1 Running 0 30s
deploymentdemo-56fb57bf59-fldk5 1/1 Running 0 31s
deploymentdemo-56fb57bf59-msl9r 1/1 Running 0 31s
deploymentdemo-56fb57bf59-wmrlw 1/1 Running 0 29s
# 4、进去某一个pod内部,查看nginx回滚版本信息
[root@k8s-master01 controller]# kubectl exec -it deploymentdemo-56fb57bf59-729m2 sh
/ # nginx -v
nginx version: nginx/1.19.2
/ # exit
[root@k8s-master01 controller]#
1、Deployment 可以保证在升级时只有一定数量的 Pod 是 down 的。默认的,它会确保至少有比期望的Pod数量少,一个是up状态(最多一个不可用)
2、Deployment 同时也可以确保只创建出超过期望数量的一定数量的 Pod。默认的,它会确保最多比期望的Pod数量多一个的Pod 是up的(最多1个 surge)。
[root@k8s-master01 controller]# kubectl describe deployments.apps deploymentdemo
Name: deploymentdemo
Namespace: default
CreationTimestamp: Fri, 09 Jul 2021 10:04:24 +0800
Labels: app=deploymentdemo
Annotations: deployment.kubernetes.io/revision: 5
kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app":"deploymentdemo"},"name":"deploymentdemo","namesp...
Selector: app=deploymentdemo
Replicas: 4 desired | 4 updated | 4 total | 4 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
## 1、Kuberentes 版本v1.17.5中,从1-1变成25%-25%
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=deploymentdemo
Containers:
..........
Normal ScalingReplicaSet 48m deployment-controller Scaled up replica set deploymentdemo-56fb57bf59 to 4
Normal ScalingReplicaSet 48m deployment-controller Scaled down replica set deploymentdemo-795cbd9bbd to 0
[root@k8s-master01 controller]#
Deployment为Pod和Replica Set(下一代Replication Controller)提供声明式更新,只需要在 Deployment 中描述想要的目标状态是什么。Deployment controller
就会帮将 Pod 和ReplicaSet 的实际状态改变到目标状态。也可以通过定义一个全新的 Deployment 来创建 ReplicaSet或者删除已有的 Deployment 并创建一个新的来替换。
1、DaemonSet 确保全部Node 上运行一个 Pod 的副本,当有 Node 加入集群时,也会为它们新增一个 Pod 。当有 Node 从集群移除时,这些 Pod 也会被回收删除 DaemonSet 将会删除它创建的所有Pod。
2、在每一个node节点上只调度一个Pod,因此无需指定replicas的个数,比如
1、在k8sdemo01
工程创建daemonsetdemo.yml
文件
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: daemonsetdemo
labels:
app: daemonsetdemo
spec:
template:
metadata:
name: daemonsetdemo
labels:
app: daemonsetdemo
spec:
containers:
- name: daemonsetdemo
image: nginx:1.17.10-alpine
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
restartPolicy: Always
selector:
matchLabels:
app: daemonsetdemo
2、通过idea的Remote Host快速将yml文件上传k8s集群进行测试
[root@k8s-master01 controller]# ls
daemonsetdemo.yml deploymentdemo.yml replicasetdemo.yml
# 1、运行demonset
[root@k8s-master01 controller]# kubectl apply -f daemonsetdemo.yml
daemonset.apps/daemonsetdemo created
[root@k8s-master01 controller]# kubectl get daemonset
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
daemonsetdemo 2 2 2 2 2 <none> 20s
[root@k8s-master01 controller]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
daemonsetdemo-7pm5f 1/1 Running 0 40s 10.81.85.252 k8s-node01 <none> <none>
daemonsetdemo-d2crz 1/1 Running 0 40s 10.81.58.238 k8s-node02 <none> <none>
[root@k8s-master01 controller]# kubectl delete pod daemonsetdemo-7pm5f
pod "daemonsetdemo-7pm5f" deleted
# 2、查看pod详细信息:只有工作节点创建pod,master节点并不会创建。
[root@k8s-master01 controller]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
daemonsetdemo-4pgc9 1/1 Running 0 2m40s 10.81.85.253 k8s-node01 <none> <none>
daemonsetdemo-d2crz 1/1 Running 0 4m20s 10.81.58.238 k8s-node02 <none> <none>
[root@k8s-master01 controller]#
1、DaemonSet有两种更新策略类型
OnDelete
:这是向后兼容性的默认更新策略。使用OnDelete
更新策略,在更新DaemonSet
模板后,只有在手动删除旧的DaemonSet pod
时才会创建新的DaemonSet pod
。这与Kubernetes1.5或更早版本中DaemonSet
的行为相同。RollingUpdate
更新策略,在更新DaemonSet
模板后,旧的DaemonSet pod
将被终止,并且将以受控方式自动创建新的DaemonSet pod
。1、使用基本镜像
docker pull perl:slim
2、具体简介
1、在k8sdemo01
工程创建jobdemo.yml
文件
apiVersion: batch/v1
kind: Job
metadata:
name: jobdemo
labels:
app: jobdemo
spec:
template:
metadata:
name: jobdemo
labels:
app: jobdemo
spec:
containers:
- name: jobdemo
image: perl:slim
imagePullPolicy: IfNotPresent
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(6000)"]
restartPolicy: Never
2、通过idea的Remote Host快速将yml文件上传k8s集群进行测试
[root@k8s-master01 controller]# ls
daemonsetdemo.yml deploymentdemo.yml jobdemo.yml replicasetdemo.yml
[root@k8s-master01 controller]# kubectl apply -f jobdemo.yml
job.batch/jobdemo created
[root@k8s-master01 controller]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
jobdemo-cc5wr 0/1 ContainerCreating 0 29s
jobdemo-cc5wr 1/1 Running 0 32s
jobdemo-cc5wr 0/1 Completed 0 81s
^C[root@k8s-master01 controller]#
[root@k8s-master01 controller]# kubectl delete -f jobdemo.yml
job.batch "jobdemo" deleted
[root@k8s-master01 controller]# kubectl get pod
No resources found in default namespace.
[root@k8s-master01 controller]#
1、在kubernetes系统中,Pod的管理对象RC
,Deployment
,DaemonSet
,Job
都面向无状态的服务,但现实中有很多服务时有状态的,集群有这四个特点:
2、通过RC
或Deployment
控制Pod副本数量来实现上述有状态的集群,发现第一点是无法满足的,因为Pod名称和ip是随机产生的,并且各Pod中的共享存储中的数据不能都动,因此StatefulSet在这种情况下就派上用场了。StatefulSet具有以下特性:
3、结论
StatefulSet除了要与PV卷
捆绑使用以存储Pod的状态数据,还要与Headless
,Service
配合使用,每个StatefulSet
定义中都要生命它属于哪个Handless Service
,Handless Service
与普通Service
的关键区别在于,它没有Cluster IP
。