In this section, we will cover the steps to deploy a Kubernetes cluster. Kubernetes is an open-source platform designed to automate deploying, scaling, and operating application containers. By the end of this module, you will have a functional Kubernetes cluster running on your Linux system.

Objectives

  • Understand the basics of Kubernetes architecture.
  • Install and configure Kubernetes components.
  • Deploy a simple application on the Kubernetes cluster.

Prerequisites

  • Basic understanding of Linux commands.
  • Familiarity with Docker and containerization concepts.
  • A Linux system with at least 2GB of RAM and 2 CPUs.

  1. Understanding Kubernetes Architecture

Kubernetes architecture consists of the following key components:

  • Master Node: Manages the Kubernetes cluster. It includes components like the API server, scheduler, and controller manager.
  • Worker Nodes: Run the containerized applications. Each worker node has a kubelet, kube-proxy, and a container runtime (e.g., Docker).

Key Components

Component Description
API Server Exposes the Kubernetes API.
etcd A key-value store for all cluster data.
Scheduler Assigns workloads to nodes based on resource availability.
Controller Manager Manages controllers that regulate the state of the cluster.
Kubelet Ensures containers are running in a Pod.
Kube-proxy Manages network rules on nodes.

  1. Installing Kubernetes

Step 1: Install Docker

Kubernetes uses Docker as the container runtime. Install Docker using the following commands:

sudo apt-get update
sudo apt-get install -y docker.io
sudo systemctl start docker
sudo systemctl enable docker

Step 2: Install kubeadm, kubelet, and kubectl

These are the essential components for setting up a Kubernetes cluster.

sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl
sudo curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo bash -c 'cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF'
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

Step 3: Initialize the Master Node

Initialize the master node using kubeadm.

sudo kubeadm init --pod-network-cidr=192.168.0.0/16

After initialization, you will see a command to join worker nodes to the cluster. Save this command for later use.

Step 4: Configure kubectl

Set up the local kubeconfig file to manage the cluster.

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Step 5: Install a Pod Network Add-on

Install a network add-on to enable communication between pods.

kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

  1. Adding Worker Nodes

Step 1: Join Worker Nodes

Run the saved join command on each worker node to add them to the cluster.

sudo kubeadm join <master-node-ip>:6443 --token <token> --discovery-token-ca-cert-hash sha256:<hash>

Step 2: Verify the Cluster

Check the status of the nodes to ensure they are ready.

kubectl get nodes

  1. Deploying an Application

Step 1: Create a Deployment

Deploy a simple Nginx application.

kubectl create deployment nginx --image=nginx

Step 2: Expose the Deployment

Expose the deployment to make it accessible.

kubectl expose deployment nginx --port=80 --type=NodePort

Step 3: Verify the Deployment

Check the status of the deployment and the service.

kubectl get deployments
kubectl get services

  1. Practical Exercise

Exercise: Deploy a Custom Application

  1. Create a deployment for a custom application (e.g., a simple web server).
  2. Expose the deployment to make it accessible.
  3. Verify the deployment and access the application.

Solution

  1. Create a deployment:
kubectl create deployment myapp --image=myapp-image
  1. Expose the deployment:
kubectl expose deployment myapp --port=8080 --type=NodePort
  1. Verify the deployment:
kubectl get deployments
kubectl get services

Conclusion

In this module, you learned how to deploy a Kubernetes cluster on a Linux system. You installed and configured Kubernetes components, added worker nodes, and deployed a simple application. This knowledge provides a strong foundation for managing containerized applications at scale. In the next module, we will explore more advanced topics in Kubernetes and container orchestration.

© Copyright 2024. All rights reserved