Scaling Kubernetes Cluster

Posted by: Arun Gupta on July 15, 2015

kubernetes-logo

Automatic Restarting of Pods inside Replication Controller of Kubernetes Cluster shows how Kubernetes reschedule pods in the cluster if one or more of existing Pods disappear for some reason. This is a common usage pattern and one of the key features of Kubernetes.

Another common usage pattern of Replication Controller is scaling:

The replication controller makes it easy to scale the number of replicas up or down, either manually or by an auto-scaling control agent, by simply updating the replicas field.

This blog will show how a Kubernetes cluster can be easily scaled up and down.

All the code used in this blog is available at kubernetes-java-sample.

Start Replication Controller and Verify

  1. Start a Replication Controller as:
    ./cluster/kubectl.sh create -f ~/workspaces/kubernetes-java-sample/wildfly-rc.yaml 
    replicationcontrollers/wildfly-rc
  2. Get status of the Pods:
    ./cluster/kubectl.sh get -w po
    NAME      READY     STATUS    RESTARTS   AGE
    NAME               READY     STATUS    RESTARTS   AGE
    wildfly-rc-bgtkg   0/1       Pending   0          1s
    wildfly-rc-l8fqv   0/1       Pending   0         1s
    wildfly-rc-bgtkg   0/1       Pending   0         1s
    wildfly-rc-l8fqv   0/1       Pending   0         1s
    wildfly-rc-bgtkg   0/1       Pending   0         1s
    wildfly-rc-l8fqv   0/1       Pending   0         1s
    wildfly-rc-bgtkg   0/1       Running   0         1m
    wildfly-rc-l8fqv   0/1       Running   0         1m
    wildfly-rc-bgtkg   1/1       Running   0         1m
    wildfly-rc-l8fqv   1/1       Running   0         1m

    Make sure to wait for the status to change to Running.

    Note down name of the Pods as wildfly-rc-bgtkg” and wildfly-rc-bgtkg”.

  3. Get status of the Replication Controller:

    ./cluster/kubectl.sh get rc
    CONTROLLER   CONTAINER(S)     IMAGE(S)        SELECTOR       REPLICAS
    wildfly-rc   wildfly-rc-pod   jboss/wildfly   name=wildfly   2

    If multiple Replication Controllers are running then you can query for this specific one using the label:

    ./cluster/kubectl.sh get rc -l name=wildfly
    CONTROLLER   CONTAINER(S)     IMAGE(S)        SELECTOR       REPLICAS
    wildfly-rc   wildfly-rc-pod   jboss/wildfly   name=wildfly   2

Scaling Kubernetes Cluster Up

Replication Controller allows dynamic scaling up and down of Pods.

  1. Scale up the number of Pods:
    ./cluster/kubectl.sh scale --replicas=3 rc wildfly-rc
    scaled
  2. Status of the Pods can be seen in another shell:
    ./cluster/kubectl.sh get -w po
    NAME               READY     STATUS    RESTARTS   AGE
    wildfly-rc-k6pk2   1/1       Running   0          47s
    wildfly-rc-wez29   1/1       Running   0          47s
    NAME               READY     STATUS    RESTARTS   AGE
    wildfly-rc-aqaqn   0/1       Pending   0          0s
    wildfly-rc-aqaqn   0/1       Pending   0         0s
    wildfly-rc-aqaqn   0/1       Pending   0         0s
    wildfly-rc-aqaqn   0/1       Running   0         2s
    wildfly-rc-aqaqn   1/1       Running   0         11s

    Notice a new Pod with the name wildfly-rc-aqaqn is created.

Scale Kubernetes Cluster Down

  1. Scale down the number of Pods:
    ./cluster/kubectl.sh scale --replicas=1 rc wildfly-rc
    scaled
  2. Status of the Pods using -w is not correctly updated (#11338). But status of the Pods can be seen correctly as:
    ./cluster/kubectl.sh get po
    NAME               READY     STATUS    RESTARTS   AGE
    wildfly-rc-k6pk2   1/1       Running   0          9m

    Notice only one Pod is now running.

Kubernetes dynamically scales the Pods up and down using the scale --replicas command.

All code used in this blog is available at kubernetes-java-sample.

Enjoy!

The post Scaling Kubernetes Cluster appeared first on Miles to go 2.0 ....

Arun Gupta

About Arun Gupta

Arun Gupta is Director of Developer Advocacy at Red Hat and focuses on JBoss Middleware. As a founding member of the Java EE team at Sun Microsystems, he spread the love for technology all around the world. At Oracle, he led a cross-functional team to drive the global launch of the Java EE 7 platform through strategy, planning, and execution of content, marketing campaigns, and program. He is a prolific blogger since 2005 and have authored 1500+ blogs on technology. Arun has extensive speaking experience in ~40 countries on myriad topics and is a JavaOne Rockstar. He also founded the Devoxx4Kids chapter in the USA and continues to promoting technology education amongst kids. An author of a best-selling book, an avid runner, a globe trotter, a Java Champion, JUG leader, he is easily accessible at @arungupta.