Setup Local Kubernetes Cluster with Docker, WSL2 and KinD – Part 2

In first part of this post, we setup WSL2, Docker Desktop, Enabled Integration of WSL2 with Docker, Setup KinD cluster and spin up a single node and multi node cluster. Since 6 nodes HA cluster was too much for our learning needs, lets first delete it using kind delete cluster --name multicluster and provision a fresh single node cluster for us.

Setup Kubernetes Dashboard

Some people like to see using visual mode, to get on started with things. For this, we can use Kubernetes Dashboard. To set it up, we can use below commands:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-rc6/aio/deploy/recommended.yaml

This will spin up a kubernetes service name dashboard, which is by default of the type ClusterIP:

Let’s run a kubectl proxy using below command:

kubectl proxy

We can then open url http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/#/login in our windows browser to see kubernetes dashboard. However, to login, we first need to setup RBAC and create a token. Let’s first setup RBAC using below commands in a new WSL2 session:

# Create a new ServiceAccount
kubectl apply -f - <<EOF
apiVersion: v1
kind: ServiceAccount
metadata:
  name: admin-user
  namespace: kubernetes-dashboard
EOF

# Create a ClusterRoleBinding for the ServiceAccount
kubectl apply -f - <<EOF
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
EOF

Now we can create token using below command for our service account admin-user:

kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')

Let’s copy the token and paste into window for Kubernetes dashboard. We should now be able to login:

Deploy and Run Pods

To create a pod, we first need to create a configuration file containing pod spec. Let’s save below code in a file named nginx-pod.yaml:

apiVersion: v1
kind: Pod
metadata:
    name: nginx
    labels:
      app: kubia
spec:
    containers:
    - image: nginx:latest
      name: nginx
      ports: 
      - containerPort: 80
        hostPort: 80
        protocol: TCP

We can now use kubectl apply -f nginx-pod.yaml to create and deploy a pod. We can get running pods using kubectl get pods command:

Similarly, you can create other Kubernetes resources like services, daemon-sets etc. However note that we would not be able to see traffic getting to our Pod from our local machine even if we have defined hostPost property. If we want to redirect traffic from local machine, we first need to define extra port mappings.

Defining Extra Port Mappings and Run Pods

We can define the extra port mappings using the Kind cluster configuration file. Save below config in a file named kind-config-port-mapping.yaml:

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
# port forward 80 on the host to 80 on this node
  extraPortMappings:
  - containerPort: 80
    hostPort: 80
    listenAddress: "127.0.0.1"
    protocol: TCP

We’ll now spin-up Kubernetes cluster and create the pod. We can now see traffic getting redirected from our local machine to nginx pod:

In a similar way, we can create other Kubernetes resources like services, etc and test our application.

2 thoughts on “Setup Local Kubernetes Cluster with Docker, WSL2 and KinD – Part 2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s