<<

Install a MicroK8s single node cluster locally on Windows 10

By Philippe Beraud, France

This walkthrough guides you through all the steps required to create and configure a MicroK8s cluster on Windows 10. Kubernetes1 runs on . As such, MicroK8s2 provides a single command installation of the latest Kubernetes release on a Linux machine for development and testing. MicroK8s is easy to install and use on or any other Linux distro which supports snaps, i.e. pre-packaged applications (similar to Docker containers). Although Windows 10 now has some very useful features, such as the ability to install Ubuntu as an app, the integration of Windows Subsystem for Linux 2 (WSL2) doesn’t provide (yet) all the Ubuntu functionalities required to make MicroK8s run smoothly right out-of-the-box (OOB) and thus use the snap daemon. If you wish to experiment with running MicroK8s semi-natively, see: • Discourse post Using snapd in WSL2 • Post Running Snaps on WSL2 (Insiders only for now) along with the short YouTube video Use this ONE trick to run Linux Snap packages in Windows Subsystem for Linux 2 (WSL2) For now, and for the sake of simplicity for anyone looking to try out Kubernetes on Windows 10, let’s run MicroK8s on Windows 10 with virtualization. MicroK8s will install without problems on Ubuntu on a virtual machine (VM). The way to get a Linux VM on Windows 10 and to run MicroK8s in it is with multipass. Multipass gives you an easy to use interface to manage VMs on Windows 10 – Same is true on MacOS and Linux -. On Windows platforms, multipass defaults to using Microsoft Hyper-V as it’s virtualization provider. So, Microsoft Hyper-V is a prerequisite. (You can also take advantage of a local installation of VirtualBox if you want to.)

Note For more information, see Instant Ubuntu VMs.

Enabling Hyper-V

The Windows 10 Hyper-V System requires: • Windows 10 Enterprise, Pro, or Education • 64-bit Processor with Second Level Address Translation (SLAT). • CPU support for VM Monitor Mode Extension (VT- on Intel CPUs). • Minimum of 4 GB memory.

Note For more information and troubleshooting, see Windows 10 Hyper-V System Requirements.

Perform the following steps: 1. Open a PowerShell console as an Administrator.

1 https://kubernetes.io/ 2 https://github.com/ubuntu/microk8s 2. Check if virtualization is supported:

PS C:> systeminfo

If you see the following output, virtualization is supported. You can proceed with the next steps.

Hyper-V Requirements: VM Monitor Mode Extensions: Yes Virtualization Enabled In Firmware: Yes Second Level Address Translation: Yes Data Execution Prevention Available: Yes

If you see the following output, your system already has a Hypervisor installed and you can skip the next steps below.

Hyper-V Requirements: A hypervisor has been detected. Features required for Hyper-V will not be displayed.

Otherwise, virtualization is unfortunately not supported on your machine, and you can not continue with this walkthrough. 3. Run the following command:

PS C:> Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All

Note If the command couldn't be found, make sure you're running PowerShell as Administrator.

4. Once the installation has completed, reboot your Windows 10 machine.

Creating the Ubuntu 18.04 LTS VM

Perform the following steps: 1. Download and install multipass. The latest Windows 10 installer is available for download here on the multipass GitHub releases page. 2. Once multipass is installed, you can now create a VM to run MicroK8s. At least 4 Gb of RAM and 40 Gb of storage is recommended – you can pass these requirements when you launch the VM – a. Open a PowerShell console. b. Creates a new Ubuntu VM instance using the default, at this point in time, image:

PS C:> multipass launch --name microk8s-vm --mem 4G --disk 40G

It’s most likely going to be the latest cloud image of the newest Ubuntu LTS (Long Term Support) release, at the time of this writing a 18.04 LTS release.

Note You can also choose another image, you can use the find command to see what’s available.

Note For a complete listing of the available commands and their options in multipass, see the Command-line reference.

When the command completes, a new VM named microk8s-vm is running. You can see it from the Hyper-V Manager.

One should note that multipass uses the “Default Switch” network interface on Hyper-V, that, in turn, uses the “Internet Sharing” functionality, providing DHCP (IP addresses) and DNS (domain name resolution) to the instances. 3. Find the IP address which has been allocated to the VM:

PS C:> multipass list Name State IPv4 Image microk8s-vm Running 172.22.82.139 Ubuntu 18.04 LTS

Take a note of this IP as services will become available there when accessed from the Windows 10 host machine, see below. For example ,172.22.82.139 in our configuration. 4. To work within the VM environment more easily, get a shell inside the VM:

C:\> multipass shell microk8s-vm

This opens up a new Shell window:

From within the VM shell, you can now follow along the rest of the quick start instructions for installing MicroK8s on multipass.

Installing the MicroK8s cluster

Perform the following steps: 1. Install the latest version of the MicroK8s snap:

$ sudo snap install microk8s --classic

Note Microk8s is a snap deploying Kubernetes. The MicroK8s snap closely follows upstream Kubernetes, so understanding a bit about the Kubernetes release cycle is helpful for more insight into MicroK8s releases. Upstream Kubernetes ships a new release series (e.g. 1.16.x) approximately every three months. For more information, see Snap channel overview.

That’s all it takes for the installation, which is quick and painless! ;-) 2. MicroK8s has a built-in command to display its status (see hereafter). During installation you can use the --wait-ready flag to wait for the Kubernetes services to initialize. Check the status:

$ sudo microk8s.status --wait-ready

The microk8s-vm VM now runs a MicroK8s cluster on to of Ubuntu 18.04 LTS. MicroK8s comes with a set of tools: microk8s.start, microk8s.stop, microk8s.inspect, microk8s.status, microk8s.enable, microk8s.disable, microk8s.kubectl, microk8s.istioctl, and microk8s.config: • microk8s.start and microk8s.stop do what you would expect: start or stop your Kubernetes cluster. • microk8s.inspect gives you see the actual Kubernetes cluster status. • microk8s.status is a little less intuitive, as it shows the status of the add-ons and not the Kubernetes cluster status. You will use it alongside microk8s.enable and microk8s.disable to control add-ons. • microk8s.kubectl is a wrapper around kubectl, i.e. the cluster manager tool for Kubernetes. • microk8s.istioctl is used to control Istio, which can be enabled as an add-on via microk8s.enable istio. • microk8s.config, shows the client configuration that can be used to connect to your Kubernetes cluster, should you decide not to use microk8s.kubectl to do that. This command will result in output like the following: apiVersion: v1 clusters: - cluster: server: https://172.17.18.79:16443 name: microk8s-cluster contexts: - context: cluster: microk8s-cluster user: admin name: microk8s current-context: microk8s kind: Config preferences: {} users: - name: admin user: username: admin password: UFpWSGlHZDJuSkxpcCtJZ0JTU1dDK2d0eWk3NTdhZEg0TjVQcWVDc3Ywdz0K

To feed this client config to microk8s.kubectl, you can for example write it to ~/.kube/config, i.e. the default configuration location.

$ sudo microk8s.config > $HOME/.kube/config

3. Configure the network:

$ sudo iptables -P FORWARD ACCEPT

4. MicroK8s creates a group to enable seamless usage of commands which require admin privilege. Add your current user to the group:

$ sudo usermod -a -G microk8s ubuntu

You will also need to re-enter the session for the group update to take place. 5. As mentioned above, MicroK8s bundles its own version of kubectl for accessing Kubernetes. Use it to run commands to monitor and control your Kubernetes. For example, view your node:

$ microk8s.kubectl get nodes NAME STATUS ROLES AGE VERSION microk8s-vm Ready 5m27s v1.17.0

6. Or alternatively see the running services:

$ microk8s.kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.152.183.1 443/TCP 6m17s

7. MicroK8s uses a namespaced kubectl command to prevent conflicts with any existing installs of the Kubernetes command-line tool, kubectl. kubectl allows you to run commands against Kubernetes clusters. You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs. See Overview of kubectl. If you don't have an existing install, it is easier to add an alias (append to ~/.bash_aliases) like this:

$ sudo snap alias microk8s.kubectl kubectl Added: - microk8s.kubectl as kubectl

Otherwise, from the Bash terminal console, to install the latest version of kubectl, run the following command:

$ snap install kubectl –classic $ kubectl version

See Install kubectl on Linux. At this stage, you have a fully functional Kubernetes cluster. Creating a test deployment on the MicroK8s cluster

Kubernetes is meant for deploying apps and services. You now can use the kubectl (alias) command (or the microk8s.kubectl command) to do that as with any Kubernetes. To install a first app, perform the following steps: 1. From the VM Shell, deploy NGINX with 3 replicas (instances) available:

$ kubectl run nginx --image nginx:alpine --replicas 3

2. It may take a minute or two to install, but you can check the status. Once NGINX has been deployed, expose the application:

$ kubectl expose deployment nginx --port 80 --target-port 80 --type ClusterIP -- selector=run=nginx --name nginx

3. See the new Service and ClusterIP address assigned:

$ kubectl get svc

You now have a deployed an application to a fully functional Kubernetes cluster using Microk8s Further configuring the MicroK8s cluster MicroK8s uses the minimum of components for a pure, lightweight Kubernetes. However, plenty of extra features are available thanks to "add-ons", i.e. pre-packaged components that provide extra capabilities. To turn on standards services, perform the following steps: 1. From the VM Shell, deploy a series addons:

$ sudo microk8s.enable dns dashboard registry

This will deploy: • DNS (dns). This addon may be required by others, thus we recommend you always enable it. • Kubernetes dashboard (dashboard) as well as Grafana and influxDB. • A docker private registry (registry) and expose it on localhost:32000. The storage addon (storage) will be enabled as part of this addon.

Note Similarly, microk8s.disable turns off a service. Try microk8s.enable --help for a list of available services built in. For more information, see MicroK8s Addons.

2. Check the deployment progress of your addons:

$ kubectl get all --all-namespaces

3. Let’s now access the Kubernetes dashboard. As you can see above, the kubernetes-dashboard service in the kube-system namespace has an internal ClusterIP of 10.152.183.231 and listens on TCP port 443. a. Set up a proxy to pass and allow external requests to the dashboard:

$ sudo kubectl proxy --accept-hosts=.* --address=0.0.0.0 &

Note that the & at the end of this command string will run the command in the background. b. You now should be able to access the Kubernetes dashboard by entering the following URL: http://:8001/api/v1/namespaces/kube-system/services/https:kubernetes- dashboard:/proxy/ Since you are running a MicroK8s cluster in a VM, should be the IP address of the microk8s-vm VM so that the Kubernetes dashboard can be accessed from your local Windows 10 machine. For example, 172.22.82.139 in our configuration. http://172.22.82.139:8001/api/v1/namespaces/kube-system/services/https:kubernetes- dashboard:/proxy/ From your Windows 10 host machine, open a browser session and navigate to the above URL and you will see the Kubernetes dashboard UI.

c. At this point, the Kubernetes dashboard requires a token for gaining access. Create a list of all the service names, with their secret name attached:

$ sudo kubectl -n kube-system get secret

d. Retrieve the secret token for the Kubernetes dashboard:

Important note Make sure to modify the 8bt92 entry to match the secret key associated with the Kubernetes dashboard instance for your MicroK8s installation.

$ sudo kubectl -n kube-system describe secret kubernetes-dashboard-token-8bt92 Name: kubernetes-dashboard-token-8bt92 Namespace: kube-system Labels: Annotations: kubernetes.io/service-account.name: kubernetes-dashboard kubernetes.io/service-account.uid: fcafb527-b246-4a39-8a9e-8e8d7c6c5225

Type: kubernetes.io/service-account-token

Data ==== ca.crt: 1103 bytes namespace: 11 bytes token: eyJhbGciOiJSUzI1NiIsImtpZCI6Im9YZlF6c0pzNjlESmRUYXkzSlp3cjVYZkpwZDlidmNIUGdITG5CWUt5REUifQ. eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1 lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJrdW Jlcm5ldGVzLWRhc2hib2FyZC10b2tlbi04YnQ5MiIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZ S1hY2NvdW50Lm5hbWUiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQv c2VydmljZS1hY2NvdW50LnVpZCI6ImZjYWZiNTI3LWIyNDYtNGEzOS04YTllLThlOGQ3YzZjNTIyNSIsInN1YiI6InN 5c3RlbTpzZXJ2aWNlYWNjb3VudDprdWJlLXN5c3RlbTprdWJlcm5ldGVzLWRhc2hib2FyZCJ9.iYtoRdb4Qq9c4JL1n SlFec631agfa-iV69shGoYhOWsBJCSuYCEal5Z5QxBC1lmuMp8fA- vlXqm4vGboPIRHcrBZMHjzuNzWi0AsOdWQNWMqjoOsPA4NEYYsHN8embQBo5n3GNBYEMXNcTmZ5YY7qblHfzzZnje2S S33ETQI_NuO4-cGVWk1mydUtnGhQkOLsYiDmkJKYt5CUIsgbTZvkynUmFHUZmjMSIAz1K2gdU1MqhGI2- n9BpmdWQWHlpWH27_yTmvCivYznX6KPIsjG6jHHv4PFyw3PEFIXhkuOPL2MTwuKCx8Bcw7cEYSPd0uay8zVpvcipdeC JORKUxTfw

The above command displays a long string of characters after token :. Copy that string and then go back to your browser session. e. In the above Kubernetes Dashboard dialog, select Token and then paste the copied token into the Enter token* text field. Click Sign in and the Kubernetes dashboard should open up.

Alternatively, you can allow accessing the Kubernetes dashboard without requiring such a token. To do so: a. Edit the deployment file for the dashboard service:

$ sudo kubectl -n kube-system edit deploy kubernetes-dashboard -o yaml

b. Add the –enable-skip-login flag to the deployment’s specs. Scroll down to the line after - --namespace=kube-system, press i, type “- --enable-skip-login”, press ESC, and then type “:w”. spec: progressDeadlineSeconds: 600 replicas: 1 revisionHistoryLimit: 10 selector: matchLabels: k8s-app: kubernetes-dashboard strategy: rollingUpdate: maxSurge: 25% maxUnavailable: 25% type: RollingUpdate template: metadata: creationTimestamp: null labels: k8s-app: kubernetes-dashboard spec: containers: - args: - --auto-generate-certificates - --namespace=kube-system - --enable-skip-login

c. Type “:x” to exit the vim editor. d. Navigate to the previous URL: http://172.22.82.139:8001/api/v1/namespaces/kube-system/services/https:kubernetes- dashboard:/proxy/

e. Click Skip. Et voilà! 4. Let’s now access the Grafana dashboard instead. The API server proxies your services. Get to them:

$ kubectl cluster-info Kubernetes master is running at https://127.0.0.1:16443 Heapster is running at https://127.0.0.1:16443/api/v1/namespaces/kube- system/services/heapster/proxy CoreDNS is running at https://127.0.0.1:16443/api/v1/namespaces/kube-system/services/kube- dns:dns/proxy Grafana is running at https://127.0.0.1:16443/api/v1/namespaces/kube- system/services/monitoring-grafana/proxy InfluxDB is running at https://127.0.0.1:16443/api/v1/namespaces/kube- system/services/monitoring-influxdb:http/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

Grafana is running at https://127.0.0.1:16443/api/v1/namespaces/kube- system/services/monitoring-grafana/proxy/. 5. Open a browser on your Windows 10 host machine and navigate to the following URL: http://:8001/api/v1/namespaces/kube-system/services/https:kubernetes- dashboard:/proxy/ As before, since you are running a MicroK8s cluster in a VM, should be the IP address of the microk8s-vm VM so that the Kubernetes dashboard can be accessed from your local Windows 10 machine. For example, 172.22.82.139 in our configuration. http://172.22.82.139:8001/api/v1/namespaces/kube-system/services/monitoring-grafana/proxy The Grafana dashboard should open up.

So, let’s remove the service and pods you have previously deployed:

$ kubectl delete service nginx service "nginx" deleted $ kubectl delete deployment nginx deployment.apps "nginx" deleted

At this stage, your MicroK8s cluster will continue running until you decide to stop it. You can stop MicroK8s with this simple command:

$ microk8s.stop

You can start again any time by running:

$ microk8s.start

This concludes this walkthrough. You can now start using your MicroK8s single node cluster the way you expect on your Windows 10 machine. This is great for technical ram-up on Kubernetes, local development, and CI/CD.

Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0)