Set up docker and kubernetes in ubuntu 22.04
Tags: docker, kubernetes, beginners, programming
Published: 2023-03-18 15:54:03

There are a lot of resources on the official websites for this topic. Then why am I writing this blog?
This blog is for someone who is trying to set up docker and kubernetes in a ubuntu machine, but has no experience in dev-ops whatsoever and wants to setup the environment quickly for running some piece of code from a developer bootcamp or any tutorial.
So let's dive into it without further ado.

We will be using [docker](https://www.docker.com/) and [microk8s](https://microk8s.io/) from Canonical.
For running our software during development, we will be using [skaffold](https://skaffold.dev/) which is a great tool developed by Google.

---

<u>

## PART 1: Docker
</u>

Docker makes development easier, more efficient and predictable. It solves the age old issue of developers (*it worked fine on my device*) by running the code in small instances (containers) of popular server OSes. Multiple containers can be run in a simple laptop machine hardware without needing any special tools or expertise in setting up OS.

### Installation Guide

1. Go to [docker docs for ubuntu](https://docs.docker.com/desktop/install/ubuntu/).
2. Follow the steps provided in this site. These steps are very comprehensive and easy to follow. So we will not be discussing further steps here.

Just follow the steps on the site and you are good to go!

---

<u>

## PART 2: Kubernetes
</u>

Kubernetes is a tool for automating certain aspects of *operations* part of IT. It is popularly used to manage deployments, roll out changes to deployed applications, scaling applications horizontally, monitoring and much more.

We will be using kubernetes (hereafter referred to as ***k8s***) for creating an orchestration of all our different services. In other words, the kubernetes environment will contain all the logic for inter-service connection and exposing the functionalities of the overall application, i.e., it will create a black-box environment for the outside world and only expose a few API endpoints for the functionality.

### Installation Guide

- Use snap to install the microk8s module. We will go with channel 1.26 which is a stable channel as of the writing of this blog.

```install.sh
sudo snap install microk8s --classic --channel=1.26
```

- Join the group, so that the user does not need root access to run any command from microk8s

```create-group.sh
sudo usermod -a -G microk8s $USER
sudo chown -f -R $USER ~/.kube

su - $USER
```

- Check status of microk8s. We have completed installation of microk8s by this point. Sadly, many more steps to come.

```status.sh
microk8s status
# Wait till the microk8s cluster is ready
microk8s status --wait-ready
```

- Deploy an application (say *nginx*) and test

```deploy.sh
microk8s kubectl create deployment nginx --image=nginx
```

Here the application name is nginx and we are using the docker image *nginx* which is the official image provided by nginx team.

- Check if the application was deployed properly.

```check.sh
microk8s kubectl get pods
```

The pod(s) should have STATUS as ***Running*** and READY as ***1/1*** or ***2/2*** and **not** like ***0/1***

- Now, we want to be able to use kubectl command in the *microk8s* context without having to type <code>microk8s kubectl</code>. We want to type only <code>kubectl</code> and that should be enough to process our request inside the microk8s cluster. For this, follow the below steps:
   a. <code>sudo snap unalias kubectl</code> in case kubectl is already aliased somewhere in your environment.
   b. <code>sudo snap install kubectl --classic</code>
   c. <code>microk8s.kubectl config view --raw > $HOME/.kube/config</code> will copy the microk8s kubectl configs into our user's HOME, so that they can be directly accessed by the user using the kubectl command.
- At this point, we can use microk8s normally and deploy applications. Further steps are specific to our use-cases.
- Install ingress-nginx

```ingress.sh
microk8s enable ingress
```

- Install skaffold for development in k8s
    a. Get the stable binaries only. [Installation Guide](https://skaffold.dev/docs/install/). Go to the Linux tab and follow the instructions to get the stable binaries

    ```skaffold.sh
    # For Linux x86_64 (amd64)
    curl -Lo skaffold https://storage.googleapis.com/skaffold/releases/latest/skaffold-linux-amd64 && \
    sudo install skaffold /usr/local/bin/
    ------------------------------------------------------------------------------
    # For Linux ARMv8 (arm64)
    curl -Lo skaffold https://storage.googleapis.com/skaffold/releases/latest/skaffold-linux-arm64 && \
    sudo install skaffold /usr/local/bin/
    ```

- Now if you run <code>skaffold dev</code> in your project that contains a *skaffold.yaml* file, you will get some error related to the build process. So, basically skaffold will complain that it can not find the tags that it itself associated with the images present in the yaml file during build. ***Please note***, that this error is specific to *microk8s*. It does not occur with Docker Desktop kubernetes implementations for Mac/Windows OS.
- We will have to therefore, use private registry in the local environment to build the containers from images during development. ***Please note*** that skaffold and microk8s kubectl can still pull images from docker registry during production CI/CD deployments.
    a. Go to `/etc/docker` folder.
    b. Create a *daemon.json* file in this location. `sudo nano daemon.json`. This will open up nano editor with this new file.
    c. Put the following entry inside the file.

    ```daemon.json
    {
    "insecure-registries" : ["localhost:32000"]
    }
    ```

    d. Press `Ctrl + O` then press `Enter`. This will save the file.
    e. Now press `Ctrl + X`. This will exit the editor.
    f. Now we have to restart docker to make sure the changes are accepted. For this, run the following command:
    
    ```restart-docker.sh
    sudo systemctl restart docker
    ```

    g. After you run this command, wait for a few moments and then check if the docker engine has restarted with the command:

    ```status-docker.sh
    sudo systemctl status docker
    ```

    Check if the *Active* state is in green and it says *'(running)'*. Then proceed further.
- Change the image value in skaffold.yaml file like so:
    `username/image-name` -> `localhost:32000/image-name`
- Run `skaffold dev` command now. It will take a few minutes for the pods to be created and stabilized.

That's it! I promise there is nothing left to do to start running microservices codes on your ubuntu machine, be it during development or during deployment.