DEV Community


Posted on

Set up docker and kubernetes in ubuntu 22.04

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 and microk8s from Canonical.
For running our software during development, we will be using skaffold which is a great tool developed by Google.

PART 1: Docker

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.
  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!

PART 2: Kubernetes

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.
sudo snap install microk8s --classic --channel=1.26
Enter fullscreen mode Exit fullscreen mode
  • Join the group, so that the user does not need root access to run any command from microk8s
sudo usermod -a -G microk8s $USER
sudo chown -f -R $USER ~/.kube

su - $USER
Enter fullscreen mode Exit fullscreen mode
  • Check status of microk8s. We have completed installation of microk8s by this point. Sadly, many more steps to come.
microk8s status
# Wait till the microk8s cluster is ready
microk8s status --wait-ready
Enter fullscreen mode Exit fullscreen mode
  • Deploy an application (say nginx) and test
microk8s kubectl create deployment nginx --image=nginx
Enter fullscreen mode Exit fullscreen mode

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.
microk8s kubectl get pods
Enter fullscreen mode Exit fullscreen mode

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 microk8s kubectl. We want to type only kubectl and that should be enough to process our request inside the microk8s cluster. For this, follow the below steps: a. sudo snap unalias kubectl in case kubectl is already aliased somewhere in your environment. b. sudo snap install kubectl --classic c. microk8s.kubectl config view --raw > $HOME/.kube/config 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
microk8s enable ingress
Enter fullscreen mode Exit fullscreen mode
  • Install skaffold for development in k8s
    a. Get the stable binaries only. Installation Guide. Go to the Linux tab and follow the instructions to get the stable binaries

    # For Linux x86_64 (amd64)
    curl -Lo skaffold && \
    sudo install skaffold /usr/local/bin/
    # For Linux ARMv8 (arm64)
    curl -Lo skaffold && \
    sudo install skaffold /usr/local/bin/
  • Now if you run skaffold dev 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.

    "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:

    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:

    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.

Top comments (0)