Kubernetes基础:资源扩缩容方法(Replica Set)

Kubernetes提供了多种方式对于资源的扩缩容进行操作,这篇文章以RS(Replica Set)为例进行说明。

常用方法

方法1: 使用kubectl scale进行扩缩容

执行kubectl scale指定资源进行扩缩容操作

扩缩容命令:kubectl scale

方法2: 修改YAML文件执行kubectl apply进行扩缩容

通过如下步骤进行扩缩容:

  • 步骤1: 修改YAML文件的replica的实例数量
  • 步骤2: 通过kubectl apply -f 命令执行扩缩容

RS示例说明

示例YAML文件如下所示:

[root@host131 ReplicaSet]# cat selecotr-busybox-rs.yaml 
---
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: test-rs
spec:
  replicas: 1
  selector:
    matchLabels:
      app: busybox-rs-prod
  template:
    metadata:
      name: busybox
      labels:
        app: busybox-rs-prod
    spec:
      containers:
      - name: busybox-host
        image: busybox:latest
        command: ["sleep"]
        args: ["10000"]
...
[root@host131 ReplicaSet]# 

生成RS和Pod

[root@host131 ReplicaSet]# kubectl create -f selecotr-busybox-rs.yaml 
replicaset.apps/test-rs created
[root@host131 ReplicaSet]# 
[root@host131 ReplicaSet]# kubectl get rs -o wide
NAME      DESIRED   CURRENT   READY   AGE   CONTAINERS     IMAGES           SELECTOR
test-rs   1         1         1       7s    busybox-host   busybox:latest   app=busybox-rs-prod
[root@host131 ReplicaSet]# 
[root@host131 ReplicaSet]# kubectl get pods -o wide
NAME            READY   STATUS    RESTARTS   AGE   IP             NODE              NOMINATED NODE   READINESS GATES
test-rs-nrjhh   1/1     Running   0          16s   10.254.152.5   192.168.163.131   <none>           <none>
[root@host131 ReplicaSet]#
  • 使用kubectl scale将Pod从1调整为3
[root@host131 ReplicaSet]# kubectl scale rs/test-rs --replicas=3
replicaset.apps/test-rs scaled
[root@host131 ReplicaSet]# 

也可以使用如下格式执行操作

[root@host131 ReplicaSet]# kubectl scale rs test-rs --replicas=3
replicaset.apps/test-rs scaled
[root@host131 ReplicaSet]#

执行结果确认

[root@host131 ReplicaSet]# kubectl get pods -o wide
NAME            READY   STATUS              RESTARTS   AGE   IP             NODE              NOMINATED NODE   READINESS GATES
test-rs-97thq   0/1     ContainerCreating   0          5s    <none>         192.168.163.131   <none>           <none>
test-rs-gw72x   0/1     ContainerCreating   0          5s    <none>         192.168.163.131   <none>           <none>
test-rs-nrjhh   1/1     Running             0          53s   10.254.152.5   192.168.163.131   <none>           <none>
[root@host131 ReplicaSet]# kubectl get rs -o wide
NAME      DESIRED   CURRENT   READY   AGE   CONTAINERS     IMAGES           SELECTOR
test-rs   3         3         3       57s   busybox-host   busybox:latest   app=busybox-rs-prod
[root@host131 ReplicaSet]# kubectl get pods -o wide
NAME            READY   STATUS    RESTARTS   AGE   IP             NODE              NOMINATED NODE   READINESS GATES
test-rs-97thq   1/1     Running   0          14s   10.254.152.7   192.168.163.131   <none>           <none>
test-rs-gw72x   1/1     Running   0          14s   10.254.152.6   192.168.163.131   <none>           <none>
test-rs-nrjhh   1/1     Running   0          62s   10.254.152.5   192.168.163.131   <none>           <none>
[root@host131 ReplicaSet]# 
  • 使用kubectl apply方式将Pod从3调整为2
[root@host131 ReplicaSet]# vi selecotr-busybox-rc.yaml 
[root@host131 ReplicaSet]# grep replicas selecotr-busybox-rc.yaml 
  replicas: 2
[root@host131 ReplicaSet]# 
[root@host131 ReplicaSet]# kubectl apply -f selecotr-busybox-rc.yaml 
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
replicationcontroller/test-rc configured
[root@host131 ReplicaSet]# 

确认结果如下所示

[root@host131 ReplicaSet]# kubectl get rs -o wide
NAME      DESIRED   CURRENT   READY   AGE     CONTAINERS     IMAGES           SELECTOR
test-rs   2         2         2       2m36s   busybox-host   busybox:latest   app=busybox-rs-prod
[root@host131 ReplicaSet]# 
[root@host131 ReplicaSet]# kubectl get pods -o wide
NAME            READY   STATUS        RESTARTS   AGE     IP             NODE              NOMINATED NODE   READINESS GATES
test-rs-97thq   1/1     Running       0          117s    10.254.152.7   192.168.163.131   <none>           <none>
test-rs-gw72x   1/1     Terminating   0          117s    10.254.152.6   192.168.163.131   <none>           <none>
test-rs-nrjhh   1/1     Running       0          2m45s   10.254.152.5   192.168.163.131   <none>           <none>
[root@host131 ReplicaSet]# 
[root@host131 ReplicaSet]# kubectl get pods -o wide
NAME            READY   STATUS    RESTARTS   AGE     IP             NODE              NOMINATED NODE   READINESS GATES
test-rs-97thq   1/1     Running   0          4m1s    10.254.152.7   192.168.163.131   <none>           <none>
test-rs-nrjhh   1/1     Running   0          4m49s   10.254.152.5   192.168.163.131   <none>           <none>
[root@host131 ReplicaSet]#
发布了1084 篇原创文章 · 获赞 1299 · 访问量 402万+

猜你喜欢

转载自blog.csdn.net/liumiaocn/article/details/104200294