Set up a Kubernetes cluster using microk8s on macOS
Tags: kubernetes, devops, macos, microservices
Published: 2020-08-18 18:37:05

**Disclaimer:** This post follows a simple approach, there are probably better ways to do this (please share them if you know any!), and your mileage most definitely will vary.
***

This post is part of a series of posts on how to **Set up a Kubernetes cluster on macOS**, in this case, using **microk8s**. 

Kubernetes is a complex container orchestration tool that can be overwhelming for beginners. Had you wondered how to get started on Kubernetes by yourself easily without the complexity of Turnkey Solutions (Oracle OKE, AWS EKS) and Managed Solutions (Google GKE), this post would give you the simplicity to deploy a Kubernetes cluster on macOS intended for dev/test purposes.

Table of Contents
=================
1. [Microk8s](#microk8s)
1. [Prerequisites](#prerequisites)
2. [Installation](#installation)
3. [Create the cluster](#create-the-cluster)
4. [Test Kubernetes](#test-kubernetes)
5. [See the Cluster IP](#see-the-cluster-ip)
6. [Deploy a Pod](#deploy-a-pod)

<a href="#microk8s"></a>

### 💠 Microk8s
Enter **microk8s**, it is the smallest, most straightforward production-grade cluster of K8s. There are no VMs in the middle like other deployments **Minikube**. Instead, it uses snap packages, snap is an application packaging and isolation technology; **microk8s** is so small than even you can use it on Raspberry Pi.

Although our goal is to deploy a dev/test environment, microk8s is intended for all 3 environments (production, dev & test). Whether you are running a production environment or interested in exploring K8s, **microK8s** serves your needs.

<a href="#prerequisites"></a>

### ☑️ Prerequisites 

Before starting, you need these prechecks:
  * Install the [brew](https://brew.sh/) package manager. 
  * At least 20G of disk space and 4G of memory is recommended.
  * An internet connection.

Don't have the [brew](https://brew.sh/) command? You need to open a terminal and run the following command: 
  ```toml 
  /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
  ```

<a href="#installation"></a>

### ⚙️ Installation

In terminal, install **microk8s** with brew.
```toml 
brew install ubuntu/microk8s/microk8s
```
_On macOS **microk8s** need multipass, when prompted say yes._

<a href="#create-the-cluster"></a>

### 🧑‍🚀 Create the cluster

1. To deploy a **Kubernetes cluster**, you can go with the install command without parameters.
    ```toml 
    microk8s install
    ```
    
    Or you can be more specific like me (2 CPU, 2GB RAM, 30GB maximum space of disk).

    ```toml 
    microk8s install --cpu 2 --mem 2 --disk 30
    ```

    _While it is running, you can open another terminal window and check the installation's status with the following command:_
  
    ```toml 
    microk8s status --wait-ready
    ```

1. Enable the DNS service.
    ```toml
    microk8s enable dns
    ```

<a href="#test-kubernetes"></a>

### ✅ Test Kubernetes
Let's test our command center
  ```toml
  kubectl get nodes
  ``` 
  _You should see one node named `microk8s-vm` in status Ready_
  ```toml
  NAME          STATUS   ROLES    AGE     VERSION
  microk8s-vm   Ready    <none>   4d19h   v1.18.6-1+64f53401f200a7
  ```

<a href="#see-the-cluster-ip"></a>

### 🗺️ See the Cluster IP

Run the following command and find the cluster IP in the endpoint property.
  ```toml
  kubectl describe svc kubernetes
  ``` 

  ```toml
  Name:              kubernetes
  Namespace:         default
  Labels:            component=apiserver
                     provider=kubernetes
  Annotations:       <none>
  Selector:          <none>
  Type:              ClusterIP
  IP:                10.152.183.1
  Port:              https  443/TCP
  TargetPort:        16443/TCP
  Endpoints:         192.168.64.4:16443
  Session Affinity:  None
  Events:            <none>
  ```
  Mine is `192.168.64.4`.

<a href="#deploy-a-pod"></a>

### 🚀 Deploy a Pod 

I make a definition file just for you, Time to make a simple cluster:
  ```toml
  kubectl create -f https://raw.githubusercontent.com/reybis/set-up-kubernetes-cluster/master/nginx-deployment.yml
  ``` 
  _This will create a pod of nginx and one nodePort service at 30005._

Since you know the Cluster IP, you can see your nginx pod `http://cluster-ip:30005` deployed on your new local cluster! 🎉 

That's it, now you have a Kubernetes cluster in your machine without VMs.