Basic Operations

Tutorial 3 of 4

Introduction

Goal

The goal of this tutorial is to introduce you to the basic operations in Kubernetes, including how to deploy, scale, update, and expose applications.

Learning Outcomes

By the end of this tutorial, you'll be able to:
- Understand the basics of Kubernetes
- Deploy, update, and scale applications using Kubernetes
- Expose your applications to the internet

Prerequisites

Before starting this tutorial, you should have:
- Basic knowledge of Docker and containers
- A working installation of Kubernetes (you can use Minikube for local development)

Step-by-Step Guide

Concepts

Kubernetes is a container orchestration platform that automates the deployment, scaling, and management of containerized applications. Here are the key concepts we'll be using in this tutorial:

  • Pods: The smallest and simplest unit in the Kubernetes object model. A pod represents a single instance of a running process in a cluster and can contain one or more containers.

  • Deployments: A Deployment controller provides declarative updates for Pods and ReplicaSets. You describe a desired state in a Deployment, and the Deployment controller changes the actual state to the desired state at a controlled rate.

  • Services: An abstract way to expose an application running on a set of Pods as a network service.

Best Practices

  • Always specify resource limits for your Pods to prevent them from consuming too much CPU or memory.
  • Use liveness and readiness probes to check the health of your application.
  • Use namespaces to logically separate different parts of your application.

Code Examples

Let's create a simple Deployment for a Node.js application.

# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nodejs-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nodejs
  template:
    metadata:
      labels:
        app: nodejs
    spec:
      containers:
      - name: nodejs
        image: node:10
        ports:
        - containerPort: 8080

In this example, we're creating a Deployment named nodejs-deployment with 3 replicas. We're using the node:10 Docker image and exposing port 8080.

To apply this Deployment, you can use the following command:

kubectl apply -f deployment.yaml

Summary

In this tutorial, we've covered the basics of Kubernetes, including Deployments, Pods, and Services. We've also seen how to deploy, scale, update, and expose applications in Kubernetes.

Practice Exercises

  1. Exercise 1: Create a Deployment for an application of your choice with 2 replicas. Expose it using a Service.

  2. Exercise 2: Update the Deployment from Exercise 1 to use a newer version of the application. Scale it to 5 replicas.

  3. Exercise 3: Create a new Namespace and deploy a different application in it.

For further practice, you could try deploying more complex applications, using ConfigMaps and Secrets to manage configuration, or exploring other Kubernetes resources like Jobs and CronJobs.