Setting up a Kubernetes cluster is a fundamental step in learning and using Kubernetes. This section will guide you through the process of setting up a Kubernetes cluster using different methods. We will cover the following:

  1. Prerequisites
  2. Setting Up a Local Kubernetes Cluster with Minikube
  3. Setting Up a Kubernetes Cluster on the Cloud with Google Kubernetes Engine (GKE)
  4. Setting Up a Kubernetes Cluster with kubeadm
  5. Verifying the Cluster Setup

  1. Prerequisites

Before setting up a Kubernetes cluster, ensure you have the following:

  • A computer with a supported operating system (Linux, macOS, or Windows).
  • Basic knowledge of command-line interface (CLI) operations.
  • Installed Docker (for local setups).
  • Installed kubectl (Kubernetes command-line tool).

  1. Setting Up a Local Kubernetes Cluster with Minikube

Minikube is a tool that makes it easy to run Kubernetes locally. It runs a single-node Kubernetes cluster inside a virtual machine (VM) on your local machine.

Steps to Set Up Minikube:

  1. Install Minikube:

  2. Start Minikube:

    minikube start
    
  3. Verify Minikube Installation:

    kubectl get nodes
    

    You should see a single node named minikube.

Example:

$ minikube start
😄  minikube v1.23.2 on Darwin 11.6
✨  Automatically selected the docker driver
👍  Starting control plane node minikube in cluster minikube
🚜  Pulling base image ...
🔄  Restarting existing docker container for "minikube" ...
🐳  Preparing Kubernetes v1.22.2 on Docker 20.10.8 ...
🔎  Verifying Kubernetes components...
🌟  Enabled addons: default-storageclass, storage-provisioner
🏄  Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

$ kubectl get nodes
NAME       STATUS   ROLES                  AGE   VERSION
minikube   Ready    control-plane,master   2m    v1.22.2

  1. Setting Up a Kubernetes Cluster on the Cloud with Google Kubernetes Engine (GKE)

Google Kubernetes Engine (GKE) is a managed Kubernetes service provided by Google Cloud Platform (GCP).

Steps to Set Up GKE:

  1. Install Google Cloud SDK:

  2. Authenticate with GCP:

    gcloud auth login
    
  3. Set Your Project:

    gcloud config set project [PROJECT_ID]
    
  4. Create a GKE Cluster:

    gcloud container clusters create my-cluster --zone us-central1-a
    
  5. Get Cluster Credentials:

    gcloud container clusters get-credentials my-cluster --zone us-central1-a
    
  6. Verify GKE Cluster:

    kubectl get nodes
    

Example:

$ gcloud container clusters create my-cluster --zone us-central1-a
Creating cluster my-cluster in us-central1-a... Cluster is being created...done.
Created [https://container.googleapis.com/v1/projects/my-project/zones/us-central1-a/clusters/my-cluster].
kubeconfig entry generated for my-cluster.

$ gcloud container clusters get-credentials my-cluster --zone us-central1-a
Fetching cluster endpoint and auth data.
kubeconfig entry generated for my-cluster.

$ kubectl get nodes
NAME                                           STATUS   ROLES    AGE   VERSION
gke-my-cluster-default-pool-1a2b3c4d-5e6f      Ready    <none>   5m    v1.22.2

  1. Setting Up a Kubernetes Cluster with kubeadm

kubeadm is a tool that helps you bootstrap a Kubernetes cluster. It is designed to be a simple way to set up a cluster on any machine.

Steps to Set Up kubeadm:

  1. Install Docker:

  2. Install kubeadm, kubelet, and kubectl:

  3. Initialize the Master Node:

    sudo kubeadm init --pod-network-cidr=192.168.0.0/16
    
  4. Set Up kubeconfig for kubectl:

    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config
    
  5. Install a Pod Network Add-on:

    kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
    
  6. Join Worker Nodes:

    • On each worker node, run the command provided by kubeadm init to join the cluster.

Example:

$ sudo kubeadm init --pod-network-cidr=192.168.0.0/16
[init] Using Kubernetes version: v1.22.2
[preflight] Running pre-flight checks
...
Your Kubernetes control-plane has initialized successfully!

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

$ kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
configmap/calico-config created
...
daemonset.apps/calico-node created

$ kubectl get nodes
NAME           STATUS   ROLES                  AGE   VERSION
master-node    Ready    control-plane,master   5m    v1.22.2

  1. Verifying the Cluster Setup

After setting up your Kubernetes cluster, it's essential to verify that everything is working correctly.

Verification Steps:

  1. Check Nodes:

    kubectl get nodes
    
  2. Check System Pods:

    kubectl get pods --all-namespaces
    
  3. Deploy a Test Application:

    kubectl create deployment hello-world --image=k8s.gcr.io/echoserver:1.4
    kubectl expose deployment hello-world --type=NodePort --port=8080
    
  4. Access the Test Application:

    • Get the NodePort:
      kubectl get service hello-world
      
    • Access the application using the Node IP and NodePort.

Example:

$ kubectl get nodes
NAME           STATUS   ROLES                  AGE   VERSION
minikube       Ready    control-plane,master   10m   v1.22.2

$ kubectl get pods --all-namespaces
NAMESPACE     NAME                               READY   STATUS    RESTARTS   AGE
kube-system   coredns-558bd4d5db-7x4j2           1/1     Running   0          10m
kube-system   etcd-minikube                      1/1     Running   0          10m
...

$ kubectl create deployment hello-world --image=k8s.gcr.io/echoserver:1.4
deployment.apps/hello-world created

$ kubectl expose deployment hello-world --type=NodePort --port=8080
service/hello-world exposed

$ kubectl get service hello-world
NAME          TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
hello-world   NodePort   10.96.0.1       <none>        8080:30001/TCP   1m

Conclusion

In this section, we covered the essential steps to set up a Kubernetes cluster using Minikube, GKE, and kubeadm. Each method has its use cases, and you can choose the one that best fits your needs. By following these steps, you should now have a functional Kubernetes cluster ready for deploying applications and exploring Kubernetes further. In the next section, we will dive into using the Kubernetes CLI (kubectl) to manage your cluster.

Kubernetes Course

Module 1: Introduction to Kubernetes

Module 2: Core Kubernetes Components

Module 3: Configuration and Secrets Management

Module 4: Networking in Kubernetes

Module 5: Storage in Kubernetes

Module 6: Advanced Kubernetes Concepts

Module 7: Monitoring and Logging

Module 8: Security in Kubernetes

Module 9: Scaling and Performance

Module 10: Kubernetes Ecosystem and Tools

Module 11: Case Studies and Real-World Applications

Module 12: Preparing for Kubernetes Certification

© Copyright 2024. All rights reserved