# Install MicroK8s

As a prerequisite to these Kubernetes exercises, you will install MicroK8s on your laptop or desktop. MicroK8s is a lightweight Kubernetes distribution that includes a cluster.

---

## What Will You Do

For the prerequisites, you will:

- Install MicroK8s.
- Deploy your first application.

**Estimated Time**

Estimated time for this exercise is 20 minutes.

---

## Install MicroK8s

[macOS](https://docs.rafay.co/learn/quickstart/kubernetes/prerequisites/#__tabbed_1_1)[Windows](https://docs.rafay.co/learn/quickstart/kubernetes/prerequisites/#__tabbed_1_2)[Ubuntu](https://docs.rafay.co/learn/quickstart/kubernetes/prerequisites/#__tabbed_1_3)

1. Open the Terminal.
2. Use the following command to install Homebrew. Installing MicroK8s requires the `brew` command.

```
   /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
   ```

- You may be prompted to enter your password to install.
   - When prompted, press **Return** to install the XCode Command Line Tools.
   - This may take several minutes.
3. Download and install MicroK8s and its dependencies.

```
   brew install ubuntu/microk8s/microk8s
   ```

4. Run the following to start with MicroK8s.

```
   microk8s install
   ```

- When prompted, type **Y** and press **Return** to include Multipass.
   - You may be prompted to enter your password to install Multipass.
5. Get a list of the namespaces in your cluster.

```
   microk8s kubectl get namespaces
   ```

1. Download [MicroK8s](https://microk8s.io/docs/install-alternatives#heading--windows)
2. Run the MicroK8s installer by double-clicking the microk8s installer EXE file in your Downloads folder.
3. Use the installation wizard and accept the default settings.
   - Multipass is included with the Windows installation.
   - For the Hypervisor, make sure Microsoft Hyper-V is selected.
   - Make sure Add Multipass to the PATH is selected.
   - A system reboot is required to finish configuring the virtual machine.
4. After the system reboots, run the microk8s installer EXE file to complete configuration.
   - When prompted to configure and launch the MicroK8s virtual machine, click **Yes**.
   - Accept the default settings, then click **Next**. This may take a few minutes.
5. When installation is complete, click **Finish**.
6. Open the command prompt.
7. Use the following command to get a list of namespaces in your cluster.

```
   microk8s kubectl get namespaces
   ```

1. Open the Terminal.
2. Use the following command to install MicroK8s.

```
   sudo snap install microk8s --classic
   ```

3. Type your password, then press **Enter**.
4. Add yourself as a user to the MicroK8s group. This will make using MicroK8s commands easier.
   - Replace **username** with your username.

```
     sudo usermod -a -G microk8s username
     ```

- Replace **username** with your username.

```
     sudo chown -f -R username ~/.kube
     ```

5. Reload the user groups to apply your changes.

```
   newgrp microk8s
   ```

6. Get a list of namespaces in your cluster.

```
   microk8s kubectl get namespaces
   ```

---

## Use an alias

MicroK8s uses a namespaced kubectl command to prevent conflicts with any existing installs of kubectl. If you don't have an existing install of kubectl, you can add an alias to make entering commands easier.

**Note**: `alias` is not recognized on Windows operating systems.

Run the following command:

```
   alias kubectl='microk8s kubectl'
   ```

You can now enter `kubectl` instead of `microk8s kubectl`.

---

## Use short names

You can use some short names with the kubectl command. The short name allows you to use a few letters instead of typing out the entire command name.

1. Open the Terminal or the command prompt.
2. Use the following command to see a list of kubectl commands and any shortnames.

```
   microk8s kubectl api-resources
   ```

3. To see the list of namespaces using the shortname.

```
   microk8s kubectl get ns
   ```

---

## Troubleshooting

### Cannot connect to server

**Issue**

Some users might experience an issue with MicroK8s running on Windows. When using the `microk8s kubectl` command, an error message "Unable to connect to the server".

**Workaround**

- Open the `config` file in the following location: C:\Users\username\AppData\Local\MicroK8s.
- Change the IP address to the one in the message. Leave the port number as-is.
- Save the file and try the `microk8s kubectl` command again.
