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.
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
- Easiest way to start a Kubernetes cluster on a Mac OS is using Vagrant:12
exportKUBERNETES_PROVIDER=vagrantcurl -sS https://get.k8s.io |bash - Alternatively, Kubernetes can be downloaded from github.com/GoogleCloudPlatform/kubernetes/releases/download/v1.0.0/kubernetes.tar.gz, and cluster can be started as:123
cdkubernetesexportKUBERNETES_PROVIDER=vagrantcluster/kube-up.sh
Start and Verify Replication Controller and Pods
- 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 - Start a Replication Controller that has two replicas of a pod, each with a WildFly container:12
./cluster/kubectl.sh create -f ~/workspaces/kubernetes-java-sample/wildfly-rc.yamlreplicationcontrollers/wildfly-rcThe configuration file used is shown:
010203040506070809101112131415161718apiVersion: v1kind: ReplicationControllermetadata:name: wildfly-rclabels:name: wildflyspec:replicas: 2template:metadata:labels:name: wildflyspec:containers:- name: wildfly-rc-podimage: jboss/wildflyports:- containerPort: 8080Default WildFly Docker image is used here.
- Get status of the Pods:0102030405060708091011
./cluster/kubectl.sh get -w poNAME READY STATUS RESTARTS AGEwildfly-rc-15xg5 0/1Pending 0 2swildfly-rc-d5fbs 0/1Pending 0 2sNAME READY STATUS RESTARTS AGEwildfly-rc-15xg5 0/1Pending 0 5swildfly-rc-d5fbs 0/1Pending 0 5swildfly-rc-d5fbs 0/1Running 0 8swildfly-rc-15xg5 0/1Running 0 8swildfly-rc-d5fbs 1/1Running 0 15swildfly-rc-15xg5 1/1Running 0 15sNotice
-wrefreshes the status whenever there is a change. The status changes from Pending to Running and then Ready to receive requests. - Get status of the Replication Controller:123
./cluster/kubectl.sh get rcCONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICASwildfly-rc wildfly-rc-pod jboss/wildflyname=wildfly 2If multiple Replication Controllers are running then you can query for this specific one using the label:
123./cluster/kubectl.sh get rc -l name=wildflyCONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICASwildfly-rc wildfly-rc-pod jboss/wildflyname=wildfly 2 - Get name of the running Pods:1234
./cluster/kubectl.sh get poNAME READY STATUS RESTARTS AGEwildfly-rc-15xg5 1/1Running 0 36mwildfly-rc-d5fbs 1/1Running 0 36m - Find IP address of each Pod (using the name):12
./cluster/kubectl.sh get -o template po wildfly-rc-15xg5 --template={{.status.podIP}}10.246.1.18And of the other Pod as well:
12./cluster/kubectl.sh get -o template po wildfly-rc-d5fbs --template={{.status.podIP}}10.246.1.19 - Pod’s IP address is accessible only inside the cluster. Login to the minion to access WildFly’s main page hosted by the containers:010203040506070809101112131415161718192021
kubernetes> vagrantsshminion-1Last 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-15xg5pods/wildfly-rc-15xg5kubernetes> ./cluster/kubectl.sh get -w poNAME READY STATUS RESTARTS AGEwildfly-rc-0xoms 0/1 Pending 0 2swildfly-rc-d5fbs 1/1 Running 0 48mNAME READY STATUS RESTARTS AGEwildfly-rc-0xoms 0/1 Pending 0 11swildfly-rc-0xoms 0/1 Running 0 13swildfly-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!
| Reference: | Automatic Restarting of Pods inside Replication Controller of Kubernetes Cluster from our JCG partner Arun Gupta at the Miles to go 2.0 … blog. |







