--- title: 'Build-up Microk8s in Local Single PC Step by Step' disqus: JosephKL.C --- # Build-up Microk8s in Local Single PC ![ubuntu](https://img.shields.io/badge/Ubuntu-22.04_LTS-orange) ![Microk8s](https://img.shields.io/badge/Microk8s-1.22.17-blue) ![Docker](https://img.shields.io/badge/Docker-20.10.12-green) # Beginners Guide If you are a total beginner to this, start here! 1. Install OS (Ubuntu 22.04 LTS) 2. Install Docker (sudo apt-get install docker.io) 3. Install microk8s visit here # 1. Use snap to check microk8s version ```shell= ubuntu@microk8s:~$ snap info microk8s ``` ``` name: microk8s summary: Kubernetes for workstations and appliances publisher: Canonical✓ store-url: https://snapcraft.io/microk8s contact: https://github.com/ubuntu/microk8s license: Apache-2.0 description: | MicroK8s is a small, fast, secure, single node Kubernetes that installs on just about any Linux box. Use it for offline development, prototyping, testing, or use it on a VM as a small, cheap, reliable k8s for CI/CD. It's also a great k8s for appliances - develop your IoT apps for k8s and deploy them to MicroK8s on your boxes. snap-id: EaXqgt1lyCaxKaQCU349mlodBkDCXRcg channels: 1.24/stable: v1.24.0 2022-05-13 (3272) 230MB classic 1.24/candidate: v1.24.0 2022-05-13 (3272) 230MB classic ... ``` --- # 2. Use snap to check microk8s version - Because our GPU is Nvidia GeFprce RTX 2080 *2 - Gpu plug in stable support in microk8s 1.21-22 version ```shell= ubuntu@microk8s:~$ sudo snap install microk8s --classic --channel=1.22/stable ``` ``` microk8s (1.22/stable) v1.22.17 from Canonical✓ installed ``` # 3. (Not need)Disable HA-Cluster(For Single PC/IF in multi-Machines can SKIP) ```shell= ubuntu@microk8s-singleton:~/$ sudo microk8s.disable ha-cluster --force ``` ``` Infer repository core for addon ha-cluster Reverting to a non-HA setup Generating new cluster certificates. Waiting for node to start. Enabling flanneld and etcd HA disabled ``` # 4. Start microk8s ```shell= ubuntu@microk8s:~$ sudo microk8s start ``` # 5. Simplify microk8s command ```shell= ubuntu@microk8s:~$ sudo snap alias microk8s.kubectl kubectl ``` # 6. Add user group ```shell= ubuntu@microk8s:~$ sudo usermod -a -G microk8s ubuntu ubuntu@microk8s:~$ sudo chown -f -R ubuntu ~/.kube ubuntu@microk8s:~$ newgrp microk8s ubuntu@microk8s:~$ kubectl version #Check Version ``` ## 6.6. Check Environment ```shell= ubuntu@microk8s:~$ kubectl get nodes ubuntu@microk8s:~$ kubectl get pod -A ``` # 7. Check plug-in status ```shell= ubuntu@microk8s:~$ sudo microk8s status ``` ``` microk8s is running high-availability: no addons: enabled: hostpath-storage # (core) Storage class; allocates storage from host directory storage # (core) Alias to hostpath-storage add-on, deprecated disabled: community # (core) The community addons repository dashboard # (core) The Kubernetes dashboard dns # (core) CoreDNS gpu # (core) Automatic enablement of Nvidia CUDA ...... ``` # 8. Enable microk8s plug-in :::info **Command:** [**Enable Plug-in**](#Enable-Plug-in) [**Disable Plug-in**](Disable-Plug-in) :::: ```shell= sudo microk8s.enable storage ``` ```shell= sudo microk8s.enable dns helm helm3 metrics-server sudo microk8s.enable ingress sudo microk8s.enable registry sudo microk8s.enable metallb ``` # 9. Enable community ```shell= microk8s enable community ``` ## 9.1. Enable istio ```shell= sudo microk8s.enable istio ``` # 10. Install Kubernetes Dashboard ```shell= sudo microk8s.enable dashboard ``` ```shell= microk8s dashboard-proxy ``` ```shell= ifconfig / #Check your host IP ip addr ``` :::info **https://{your host ip}:10443/** ### You Got: ![](https://i.imgur.com/7Y5X84x.png) You need to key "thisisunsafe" in borwsor by your keyboard. ### You will get: ![](https://i.imgur.com/LTKIGcx.png) ### Key your token .Then, you will get Dashboard IU ![](https://i.imgur.com/ICYHFsX.png) ::: # 11. Give Cluster Permission ```shell= kubectl get deploy -A ``` ``` NAMESPACE NAME READY UP-TO-DATE AVAILABLE AGE kube-system coredns 1/1 1 1 17m kube-system dashboard-metrics-scraper 1/1 1 1 16m ...... ``` ### Check deploy yaml --> [Check deployment yaml](#Check-deployment-yaml) ``` apiVersion: apps/v1 kind: Deployment metadata: annotations: deployment.kubernetes.io/revision: "1" kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"k8s-app":"kubernetes-dashboard"},"name":"kubernetes-dashboard","namespace":"kube-system"},"spec":{"replicas":1,"revisionHistoryLimit":10,"selector":{"matchLabels":{"k8s-app":"kubernetes-dashboard"}},"template":{"metadata":{"labels":{"k8s-app":"kubernetes-dashboard"}},"spec":{"containers":[{"args":["--auto-generate-certificates","--namespace=kube-system"],"image":"kubernetesui/dashboard:v2.3.0","imagePullPolicy":"IfNotPresent","livenessProbe":{"httpGet":{"path":"/","port":8443,"scheme":"HTTPS"},"initialDelaySeconds":30,"timeoutSeconds":30},"name":"kubernetes-dashboard","ports":[{"containerPort":8443,"protocol":"TCP"}],"securityContext":{"allowPrivilegeEscalation":false,"readOnlyRootFilesystem":true,"runAsGroup":2001,"runAsUser":1001},"volumeMounts":[{"mountPath":"/certs","name":"kubernetes-dashboard-certs"},{"mountPath":"/tmp","name":"tmp-volume"}]}],"nodeSelector":{"kubernetes.io/os":"linux"},"serviceAccountName":"kubernetes-dashboard","tolerations":[{"effect":"NoSchedule","key":"node-role.kubernetes.io/master"}],"volumes":[{"name":"kubernetes-dashboard-certs","secret":{"secretName":"kubernetes-dashboard-certs"}},{"emptyDir":{},"name":"tmp-volume"}]}}}} creationTimestamp: "2022-05-29T05:51:49Z" generation: 1 labels: ``` ### **Find the namespace & Service AccountName** ``` namespace: kube-system serviceAccountName: kubernetes-dashboard ``` ### Check Cluster Name [Check Clusters](#Check-Clusters) ### Give cluster permission ```shell= ubuntu@microk8s:~$ kubectl create clusterrolebinding kubernetes-dashboard-clusterbingding_kube-system_kubernetes-dashboard --clusterrole=cluster-admin --serviceaccount=kube-system:kubernetes-dashboard clusterrolebinding.rbac.authorization.k8s.io/kubernetes-dashboard-clusterbingding_kube-system_kubernetes-dashboard created ``` :::warning ### **If need to delete cluster role binding:** [ Delete Cluster Role Binding](#Delete-Cluster-Role-Binding) ::: :::success ## IF Successful, you cna change your namespace in dashboard! ![](https://i.imgur.com/oJBk3QY.png) ![](https://i.imgur.com/iM0qFlt.png) ::: ## Get Token can use: [Get Token](#Get-Token) # 12. Run Container Sample: :::success ### Example: ```shell= ubuntu@microk8s:~$ kubectl run nginx --image=nginx ``` ``` pod/nginx created ``` ```shell= ubuntu@microk8s:~$ kubectl get pod ``` ``` NAME READY STATUS RESTARTS AGE nginx 0/1 ContainerCreating 0 4s ``` ```shell= ubuntu@microk8s:~$ kubectl get pod -w ``` ``` NAME READY STATUS RESTARTS AGE nginx 0/1 ContainerCreating 0 8s nginx 1/1 Running 0 25s ``` ::: ## *Initial Container Configuration (If Nedded) :::info ```shell= sudo vim /var/snap/microk8s/current/args/containerd-template.toml ``` ``` 24 # The 'plugins."io.containerd.grpc.v1.cri"' table contains all of the server options. 25 [plugins."io.containerd.grpc.v1.cri"] 26 27 stream_server_address = "127.0.0.1" 28 stream_server_port = "0" 29 enable_selinux = false ``` Add ``` 30 sandbox_image = "k8s.gcr.io/pause:3.1" #Your Image hub source ``` Go to [plugins."io.containerd.grpc.v1.cri".registry] ``` 75 # 'plugins."io.containerd.grpc.v1.cri".registry' contains config related to the registry 76 [plugins."io.containerd.grpc.v1.cri".registry] 77 config_path = "${SNAP_DATA}/args/certs.d" ``` Add ``` 78 [plugins."io.containerd.grpc.v1.cri".registry.mirrors] 79 [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"] 80 endpoint = [ 81 "https://xxxxxxxx", ] ``` ```bash= sudo vim /var/snap/microk8s/current/args/kubelet ``` Add ``` --pod-infra-container-image=<Your Image hub source> ``` ::: # 13 Docker Image Registry :::danger **Make sure you have enable registry plug-in!** [**Enable microk8s plug-in**](#Enable-microk8s-plug-in) ::: ## 13.1 We need to edit/Add the file ```shell= sudo vim /var/snap/microk8s/current/args/containerd-template.toml ``` :::success [plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:32000"] endpoint = ["http://localhost:32000"] ::: [**Stop microk8s Service**](#Stop-microk8s-Service) [**Start microk8s Service**](#Start-microk8s-Service) ## 13.2 Take your Dockerfile in the floder & cd to tere ```shell= sudo docker build -t localhost:32000/paulapp:latest -f Dockerfile . sudo docker images #Check your docker image ``` ## 13.3 Push image to local hub ```shell= sudo docker push localhost:32000/paulapp:latest ``` ## 13.4 Kubenete create deployment by local image ```shell= kubectl create deployment paulAppService --image= localhost:32000/paulapp:latest ``` :::info Also, you can use specified image in yaml file. ::: ``` spec: containers: - name: abc image: localhost:32000/paulapp:latest ``` --- # FAQ :::info #### **Get all namespaces Deployment/Node/Pod** ```shell= ubuntu@microk8s:~$ kubectl get pod -A ubuntu@microk8s:~$ kubectl get node -A ubuntu@microk8s:~$ kubectl get deploy -A ``` #### **Check Clusters** ```shell= ubuntu@microk8s:~$ kubectl get clusterrole ``` #### **Check Cluster Permission** ```shell= ubuntu@microk8s:~$ kubectl describe clusterrole cluster-admin ``` #### **Start microk8s Service** ```shell= ubuntu@microk8s:~$ microk8s.start ``` #### **Stop microk8s Service** ```shell= ubuntu@microk8s:~$ microk8s.stop ``` #### **Reset microk8s Service** ```shell= ubuntu@microk8s:~$ microk8s.reset ``` #### **Uninstall microk8s Service** ```shell= ubuntu@microk8s:~$ sudo snap remove microk8s ``` #### **Status of microk8s** ```shell= ubuntu@microk8s:~$ microk8s.status ``` #### **Check Container Runtime Interface(CRI) Version** ```shell= ubuntu@microk8s:~$ microk8s.ctr -v ``` #### **Check deployment yaml** ```shell= ubuntu@microk8s:~$ kubectl get deploy kubernetes-dashboard -n kube-system -o yaml ``` #### **Check pod log** ```shell= kubectl -n kube-system logs kubernetes-dashboard-765646474b-xxxx ``` #### **Delete Cluster Role Binding** ```shell= kubectl delete clusterrolebinding.rbac.authorization.k8s.io/<name> ``` #### **Get Token** ```shell= token=$(kubectl -n kube-system get secret | grep default-token | cut -d " " -f1) ubuntu@microk8s:~$ kubectl -n kube-system describe secret $token ``` #### **Enable Plug-in** ```shell= ubuntu@microk8s:~$ microk8s.enable dns dashboard ``` #### **Disable Plug-in** ```shell= ubuntu@microk8s:~$ microk8s.disable dns dashboard ``` :::