Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Automation of Replication Controller in Kubernetes Cluster

DZone's Guide to

Automation of Replication Controller in Kubernetes Cluster

Learn how to use the Replication Controller to maintain state in a Kubernetes cluster.

· Cloud Zone
Free Resource

MongoDB Atlas is a database as a service that makes it easy to deploy, manage, and scale MongoDB. So you can focus on innovation, not operations. Brought to you in partnership with MongoDB.

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:
    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:
    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:
    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:
    ./cluster/kubectl.sh create -f ~/workspaces/kubernetes-java-sample/wildfly-rc.yaml
    replicationcontrollers/wildfly-rc
    The configuration file used is shown:
    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:
    ./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:
    ./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
  5. Get name of the running Pods:
    ./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):
    ./cluster/kubectl.sh get -o template po wildfly-rc-15xg5 --template={{.status.podIP}}
    10.246.1.18
    And of the other Pod as well:
    ./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:
    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.

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:

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!

MongoDB Atlas is the best way to run MongoDB on AWS — highly secure by default, highly available, and fully elastic. Get started free. Brought to you in partnership with MongoDB.

Topics:
kubernetes ,docker ,containers ,clustering ,cloud

Published at DZone with permission of Arun Gupta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}