Deploying Applications on Kubernetes

Tutorial 2 of 5

1. Introduction

In this tutorial, we will learn how to deploy an application on Kubernetes, a popular open-source platform for managing containerized workloads and services. We will create and manage Deployments and Services.

By the end of this tutorial, you will be able to:

  • Understand the basics of Kubernetes
  • Create a Deployment
  • Deploy an application on Kubernetes
  • Create a Service

Prerequisites

  • Basic knowledge of Docker
  • A local setup of Kubernetes (like Minikube) or an account on a cloud platform offering Kubernetes (like Google Cloud)
  • kubectl command line tool installed. It is the Kubernetes command-line tool, which allows you to run commands against Kubernetes clusters.

2. Step-by-Step Guide

Kubernetes Deployments

A Deployment 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.

Kubernetes Services

A Kubernetes Service is an abstraction which defines a logical set of Pods and a policy by which to access them - sometimes called a micro-service. It can be exposed in different ways by specifying a type in the ServiceSpec.

Deploying an Application

Step 1: Create a Deployment

Let's assume you have a Docker image of your application named my-app:v1. To create a Deployment for this:

kubectl create deployment my-app --image=my-app:v1

This command creates a Deployment named my-app using the Docker image my-app:v1.

Step 2: Check Deployment Status

kubectl get deployments

This command lists all the Deployments in the Kubernetes cluster.

Step 3: Create a Service

To expose your application to external traffic, you need to create a Service:

kubectl expose deployment my-app --type=LoadBalancer --port=8080

This command creates a Service named my-app which exposes the Deployment my-app on port 8080.

Step 4: Check Service Status

kubectl get services

This command lists all Services in your Kubernetes cluster.

3. Code Examples

Creating a Deployment

kubectl create deployment my-app --image=my-app:v1

In this command:
- kubectl create deployment: Command to create a new Deployment.
- my-app: Name of the Deployment.
- --image=my-app:v1: Docker image to use for the Deployment.

Creating a Service

kubectl expose deployment my-app --type=LoadBalancer --port=8080

In this command:
- kubectl expose deployment: Command to expose the Deployment as a Service.
- my-app: Name of the Deployment to expose.
- --type=LoadBalancer: Type of the Service. A LoadBalancer Service is a Service accessible externally via a cloud provider's load balancer.
- --port=8080: Port to expose the Service on.

4. Summary

In this tutorial, we learned how to create and manage Deployments and Services in Kubernetes. We have also learned how to deploy an application and expose it to external traffic.

For more learning, you can read the official Kubernetes documentation.

5. Practice Exercises

  1. Exercise 1: Create a Deployment for an Nginx server using the Docker image nginx:1.17.10 and name it nginx-deployment.

Solution: kubectl create deployment nginx-deployment --image=nginx:1.17.10

  1. Exercise 2: Create a LoadBalancer Service for the nginx-deployment on port 80.

Solution: kubectl expose deployment nginx-deployment --type=LoadBalancer --port=80

  1. Exercise 3: Scale the nginx-deployment to 3 replicas.

Solution: kubectl scale deployment/nginx-deployment --replicas=3

Remember to practice regularly and experiment with different commands and configurations to deepen your understanding of Kubernetes.