Kubernetes(k8s)集群部署五、控制器(rs,rc,Deployment,DaemonSet、StatefulSet、Job、CronJob、HPA)
作者:互联网
控制器
一、 Pod 的分类:
自主式 Pod:Pod 退出后不会被创建(没有控制器)
控制器管理的 Pod:在控制器的生命周期里,始终要维持 Pod 的副本数目
二、控制器类型:
Replication Controller和ReplicaSet
Deployment
DaemonSet
StatefulSet:前提:唯一的网络标识,持久化存储
Job
CronJob
HPA全称Horizontal Pod Autoscaler #自动化扩容
1 Replication Controller(rc)和ReplicaSet(rs)
ReplicaSet 是下一代的 Replication Controller,官方推荐使用ReplicaSet。
ReplicaSet 和 Replication Controller 的唯一区别是选择器的支持,ReplicaSet 支持新的基于集合的选择器需求。ReplicaSet 确保任何时间都有指定数量的 Pod 副本在运行
。
虽然 ReplicaSets 可以独立使用,但今天它主要被Deployments 用作协调 Pod 创建、删除和更新的机制。
1.1ReplicaSet控制器示例:
官方文档
https://kubernetes.io/zh/docs/concepts/workloads/controllers/replicaset/
[root@server2 ~]# cat rs.yml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: replicaset-example
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: myapp:v1
控制副本数
2.Deployment
Deployment 为 Pod 和 ReplicaSet 提供了一个申明式的定义方法。
典型的应用场景:
用来创建Pod和ReplicaSet
滚动更新和回滚
扩容和缩容
暂停与恢复
2.1Deployment控制器示例
deployment 连rs ,rs控制副本数
本身rs也是一个控制器,deployment主要更新,灰度发布
[root@server2 ~]# cat deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment
spec:
replicas: 3
selector:
matchLabels: ###pod 上匹配标签是nginx,才会进行操作
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: myapp:v1
[root@server2 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
demo 1/1 Running 6 22h
deployment-6456d7c676-p4x6p 1/1 Running 0 17s
deployment-6456d7c676-rspg8 1/1 Running 0 17s
deployment-6456d7c676-t4bgd 1/1 Running 0 17s
[root@server2 ~]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
demo 1/1 Running 6 23h run=demo
deployment-6456d7c676-p4x6p 1/1 Running 0 3m34s app=nginx,pod-template-hash=6456d7c676
当修改标签的时候,会有一个新的pod,nginx标签依然保持3个副本
[root@server2 ~]# kubectl label pod deployment-6456d7c676-p4x6p app=myapp --overwrite
pod/deployment-6456d7c676-p4x6p labeled
[root@server2 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
demo 1/1 Running 6 23h
deployment-6456d7c676-p4x6p 1/1 Running 0 5m25s
deployment-6456d7c676-qgttz 1/1 Running 0 7s
deployment-6456d7c676-rspg8 1/1 Running 0 5m25s
deployment-6456d7c676-t4bgd 1/1 Running 0 5m25s
[root@server2 ~]# kubectl get pod -L app
NAME READY STATUS RESTARTS AGE APP
demo 1/1 Running 6 23h
deployment-6456d7c676-p4x6p 1/1 Running 0 6m16s myapp
deployment-6456d7c676-qgttz 1/1 Running 0 58s nginx
deployment-6456d7c676-rspg8 1/1 Running 0 6m16s nginx
deployment-6456d7c676-t4bgd 1/1 Running 0 6m16s nginx
[root@server2 ~]#
当把文件里面的镜像myapp:v1改变为myapp:v2时回收rs里面所有的副本,创建新的rs,在新的rs创建新的pod,原本的rs不会删除,以便回滚,除非删除控制器, 通过标签匹配pod,一旦改变标签,不受管控,因为控制器已经定义了标签
[root@server2 ~]# cat rs.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: myapp:v2 ###进行更新
[root@server2 ~]# kubectl get pod -L app
NAME READY STATUS RESTARTS AGE APP
demo 1/1 Running 6 23h
deployment-6456d7c676-p4x6p 1/1 Running 0 9m19s myapp
deployment-6456d7c676-rspg8 0/1 Terminating 0 9m19s nginx
deployment-6456d7c676-t4bgd 0/1 Terminating 0 9m19s nginx
deployment-6d4f5bf58f-4qfbq 1/1 Running 0 13s nginx
deployment-6d4f5bf58f-b6whd 1/1 Running 0 15s nginx
deployment-6d4f5bf58f-mk949 1/1 Running 0 12s nginx
[root@server2 ~]# kubectl get pod -L app
NAME READY STATUS RESTARTS AGE APP
demo 1/1 Running 6 23h
deployment-6456d7c676-p4x6p 1/1 Running 0 9m28s myapp
deployment-6d4f5bf58f-4qfbq 1/1 Running 0 22s nginx
deployment-6d4f5bf58f-b6whd 1/1 Running 0 24s nginx
deployment-6d4f5bf58f-mk949 1/1 Running 0 21s nginx
之前的rs并不会被删除deployment-6456d7c676
[root@server2 ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
deployment-6456d7c676 0 0 0 10m
deployment-6d4f5bf58f 3 3 3 66s
回滚
当再次改变文件----》 myapp:v1 又恢复为之前的状态
[root@server2 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
demo 1/1 Running 6 23h
deployment-6456d7c676-jbdx9 1/1 Running 0 9s
deployment-6456d7c676-mzggd 1/1 Running 0 12s
deployment-6456d7c676-wpzbn 1/1 Running 0 10s
deployment-6d4f5bf58f-4qfbq 0/1 Terminating 0 101s
deployment-6d4f5bf58f-b6whd 0/1 Terminating 0 103s
deployment-6d4f5bf58f-mk949 0/1 Terminating 0 100s
[root@server2 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
demo 1/1 Running 6 23h
deployment-6456d7c676-jbdx9 1/1 Running 0 24s
deployment-6456d7c676-mzggd 1/1 Running 0 27s
deployment-6456d7c676-wpzbn 1/1 Running 0 25s
[root@server2 ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
deployment-6456d7c676 3 3 3 11m
deployment-6d4f5bf58f 0 0 0 2m11s
补充: 标签
创建svc
[root@server2 ~]# kubectl expose deployment deployment --port=80
service/deployment exposed
[root@server2 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
deployment ClusterIP 10.108.76.242 <none> 80/TCP 3s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 9d
[root@server2 ~]# kubectl describe svc deployment
Name: deployment
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=nginx
Type: ClusterIP
IP Families: <none>
IP: 10.108.76.242
IPs: 10.108.76.242
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.1.57:80,10.244.1.58:80,10.244.1.59:80 ###三个
Session Affinity: None
Events: <none>
[root@server2 ~]# kubectl label pod deployment-6456d7c676-jbdx9 app=myapp --overwrite
pod/deployment-6456d7c676-jbdx9 labeled
[root@server2 ~]# kubectl get pod -L app
NAME READY STATUS RESTARTS AGE APP
demo 1/1 Running 6 23h
deployment-6456d7c676-b478b 1/1 Running 0 3s nginx
deployment-6456d7c676-jbdx9 1/1 Running 0 14m myapp
deployment-6456d7c676-mzggd 1/1 Running 0 14m nginx
deployment-6456d7c676-wpzbn 1/1 Running 0 14m nginx
[root@server2 ~]# kubectl edit svc deployment
service/deployment edited
selector:
app: myapp
[root@server2 ~]# kubectl describe svc deployment
Name: deployment
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=myapp
Type: ClusterIP
IP Families: <none>
IP: 10.108.76.242
IPs: 10.108.76.242
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.1.59:80 ###根据标签选择
Session Affinity: None
Events: <none>
3.DaemonSet
DaemonSet 确保全部(或者某些)节点上运行一个 Pod 的副本。当有节点加入集群时, 也会为他们新增一个 Pod 。当有节点从集群移除时,这些 Pod 也会被回收。删除 DaemonSet 将会删除它创建的所有 Pod。
DaemonSet 的典型用法:
在每个节点上运行集群存储 DaemonSet,例如 glusterd、ceph。
在每个节点上运行日志收集 DaemonSet,例如 fluentd、logstash。
在每个节点上运行监控 DaemonSet,
例如 Prometheus Node Exporter、zabbix agent等
一个简单的用法是在所有的节点上都启动一个 DaemonSet,将被作为每种类型的 daemon 使用。
一个稍微复杂的用法是单独对每种 daemon 类型使用多个 DaemonSet,但具有不同的标志, 并且对不同硬件类型具有不同的内存、CPU 要求。
3.1DaemonSet控制器示例
一个节点布一个 但只有一个server3节点,效果不太明显
[root@server1 harbor]# docker pull zabbix/zabbix-agent
docker tag zabbix/zabbix-agent:latest reg.westos.org/library/zabbix-agent:latest
docker push reg.westos.org/library/zabbix-agent:latest
[root@server2 ~]# cat daemonset.yml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: daemonset-example
labels:
k8s-app: zabbix-agent
spec:
selector:
matchLabels:
name: zabbix-agent
template:
metadata:
labels:
name: zabbix-agent
spec:
containers:
- name: zabbix-agent
image: zabbix-agent
[root@server2 ~]# kubectl apply -f daemonset.yml
daemonset.apps/daemonset-example created
[root@server2 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
daemonset-example-gj4dm 1/1 Running 0 5s
demo 1/1 Running 6 23h
[root@server2 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
daemonset-example-gj4dm 1/1 Running 0 14s 10.244.1.61 server3 <none> <none>
demo 1/1 Running 6 23h 10.244.1.40 server3 <none> <none>
4.StatefulSet
StatefulSet 是用来管理有状态应用的工作负载 API 对象。实例之间有不对等关系,以及实例对外部数据有依赖关系的应用,称为“有状态应用”
StatefulSet 用来管理 Deployment 和扩展一组 Pod,并且能为这些 Pod 提供序号和唯一性保证。
StatefulSets 对于需要满足以下一个或多个需求的应用程序很有价值:
稳定的、唯一的网络标识符。
稳定的、持久的存储。
有序的、优雅的部署和缩放。有序的
、自动的滚动更新。
5.job控制器
执行批处理任务,仅执行一次任务,保证任务的一个或多个Pod成功结束。
5.1job控制器示例
私有仓库server1中准备好镜像
docker pull perl
docker tag perl:latest reg.westos.org/library/perl:latest
docker push reg.westos.org/library/perl:latest
[root@server2 ~]# cat job.yml
apiVersion: batch/v1
kind: Job
metadata:
name: pi
spec:
template:
spec:
containers:
- name: pi
image: perl
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never #不重启
backoffLimit: 4 #如果发生错误,停止
[root@server2 ~]# kubectl apply -f job.yml
job.batch/pi created
[root@server2 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
demo 1/1 Running 6 23h
pi-59ld8 0/1 ContainerCreating 0 15s
[root@server2 ~]# kubectl describe pod
demo pi-59ld8
[root@server2 ~]# kubectl describe pod pi-59ld8
Name: pi-59ld8
Namespace: default
Priority: 0
Node: server3/172.25.10.3
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 34s default-scheduler Successfully assigned default/pi-59ld8 to server3
Normal Pulling 33s kubelet Pulling image "perl"
Normal Pulled 11s kubelet Successfully pulled image "perl" in 21.814470215s
Normal Created 11s kubelet Created container pi
Normal Started 11s kubelet Started container pi
[root@server2 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
demo 1/1 Running 6 23h
pi-59ld8 0/1 Completed 0 47s
[root@server2 ~]# kubectl logs pi-59ld8
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981
6.CronJob控制器
Cron Job 创建基于时间调度的 Jobs。
一个 CronJob 对象就像 crontab (cron table) 文件中的一行,它用 Cron 格式进行编写,并周期性地在给定的调度时间执行 Job。
https://kubernetes.io/zh/docs/concepts/workloads/controllers/cron-jobs/
Cron 时间表语法
# ┌───────────── 分钟 (0 - 59)
# │ ┌───────────── 小时 (0 - 23)
# │ │ ┌───────────── 月的某天 (1 - 31)
# │ │ │ ┌───────────── 月份 (1 - 12)
# │ │ │ │ ┌───────────── 周的某天 (0 - 6) (周日到周一;在某些系统上,7 也是星期日)
# │ │ │ │ │
# │ │ │ │ │
# │ │ │ │ │
# * * * * *
6.1 CronJob控制器示例
每隔一分钟执行
[root@server2 ~]# cat cronjob.yml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: hello
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busyboxplus
imagePullPolicy: IfNotPresent
args:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure
[root@server2 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
demo 1/1 Running 6 24h
hello-1614713340-rsrj5 0/1 Completed 0 4s
pi-59ld8 0/1 Completed 0 6m11s
[root@server2 ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
demo 1/1 Running 6 24h
hello-1614713340-rsrj5 0/1 Completed 0 4s
pi-59ld8 0/1 Completed 0 6m11s
[root@server2 ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/demo 1/1 Running 6 24h
pod/hello-1614713340-rsrj5 0/1 Completed 0 14s
pod/pi-59ld8 0/1 Completed 0 6m21s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 9d
NAME COMPLETIONS DURATION AGE
job.batch/hello-1614713340 1/1 1s 14s
job.batch/pi 1/1 28s 6m21s
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
cronjob.batch/hello */1 * * * * False 0 19s 28s
每隔一分钟调用一个
[root@server2 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
demo 1/1 Running 6 24h
hello-1614713340-rsrj5 0/1 Completed 0 97s
hello-1614713400-5s9fg 0/1 Completed 0 37s
pi-59ld8 0/1 Completed 0 7m44s
[root@server2 ~]# kubectl logs hello-1614713340-rsrj5
Tue Mar 2 19:29:06 UTC 2021
[root@server2 ~]# kubectl logs hello-1614713400-5s9fg
Tue Mar 2 19:30:06 UTC 2021
7. HPA
根据资源利用率自动调整service中Pod数量,实现Pod水平自动缩放。
标签:kubectl,6456d7c676,StatefulSet,Kubernetes,rs,server2,Running,deployment,root 来源: https://www.cnblogs.com/lidabo/p/16424387.html