Welcome!

Basic Deployments

Difficulty:beginner

Estimated Time:5-10 minutes

Overview

The Kubernetes documentation states that a Deployment allows you to:

... describe a desired state in a Deployment object, and the Deployment controller changes the actual state to the desired state at a controlled rate. You can define Deployments to create new ReplicaSets, or to remove existing Deployments and adopt all their resources with new Deployments.

In this scenario, we’ll explain both how Deployments work from a high-level perspective, and then get our hands dirty by creating a Deployment and seeing how it relates to ReplicaSet and Pod objects.

Congratulations!

You've completed the scenario!

Scenario Rating

Congratulations. You've now created, manipulated and deleted a Deployment, both through the Kubernetes CLI (kubectl) and through YAML manifests.

Steps

Basic Deployments

Step1 of 4

Creating a deployment through kubectl

Kubernetes deployments manage stateless services running in your cluster (as opposed to - for example - StatefulSets, which manage stateful services). Their purpose is to keep a set of identical pods running and upgrade them in a controlled way – performing a rolling update by default. There are different deployment strategies that work with Deployments. They are, however, out of scope of this scenario. For more information on deployment strategies, read the Kubernetes Documentation here.

Creating a Deployment in kubectl

Before we start, you should already have a namespace created called contino. If you do not have this namespace yet, or you have deleted it, then please re-create it:

kubectl create namespace contino

Create a basic deployment called nginx-deployment using the nginx image, and expose port 80 in the container:

The metadata contains the name of the deployment (which must be unique), an internal uid used by Kubernetes, and the annotations object. It contains one annotation, namely that the current deployment revision is 1. Also as seen in other scenarios throughout this course, each object in Kubernetes can have a set of labels, which are key-value pairs.

replicas: describes how many pods this deployment should have. In our case, there will be one only one pod created.

template: describes how each pod should look like. It describes a list of containers that should be in the Pod.

The two other keys can be set to customize the behavior of the deployment.

selector: determines which pods are considered to be part of this deployment. This uses labels to 'select' pods.

strategy: states how an update to a deployment should be rolled out. See earlier at the start of this chapter for the Kubernetes API link on more information on deployment strategies.

Creating a deployment through manifests

Because you have already created a deployment earlier called nginx-deployment, we need to delete it before we can create a new deployment with the YAML manifest:

kubectl delete deployment nginx-deployment -n contino

Before we create the new deployment, let's inspect the file:

cat dep-manifest.yaml; echo

As you can see, it essentially re-creates the YAML output from the deployment that we created manually in the previous chapter.

Now create the deployment:

kubectl create -f dep-manifest.yaml

Get the deployment:

kubectl get deployments -n contino -o yaml

If you go back to the previous chapter where we created the deployment manually, you'll see that the output is very similar to the output above.

Scaling replicas in a deployment

Now that you've created your deployment, let's look at scaling. Similar to how we created a deployment, you can update the replicas through the deployment manifest or by the kubectl command. For example:

kubectl scale deployment nginx-deployment --replicas=10 -n contino

This will scale the nginx-deployment Deployment to 10 replicas.

You can also edit this in the manifest. For example:

vi dep-manifest.yaml

Find the line 9 in the manifest, and update it to 10 replicas. :wq in vim to save and exit. Then replace the deployment:

kubectl replace -f dep-manifest.yaml

Once the deployment has been updated, check the pods:

kubectl get po -n contino

You should see 10 nginx pods now. You've successfully just scaled your deployment. Equally, to scale down your deployment, simply change the number of replicas and update the deployment again.

Proportional scaling

RollingUpdate Deployments support running multiple versions of an application at the same time. When you scale a RollingUpdate Deployment that is in the middle of a rollout (either in progress or paused), then the Deployment controller will balance the additional replicas in the existing active ReplicaSets (ReplicaSets with Pods) in order to mitigate risk. This is called proportional scaling.

For example, you are running a Deployment with 10 replicas, maxSurge=3, and maxUnavailable=2.

kubectl get deployments -n contino

NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx-deployment 10 10 10 10 50s

You update to a new image which happens to be unresolvable from inside the cluster.

Create and scaling a replication controller

The Replication Controller is the original form of replication in Kubernetes. It has been replaced by ReplicaSets, so we won't cover it in too much detail, but it's worth understanding.

Similar to Deployments, it allows you to tell the Kubernetes scheduler how many pods to ensure stay running. If a pod does die, then the replication controller will create another pod to ensure the replica count is matched.

Creating a Replication Controller

Look at the replication controller manifest:

cat rc-nginx.yaml; echo

Now create the replication controller:

kubectl create -f rc-nginx.yaml

Check for RCs:

kubectl get rc -n contino

Now check for pods:

kubectl get po -n contino

As you can see, there are now 3 pods, as per the replication controller manifest that we just created.

To delete a replication controller:

kubectl delete rc nginx -n contino

Scaling RCs

Scaling RCs is very similar to deployments. Edit the replicas in the manifest and use the kubectl replace -f command:

Debugging Scenarios

Help

Katacoda offerings an Interactive Learning Environment for Developers. This course uses a command line and a pre-configured sandboxed environment for you to use. Below are useful commands when working with the environment.

cd <directory>

Change directory

ls

List directory

echo 'contents' > <file>

Write contents to a file

cat <file>

Output contents of file

Vim

In the case of certain exercises you will be required to edit files or text. The best approach is with Vim. Vim has two different modes, one for entering commands (Command Mode) and the other for entering text (Insert Mode). You need to switch between these two modes based on what you want to do. The basic commands are: