资源调度-Deployment,StatefulSet,DaemonSet

1. Replication Controller和ReplicaSet

Replication Controller(复制控制器,RC)和ReplicaSet(复制集,RS)是两种简单部署Pod的方式。在生产环境中,主要使用更高级的Deployment等方式进行Pod的管理和部署.

Replication Controller

Replication Controller(简称RC)可确保Pod副本数达到期望值,也就是RC定义的数量。换句话说,Replication Controller可确保一个Pod或一组同类Pod总是可用。
如果存在的Pod大于设定的值,则Replication Controller将终止额外的Pod。如果太小,Replication Controller将启动更多的Pod用于保证达到期望值。与手动创建Pod不同的是,用Replication Controller维护的Pod在失败、删除或终止时会自动替换。因此即使应用程序只需要一个Pod,也应该使用Replication Controller或其他方式管理。Replication Controller类似于进程管理程序,但是Replication Controller不是监视单个节点上的各个进程,而是监视多个节点上的多个Pod。
定义一个Replication Controller的示例如下。

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx
spec:
  replicas: 3
  selector:
    app: nginx
  template:
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80

ReplicaSet

ReplicaSet是支持基于集合的标签选择器的下一代Replication Controller,它主要用作Deployment协调创建、删除和更新Pod,和Replication Controller唯一的区别是,ReplicaSet支持标签选择器。在实际应用中,虽然ReplicaSet可以单独使用,但是一般建议使用Deployment来自动管理ReplicaSet,除非自定义的Pod不需要更新或有其他编排等。
对应关系:deployment管理rs,rs管理pod
查看rs

[root@k8s-master01 ~]# kubectl get rs

定义一个ReplicaSet的示例如下:

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: frontend
  labels:
    app: guestbook
    tier: frontend
spec:
  # modify replicas according to your case
  replicas: 3
  selector:
    matchLabels:
      tier: frontend
    matchExpressions:
      - {key: tier, operator: In, values: [frontend]}
  template:
    metadata:
      labels:
        app: guestbook
        tier: frontend
    spec:
      containers:
      - name: php-redis
        image: gcr.io/google_samples/gb-frontend:v3
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        env:
        - name: GET_HOSTS_FROM
          value: dns
          # If your cluster config does not include a dns service, then to
          # instead access environment variables to find service host
          # info, comment out the 'value: dns' line above, and uncomment the
          # line below.
          # value: env
        ports:
        - containerPort: 80

Replication Controller和ReplicaSet的创建删除和Pod并无太大区别,Replication Controller目前几乎已经不在生产环境中使用,ReplicaSet也很少单独被使用,都是使用更高级的资源Deployment、DaemonSet、StatefulSet进行管理Pod。

2.Deployment

概念:用于部署无状态的服务,这个最常用的控制器。一般用于管理维护企业内部无状态的微服务,比如configserver、zuul、springboot。他可以管理多个副本的Pod实现无缝迁移、自动扩容缩容、自动灾难恢复、一键回滚等功能。

Ⅰ.Deployment的创建

手动创建(选择容器)

[root@k8s-master01 ~]# kubectl create deployment nginx --image=nginx:1.15.2

从文件创建:

[root@k8s-master01 ~]# 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
  name: nginx
  namespace: default
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

状态解析:

[root@k8s-master01 ~]# kubectl get deploy -owide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES         SELECTOR
nginx   2/2     2            2           9m29s   nginx        nginx:1.15.2   app=nginx
	
NAME: Deployment名称
READY:Pod的状态,已经Ready的个数
UP-TO-DATE:已经达到期望状态的被更新的副本数
AVAILABLE:已经可以用的副本数
AGE:显示应用程序运行的时间
CONTAINERS:容器名称
IMAGES:容器的镜像
SELECTOR:管理的Pod的标签

Ⅱ.Deployment的更新

更改deployment的镜像并记录:

[root@k8s-master01 ~]# kubectl set image deploy nginx nginx=nginx:1.15.3 -–record

查看更新过程:

[root@k8s-master01 ~]# kubectl rollout status deploy nginx

使用describe查看更新过程:

[root@k8s-master01 ~]# kubectl describe deploy nginx

Ⅲ.Deployment的回滚

1.回滚上一版本
[root@k8s-master01 ~]# kubectl set image deploy nginx nginx=nginx:787977da --record  //指定不存在的镜像
[root@k8s-master01 ~]# kubectl get po    //查看pod
[root@k8s-master01 ~]# kubectl rollout history deploy nginx    //查看历史版本
[root@k8s-master01 ~]# kubectl rollout undo deploy nginx       //回滚到上一版本
[root@k8s-master01 ~]# kubectl get po    //查看pod
[root@k8s-master01 ~]# kubectl get deploy nginx -oyaml | grep nginx   //查看具体镜像版本
2.多次更新,回滚到指定版本

进行多次更新

[root@k8s-master01 ~]# kubectl set image deploy nginx nginx=nginx:787977da --record              //第一次更新(镜像不存在)
[root@k8s-master01 ~]# kubectl set image deploy nginx nginx=nginx:787977dadaa --record           //第二次更新(镜像不存在)
[root@k8s-master01 ~]# kubectl set image deploy nginx nginx=nginx:787977xxxxxdadaa --record      //第三次更新(镜像不存在)
[root@k8s-master01 ~]# kubectl set image deploy nginx nginx=nginx:787977dadxxxxxdadaa --record   //第四次更新(镜像不存在)
[root@k8s-master01 ~]# kubectl  rollout history deploy nginx      //查看更新历史记录
[root@k8s-master01 ~]# kubectl rollout history deploy nginx --revision=5      //查看指定版本的详细信息
[root@k8s-master01 ~]# kubectl rollout undo deploy nginx --to-revision=5      //回到刚查看指定的历史版本
[root@k8s-master01 ~]# kubectl get deploy -oyaml                  //查看deploy的状态

Ⅳ.Deployment的暂停和恢复

1.Deployment暂停

目的:多次修改不频繁换镜像,可以先暂停deployment

[root@k8s-master01 ~]# kubectl rollout pause deployment nginx   //适合多次修改的时候,不需要多次更新
[root@k8s-master01 ~]# kubectl set image deploy nginx nginx=nginx:1.15.4  --record    //修改版本
[root@k8s-master01 ~]# kubectl get  pod       //修改配置不会生效
[root@k8s-master01 ~]# kubectl set resources deploy nginx -c nginx --limits=cpu=200m,memory=128Mi --requests=cpu=10m,memory=16Mi      //第二次修改nginx容器使用最小cpu和最大cpu。
[root@k8s-master01 ~]# kubectl get  pod       //修改配置不会生效
[root@k8s-master01 ~]# kubectl get deploy nginx -oyaml   //查看配置文件已更改过来了 
2.Deployment恢复
[root@k8s-master01 ~]# kubectl rollout resume deploy nginx   //恢复deployment 
[root@k8s-master01 ~]# kubectl get rs            //查看最新镜像
[root@k8s-master01 ~]# kubectl get deploy nginx -oyaml | grep nginx    //查看回滚后版本
[root@k8s-master01 ~]# kubectl rollout history deploy nginx   //查看历史版本

Ⅴ.Deployment的删除

[root@k8s-master01 ~]# kubectl delete deploy nginx  //删除deploy-nginx 
[root@k8s-master01 ~]# kubectl get deploy    //查看

Ⅵ.Deployment的注意事项

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. 

3.StatefulSet

概念:StatefulSet主要用于管理有状态应用程序的工作负载API对象。比如在生产环境中,可以部署ElasticSearch集群、MongoDB集群或者需要持久化的RabbitMQ集群、Redis集群、Kafka集群和ZooKeeper集群等。
和Deployment类似,一个StatefulSet也同样管理着基于相同容器规范的Pod。不同的是,StatefulSet为每个Pod维护了一个粘性标识。这些Pod是根据相同的规范创建的,但是不可互换,每个Pod都有一个持久的标识符,在重新调度时也会保留,一般格式为StatefulSetName-Number。比如定义一个名字是Redis-Sentinel的StatefulSet,指定创建三个Pod,那么创建出来的Pod名字就为Redis-Sentinel-0、Redis-Sentinel-1、Redis-Sentinel-2。而StatefulSet创建的Pod一般使用Headless Service(无头服务)进行通信,和普通的Service的区别在于Headless Service没有ClusterIP,它使用的是Endpoint进行互相通信,Headless一般的格式为:

statefulSetName-{0..N-1}.serviceName.namespace.svc.c
uster.local。
serviceName为Headless Service的名字,创建StatefulSet时,必须指定Headless Service名称;
0..N-1为Pod所在的序号,从0开始到N-1;
statefulSetName为StatefulSet的名字;
namespace为服务所在的命名空间;
.cluster.local为Cluster Domain(集群域)。

Ⅰ.创建一个StatefulSet

1.定义一个statefulset资源文件
apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  serviceName: "nginx"
  replicas: 2
  selector:
    matchLabels:
      app: nginx

  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.15.3
        ports:
        - containerPort: 80
          name: web
2.StatefulSet的创建
[root@k8s-master01 ~]# kubectl create -f nginx-sts.yaml   //创建
[root@k8s-master01 ~]# kubectl get svc       //查看svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1            443/TCP   91d
nginx        ClusterIP   None                 80/TCP    4m11s
[root@k8s-master01 ~]# kubectl get pod   //查看pod
NAME    READY   STATUS    RESTARTS   AGE
web-0   1/1     Running   0          4m39s
web-1   1/1     Running   0          4m32s
[root@k8s-master01 ~]# kubectl scale --replicas=3 sts web  //扩容副本数为3
statefulset.apps/web scaled
[root@k8s-master01 ~]# kubectl get pod -owide 
NAME    READY   STATUS    RESTARTS   AGE     IP                NODE           NOMINATED NODE   READINESS GATES
web-0   1/1     Running   0          8m32s   172.169.244.254   k8s-master01              
web-1   1/1     Running   0          8m25s   172.161.125.47    k8s-node01                
web-2   1/1     Running   0          2m13s   172.169.244.255   k8s-master01                   //可以看到名称有序排列

Ⅱ.StatefulSet的扩容

[root@k8s-master01 ~]# kubectl scale --replicas=5 sts web   //增加到5个节点
statefulset.apps/web scaled
[root@k8s-master01 ~]# kubectl get sts -owide //查看
NAME   READY   AGE   CONTAINERS   IMAGES
web    5/5     34m   nginx        nginx:1.15.3
[root@k8s-master01 ~]# kubectl get pods -w -l app=nginx   //动态查看(-w)
[root@k8s-master01 ~]# kubectl get pod -owide 
NAME    READY   STATUS    RESTARTS   AGE   IP                NODE           NOMINATED NODE   READINESS GATES
web-0   1/1     Running   0          34m   172.169.244.254   k8s-master01              
web-1   1/1     Running   0          34m   172.161.125.47    k8s-node01                
web-2   1/1     Running   0          28m   172.169.244.255   k8s-master01              
web-3   1/1     Running   0          24s   172.169.244.195   k8s-master01              
web-4   1/1     Running   0          22s   172.171.14.232    k8s-node02                

Ⅲ.StatefulSet的缩容

[root@k8s-master01 ~]# kubectl patch sts web -p '{"spec":{"replicas":3}}'   //缩容为3台
[root@k8s-master01 ~]# kubectl get pod -owide   //查看
NAME    READY   STATUS    RESTARTS   AGE   IP                NODE           NOMINATED NODE   READINESS GATES
web-0   1/1     Running   0          37m   172.169.244.254   k8s-master01              
web-1   1/1     Running   0          37m   172.161.125.47    k8s-node01                
web-2   1/1     Running   0          31m   172.169.244.255   k8s-master01              

Ⅳ.StatefulSet的删除

[root@k8s-master01 ~]# kubectl delete sts web  //正常删除
statefulset.apps "web" deleted
[root@k8s-master01 ~]# kubectl delete sts web  --cascade=false    //非级联删除,pod会保留,成为孤儿

Ⅴ.StatefulSet的更新机制(灰度发布)

可以通过kubectl edit sts web 修改yaml中spec.updateStrategy.rollingUpdate.partition的数量来控制最新版本的发布。
每次更新小于partition设置数的序号容器,假设sts中web的数量有5个(web-0,web-1,web-2,web-3,web-4),partition设置数为4,将只更新web-4,序号小于4的sts将不更新
此操作也能用于灰度发布

[root@k8s-master01 ~]# kubectl edit sts  web   //手动修改image: nginx:1.15.3
[root@k8s-master01 ~]# kubectl get pod -oyaml | grep image:
              f:image: {}
    - image: nginx:1.15.2
      image: nginx:1.15.2
              f:image: {}
    - image: nginx:1.15.2
      image: nginx:1.15.2
              f:image: {}
    - image: nginx:1.15.2
      image: nginx:1.15.2
              f:image: {}
    - image: nginx:1.15.2
      image: nginx:1.15.2
              f:image: {}
    - image: nginx:1.15.3          //可以看到只有最后一个web-4更新了镜像
      image: nginx:1.15.3

4.DaemonSet

DaemonSet(守护进程集)和守护进程类似,它在符合匹配条件的节点上均部署一个Pod。(所有节点)
使用DaemonSet的场景
·运行集群存储的daemon,比如ceph或者glusterd
·节点的CNI网络插件,calico
·节点日志的收集:fluentd或者是filebeat
·节点的监控:node exporter
·服务暴露:部署一个ingress nginx
DaemonSet确保全部(或者某些)节点上运行一个Pod副本。当有新节点加入集群时,也会为它们新增一个Pod。当节点从集群中移除时,这些Pod也会被回收,删除DaemonSet将会删除它创建的所有Pod。

Ⅰ.DaemonSet的创建

[root@k8s-master01 ~]# cat nginx-ds.yaml   //编辑创建yaml,可以从deployment或者sts中修改
apiVersion: apps/v1
kind: DaemonSet
metadata:
  labels:
    app: nginx
  name: nginx
spec:
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: nginx
  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-master01 ~]# kubectl create -f nginx-ds.yaml   //创建ds-pod
daemonset.apps/nginx created
[root@k8s-master01 ~]# kubectl get pod -owide     //查看,每一台都创建一个pod
NAME          READY   STATUS    RESTARTS   AGE   IP                NODE           NOMINATED NODE   READINESS GATES
nginx-4wb2l   1/1     Running   0          30s   172.169.244.201   k8s-master01              
nginx-fntc7   1/1     Running   0          30s   172.162.195.9     k8s-master03              
nginx-nr2rv   1/1     Running   0          30s   172.171.14.234    k8s-node02                
nginx-ttjq5   1/1     Running   0          30s   172.161.125.51    k8s-node01                
nginx-vbwfx   1/1     Running   0          30s   172.169.92.74     k8s-master02              

Ⅱ.DaemonSet的label(标签使用)

我们可以对每个节点打上标签,对有标签的节点发布pod。

1.增加/查看标签
[root@k8s-master01 ~]# kubectl label node k8s-node01 k8s-node02 ds=true   //对node01和02打上ture的标签
[root@k8s-master01 ~]# kubectl get node --show-labels    //查看标签
NAME           STATUS   ROLES    AGE   VERSION   LABELS
k8s-master01   Ready    master   13d   v1.19.0   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master01,kubernetes.io/os=linux,node-role.kubernetes.io/master=,node.kubernetes.io/master=,node.kubernetes.io/node=
k8s-master02   Ready       13d   v1.19.0   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master02,kubernetes.io/os=linux,node.kubernetes.io/node=
k8s-master03   Ready       13d   v1.19.0   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master03,kubernetes.io/os=linux,node.kubernetes.io/node=    
k8s-node01     Ready       13d   v1.19.0   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,ds=true,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node01,kubernetes.io/os=linux,node.kubernetes.io/node=      //多了ds=true
k8s-node02     Ready       13d   v1.19.0   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,ds=true,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node02,kubernetes.io/os=linux,node.kubernetes.io/node=      //多了ds=true
2.增加yaml标签生效

[root@k8s-master01 ~]# vim nginx-ds.yaml //yaml增加标签

apiVersion: apps/v1
kind: DaemonSet
metadata:
  labels:
    app: nginx
  name: nginx
spec:
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      nodeSelector:      //增加2行,
        ds: "true"
      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-master01 ~]# kubectl replace -f nginx-ds.yaml    //更新
daemonset.apps/nginx replaced
[root@k8s-master01 ~]# kubectl get po -owide   //查看,不符合标签tag的会自动删掉
[root@k8s-master01 ~]# kubectl get po -owide 
NAME          READY   STATUS    RESTARTS   AGE   IP               NODE         NOMINATED NODE   READINESS GATES
nginx-wp8kd   1/1     Running   0          4s    172.171.14.235   k8s-node02              
nginx-wz9bw   1/1     Running   0          7s    172.161.125.52   k8s-node01              
[root@k8s-master01 ~]# kubectl label node k8s-master03 ds=true    //在k8s-master03上加上标签,pod会自动启动
node/k8s-master03 labeled
[root@k8s-master01 ~]# kubectl get po -owide 
NAME          READY   STATUS    RESTARTS   AGE     IP               NODE           NOMINATED NODE   READINESS GATES
nginx-hv84g   1/1     Running   0          7s      172.162.195.10   k8s-master03              
nginx-wp8kd   1/1     Running   0          2m11s   172.171.14.235   k8s-node02                
nginx-wz9bw   1/1     Running   0          2m14s   172.161.125.52   k8s-node01                
3.修改/删除标签

修改:在增加的基础上加上–overwrite参数即可:

[root@k8s-master01 ~]# kubectl label node k8s-node01 k8s-node02 ds=false  ----overwrite

删除:加上-号

[root@k8s-master01 ~]# kubectl label node k8s-node01 k8s-node02 ds-  

Ⅲ.DaemonSet的更新和回滚

更新:

[root@k8s-master01 ~]# kubectl set image ds nginx nginx=nginx:1.15.3 --record  //更换镜像方式
[root@k8s-master01 ~]# kubectl edit ds  nginx      //命令行方式
[root@k8s-master01 ~]# kubectl rollout status ds      //查看更新状态
[root@k8s-master01 ~]# ubectl rollout history ds     //查看历史版本

回滚:

[root@k8s-master01 ~]# ubectl rollout history ds     //查看历史版本
[root@k8s-master01 ~]# kubectl rollout undo ds nginx  --to-revision=5   //根据历史版本回滚到指定版本
[root@k8s-master01 ~]# kubectl get pod  -owide    //查看状态

5.HPA

容器根据监控自动扩容(metrics-server监控),必须requests参数,不能扩容无法缩放的参数。
HPA接口类型:

  v1:稳定版自动绳索:只支持cpu
  v2:v2beat1(cpu,内存,自定义指标)
  v2beat2:cpu,内存,自定义指标,额外指标(ExternalMetrics)

[root@k8s-master01 ~]# kubectl get apiservices | grep autosca  //查看接口类型(三版本)
v1.autoscaling                         Local                        True        92d
v2beta1.autoscaling                    Local                        True        92d
v2beta2.autoscaling                    Local                        True        92d

实践:

[root@k8s-master01 ~]# kubectl create deployment hpa-nginx --image=registry.cn-beijing.aliyuncs.com/dotbalo/nginx --dry-run=client -oyaml  > hpa-nginx.yaml   //通过创建一个deployment的nginx导出yaml文件
[root@k8s-master01 ~]# vim hpa-nginx.yaml   //修改参数,1颗cpu=1000m
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: hpa-nginx
  name: hpa-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hpa-nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: hpa-nginx
    spec:
      containers:
      - image: registry.cn-beijing.aliyuncs.com/dotbalo/nginx
        name: nginx
        resources:
          requests:                 //添加cpu参数
            cpu: 10m
status: {}

[root@k8s-master01 ~]# kubectl create -f hpa-nginx.yaml   //创建deployment-nginx
deployment.apps/hpa-nginx created
[root@k8s-master01 ~]# kubectl get po -owide 
NAME                         READY   STATUS    RESTARTS   AGE   IP                NODE           NOMINATED NODE   READINESS GATES
hpa-nginx-58ddb65c8d-m7m69   1/1     Running   0          7s    172.169.244.203   k8s-master01              

[root@k8s-master01 ~]# curl 172.169.244.203    //测试是否能curl
...
Welcome to nginx!
...

[root@k8s-master01 ~]# kubectl expose deployment hpa-nginx --port=80    //暴露svc端口
service/hpa-nginx exposed
[root@k8s-master01 ~]# kubectl get svc   //查看svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
hpa-nginx    ClusterIP   10.106.84.130           80/TCP    3s
kubernetes   ClusterIP   10.96.0.1               443/TCP   92d
nginx        ClusterIP   None                    80/TCP    138m
[root@k8s-master01 ~]# curl 10.106.84.130     //测试curl,没问题
...
Welcome to nginx!
...

[root@k8s-master01 ~]# kubectl top po   //查看cpu指标
NAME                         CPU(cores)   MEMORY(bytes)   
hpa-nginx-58ddb65c8d-m7m69   0m           5Mi


[root@k8s-master01 ~]# kubectl autoscale deploy hpa-nginx --cpu-percent=10 --min=1 --max=10  //设置参数最小pod,最大pod,扩容百分比
[root@k8s-master01 ~]# kubectl get hpa    //查看hpa,TARGETS为扩容百分比,MINPODS最小pod数,MAXPODS最大pod数,REPLICAS目前pod数。
NAME        REFERENCE              TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
hpa-nginx   Deployment/hpa-nginx   0%/10%    1         10        10         5m55s

[root@k8s-master01 ~]# while true; do wget -q -O- http://10.106.84.130 > /dev/null; done   //循环请求,提高nginx的cpu
[root@k8s-master01 ~]# kubectl get pod     //查看pod,自动扩容
NAME                         READY   STATUS    RESTARTS   AGE
hpa-nginx-58ddb65c8d-2qvdr   1/1     Running   0          4m10s
hpa-nginx-58ddb65c8d-42hz5   1/1     Running   0          3m55s
hpa-nginx-58ddb65c8d-4z6h4   1/1     Running   0          4m25s
hpa-nginx-58ddb65c8d-8bqk2   1/1     Running   0          4m10s
hpa-nginx-58ddb65c8d-9gmg7   1/1     Running   0          4m10s
hpa-nginx-58ddb65c8d-bq974   1/1     Running   0          3m55s
hpa-nginx-58ddb65c8d-hf2wr   1/1     Running   0          4m25s
hpa-nginx-58ddb65c8d-jxr5n   1/1     Running   0          4m10s
hpa-nginx-58ddb65c8d-m7m69   1/1     Running   0          25m
hpa-nginx-58ddb65c8d-szd6f   1/1     Running   0          4m26s
[root@k8s-master01 ~]# kubectl get pod    //中断循环请求,再次查看,发现在删除中
[root@k8s-master01 ~]# kubectl get pod 
NAME                         READY   STATUS        RESTARTS   AGE
hpa-nginx-58ddb65c8d-2qvdr   1/1     Running       0          6m16s
hpa-nginx-58ddb65c8d-4z6h4   0/1     Terminating   0          6m31s
hpa-nginx-58ddb65c8d-hf2wr   0/1     Terminating   0          6m31s
hpa-nginx-58ddb65c8d-jxr5n   0/1     Terminating   0          6m16s
hpa-nginx-58ddb65c8d-m7m69   0/1     Terminating   0          27m

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇