Kubernetes基础:资源扩缩容方法(Replication Controller)

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

常用方法

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

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

扩缩容命令:kubectl scale

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

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

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

RC示例说明

示例YAML文件如下所示:

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

生成RC和Pod

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

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

[root@host131 ReplicaSet]# kubectl scale rc test-rc --replicas=3
replicationcontroller/test-rc scaled
[root@host131 ReplicaSet]#

执行结果确认

[root@host131 ReplicaSet]# kubectl get pods -o wide
NAME            READY   STATUS              RESTARTS   AGE   IP             NODE              NOMINATED NODE   READINESS GATES
test-rc-6dpmh   0/1     ContainerCreating   0          4s    <none>         192.168.163.131   <none>           <none>
test-rc-gp8fc   0/1     ContainerCreating   0          4s    <none>         192.168.163.131   <none>           <none>
test-rc-zfhj7   1/1     Running             0          85s   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-rc-6dpmh   1/1     Running   0          10s   10.254.152.6   192.168.163.131   <none>           <none>
test-rc-gp8fc   1/1     Running   0          10s   10.254.152.7   192.168.163.131   <none>           <none>
test-rc-zfhj7   1/1     Running   0          91s   10.254.152.5   192.168.163.131   <none>           <none>
[root@host131 ReplicaSet]# kubectl get rc -o wide
NAME      DESIRED   CURRENT   READY   AGE   CONTAINERS     IMAGES           SELECTOR
test-rc   3         3         3       98s   busybox-host   busybox:latest   app=busybox-prod
[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 pods -o wide
NAME            READY   STATUS        RESTARTS   AGE     IP             NODE              NOMINATED NODE   READINESS GATES
test-rc-6dpmh   1/1     Terminating   0          2m      10.254.152.6   192.168.163.131   <none>           <none>
test-rc-gp8fc   1/1     Running       0          2m      10.254.152.7   192.168.163.131   <none>           <none>
test-rc-zfhj7   1/1     Running       0          3m21s   10.254.152.5   192.168.163.131   <none>           <none>
[root@host131 ReplicaSet]# kubectl get rc -o wide
NAME      DESIRED   CURRENT   READY   AGE     CONTAINERS     IMAGES           SELECTOR
test-rc   2         2         2       3m28s   busybox-host   busybox:latest   app=busybox-prod
[root@host131 ReplicaSet]# 
[root@host131 ReplicaSet]# kubectl get pods -o wide
NAME            READY   STATUS        RESTARTS   AGE     IP             NODE              NOMINATED NODE   READINESS GATES
test-rc-6dpmh   1/1     Terminating   0          2m13s   10.254.152.6   192.168.163.131   <none>           <none>
test-rc-gp8fc   1/1     Running       0          2m13s   10.254.152.7   192.168.163.131   <none>           <none>
test-rc-zfhj7   1/1     Running       0          3m34s   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-rc-gp8fc   1/1     Running   0          6m28s   10.254.152.7   192.168.163.131   <none>           <none>
test-rc-zfhj7   1/1     Running   0          7m49s   10.254.152.5   192.168.163.131   <none>           <none>
[root@host131 ReplicaSet]#
发布了1084 篇原创文章 · 获赞 1299 · 访问量 402万+

猜你喜欢

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