Deployment Control

Tutorial 2 of 4

Introduction

Welcome to the Kubernetes Deployment Control tutorial.

The primary goal of this tutorial is to provide an understanding of how to control and manage deployments in Kubernetes. Deployments are a key concept in Kubernetes which allow you to define the desired state of your application and Kubernetes works to maintain that state.

By the end of this tutorial, you will have learned:
- What Kubernetes deployments are and why they are important
- Different strategies to update applications in Kubernetes
- How to control and manage deployments

Prerequisites

  • Basic understanding of Kubernetes and its architecture
  • Familiarity with Docker and containerization
  • Access to a Kubernetes cluster for hands-on practice

Step-by-Step Guide

What is a Kubernetes Deployment?

A Kubernetes Deployment is a resource object in Kubernetes that provides declarative updates for Pods and ReplicaSets. You describe the desired state in a Deployment, and the Deployment Controller changes the actual state to the desired state at a controlled rate.

Updating Deployments

There are two strategies for updating deployments:
1. Rolling Update: This is the default strategy which gradually replaces old pods with new ones.
2. Recreate: This strategy kills all existing pods before new ones are created.

Code Examples

Creating a Deployment

Here's an example of a Kubernetes Deployment. Save it as deployment.yaml:

# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

This Deployment creates three nginx Pods.

Updating Deployments

Let's perform a rolling update to the new version of nginx, 1.16.1. You can do this by updating the image field in the Deployment spec:

kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1

Summary

In this tutorial, we've covered how to control and manage deployments in Kubernetes, including different strategies to update applications. We've learned that Deployments are a key concept in Kubernetes that allow you to define the desired state of your application, and Kubernetes works to maintain that state.

Practice Exercises

  1. Create a Deployment with 2 replicas of the nginx:1.14.2 image and expose it on port 80.
  2. Update this Deployment to use the nginx:1.16.1 image instead.
  3. Scale this Deployment to use 5 replicas.

Solutions and explanations will be given in the following section.

Solutions to Practice Exercises

  1. Create a Deployment with 2 replicas of the nginx:1.14.2 image and expose it on port 80.

You can create the Deployment using the following command:

bash kubectl create deployment nginx --image=nginx:1.14.2 --replicas=2

  1. Update this Deployment to use the nginx:1.16.1 image instead.

You can update the Deployment using the following command:

bash kubectl set image deployment/nginx nginx=nginx:1.16.1

  1. Scale this Deployment to use 5 replicas.

You can scale the Deployment using the following command:

bash kubectl scale deployment nginx --replicas=5

Now, you are ready to go forth and manage your own Deployments in Kubernetes! Enjoy the power of orchestration!