Easy App Deployment in Kubernetes Using Minikube

Easy App Deployment in Kubernetes Using Minikube

Learn to deploy your app in Kubernetes with minikube, no K8s cluster needed

Many of us have a hard time finding a platform with a Kubernetes cluster setup to deploy applications. Though, setting up your own K8s cluster with cloud providers isn't too difficult. But if you want something easy to set up and get you started in minutes, you should definitely try Minikube.

What is minikube and why to use?

Minikube is a lightweight tool that lets you run a single-node Kubernetes cluster locally on your personal computer. It is mainly used for development and testing purposes, allowing developers to work with Kubernetes without the complexity of managing a full-scale cloud or multi-node cluster.

Few reasons to use minikube are local development, easy setup, fast testing, supports add-ons and Learning and Experimentation. Overall, Minikube is a powerful tool for developers who need a lightweight, flexible Kubernetes environment for local use.

What you’ll need

  • 2 CPUs or more

  • 2GB of free memory

  • 20GB of free disk space

  • Internet connection

  • Container or virtual machine manager, such as: Docker, QEMU, Hyperkit, Hyper-V, KVM, Parallels, Podman, VirtualBox, or VMware Fusion/Workstation


Let's jump straight into the implementation and for this, an Azure VM will be utilized to install Minikube. This approach provides the flexibility to remove both Minikube and the VM once the task is completed.

  1. Deploy an Azure VM with Ubuntu and SSH onto the VM

  2. Install docker.io on the VM and verify the installation by checking the version using sudo docker -v

sudo apt update -y
sudo apt install docker.io -y
sudo docker -v

The Docker daemon binds to a Unix socket, not a TCP port. By default it's the root user that owns the Unix socket, and other users can only access it using sudo. The Docker daemon always runs as the root user.

  1. Add your user to the docker group. This command adds the user azureuser to the docker group, allowing the user to run Docker commands without needing to use sudo each time. Make sure you log off and login again to the VM for the changes to take effect.

     sudo usermod -aG docker azureuser
    
  2. Install minikube and start it using minikube start

     curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
     sudo install minikube-linux-amd64 /usr/local/bin/minikube && rm minikube-linux-amd64
    

     minikube start
    

  3. Now, check by running some kubectl commands and you will see pods in the control plane are up and running

     kubectl get ns
     kubectl get pods -n kube-system
    

  4. If kubectl does not work then, create a file called .bash_aliases and create an alias as: alias kubectl="minikube kubectl --". Run the source command to activate the alias shown below

     cat .bash_aliases
     source ~/.bash_aliases.
    

  5. Now, we will deploy a sample application on kubernetes using minikube. For this, clone a git repository. I have used a sample python web-app in Docker-Zero-to-Hero repo by Abhishek Veeramalla.

     ls
     cd /Docker-Zero-to-Hero/examples/python-web-app
     ls
    

  6. Build the Dockerfile using docker build -t sample-python:v1 .

     docker build -t sample-python:v1 .
    

  7. If you get the above error, edit the docker file and append --break-system-packages as shown below and build the image again.

     FROM ubuntu
    
     WORKDIR /app
    
     COPY requirements.txt /app
     COPY devops /app
    
     RUN apt update
     RUN apt install -y python3 python3-pip
     RUN pip install -r requirements.txt --break-system-packages
     RUN cd devops
    
     ENTRYPOINT ["python3"]
     CMD ["manage.py", "runserver", "0.0.0.0:8000"]
    
  8. This time the image will be built successfully and can be seen by running below command

    docker images
    

  9. Now the image is successfully created, it is time to create deployment deployment.yaml. Use labels and selectors as well as shown below

  10. Apply the deployment.yaml using kubectl apply -f deployment.yaml

    You will notice that the deployment is created but pods are not running and gives an error status ErrImagePull or ImagePullBackOff. This is because of the fact that the local Docker daemon might not be properly set up. This can occur if we are using Minikube, Docker Desktop, or another local Kubernetes environment.

  11. To resolve this, build the image directly inside Minikube's Docker environment, which allows Kubernetes running in Minikube to use the image without needing to pull it from an external source. You will see that the pods will be up and running

    eval $(minikube docker-env)
    docker build -t sample-python:v1 .
    kubectl apply -f deployment.yaml
    kubectl get deployment
    kubectl get pods
    

  12. Now, hit the below commands and you will get a HTML response from the python app. But if you try to hit the url from outside the minikube, it will not work because by default, the pods are communicating using clusterIP service which allows access to the application only from inside the cluster.

    kubectl get pods -o wide
    minikube ssh
    curl -L http://<any of the pod IPs>:8000/demo
    

  13. To access application from the node, that is, the VM, we need to create a service of type NodePort which will allow access to the application from the node by executing the below commands. Now, the application is being accessed by the node IP and not the cluster IP and the port 80 of cluster is exposed and mapped to port 30007 of the node.

    kubectl get pods -o wide
    curl -L http://10.244.0.5:8000/demo
    cat services.yaml
    kubectl apply -f services.yaml
    kubectl get svc
    minikube ip
    curl -L http://192.168.49.2:30007/demo
    

    In this article, we explored the process of deploying a sample application on Kubernetes using Minikube. By leveraging Minikube, we were able to set up a local Kubernetes environment quickly and efficiently, making it an excellent tool for development and testing purposes. We walked through the steps of setting up an Azure VM, installing Docker and Minikube, and deploying a sample Python web application. Additionally, we discussed how to resolve common issues such as image pulling errors and how to expose the application using NodePort service. Minikube proves to be a valuable resource for developers looking to experiment with Kubernetes in a simplified and controlled environment.

    This demonstration covered the deployment of a sample application on Kubernetes using Minikube. In this example, ClusterIP and NodePort services were discussed. There are additional service types that will be addressed in a future blog post. Until then, enjoy Minikubing!