Tutorials

Detailed walkthroughs of common Kubernetes operations and workflows.

Documentation for Kubernetes v1.5 is no longer actively maintained. The version you are currently viewing is a static snapshot. For up-to-date documentation, see the latest version.

Edit This Page

Run Stateless AP Replication Controller

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.

Creating a replication controller

Replication controllers are created with kubectl create:

$ kubectl create -f FILE

Where:

You can use the sample file below to try a create request.

A successful create request returns the name of the replication controller. To view more details about the controller, see Viewing replication controllers below.

Replication controller configuration file

When creating a replication controller, you must point to a configuration file as the value of the -f flag. The configuration file can be formatted as YAML or as JSON, and supports the following fields:

{
  "apiVersion": "v1",
  "kind": "ReplicationController",
  "metadata": {
    "name": "",
    "labels": "",
    "namespace": ""
  },
  "spec": {
    "replicas": int,
    "selector": {
      "":""
    },
    "template": {
      "metadata": {
        "labels": {
          "":""
        }
      },
      "spec": {
        // See 'The spec schema' below
      }
    }
  }
}

Required fields are:

The spec schema

The spec schema (that is a child of template) is described in the locations below:

Sample file

The following sample file creates 2 pods, each containing a single container using the redis image. Port 80 on each container is opened. The replication controller is tagged with the serving label. The pods are given the label frontend and the selector is set to frontend, to indicate that the controller should manage pods with the frontend label.

{
  "kind": "ReplicationController",
  "apiVersion": "v1",
  "metadata": {
    "name": "frontend-controller",
    "labels": {
      "state": "serving"
    }
  },
  "spec": {
    "replicas": 2,
    "selector": {
      "app": "frontend"
    },
    "template": {
      "metadata": {
        "labels": {
          "app": "frontend"
        }
      },
      "spec": {
        "volumes": null,
        "containers": [
          {
            "name": "php-redis",
            "image": "redis",
            "ports": [
              {
                "containerPort": 80,
                "protocol": "TCP"
              }
            ],
            "imagePullPolicy": "IfNotPresent"
          }
        ],
        "restartPolicy": "Always",
        "dnsPolicy": "ClusterFirst"
      }
    }
  }
}

Updating replication controller pods

See Rolling Updates.

Resizing a replication controller

To increase or decrease the number of pods under a replication controller’s control, use the kubectl scale command:

$ kubectl scale rc NAME --replicas=COUNT \
    [--current-replicas=COUNT] \
    [--resource-version=VERSION]

Tip: You can use the rc alias in your commands in place of replicationcontroller.

Required fields are:

Optional fields are:

Viewing replication controllers

To list replication controllers on a cluster, use the kubectl get command:

$ kubectl get rc

A successful get command returns all replication controllers on the cluster in the specified or default namespace. For example:

NAME       DESIRED   CURRENT   READY     AGE
frontend   2         2         2         1h

You can also use get rc NAME to return information about a specific replication controller.

To view detailed information about a specific replication controller, use the kubectl describe command:

$ kubectl describe rc NAME

A successful describe request returns details about the replication controller including number and status of pods managed, and recent events:

Name:        frontend
Namespace:      default
Image(s):       gcr.io/google_samples/gb-frontend:v3
Selector:       name=frontend
Labels:        name=frontend
Replicas:       2 current / 2 desired
Pods Status:    2 Running / 0 Waiting / 0 Succeeded / 0 Failed
No volumes.
Events:
  FirstSeen                        LastSeen                        Count   From                         SubobjectPath  Reason            Message
  Fri, 06 Nov 2015 16:52:50 -0800  Fri, 06 Nov 2015 16:52:50 -0800 1       {replication-controller }                   SuccessfulCreate  Created pod: frontend-gyx2h
  Fri, 06 Nov 2015 16:52:50 -0800  Fri, 06 Nov 2015 16:52:50 -0800 1       {replication-controller }                   SuccessfulCreate  Created pod: frontend-vc9w4

Deleting replication controllers

To delete a replication controller as well as the pods that it controls, use kubectl delete:

$ kubectl delete rc NAME

By default, kubectl delete rc will resize the controller to zero (effectively deleting all pods) before deleting it.

To delete a replication controller without deleting its pods, use kubectl delete and specify --cascade=false:

$ kubectl delete rc NAME --cascade=false

A successful delete request returns the name of the deleted resource.

Analytics

Create an Issue Edit this Page