Scaling your containers

Kubernetes has a scheduler to assign the container to the right node. In addition, you can easily scale out and scale down the number of containers. The Kubernetes scaling function will conduct the replication controller to adjust the number of containers.

Getting ready

Prepare the following YAML file, which is a simple replication controller to launch two nginx containers. Also, service will expose the TCP port 30080:

# cat nginx-rc-svc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: my-nginx
spec:
  replicas: 2
  selector:
      sel : my-selector
  template:
    metadata:
        labels:
          sel : my-selector
    spec:
      containers:
      - name: my-container
        image: nginx
---
apiVersion: v1
kind: Service
metadata:
name: my-nginx

spec:
  ports:
    - protocol: TCP
      port: 80
      nodePort: 30080
  type: NodePort
  selector:
     sel: my-selector

Tip

NodePort will bind all the Kubernetes nodes; therefore, make sure NodePort is not used by other processes.

Use the kubectl command to create resources as follows:

# kubectl create -f nginx-service.yaml
replicationcontroller "my-nginx" created
service "my-nginx" created

Wait for a moment to completely launch two nginx containers as follows:

# kubectl get pods
NAME             READY     STATUS    RESTARTS   AGE
my-nginx-iarzy   1/1       Running   0          7m
my-nginx-ulkvh   1/1       Running   0          7m

# kubectl get services
NAME         CLUSTER_IP       EXTERNAL_IP   PORT(S)   SELECTOR          AGE
kubernetes   192.168.0.1      <none>        443/TCP   <none>            44d
my-nginx     192.168.95.244   nodes         80/TCP    sel=my-selector   7m

How to do it…

Kubernetes has a command that changes the number of replicas for service:

  1. Type the kubectl scale command as follows to specify the desired replicas:
    # kubectl scale --replicas=4 rc my-nginx
    replicationcontroller "my-nginx" scaled
    

    This example indicates that the replication controller, which is named my-nginx, changes the replicas to 4.

  2. Type kubectl get pods to confirm the result as follows:
    # kubectl get pods
    NAME             READY     STATUS    RESTARTS   AGE
    my-nginx-iarzy   1/1       Running   0          20m
    my-nginx-r5lnq   1/1       Running   0          1m
    my-nginx-uhe8r   1/1       Running   0          1m
    my-nginx-ulkvh   1/1       Running   0          20m
    

How it works…

The kubectl scale feature can change the number of replicas; not only increase, but also decrease. For example, you can change back to two replicas as follows:

# kubectl scale --replicas=2 rc my-nginx
replicationcontroller "my-nginx" scaled

# kubectl get pods
NAME             READY     STATUS        RESTARTS   AGE
my-nginx-iarzy   0/1       Terminating   0          40m
my-nginx-r5lnq   1/1       Running       0          21m
my-nginx-uhe8r   1/1       Running       0          21m
my-nginx-ulkvh   0/1       Terminating   0          40m
# kubectl get pods
NAME             READY     STATUS    RESTARTS   AGE
my-nginx-r5lnq   1/1       Running   0          25m
my-nginx-uhe8r   1/1       Running   0          25m

There is an option --current-replicas that specifies the expected current replicas. If it doesn't match, Kubernetes doesn't perform the scale function as follows:

//abort scaling, because current replica is 2, not 3
# kubectl scale --current-replicas=3 --replicas=4
rc my-nginx
Expected replicas to be 3, was 2

# kubectl get pods
NAME             READY     STATUS    RESTARTS   AGE
my-nginx-r5lnq   1/1       Running   0          27m
my-nginx-uhe8r   1/1       Running   0          27m

It will help prevent human error. By default, --current-replicas equals -1, which means bypass to check the current number of replicas:

//no matter current number of replicas, performs to change to 4
# kubectl scale --current-replicas=-1 --replicas=4
 rc my-nginx
replicationcontroller "my-nginx" scaled

# kubectl get pods
NAME             READY     STATUS    RESTARTS   AGE
my-nginx-dimxj   1/1       Running   0          5s
my-nginx-eem3a   1/1       Running   0          5s
my-nginx-r5lnq   1/1       Running   0          35m
my-nginx-uhe8r   1/1       Running   0          35m

See also

This recipe described how to change the number of pods using the scaling option by the replication controller. It is useful to scale up and scale down your application quickly. To know more about how to update your container, refer to the following recipes:

  • Updating live containers
  • Ensuring flexible usage of your containers
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset