DEV Community

DevCorner
DevCorner

Posted on

Setting Up a Kubernetes Cluster and More

Introduction

Kubernetes (K8s) is a powerful orchestration tool for managing containerized applications. In this blog, we'll walk through setting up a Kubernetes cluster from scratch and cover key components such as:

  • Setting up a Kubernetes Cluster
  • Installing the Admin Dashboard
  • Deploying Services
  • Implementing Health Monitoring
  • Configuring Load Balancing
  • Setting up a Service Mesh (Istio)
  • Deploying Kafka and Redis
  • Scaling and managing workloads

Let's dive in! 🚀


1. Setting Up a Kubernetes Cluster

Before deploying applications, we need a Kubernetes cluster. We can set up a cluster using kubeadm, Minikube, or a managed service like AWS EKS, Google GKE, or Azure AKS.

Option 1: Using Minikube (For Local Testing)

Minikube is ideal for local development and testing.

Step 1: Install Minikube and Kubectl

# Install Minikube
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube

# Install kubectl
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl
sudo mv kubectl /usr/local/bin/
Enter fullscreen mode Exit fullscreen mode

Step 2: Start Minikube

minikube start --driver=docker
Enter fullscreen mode Exit fullscreen mode

Step 3: Verify Cluster Status

kubectl cluster-info
kubectl get nodes
Enter fullscreen mode Exit fullscreen mode

Option 2: Using kubeadm (For Production)

If you need a multi-node cluster for production:

Step 1: Install Dependencies

sudo apt update && sudo apt install -y apt-transport-https curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"
sudo apt install -y kubelet kubeadm kubectl
Enter fullscreen mode Exit fullscreen mode

Step 2: Initialize the Kubernetes Cluster

sudo kubeadm init
Enter fullscreen mode Exit fullscreen mode

Step 3: Configure kubectl

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

Step 4: Join Worker Nodes

Run the join command (provided in kubeadm init output) on worker nodes.


2. Installing the Kubernetes Admin Dashboard

The Kubernetes Dashboard provides a web-based UI for managing your cluster.

Step 1: Deploy the Dashboard

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml
Enter fullscreen mode Exit fullscreen mode

Step 2: Create an Admin User

Create a file admin-user.yaml:

apiVersion: v1
kind: ServiceAccount
metadata:
  name: admin-user
  namespace: kubernetes-dashboard
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: admin-user
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: admin-user
  namespace: kubernetes-dashboard
Enter fullscreen mode Exit fullscreen mode

Apply it:

kubectl apply -f admin-user.yaml
Enter fullscreen mode Exit fullscreen mode

Step 3: Get the Admin Token

kubectl -n kubernetes-dashboard create token admin-user
Enter fullscreen mode Exit fullscreen mode

Step 4: Access the Dashboard

kubectl proxy
Enter fullscreen mode Exit fullscreen mode

Then open:

http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/
Enter fullscreen mode Exit fullscreen mode

3. Deploying a Service in Kubernetes

Create a simple Nginx deployment.

Step 1: Create a Deployment YAML

Save the following to nginx-deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

Apply it:

kubectl apply -f nginx-deployment.yaml
Enter fullscreen mode Exit fullscreen mode

Step 2: Expose as a Service

kubectl expose deployment nginx-deployment --type=NodePort --port=80
Enter fullscreen mode Exit fullscreen mode

4. Health Monitoring in Kubernetes

Kubernetes provides liveness and readiness probes to monitor application health.

Modify your deployment:

livenessProbe:
  httpGet:
    path: /
    port: 80
  initialDelaySeconds: 5
  periodSeconds: 10

readinessProbe:
  httpGet:
    path: /
    port: 80
  initialDelaySeconds: 5
  periodSeconds: 10
Enter fullscreen mode Exit fullscreen mode

5. Load Balancing with Ingress

Deploy an NGINX Ingress Controller for load balancing:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yaml
Enter fullscreen mode Exit fullscreen mode

6. Deploying Istio Service Mesh

curl -L https://istio.io/downloadIstio | sh -
cd istio-1.*
export PATH=$PWD/bin:$PATH
istioctl install --set profile=demo -y
Enter fullscreen mode Exit fullscreen mode

Enable Istio on your namespace:

kubectl label namespace default istio-injection=enabled
Enter fullscreen mode Exit fullscreen mode

7. Deploying Kafka in Kubernetes

kubectl apply -f https://raw.githubusercontent.com/bitnami/charts/main/bitnami/kafka/templates/kafka.yaml
Enter fullscreen mode Exit fullscreen mode

8. Deploying Redis in Kubernetes

kubectl apply -f https://raw.githubusercontent.com/bitnami/charts/main/bitnami/redis/templates/redis.yaml
Enter fullscreen mode Exit fullscreen mode

Conclusion

You've now successfully set up:
✅ A Kubernetes cluster

✅ The Admin Dashboard

✅ Service deployment

✅ Health monitoring

✅ Load balancing

✅ A service mesh with Istio

✅ Kafka and Redis

Now you have a fully functional Kubernetes environment for running microservices at scale! 🚀🔥

Would you like additional details or troubleshooting tips? Let me know in the comments! 😊

Top comments (0)