Set a new size for a Deployment, ReplicaSet, Replication Controller, or StatefulSet.

Scale also allows users to specify one or more preconditions for the scale action.

If --current-replicas or --resource-version is specified, it is validated before the scale is attempted, and it is
guaranteed that the precondition holds true when the scale is sent to the server.

  # Scale a replicaset named 'foo' to 3.
  kubectl scale --replicas=3 rs/foo

  # Scale a resource identified by type and name specified in "foo.yaml" to 3.
  kubectl scale --replicas=3 -f foo.yaml

  # If the deployment named mysql's current size is 2, scale mysql to 3.
  kubectl scale --current-replicas=2 --replicas=3 deployment/mysql

  # Scale multiple replication controllers.
  kubectl scale --replicas=5 rc/foo rc/bar rc/baz

  # Scale statefulset named 'web' to 3.
  kubectl scale --replicas=3 statefulset/web

      --all=false: Select all resources in the namespace of the specified resource types
      --allow-missing-template-keys=true: If true, ignore any errors in templates when a field or map key is missing in
the template. Only applies to golang and jsonpath output formats.
      --current-replicas=-1: Precondition for current size. Requires that the current size of the resource match this
value in order to scale.
  -f, --filename=[]: Filename, directory, or URL to files identifying the resource to set a new size
  -o, --output='': Output format. One of:
      --record=false: Record current kubectl command in the resource annotation. If set to false, do not record the
command. If set to true, record the command. If not set, default to updating the existing annotation value only if one
already exists.
  -R, --recursive=false: Process the directory used in -f, --filename recursively. Useful when you want to manage
related manifests organized within the same directory.
      --replicas=0: The new desired number of replicas. Required.
      --resource-version='': Precondition for resource version. Requires that the current resource version match this
value in order to scale.
  -l, --selector='': Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)
      --template='': Template string or path to template file to use when -o=go-template, -o=go-template-file. The
template format is golang templates [].
      --timeout=0s: The length of time to wait before giving up on a scale operation, zero means don't wait. Any other
values should contain a corresponding time unit (e.g. 1s, 2m, 3h).

  kubectl scale [--resource-version=version] [--current-replicas=count] --replicas=COUNT (-f FILENAME | TYPE NAME)

Use "kubectl options" for a list of global command-line options (applies to all commands).


[root@k8s1 ~]# kubectl run myapp --image=ikubernetes/myapp:v1 --replicas=2
deployment.apps/myapp created
[root@k8s1 ~]# kubectl expose deployment myapp --name=myapp --port=80
service/myapp exposed
[root@k8s1 ~]# kubectl get svc -l run=myapp
myapp     ClusterIP   <none>        80/TCP    23s


我们对service myapp的ip的80端口请求50次,看每个主机请求了多少次。

[root@k8s1 ~]# for i in {1..50};do curl -s;done|sort |uniq -c
     22 myapp-848b5b879b-hfdnr
     28 myapp-848b5b879b-q8jnl



  • 现在我们使用scale命令将pod数量提高到5个
[root@k8s1 ~]# kubectl scale --replicas=5 deployment myapp
deployment.extensions/myapp scaled
  • 然后再访问那个service,这次我们访问500次
[root@k8s1 ~]# for i in {1..500};do curl -s;done|sort |uniq -c
    118 myapp-848b5b879b-hfdnr
     97 myapp-848b5b879b-k2dvt
    103 myapp-848b5b879b-m8gwh
     90 myapp-848b5b879b-q8jnl
     92 myapp-848b5b879b-q8wmm


  • 然后我们将pod数量减少到3个,然后访问300次。
[root@k8s1 ~]# kubectl scale --replicas=3 deployment myapp
deployment.extensions/myapp scaled
[root@k8s1 ~]#
[root@k8s1 ~]# for i in {1..300};do curl -s;done|sort |uniq -c
    106 myapp-848b5b879b-hfdnr
     89 myapp-848b5b879b-q8jnl
    105 myapp-848b5b879b-q8wmm