DevOps

Automatic Restarting of Pods inside Replication Controller of Kubernetes Cluster

A key feature of Kubernetes is its ability to maintain the “desired state” using declared primitives. Replication Controllers is a key concept that helps achieve this state.

A replication controller ensures that a specified number of pod “replicas” are running at any one time. If there are too many, it will kill some. If there are too few, it will start more.

Replication Controller

Lets take a look on how to spin up a Replication Controller with two replicas of a Pod. Then we’ll kill one pod and see how Kubernetes will start another Pod automatically.

Start Kubernetes Cluster

  1. Easiest way to start a Kubernetes cluster on a Mac OS is using Vagrant:
    1
    2
    export KUBERNETES_PROVIDER=vagrant
    curl -sS https://get.k8s.io | bash
  2. Alternatively, Kubernetes can be downloaded from github.com/GoogleCloudPlatform/kubernetes/releases/download/v1.0.0/kubernetes.tar.gz, and cluster can be started as:
    1
    2
    3
    cd kubernetes
    export KUBERNETES_PROVIDER=vagrant
    cluster/kube-up.sh

Start and Verify Replication Controller and Pods

  1. All configuration files required by Kubernetes to start Replication Controller are in kubernetes-java-sample project. Clone the workspace:
    1
    git clone https://github.com/arun-gupta/kubernetes-java-sample.git
  2. Start a Replication Controller that has two replicas of a pod, each with a WildFly container:
    1
    2
    ./cluster/kubectl.sh create -f ~/workspaces/kubernetes-java-sample/wildfly-rc.yaml
    replicationcontrollers/wildfly-rc

    The configuration file used is shown:

    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    apiVersion: v1
    kind: ReplicationController
    metadata:
      name: wildfly-rc
      labels:
        name: wildfly
    spec:
      replicas: 2
      template:
        metadata:
          labels:
            name: wildfly
        spec:
          containers:
          - name: wildfly-rc-pod
            image: jboss/wildfly
            ports:
            - containerPort: 8080

    Default WildFly Docker image is used here.

  3. Get status of the Pods:
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    ./cluster/kubectl.sh get -w po
    NAME               READY     STATUS    RESTARTS   AGE
    wildfly-rc-15xg5   0/1       Pending   0          2s
    wildfly-rc-d5fbs   0/1       Pending   0          2s
    NAME               READY     STATUS    RESTARTS   AGE
    wildfly-rc-15xg5   0/1       Pending   0          5s
    wildfly-rc-d5fbs   0/1       Pending   0         5s
    wildfly-rc-d5fbs   0/1       Running   0         8s
    wildfly-rc-15xg5   0/1       Running   0         8s
    wildfly-rc-d5fbs   1/1       Running   0         15s
    wildfly-rc-15xg5   1/1       Running   0         15s

    Notice -w refreshes the status whenever there is a change. The status changes from Pending to Running and then Ready to receive requests.

  4. Get status of the Replication Controller:
    1
    2
    3
    ./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:

    1
    2
    3
    ./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
  5. Get name of the running Pods:
    1
    2
    3
    4
    ./cluster/kubectl.sh get po
    NAME               READY     STATUS    RESTARTS   AGE
    wildfly-rc-15xg5   1/1       Running   0          36m
    wildfly-rc-d5fbs   1/1       Running   0          36m
  6. Find IP address of each Pod (using the name):
    1
    2
    ./cluster/kubectl.sh get -o template po wildfly-rc-15xg5 --template={{.status.podIP}}
    10.246.1.18

    And of the other Pod as well:

    1
    2
    ./cluster/kubectl.sh get -o template po wildfly-rc-d5fbs --template={{.status.podIP}}
    10.246.1.19
  7. Pod’s IP address is accessible only inside the cluster. Login to the minion to access WildFly’s main page hosted by the containers:
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    kubernetes> vagrant ssh minion-1
    Last login: Tue Jul 14 21:35:12 2015 from 10.0.2.2
    [vagrant@kubernetes-minion-1 ~]$ curl http://10.246.1.18:8080
    <!--
      ~ JBoss, Home of Professional Open Source.
    . . .
    </div>
    </body>
    </html>
    [vagrant@kubernetes-minion-1 ~]$ curl http://10.246.1.19:8080
    <!--
      ~ JBoss, Home of Professional Open Source.
      ~ Copyright (c) 2014, Red Hat, Inc., and individual contributors
    . . .
    </div>
    </body>
    </html>

Automatic Restart of Pods

Lets delete a Pod and see how a new Pod is automatically created.

01
02
03
04
05
06
07
08
09
10
kubernetes> ./cluster/kubectl.sh delete po wildfly-rc-15xg5
pods/wildfly-rc-15xg5
kubernetes> ./cluster/kubectl.sh get -w po
NAME               READY     STATUS    RESTARTS   AGE
wildfly-rc-0xoms   0/1       Pending   0          2s
wildfly-rc-d5fbs   1/1       Running   0          48m
NAME               READY     STATUS    RESTARTS   AGE
wildfly-rc-0xoms   0/1       Pending   0          11s
wildfly-rc-0xoms   0/1       Running   0         13s
wildfly-rc-0xoms   1/1       Running   0         21s

Notice how the Pod with name wildfly-rc-15xg5 was deleted and a new Pod with the name wildfly-rc-0xoms was created.

Finally, delete the Replication Controller:

1
kubectl.sh create -f ~/workspaces/kubernetes-java-sample/wildfly-rc.yaml

The latest configuration files and detailed instructions are at kubernetes-java-sample.

In real world, you’ll typically wrap this Replication Controller in a Service and front-end with a Load Balancer. But that’s a topic for another blog!

Enjoy!

Arun Gupta

Arun is a technology enthusiast, avid runner, author of a best-selling book, globe trotter, a community guy, Java Champion, JavaOne Rockstar, JUG Leader, Minecraft Modder, Devoxx4Kids-er, and a Red Hatter.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button