{"id":1657,"date":"2020-05-27T15:56:36","date_gmt":"2020-05-27T20:56:36","guid":{"rendered":"https:\/\/blog.iqonda.net\/?p=1657"},"modified":"2020-11-09T13:20:23","modified_gmt":"2020-11-09T19:20:23","slug":"kubernetes-development-with-microk8s","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/kubernetes-development-with-microk8s\/","title":{"rendered":"Kubernetes Development with MicroK8s"},"content":{"rendered":"

Using Ubuntu's MicroK8s Kubernetes environment to test a Nginx container with a NodePort and also Ingress so we can access from another machine.<\/p>\n

install<\/h2>\n
$ sudo snap install microk8s --classic\nmicrok8s v1.18.2 from Canonical\u2713 installed\n\n$ sudo usermod -a -G microk8s rrosso\n\n$ microk8s.kubectl get all --all-namespaces\nNAMESPACE   NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE\ndefault     service\/kubernetes   ClusterIP   10.152.183.1           443\/TCP   2m29s\n\n$\u00a0microk8s.kubectl get nodes\nNAME      STATUS   ROLES    AGE   VERSION\nserver1   Ready       3m    v1.18.2-41+b5cdb79a4060a3\n\n$ microk8s.enable dns dashboard\n...\n\n$ watch microk8s.kubectl get all --all-namespaces<\/code><\/pre>\n

NOTE: alias the command<\/p>\n

$\u00a0sudo snap alias microk8s.kubectl kubectl\nAdded:\n  - microk8s.kubectl as kubectl<\/code><\/pre>\n

nginx first attempt<\/h2>\n
$\u00a0kubectl create deployment nginx --image=nginx\ndeployment.apps\/nginx created\n\n$\u00a0kubectl get deployments\nNAME    READY   UP-TO-DATE   AVAILABLE   AGE\nnginx   1\/1     1            1           9s\n\n$\u00a0kubectl get pods\nNAME                    READY   STATUS    RESTARTS   AGE\n\nnginx-f89759699-jnlng   1\/1     Running   0          15s\n\n$\u00a0kubectl get all --all-namespaces\n\nNAMESPACE     NAME                                                  READY   STATUS    RESTARTS   AGE\ndefault       pod\/nginx-f89759699-jnlng                             1\/1     Running   0          31s\n...\nNAMESPACE     NAME                                TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                  AGE\ndefault       service\/kubernetes                  ClusterIP   10.152.183.1             443\/TCP                  94m\n...\nNAMESPACE     NAME                                             READY   UP-TO-DATE   AVAILABLE   AGE\ndefault       deployment.apps\/nginx                            1\/1     1            1           31s\nkube-system   deployment.apps\/coredns                          1\/1     1            1           90m\n...\n\nNAMESPACE     NAME                                                        DESIRED   CURRENT   READY   AGE\ndefault       replicaset.apps\/nginx-f89759699                             1         1         1       31s\nkube-system   replicaset.apps\/coredns-588fd544bf                          1         1         1       90m\n...\n\n$\u00a0kubectl get all --all-namespaces\nNAMESPACE     NAME                                                  READY   STATUS    RESTARTS   AGE\ndefault       pod\/nginx-f89759699-jnlng                             1\/1     Running   0          2m38s\n...\nNAMESPACE     NAME                                TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                  AGE\n...\nNAMESPACE     NAME                                             READY   UP-TO-DATE   AVAILABLE   AGE\ndefault       deployment.apps\/nginx                            1\/1     1            1           2m38s\n\nNAMESPACE     NAME                                                        DESIRED   CURRENT   READY   AGE\ndefault       replicaset.apps\/nginx-f89759699                             1         1         1       2m38s\n...\n\n$\u00a0wget 10.152.183.151\n--2020-05-25 14:26:14--  http:\/\/10.152.183.151\/\nConnecting to 10.152.183.151:80... connected.\nHTTP request sent, awaiting response... 404 Not Found\n2020-05-25 14:26:14 ERROR 404: Not Found.\n\n$\u00a0kubectl get deployments\nNAME    READY   UP-TO-DATE   AVAILABLE   AGE\nnginx   1\/1     1            1           3m40s\n\n$\u00a0kubectl get pods\nNAME                    READY   STATUS    RESTARTS   AGE\nnginx-f89759699-jnlng   1\/1     Running   0          3m46s\n\n$\u00a0microk8s.kubectl expose deployment nginx --port 80 --target-port 80 --type ClusterIP --selector=run=nginx --name nginx\nservice\/nginx exposed\n\n$ microk8s.kubectl get all\nNAME                        READY   STATUS    RESTARTS   AGE\npod\/nginx-f89759699-jnlng   1\/1     Running   0          9m29s\n\nNAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE\nservice\/kubernetes   ClusterIP   10.152.183.1            443\/TCP   103m\nservice\/nginx        ClusterIP   10.152.183.55           80\/TCP    3m55s\n\nNAME                    READY   UP-TO-DATE   AVAILABLE   AGE\ndeployment.apps\/nginx   1\/1     1            1           9m29s\n\nNAME                              DESIRED   CURRENT   READY   AGE\nreplicaset.apps\/nginx-f89759699   1         1         1       9m29s\n\n$ wget 10.152.183.55\n--2020-05-25 14:33:02--  http:\/\/10.152.183.55\/\nConnecting to 10.152.183.55:80... failed: Connection refused.<\/code><\/pre>\n

NOTE: Kubernetes does not provide a loadbalancer. It is assumed that loadbalancers are an external component. MicroK8s is not shipping any loadbalancer but even if it did there would not have been any nodes to balance load over. There is only one node so if you want to expose a service you should use the NodePort service type.
\nThere is no external LB shipping with MicroK8s, therefore there is no way to appoint an (LB provided) external IP to a service. What you can do is to expose a service to a host's port using NodePort.<\/p>\n

nginx attempt 2<\/h2>\n
$\u00a0kubectl delete services nginx-service\nservice \"nginx-service\" deleted\n\n$\u00a0kubectl delete deployment nginx\ndeployment.apps \"nginx\" deleted\n\n$\u00a0kubectl create deployment nginx --image=nginx\ndeployment.apps\/nginx created\n\n$\u00a0kubectl expose deployment nginx --type NodePort --port=80 --name nginx-service\nservice\/nginx-service exposed\n\n$\u00a0kubectl get all\nNAME                        READY   STATUS    RESTARTS   AGE\npod\/nginx-f89759699-jr4gz   1\/1     Running   0          23s\n\nNAME                    TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE\nservice\/kubernetes      ClusterIP   10.152.183.1             443\/TCP        19h\nservice\/nginx-service   NodePort    10.152.183.229           80:30856\/TCP   10s\n\nNAME                    READY   UP-TO-DATE   AVAILABLE   AGE\ndeployment.apps\/nginx   1\/1     1            1           23s\n\nNAME                              DESIRED   CURRENT   READY   AGE\nreplicaset.apps\/nginx-f89759699   1         1         1       23s\n\n$\u00a0wget 10.152.183.229\nConnecting to 10.152.183.229:80... connected.\nHTTP request sent, awaiting response... 200 OK\n2020-05-26 08:05:22 (150 MB\/s) - \u2018index.html\u2019 saved [612\/612]<\/code><\/pre>\n

ingress<\/h2>\n
$\u00a0cat ingress-nginx.yaml \napiVersion: networking.k8s.io\/v1beta1\nkind: Ingress\nmetadata:\n  name: http-ingress\nspec:\n  rules:\n  - http:\n      paths:\n      - path: \/\n        backend:\n          serviceName: nginx-service\n          servicePort: 80\n\n$ kubectl apply -f ingress-nginx.yaml \ningress.networking.k8s.io\/http-ingress created<\/code><\/pre>\n

NOTE: https:\/\/192.168.1.112\/<\/a> pulls up Nginx homepage<\/p>\n

after reboot:<\/h2>\n