kubernetes之常用Pod控制器的使用

生活中,最使人疲惫的往往不是道路的遥远,而是心中的郁闷;最使人痛苦的往往不是生活的不幸,而是希望的破灭;最使人颓废的往往不是前途的坎坷,而是自信的丧失;最使人绝望的往往不是挫折的打击,而是心灵的死亡。所以我们要有自己的梦想,让梦想的星光指引着我们走出落漠,走出惆怅,带着我们走进自己的理想。

导读:本篇文章讲解 kubernetes之常用Pod控制器的使用,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

Pod控制器概述

pod的创建方式分为两类

自主式pod:直接创建出来的Pod,这种pod删除后就没有了,也不会重建

控制器创建的pod:通过控制器创建的pod,这种pod删除了之后还会自动重建 

Pod控制器分类

ReplicationController:比较原始的pod控制器,已经被废弃,由ReplicaSet替代

ReplicaSet:保证副本数量一直维持在期望值,并支持pod数量扩缩容,镜像版本升级

Deployment:通过控制ReplicaSet来控制Pod,并支持滚动升级、回退版本

Horizontal Pod Autoscaler:可以根据集群负载自动水平调整Pod的数量,实现削峰填谷

DaemonSet:在集群中的指定Node上运行且仅运行一个副本,一般用于守护进程类的任务

Job:它创建出来的pod只要完成任务就立即退出,不需要重启或重建,用于执行一次性任务

Cronjob:它创建的Pod负责周期性任务控制,不需要持续后台运行

StatefulSet:管理有状态应用

ReplicaSet(RS)

ReplicaSet的主要作用是保证一定数量的pod正常运行,它会持续监听这些Pod的运行状态,一旦Pod发生故障,就会重启或重建。同时它还支持对pod数量的扩缩容和镜像版本的升降级。

基本使用

创建vim replicaset-pod.yaml配置文件

apiVersion: apps/v1
kind: ReplicaSet   
metadata:
  name: replicaset-pod
  namespace: default 
spec:
  replicas: 3 # 指定副本数量,默认为1
  selector:  # 选择器,建立pod控制器和pod之间的关联关系;在pod模板上定义label,在控制器上定义选择器,表明该控制器管理哪些pod
    matchLabels: # Labels匹配规则
      app: nginx-pod
    matchExpressions: # Expressions匹配规则
      - {key: app, operator: In, values: [nginx-pod]}  
  template: # 模板,当前控制器创建pod所使用的模板;当副本数量不足时,按照模板创建pod副本
    metadata:
      labels:
        app: nginx-pod
    spec:
      containers:
      - name: nginx-name
        image: nginx

创建replicaset控制器

[k8s@administrator ~]$ kubectl create -f  replicaset-pod.yaml
replicaset.apps/replicaset-pod created

查看控制器创建的Pod

[k8s@administrator ~]$  kubectl get pod
NAME                   READY   STATUS    RESTARTS   AGE
replicaset-pod-6jx2r   1/1     Running   0          3m13s
replicaset-pod-p7vln   1/1     Running   0          3m13s
replicaset-pod-zph4z   1/1     Running   0          3m13s

查看replicaset控制器

DESIRED:期望副本数量  

CURRENT:当前副本数量  

READY:已经准备好提供服务的副本数量
[k8s@administrator ~]$ kubectl get rs replicaset-pod -o wide
NAME             DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES   SELECTOR
replicaset-pod   3         3         3       22s   nginx-name   nginx    app=nginx-pod,app in (nginx-pod)

删除ReplicaSet

删除此RS以及管理的Pod;

删除RS前,会将RS的replicasclear调整为0,等待所有的Pod被删除后,再执行RS对象的删除

kubectl delete replicaset replicaset-pod

删除RS对象,保留Pod

kubectl delete rs replicaset-pod --cascade=false

使用yaml直接删除

kubectl delete -f replicaset-pod.yaml

扩容与缩容

1.修改配置文件副本数量进行扩缩容

# 修改spec:replicas: 5
kubectl edit replicaset replicaset-pod
[k8s@administrator ~]$  kubectl get pod
NAME                   READY   STATUS              RESTARTS   AGE
replicaset-pod-6jx2r   1/1     Running             0          6m13s
replicaset-pod-jxvv2   0/1     ContainerCreating   0          3s
replicaset-pod-p7vln   1/1     Running             0          6m13s
replicaset-pod-rxch6   0/1     ContainerCreating   0          3s
replicaset-pod-zph4z   1/1     Running             0          6m13s

2.使用scale命令实现扩缩容

kubectl scale rs replicaset-pod --replicas=3
[k8s@administrator ~]$  kubectl get pod
NAME                   READY   STATUS    RESTARTS   AGE
replicaset-pod-6jx2r   1/1     Running   0          10m
replicaset-pod-p7vln   1/1     Running   0          10m
replicaset-pod-zph4z   1/1     Running   0          10m

镜像升级与降级

1.修改配置文件镜像版本号进行镜像升级、降级

kubectl edit rs replicaset-pod

spec:
      containers:
      - image: nginx:1.20
[k8s@administrator ~]$ kubectl get rs -o wide
NAME             DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES       SELECTOR
replicaset-pod   3         3         3       15m   nginx-name   nginx:1.20   app=nginx-pod,app in (nginx-pod)

2.命令方式

 kubectl set image rs replicaset-pod nginx-name=nginx:1.19
[k8s@administrator ~]$ kubectl get rs -o wide
NAME             DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES       SELECTOR
replicaset-pod   3         3         3       17m   nginx-name   nginx:1.19   app=nginx-pod,app in (nginx-pod)

Deployment(Deploy)

Deployment控制器并不直接管理pod,而是通过管理ReplicaSet来简介管理Pod。

支持发布的停止、继续

支持滚动升级和回滚版本

基本使用

创建vim deployment-pod.yaml配置文件

apiVersion: apps/v1
kind: Deployment      
metadata:
  name: deployment-pod
  namespace: default
spec: 
  replicas: 3
  revisionHistoryLimit: 5 # 保留历史版本,便于版本回退
  paused: false # 暂停部署,默认是false
  progressDeadlineSeconds: 600 # 部署超时时间(s),默认是600
  selector:
    matchLabels:
      app: nginx-pod
    matchExpressions: # Expressions匹配规则
      - {key: app, operator: In, values: [nginx-pod]}  
  template:
    metadata:
      labels:
        app: nginx-pod
    spec:
      containers:
      - name: nginx-name
        image: nginx

创建deployment

[k8s@administrator ~]$ kubectl create -f deployment-pod.yaml  --record
deployment.apps/deployment-pod created

查看deployment

UP-TO-DATE 最新版本的pod的数量

AVAILABLE  当前可用的pod的数量
[k8s@administrator ~]$ kubectl get deploy deployment-pod
NAME             READY   UP-TO-DATE   AVAILABLE   AGE
deployment-pod   3/3     3            3           52s

查看rs

[k8s@administrator ~]$ kubectl get rs
NAME                       DESIRED   CURRENT   READY   AGE
deployment-pod-65556755b   3         3         3       78s

查看pod

[k8s@administrator ~]$ kubectl get pods
NAME                             READY   STATUS    RESTARTS   AGE
deployment-pod-65556755b-79zns   1/1     Running   0          86s
deployment-pod-65556755b-sdztj   1/1     Running   0          86s
deployment-pod-65556755b-znx6h   1/1     Running   0          86s

删除deployment,对应rs和pod也将被删除

kubectl delete  -f deployment-pod.yaml

扩容与缩容

修改spec:replicas: 5

kubectl edit deploy deployment-pod

kubectl get deploy deployment-pod

使用scale命令修改

kubectl scale deploy deployment-pod --replicas=2

kubectl get deploy deployment-pod

镜像更新

deployment支持两种更新策略:重建更新滚动更新,可以通过strategy指定策略类型,支持两个属性:

strategy:指定新的Pod替换旧的Pod的策略, 支持两个属性:
  type:指定策略类型,支持两种策略
    Recreate:在创建出新的Pod之前会先杀掉所有已存在的Pod
    RollingUpdate:滚动更新,就是杀死一部分,就启动一部分,在更新过程中,存在两个版本Pod
  rollingUpdate:当type为RollingUpdate时生效,用于为RollingUpdate设置参数,支持两个属性:
    maxUnavailable:用来指定在升级过程中不可用Pod的最大数量,默认为25%。
    maxSurge: 用来指定在升级过程中可以超过期望的Pod的最大数量,默认为25%。

1.重建更新
vim deployment-pod.yaml 添加更新策略

spec: 
  replicas: 5
  strategy:
    type: Recreate # 重建更新
  selector:

更新deployment

kubectl  apply -f deployment-pod.yaml

升级

kubectl set image deployment deployment-pod nginx-name=nginx:1.19
deployment.apps/deployment-pod image updated

重建更新过程

[k8s@administrator ~]$ kubectl get pods
NAME                             READY   STATUS    RESTARTS   AGE
deployment-pod-65556755b-cfqdh   1/1     Running   0          11s
deployment-pod-65556755b-gzkr7   1/1     Running   0          11s
deployment-pod-65556755b-jkflz   1/1     Running   0          11s
[k8s@administrator ~]$ kubectl get pods
NAME                             READY   STATUS              RESTARTS   AGE
deployment-pod-5569fd897-88cnr   0/1     Pending             0          1s
deployment-pod-5569fd897-ccg6t   0/1     Pending             0          1s
deployment-pod-5569fd897-lrw76   0/1     ContainerCreating   0          1s
[k8s@administrator ~]$ kubectl get pods
NAME                             READY   STATUS              RESTARTS   AGE
deployment-pod-5569fd897-88cnr   0/1     ContainerCreating   0          6s
deployment-pod-5569fd897-ccg6t   0/1     ContainerCreating   0          6s
deployment-pod-5569fd897-lrw76   0/1     ContainerCreating   0          6s
[k8s@administrator ~]$ kubectl get pods
NAME                             READY   STATUS              RESTARTS   AGE
deployment-pod-5569fd897-88cnr   0/1     ContainerCreating   0          9s
deployment-pod-5569fd897-ccg6t   1/1     Running             0          9s
deployment-pod-5569fd897-lrw76   1/1     Running             0          9s

2.滚动更新
vim deployment-pod.yaml 添加更新策略

spec:
  replicas: 5
  strategy:
    type: RollingUpdate 
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
  selector:

开始更新

kubectl set image deployment deployment-pod nginx-name=nginx:1.20
[k8s@administrator ~]$ kubectl get pods
NAME                             READY   STATUS    RESTARTS   AGE
deployment-pod-65556755b-dnxqt   1/1     Running   0          41s
deployment-pod-65556755b-gzfh4   1/1     Running   0          41s
deployment-pod-65556755b-mq44l   1/1     Running   0          41s
deployment-pod-65556755b-q9vj6   1/1     Running   0          41s
deployment-pod-65556755b-sn7fl   1/1     Running   0          41s

[k8s@administrator ~]$ kubectl get pods
NAME                              READY   STATUS              RESTARTS   AGE
deployment-pod-65556755b-gzfh4    1/1     Running             0          52s
deployment-pod-65556755b-mq44l    1/1     Running             0          52s
deployment-pod-65556755b-q9vj6    1/1     Running             0          52s
deployment-pod-65556755b-sn7fl    1/1     Running             0          52s
deployment-pod-686b8db7df-4gnrp   0/1     ContainerCreating   0          4s
deployment-pod-686b8db7df-krfk6   0/1     ContainerCreating   0          5s
deployment-pod-686b8db7df-mjc27   0/1     ContainerCreating   0          5s

[k8s@administrator ~]$ kubectl get pods
NAME                              READY   STATUS              RESTARTS   AGE
deployment-pod-65556755b-gzfh4    1/1     Running             0          53s
deployment-pod-65556755b-mq44l    1/1     Terminating         0          53s
deployment-pod-65556755b-q9vj6    1/1     Running             0          53s
deployment-pod-65556755b-sn7fl    1/1     Running             0          53s
deployment-pod-686b8db7df-4gnrp   0/1     ContainerCreating   0          5s
deployment-pod-686b8db7df-krfk6   1/1     Running             0          6s
deployment-pod-686b8db7df-mjc27   0/1     ContainerCreating   0          6s
deployment-pod-686b8db7df-v5754   0/1     ContainerCreating   0          0s

查看rs,原来的rs的依旧存在,只是pod数量变为0,而后又新产生了一个rs

[k8s@administrator ~]$ kubectl get rs
NAME                        DESIRED   CURRENT   READY   AGE
deployment-pod-65556755b    0         0         0       3m43s
deployment-pod-686b8db7df   5         5         5       2m56s

版本回退

deployment支持版本升级过程中的暂停、继续功能以及版本回退等

语法:kubectl rollout [参数]

status       显示当前升级状态

history     显示 升级历史记录

pause       暂停版本升级过程

resume    继续已经暂停的版本升级过程

restart      重启版本升级过程

undo        回滚到上一级版本

查看当前升级版本的状态

# 查看当前升级版本的状态
[k8s@administrator ~]$ kubectl rollout status deploy deployment-pod
deployment "deployment-pod" successfully rolled out

# 查看升级历史记录
[k8s@administrator ~]$ kubectl rollout history deploy deployment-pod
deployment.apps/deployment-pod
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

# kubectl create -f deployment-pod.yaml  --record; 使用record属性,历史记录才不会是<none>
[k8s@administrator ~]$ kubectl rollout history deploy deployment-pod
deployment.apps/deployment-pod
REVISION  CHANGE-CAUSE
1         kubectl create --filename=deployment-pod.yaml --record=true
2         kubectl create --filename=deployment-pod.yaml --record=true


# 查看镜像版本
[k8s@administrator ~]$ kubectl get deploy  -o wide
NAME             READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES       SELECTOR
deployment-pod   5/5     5            5           9m34s   nginx-name   nginx:1.20   app=nginx-pod,app in (nginx-pod)

# 版本回滚,to-revision:回滚到指定版本,省略则回退到上个版本
[k8s@administrator ~]$ kubectl rollout undo deployment deployment-pod --to-revision=1
deployment.apps/deployment-pod rolled back

# 再次查看镜像版本
[k8s@administrator ~]$ kubectl get deploy  -o wide
NAME             READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES   SELECTOR
deployment-pod   4/5     3            4           9m45s   nginx-name   nginx    app=nginx-pod,app in (nginx-pod)

# 查看RS
[k8s@administrator ~]$ kubectl get rs
NAME                        DESIRED   CURRENT   READY   AGE
deployment-pod-65556755b    5         5         5       10m
deployment-pod-686b8db7df   0         0         0       9m20s

# 再次回退
[k8s@administrator ~]$ kubectl rollout undo deployment deployment-pod --to-revision=2
deployment.apps/deployment-pod rolled back

# 查看RS
# 回滚到哪个版本,则将当前版本pod数量降为0,然后将回滚版本的pod提升为目标数量
[k8s@administrator ~]$ kubectl get rs
NAME                        DESIRED   CURRENT   READY   AGE
deployment-pod-65556755b    4         4         4       10m
deployment-pod-686b8db7df   3         3         0       9m39s
[k8s@administrator ~]$ kubectl get rs
NAME                        DESIRED   CURRENT   READY   AGE
deployment-pod-65556755b    2         2         2       10m
deployment-pod-686b8db7df   5         5         2       9m46s

灰度发布、金丝雀发布

Deployment控制器支持控制更新过程中的控制,如暂停(pause)或继续(resume)更新操作。

​ 将一批新的Pod资源创建完成后立即暂停更新过程,此时,仅存一部分新版本的应用,主体部分还是旧的版本。筛选一小部分的用户请求路由到新版本的Pod应用,继续观察能否稳定地按期望的方式运行。确定没问题之后再继续完成余下的Pod资源滚动更新,否则立即回滚更新操作。

# 创建deploy
[k8s@administrator ~]$ kubectl create -f deployment-pod.yaml
deployment.apps/deployment-pod created

# 更新deployment的版本,并暂停deployment
[k8s@administrator ~]$ kubectl set image deploy deployment-pod  nginx-name=nginx:1.20  && kubectl rollout pause deployment depl                                                                                                          oyment-pod
deployment.apps/deployment-pod image updated
deployment.apps/deployment-pod paused

# 查看更新状态
[k8s@administrator ~]$  kubectl rollout status deploy deployment-pod
Waiting for deployment "deployment-pod" rollout to finish: 1 out of 3 new replicas have been updated...

# 查看rs
# 发现已经有新增了一个资源,但是并未按照预期的状态去删除一个旧的资源,就是因为使用pause暂停命令
[k8s@administrator ~]$ kubectl get rs  -o wide
NAME                        DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES       SELECTOR
deployment-pod-65556755b    3         3         3       89s   nginx-name   nginx        app=nginx-pod,app in (nginx-pod),pod-te                                                                                                          mplate-hash=65556755b
deployment-pod-686b8db7df   1         1         1       37s   nginx-name   nginx:1.20   app=nginx-pod,app in (nginx-pod),pod-te                                                                                                          mplate-hash=686b8db7df

# 查看pod
[k8s@administrator ~]$ kubectl get pods
NAME                              READY   STATUS    RESTARTS   AGE
deployment-pod-65556755b-8fzdr    1/1     Running   0          103s
deployment-pod-65556755b-wrqc6    1/1     Running   0          103s
deployment-pod-65556755b-ztr8x    1/1     Running   0          103s
deployment-pod-686b8db7df-5dc98   1/1     Running   0          51s

# 确保更新的pod没问题,继续更新
[k8s@administrator ~]$ kubectl rollout resume deploy  deployment-pod
deployment.apps/deployment-pod resumed

# 查看pod
[k8s@administrator ~]$ kubectl get pods
NAME                              READY   STATUS              RESTARTS   AGE
deployment-pod-65556755b-8fzdr    1/1     Terminating         0          2m35s
deployment-pod-65556755b-ztr8x    1/1     Running             0          2m35s
deployment-pod-686b8db7df-5dc98   1/1     Running             0          103s
deployment-pod-686b8db7df-7sd68   0/1     ContainerCreating   0          1s
deployment-pod-686b8db7df-rzvcf   1/1     Running             0          6s

# 查看rs
[k8s@administrator ~]$  kubectl get rs  -o wide
NAME                        DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES       SELECTOR
deployment-pod-65556755b    0         0         0       2m43s   nginx-name   nginx        app=nginx-pod,app in (nginx-pod),pod-                                                                                                          template-hash=65556755b
deployment-pod-686b8db7df   3         3         3       111s    nginx-name   nginx:1.20   app=nginx-pod,app in (nginx-pod),pod-                                                                                                          template-hash=686b8db7df

Horizontal Pod Autoscaler(HPA)

Horizontal Pod Autoscaler(HPA)控制器通过监测Pod的使用情况,实现pod数量的自动调整,

HPA可以获取每个Pod利用率,然后和HPA中定义的指标进行对比,同时计算出需要伸缩的具体值,最后实现Pod的数量的调整。

HPA的实现原理:通过追踪分析RC控制的所有目标Pod的负载变化情况,来确定是否需要针对性地调整目标Pod的副本数

安装metrics-server

Metrics Server 是 Kubernetes 内置自动缩放管道的可扩展、高效的容器资源指标来源。

1.minikube方式安装

minikube方式安装前提是使用Minikube

开启metrics-server

minikube addons enable  metrics-server

查看资源使用情况,发现命令无法正常使用

kubectl top node
Error from server (ServiceUnavailable): the server is currently unable to handle the request (get nodes.metrics.k8s.io)

查看pod运行情况,发现镜像拉取失败

[k8s@administrator ~]$ kubectl get pod -n kube-system
NAME                               READY   STATUS             RESTARTS      AGE
coredns-64897985d-dhqkw            1/1     Running            1 (45h ago)   47h
etcd-minikube                      1/1     Running            1 (45h ago)   47h
kube-apiserver-minikube            1/1     Running            1 (45h ago)   47h
kube-controller-manager-minikube   1/1     Running            1 (45h ago)   47h
kube-proxy-dgqbf                   1/1     Running            1 (45h ago)   47h
kube-scheduler-minikube            1/1     Running            1 (30h ago)   47h
metrics-server-6b76bd68b6-s2nhj    0/1     ImagePullBackOff   0             15m
storage-provisioner                1/1     Running            3 (30h ago)   47h

查看pod内部详情,k8s.gcr.io国外地址无法访问

kubectl describe pod metrics-server-6b76bd68b6-s2nhj
 
Failed to pull image "k8s.gcr.io/metrics-server/metrics-server:v0.4.2@sha256:dbc33d7d35d2a9cc5ab402005aa7a0d13be6192f3550c7d42cba8d2d5e3a5d62": rpc error: code = Unknown desc = Error response from daemon: Get "https://k8s.gcr.io/v2/": context deadline exceeded

删除pod

delete pod metrics-server-6b76bd68b6-s2nhj -n  kube-system

准备Proxy代理,然后执行如下命令

# export http_proxy:添加命令行代理,让minikube可以在命令行通过proxy下载相关文件
export http_proxy=http://127.0.0.1:7890;export https_proxy=http://127.0.0.1:7890

# --docker-env http_proxy:设置minikube中docker的环境变量,让docker使用代理
# 127.0.0.1不行,就使用minikube ip获取kubemini的IP;即使用本机与minikube连接的ip
minikube start --docker-env http_proxy=http://127.0.0.1:7890 --docker-env https_proxy=http://127.0.0.1:7890 --docker-env no_proxy="" 

查看metrics-server

[k8s@administrator ~]$ kubectl get pod -n kube-system
NAME                               READY   STATUS    RESTARTS         AGE
coredns-64897985d-dhqkw            1/1     Running   19 (8m4s ago)    2d20h
etcd-minikube                      1/1     Running   19 (8m9s ago)    2d20h
kube-apiserver-minikube            1/1     Running   21 (8m8s ago)    2d20h
kube-controller-manager-minikube   1/1     Running   20 (8m9s ago)    2d20h
kube-proxy-dgqbf                   1/1     Running   16 (8m9s ago)    2d20h
kube-scheduler-minikube            1/1     Running   21 (7m59s ago)   2d20h
metrics-server-6b76bd68b6-d45vb    1/1     Running   2 (5m50s ago)    58m
storage-provisioner                1/1     Running   28 (5m51s ago)   2d20h

查看资源使用情况

[k8s@administrator ~]$ kubectl top nodes
NAME       CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%
minikube   166m         8%     630Mi           17%

2.手动安装

访问: https://github.com/kubernetes-sigs/metrics-server/releases 选则合适版本的 components.yaml

直接从YAML清单安装

kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

高可用性模式的YAML清单安装

kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/high-availability.yaml

注意:最终不出意外都将镜像拉取失败,需设置Proxy或修改YAML清单中的镜像地址

基本命令

查看node使用情况

kubectl top nodes nodeName

查看pod使用情况

kubectl top pod podName

显示pod内所有container

kubectl top pod podName --containers

显示命名空间下所有pod

kubectl top pod -n dev

创建DeployMent

vim deployment-pod.yaml

apiVersion: apps/v1
kind: Deployment      
metadata:
  name: deployment-pod
  namespace: default
spec: 
  selector:
    matchLabels:
      app: nginx-pod
  template:
    metadata:
      labels:
        app: nginx-pod
    spec:
      containers:
      - name: nginx-name
        image: nginx
        resources:
          requests:
            cpu: "1" 
            memory: "100Mi" 

创建deployment和创建service

[k8s@administrator ~]$ kubectl apply -f deployment-pod.yaml 
deployment.apps/deployment-pod created

[k8s@administrator ~]$ kubectl expose deployment deployment-pod --type=NodePort --port=80
service/deployment-pod exposed

[k8s@administrator ~]$ kubectl get deployment,pod,svc
NAME                             READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/deployment-pod   1/1     1            1           78s

NAME                                  READY   STATUS    RESTARTS   AGE
pod/deployment-pod-6644844f7b-2dxwq   1/1     Running   0          78s

NAME                     TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
service/deployment-pod   NodePort    10.104.94.119   <none>        80:31818/TCP   21s
service/kubernetes       ClusterIP   10.96.0.1       <none>        443/TCP        3d

部署HPA

创建vim hpa-pod.yaml

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: hpa-pod
  namespace: default
spec:
  minReplicas: 1  # 最小pod数量
  maxReplicas: 10 # 最大pod数量
  targetCPUUtilizationPercentage: 10 # CPU使用率
  scaleTargetRef:   # 指定控制的nginx信息
    apiVersion: apps/v1
    kind: Deployment  
    name: deployment-pod 

创建hpa

[k8s@administrator ~]$ kubectl create -f hpa-pod.yaml
horizontalpodautoscaler.autoscaling/hpa-pod created

查看hpa

[k8s@administrator ~]$ kubectl get hpa 
NAME      REFERENCE                   TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
hpa-pod   Deployment/deployment-pod   <unknown>/10%   1         10        0          14s

[k8s@administrator ~]$ kubectl get hpa 
NAME      REFERENCE                   TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
hpa-pod   Deployment/deployment-pod    0%/10%   1         10        0          14s

压测IP:31818:

hpa的变化:

[k8s@administrator ~]$ kubectl get hpa -w
NAME            REFERENCE            TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
hpa-pod   Deployment/deployment-pod   0%/10%     1         10        1          3m15s
hpa-pod   Deployment/deployment-pod   8%/10%     1         10        1          6m58s
hpa-pod   Deployment/deployment-pod   15%/10%    1         10        3          7m51s
hpa-pod   Deployment/deployment-pod   23%/10%    1         10        6          7m26s
hpa-pod   Deployment/deployment-pod   7%/10%     1         10        6          7m31s
hpa-pod   Deployment/deployment-pod   0%/10%     1         10        1          9m6s

deployment的变化

[k8s@administrator ~]$ kubectl get deployment -w
NAME            READY   UP-TO-DATE   AVAILABLE   AGE
deployment-pod   1/1     1            1           9m
deployment-pod   2/8     8            2           9m
deployment-pod   3/8     8            3           9m
deployment-pod   4/8     8            4           10m
deployment-pod   5/8     8            5           10m
deployment-pod   6/8     8            6           10m
deployment-pod   1/8     8            7           15m
deployment-pod   1/1     1            1           15m

pod的变化

[k8s@administrator ~]$ kubectl get pods  -w
NAME                             READY   STATUS            RESTARTS      AGE
deployment-pod-6644844f7b-2dxwq   1/1     Running             0          12m
deployment-pod-6644844f7b-sl9c6   0/1     Pending             0          0s
deployment-pod-6644844f7b-8zhwk   0/1     Pending             0          0s
deployment-pod-6644844f7b-sl9c6   0/1     ContainerCreating   0          0s
deployment-pod-6644844f7b-8zhwk   0/1     ContainerCreating   0          0s
deployment-pod-6644844f7b-sl9c6   1/1     Running             0          19s
deployment-pod-6644844f7b-8zhwk   1/1     Running             0          30s
deployment-pod-6644844f7b-sl9c6   1/1     Terminating         0          5m20s
deployment-pod-6644844f7b-8zhwk   1/1     Terminating         0          5m35s

DaemonSet(DS)

DaemonSet类型的控制器可以保证在集群中的每一节点或指定节点上都运行一个副本(有且只有一个)。

特点:

每当向集群中添加一个节点时,指定的Pod副本也将添加到该节点上

当节点从集群中移除时,Pod也就被垃圾回收了

创建资源清单

创建vim daemonset-pod.yaml

apiVersion: apps/v1
kind: DaemonSet      
metadata:
  name: daemonset-pod
  namespace: default
spec: 
  selector:
    matchLabels:
      app: nginx-pod
  template:
    metadata:
      labels:
        app: nginx-pod
    spec:
      containers:
      - name: nginx-name
        image: nginx

创建DS

# 创建daemonset
kubectl apply -f  daemonset-pod.yaml

# 查看daemonset
kubectl get ds -o wide

# 查看pod,每个Node上都运行一个pod
kubectl get pods -o wide
 
# 删除daemonset
kubectl delete -f daemonset-pod.yaml

Job

Job,主要用于批量处理短暂的一次性任务。

Job特点:

当Job创建的pod执行成功结束时,Job将记录成功结束的pod数量

当成功结束的pod达到指定的数量时,Job将完成执行

创建资源清单

创建vim job-pod.yaml

apiVersion: batch/v1
kind: Job      
metadata:
  name: job-pod
  namespace: default
spec:
  completions: 1 # 指定job需要成功运行Pods的次数。默认值: 1
  parallelism: 1 # 指定job在任一时刻应该并发运行Pods的数量。默认值: 1
  activeDeadlineSeconds: 30 # 指定job可运行的时间期限,超过时间还未结束,系统将会尝试进行终止
  backoffLimit: 6 # 指定job失败后进行重试的次数。默认是6
  manualSelector: true # 是否可以使用selector选择器选择pod,默认是false
  selector:
    matchLabels:
      app: nginx-pod
  template:
    metadata:
      labels:
        app: nginx-pod
    spec:
      restartPolicy: Never # 重启策略只能设置为Never或者OnFailure
      containers:
      - name: nginx-name
        image: nginx
        command: ["bin/sh","-c","/bin/echo start;sleep 10;/bin/echo end;"]

创建Job

# 创建job
kubectl create -f job-pod.yaml

# 查看job
kubectl get job  -o wide

# pod在运行完毕任务后,就会变成Completed状态
kubectl get pods

# 调整下pod运行的总数量和并行数量 即:在spec下设置下面两个选项
spec:
  completions: 4 # 指定job需要成功运行Pods的次数。默认值: 1
  parallelism: 2 # 指定job在任一时刻应该并发运行Pods的数量。默认值: 1

# 重新运行job,job每次运行2个pod,总共执行了4个pod
kubectl get pods -n 

# 删除job
kubectl delete -f job-pod.yaml

重启策略设置说明

当指定为OnFailure,则job会在pod出现故障时重启容器,而不是创建pod,failed次数不变

当指定为Never,则job会在pod出现故障时创建新的pod,并且故障pod不会消失,也不会重启,failed次数加1

当指定为Always,意味着一直重启,即job任务会重复去执行,所以不能设置为Always

CronJob(CJ)

CronJob控制器借助Job控制器资源为其管控对象,并借助它管理pod资源对象,Job控制器定义的任务在其控制器资源创建之后便会立即执行

CronJob可以在特定的时间点反复的去运行job任务。

创建资源清单

创建vim cronjob-pod.yaml

apiVersion: batch/v1beta1
kind: CronJob   
metadata:
  name: cronjob-pod
  namespace: default
  labels:
    controller: cronjob
spec:
  schedule: "*/1 * * * *" # cron格式的作业调度运行时间点,用于控制任务在什么时间执行
  concurrencyPolicy: Allow   # Allow:允许Jobs并发运行(默认)   Forbid:禁止并发运行,如果上一次运行尚未完成,则跳过下一次运行  Replace:替换,取消当前正在运行的作业并用新作业替换它
  failedJobsHistoryLimit: # 为失败的任务执行保留的历史记录数,默认为1
  successfulJobsHistoryLimit: 3 # 为成功的任务执行保留的历史记录数,默认为3
  startingDeadlineSeconds: 60 # 启动作业错误的超时时长
  jobTemplate: # job控制器模板,用于为cronjob控制器生成job对象
    metadata:
    spec:
      template:  
        spec:
          restartPolicy: Never # 重启策略只能设置为Never或者OnFailure
          containers:
          - name: nginx-name
            image: nginx
            command: ["bin/sh","-c","/bin/echo start;sleep 10;/bin/echo end;"]

创建cronjob

# 创建cronjob
kubectl create -f cronjob-pod.yaml


# 查看cronjob
kubectl get cronjobs 

# 查看job
kubectl get jobs 

# 查看pod
kubectl get pods 

# 删除cronjob
kubectl  delete -f cronjob-pod.yaml

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/136979.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!