Kubernetes:如何扩展我的豆荚

时间:2016-07-13 07:11:18

标签: kubernetes

我是Kubernetes的新手。我尝试扩展我的豆荚。首先,我开始了3个豆荚:

./cluster/kubectl.sh run my-nginx --image=nginx --replicas=3 --port=80

开始有3个豆荚。首先,我尝试使用复制控制器进行扩展/缩小,但这不存在。它现在似乎是一个replicaSet。

./cluster/kubectl.sh get rs
NAME                  DESIRED   CURRENT   AGE
my-nginx-2494149703   3         3         9h

我试图更改我的replicaset中描述的副本数量:

./cluster/kubectl.sh scale --replicas=5 rs/my-nginx-2494149703
replicaset "my-nginx-2494149703" scaled

但我仍然看到我的3个原始豆荚

./cluster/kubectl.sh get pods
NAME                        READY     STATUS    RESTARTS   AGE
my-nginx-2494149703-04xrd   1/1       Running   0          9h
my-nginx-2494149703-h3krk   1/1       Running   0          9h
my-nginx-2494149703-hnayu   1/1       Running   0          9h

我希望看到5个豆荚。

./cluster/kubectl.sh describe rs/my-nginx-2494149703
Name:       my-nginx-2494149703
Namespace:  default
Image(s):   nginx
Selector:   pod-template-hash=2494149703,run=my-nginx
Labels:     pod-template-hash=2494149703
        run=my-nginx
Replicas:   3 current / 3 desired
Pods Status:    3 Running / 0 Waiting / 0 Succeeded / 0 Failed

为什么不扩大规模?我是否还必须在部署中更改某些内容?

当我在扩展后描述我的rs时,我看到类似的东西: (这里我尝试从一个正在运行的pod扩展到3个正在运行的pod)。但它仍然是一个运行的pod。其他2人立即开始杀死

  34s       34s     1   {replicaset-controller }            Normal      SuccessfulCreate    Created pod: my-nginx-1908062973-lylsz
  34s       34s     1   {replicaset-controller }            Normal      SuccessfulCreate    Created pod: my-nginx-1908062973-5rv8u
  34s       34s     1   {replicaset-controller }            Normal      SuccessfulDelete    Deleted pod: my-nginx-1908062973-lylsz
  34s       34s     1   {replicaset-controller }            Normal      SuccessfulDelete    Deleted pod: my-nginx-1908062973-5rv8u

6 个答案:

答案 0 :(得分:9)

TL; DR:您需要直接扩展部署而不是副本集。

如果您尝试扩展副本集,那么它将(在很短的时间内)有一个新的计数5.但是部署控制器将看到副本集的当前计数是5,因为它知道它应该是3,它将重置为3.通过手动修改为您创建的副本集,您正在与系统控制器(这是不懈的,并将几乎总是比你长)。

答案 1 :(得分:7)

在该kubectl run my-nginx --image=nginx --replicas=3 --port=80中的

kubectl run将创建一个部署作业来管理创建的容器。
部署-> ReplicaSet-> Pod 就是kubernetes的工作方式。
如果更改底层对象,则其上层对象将撤消更改。您必须更改顶层对象。

enter image description here

答案 2 :(得分:3)

这对我有用

kubectl scale --replicas=<expected_replica_num> deployment <deployment_label_name>

示例

# kubectl scale --replicas=3 deployment xyz

答案 3 :(得分:0)

由于我是从kubernetes开始的,所以不确定这是否是最好的方法,但是我是通过更新yaml文件来实现的

# app.yaml
apiVersion: apps/v1
...
spec:
  replicas: <new value>

并运行$ kubectl scale -f app.yaml --replicas=<new value>

您可以通过运行$ kubectl get pods

来验证新的副本数

就我而言,我也有兴趣在Google Cloud上缩减虚拟机。我是通过$ gcloud container clusters resize appName --size=1 --zone "my-zone"

完成的

答案 4 :(得分:0)

下面的示例显示了如何放大/缩小“ pod /资源/部署”。

k8smaster@k8smaster:~/debashish$ more createdeb_deployment1.yaml 


--- 
apiVersion: apps/v1beta2
kind: Deployment
metadata: 
  name: debdeploy-webserver
spec: 
  replicas: 1
  selector: 
    matchLabels: 
      app: debdeploy1webserver
  template: 
    metadata: 
      labels: 
        app: debdeploy1webserver
    spec: 
      containers: 
        - 
          image: "docker.io/debu3645/debapachewebserver:v1"
          name: deb-deploy1-container 
          ports: 
            - 
              containerPort: 6060

已创建部署->

**kubectl -n debns1 create -f createdeb_deployment1.yaml**




k8smaster@k8smaster:~/debashish$ `kubectl scale --replicas=5 **deployment**/debdeploy-webserver -n debns1`

(扩大5个部署)

k8smaster@k8smaster:~/debashish$ kubectl get pods -n debns1


NAME                                   READY   STATUS    RESTARTS   AGE
debdeploy-webserver-7cf4fb74c5-8wvzx   1/1     Running   0          16s
debdeploy-webserver-7cf4fb74c5-jrf6v   1/1     Running   0          16s
debdeploy-webserver-7cf4fb74c5-m9fpw   1/1     Running   0          16s
debdeploy-webserver-7cf4fb74c5-q9n7r   1/1     Running   0          16s
debdeploy-webserver-7cf4fb74c5-ttw6p   1/1     Running   1          19h
resourcepod-deb1                       1/1     Running   5          6d18h




k8smaster@k8smaster:~/debashish$ **kubectl get ep -n debns1**



NAME                ENDPOINTS                                                     AGE
frontend-svc-deb    192.168.1.10:80,192.168.1.11:80,192.168.1.12:80 + 2 more...   18h
frontend-svc1-deb   192.168.1.8:80                                                14d
frontend-svc2-deb   192.168.1.8:80                                                5d19h




k8smaster@k8smaster:~/debashish$ **kubectl scale --replicas=2** deployment/debdeploy-webserver -n debns1 

从5缩小到2

deployment.extensions / debdeploy-webserver缩放

k8smaster@k8smaster:~/debashish$ **kubectl get pods -n debns1**


NAME                                   READY   STATUS        RESTARTS   AGE
debdeploy-webserver-7cf4fb74c5-8wvzx   1/1     Terminating   0          35m
debdeploy-webserver-7cf4fb74c5-jrf6v   1/1     Terminating   0          35m
debdeploy-webserver-7cf4fb74c5-m9fpw   1/1     Terminating   0          35m
debdeploy-webserver-7cf4fb74c5-q9n7r   1/1     Running       0          35m
debdeploy-webserver-7cf4fb74c5-ttw6p   1/1     Running       1          19h
resourcepod-deb1                       1/1     Running       5          6d19h


k8smaster@k8smaster:~/debashish$ **kubectl get pods -n debns1**


NAME                                   READY   STATUS    RESTARTS   AGE
debdeploy-webserver-7cf4fb74c5-q9n7r   1/1     Running   0          37m
debdeploy-webserver-7cf4fb74c5-ttw6p   1/1     Running   1          19h
resourcepod-deb1                       1/1     Running   5          6d19h

k8smaster@k8smaster:~/debashish$ kubectl **scale --current-replicas=4 --replicas=2** deployment/debdeploy-webserver -n debns1  (Check the current no. of deployments. If current replication is 4, then bring it down to 2, else dont do anything)


error: Expected replicas to be 4, was 2


k8smaster@k8smaster:~/debashish$ **kubectl scale --current-replicas=3 --replicas=10 deployment/debdeploy-webserver -n debns1**


error: Expected replicas to be 3, was 2


k8smaster@k8smaster:~/debashish$ **kubectl scale --current-replicas=2 --replicas=10 deployment/debdeploy-webserver -n debns1**

deployment.extensions / debdeploy-webserver缩放

k8smaster@k8smaster:~/debashish$ **kubectl get pods -n debns1**


    NAME                                   READY   STATUS              RESTARTS   AGE
    debdeploy-webserver-7cf4fb74c5-46bxg   1/1     Running             0          6s
    debdeploy-webserver-7cf4fb74c5-d6qsx   0/1     ContainerCreating   0          6s
    debdeploy-webserver-7cf4fb74c5-fdq6v   1/1     Running             0          6s
    debdeploy-webserver-7cf4fb74c5-gd87t   1/1     Running             0          6s
    debdeploy-webserver-7cf4fb74c5-kqdbj   0/1     ContainerCreating   0          6s
    debdeploy-webserver-7cf4fb74c5-q9n7r   1/1     Running             0          47m
    debdeploy-webserver-7cf4fb74c5-qjvm6   1/1     Running             0          6s
    debdeploy-webserver-7cf4fb74c5-skxq4   0/1     ContainerCreating   0          6s
    debdeploy-webserver-7cf4fb74c5-ttw6p   1/1     Running             1          19h
    debdeploy-webserver-7cf4fb74c5-wlc7q   0/1     ContainerCreating   0          6s
    resourcepod-deb1                       1/1     Running             5          6d19h

答案 5 :(得分:0)

将其缩小到零,然后缩小到所需的豆荚数量(猜测等于3)

kubectl scale deployment <deployment-name> --replicas=0 -n <namespace>
kubectl scale deployment <deployment-name> --replicas=3 -n <namespace>