Hosting service on Kubernetes using microk8s
Prerequisites
The basic prerequisite for this lab is to setup a virtual machine (master node) and create two worker nodes with same configuration as master node
Troubleshoot if kubectl get nodes
not working
kubectl get nodes
not workingStep 1: Login to your virtual machines
Step 2: To verify the kubectl
and microk8s
running use the following command
First, check the status of your MicroK8s cluster to ensure it's running:
microk8s status
-
If it's not running, you can start it with:
microk8s start
-
If it is running but some services are disabled, you can enable essential services like DNS, the API server, and the dashboard:
microk8s enable dns dashboard
Use MicroK8s kubectl
Command
To retrieve the node details
microk8s kubectl get nodes
Step 3: Check permissions
Ensure your user is part of the microk8s group so that you don’t need sudo for every command:
-
Add your user to the microk8s group:
sudo usermod -a -G microk8s $USER
-
Then apply the new group membership:
newgrp microk8s
Step 4: Configure Kubeconfig for kubectl
If you prefer to use the system-wide kubectl
rather than microk8s kubectl
, you need to configure kubectl
to point to the MicroK8s configuration. MicroK8s stores its kubeconfig in a specific location:
Export the MicroK8s kubeconfig for use with kubectl
:
sudo microk8s kubectl config view --raw > ~/.kube/config
This will set kubectl to use MicroK8s' configuration.
Step 5: Check Cluster Info
Once the cluster is running and configured correctly, verify that it’s up by checking the cluster information:
microk8s kubectl cluster-info
If the cluster info is correct, you should see information about the API server and other services.
Step 6: Restart MicroK8s (if needed)
If you're still facing issues, you can try restarting MicroK8s:
microk8s stop
microk8s start
Configure the master node
Step 1: Add worker nodes with master node
Run the add command in master node
microk8s add-node
Output:
student@int363:~$ microk8s add-node
From the node you wish to join to this cluster, run the following:
microk8s join 172.31.83.178:25000/a4c5194f8027dbe1df17b63b5753c0e3/1113bd240a17
Use the '--worker' flag to join a node as a worker not running the control plane, eg:
microk8s join 172.31.83.178:25000/a4c5194f8027dbe1df17b63b5753c0e3/1113bd240a17 --worker
If the node you are adding is not reachable through the default interface you can use one of the following:
microk8s join 172.31.83.178:25000/a4c5194f8027dbe1df17b63b5753c0e3/1113bd240a17
microk8s join 172.17.0.1:25000/a4c5194f8027dbe1df17b63b5753c0e3/1113bd240a17
Copy the join command and paste in the wokrre node with --worker
flag
microk8s join PRIVATE_IP:25000/TOKEN --worker
Output:
student@int363:~$ microk8s join 172.31.83.178:25000/a4c5194f8027dbe1df17b63b5753c0e3/1113bd240a17 --worker
WARNING: Hostpath storage is enabled and is not suitable for multi node clusters.
Contacting cluster at 172.31.83.178
The node has joined the cluster and will appear in the nodes list in a few seconds.
This worker node gets automatically configured with the API server endpoints.
If the API servers are behind a loadbalancer please set the '--refresh-interval' to '0s' in:
/var/snap/microk8s/current/args/apiserver-proxy
and replace the API server endpoints with the one provided by the loadbalancer in:
/var/snap/microk8s/current/args/traefik/provider.yaml
Successfully joined the cluster.
Note: Repeat the step 1 to add a new node. Should not use the same token to connect multiple nodes
Step 2: Create a deployment in the master node
kubectl create deployment microbot --image=dontrebootme/microbot:v1
Here microbot
is the deployment name
Step 3: Create replicaset of the deployment for scalability
kubectl scale deployment workshop --replicas=2
Step 4: Expost the port 80 so the application can be accessible
kubectl expose deployment workshop --type=NodePort --port=80 --name=microbot-service
Step 5: Now verify the namespace and look for service/microbot-service
and note the port (generated and assigned randomly)
default service/microbot-service NodePort 10.152.183.251 <none> 80:32475/TCP 21m
Open the browser and open the port -
Command | Description |
---|---|
Local machine | http://localhost:PORT |
Remote machine | http://PUBLIC_ADDRESS:PORT |
Optional
To remove a node from the cluster
-
Run
microk8s leave
command on worker node you want to dettach from cluster. -
RUn following command on master node
microk8s remove-node
All Verification
Command | Description |
---|---|
kubectl get all --all-namespaces | List all namespaces in the cluster |
kubectl get namespace | List namespaces in the cluster |
kubectl get nodes | List all nodes in the cluster |
kubectl cluster-info | Display cluster information |
kubectl get services | List all services in the current namespace |
kubectl get pods | List all pods in the current namespace |
kubectl get deployments | List all deployments in the current namespace |
kubectl get svc | List all services in the current namespace |
- You can append
microk8s
in the command as a prefix before excuting.- For more commands refer to Kubernetes Command Cheatsheet