Skip to main content

Setting Up Minikube and Kubectl on Ubuntu Using Docker: A Step-by-Step Guide to Deploy custom httpd image on Kubernets

Kubernetes is a powerful platform for managing containerized applications, and Minikube provides an easy way to run Kubernetes locally. In this article, we'll walk through the installation of Minikube and `kubectl` on Ubuntu using Docker as the driver. We’ll also demonstrate how to deploy a custom HTTPD application with your own index file.


Prerequisites


Before diving in, make sure you have:

- A system running Ubuntu.
- A stable internet connection.
- Administrative access to install packages.


Step 1: Install Docker


Minikube requires a hypervisor to run Kubernetes clusters, and Docker is a popular choice. Here’s how to install Docker on Ubuntu.

1. Update the package index:


   sudo apt update


2. Install prerequisites:


   sudo apt install -y apt-transport-https ca-certificates curl software-properties-common


3. Add Docker’s official GPG key:

   curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -


4.Add Docker’s stable repository:


   sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"


5. Install Docker:


   sudo apt update
   sudo apt install -y docker-ce


6.Start and enable Docker:


   sudo systemctl start docker
   sudo systemctl enable docker


7.Verify Docker installation:

   docker --version


Explanation


These commands install Docker and verify that it's running correctly on your Ubuntu system. Docker will serve as the container runtime for Minikube.

Step 2: Install Kubectl


`kubectl` is the command-line tool for interacting with Kubernetes clusters.

1.Download the latest release:




2. Make the `kubectl` binary executable:

   
   chmod +x ./kubectl
   

3. Move the binary to your PATH:

  
   sudo mv ./kubectl /usr/local/bin/kubectl
  

4. Verify the installation:


   kubectl version --client


Explanation


This process downloads the `kubectl` binary, makes it executable, and moves it to a directory that’s included in your system's PATH, allowing you to run `kubectl` commands from anywhere in the terminal.

Step 3: Install Minikube


Now, let’s install Minikube:

1. Download the latest Minikube installer:




2. Make the Minikube binary executable:

   
   chmod +x minikube-linux-amd64
   

3. Move the binary to your PATH:

 
   sudo mv minikube-linux-amd64 /usr/local/bin/minikube
 

4. Verify the installation:


   minikube version


Explanation


These steps download Minikube, make it executable, and move it to your PATH. Verifying the installation ensures everything is set up correctly.

Step 4: Start Minikube Using Docker


With everything installed, you can now start your Minikube cluster:


minikube start --driver=docker


Explanation


This command initializes a Minikube cluster using Docker as the driver, downloading and configuring the necessary Kubernetes components.

Step 5: Verify `kubectl` Configuration


Check if `kubectl` is properly configured to communicate with your Minikube cluster:


kubectl config current-context


Explanation


This command shows the current context for `kubectl`, which should indicate that it’s connected to your Minikube cluster.

Step 6: Create a Custom HTTPD Image


6.1 Create a Custom Index File


First, create a custom `index.html` file that will be served by the HTTPD container.

1. Create a directory for your custom image:


   mkdir my-httpd
   cd my-httpd


2. Create an `index.html` file:


   echo "<h1>Welcome to My Custom HTTPD Page!</h1>" > index.html


6.2 Create a Dockerfile


Next, create a `Dockerfile` to define your custom HTTPD image.

1. Create a Dockerfile:


   touch Dockerfile


2. Open the Dockerfile and add the following content:


   FROM httpd:latest
   COPY index.html /usr/local/apache2/htdocs/


6.3 Build the Custom Image


Now, build your custom Docker image:

docker build -t my-custom-httpd .


Explanation


-FROM httpd:latest: This line specifies that you want to base your custom image on the latest official HTTPD image.
-COPY index.html /usr/local/apache2/htdocs/  : This copies your custom `index.html` file into the appropriate directory for the HTTPD server.

Step 7: Create the Deployment with Your Custom HTTPD Image


Deploy your custom HTTPD image in the Minikube cluster:


kubectl create deployment my-httpd --image=my-custom-httpd --replicas=3


Explanation


This command creates a deployment named `my-httpd`, using the custom image you built earlier and specifying that you want 3 replicas.

Step 8: Expose the Deployment


Expose the deployment to make it accessible from your host machine:


kubectl expose deployment my-httpd --type=NodePort --port=80


Explanation


This command creates a service to expose your custom HTTPD deployment, making it accessible via a port on your host machine.

## Step 9: Get the Service URL


To access the HTTPD service, retrieve the URL:

minikube service my-httpd --url


Explanation


This command fetches the URL for your HTTPD service, allowing you to access it from your web browser.

Step 10: Access the Custom HTTPD Service


Open a web browser and enter the URL obtained in the previous step. You should see your custom HTTPD page displaying "Welcome to My Custom HTTPD Page!"


Explanation


By navigating to the URL, you can verify that your custom HTTPD service is running and serving your specific index file.

Step 11: Check Pod Status


To check the status of your pods, use:


kubectl get pods


Explanation


This command lists all the pods in the current namespace, displaying their statuses. You should see 3 pods in the `Running` state.

Step 12: Describe the Pods (If Needed)


If any pod is not running, describe it for more information:


kubectl describe pod <pod-name>


Explanation


Replace `<pod-name>` with the name of the problematic pod. This command provides detailed information about the pod, including events that might indicate why it’s not running.


Step 13: Clean Up

When you’re finished, you can delete the deployment and service to clean up resources:


kubectl delete service my-httpd
kubectl delete deployment my-httpd


Explanation


These commands remove the service and deployment, stopping the HTTPD pods and freeing up resources.

Conclusion


In this guide, we covered the installation of Minikube and `kubectl` using Docker on Ubuntu, how to create and deploy a custom HTTPD image with your own index file, and access it through your web browser. This setup provides a great local development environment for exploring Kubernetes and customizing applications. If you have any questions or run into issues, feel free to reach out! Happy Kuberneting!

Comments

Popular posts from this blog

Example of Maven project that interacts with a MySQL database and includes testing

Example Maven project that interacts with a MySQL database and includes testing To install Java, MySQL, Maven, and write a Java program to fetch table data, execute, and create a JAR file using Maven on Ubuntu, you can follow these steps: Step 1: Install Java You can install Java using the following commands: sudo apt update sudo apt install default-jre sudo apt install default-jdk Verify the installation by running: java -version Step 2: Install MySQL You can install MySQL using the following commands: sudo apt update sudo apt install mysql-server During the installation, you'll be prompted to set a root password for MySQL or you can set password at latter stage using following steps.  sudo mysql ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; exit Step 3: Install Maven You can install Maven using the following commands: sudo apt update sudo apt install maven Verify the installation by running: mvn -version Step 4: Create

Maven Create and Build Artifacts

In Maven, you can create and build artifacts using the package phase of the build lifecycle. The package phase is responsible for taking the compiled code and other project resources and packaging them into a distributable format, such as a JAR (Java Archive), WAR (Web Application Archive), or other custom formats. Here are the steps to create and build artifacts using Maven: Configure the Build Output: In your project's pom.xml file, you need to configure the output of the build. This includes specifying the type of artifact you want to create (e.g., JAR, WAR) and any additional resources to include. You do this in the <build> section of your pom.xml: <build>     <finalName>my-artifact</finalName> <!-- Name of the artifact without the extension -->     <plugins>         <!-- Plugin configurations for creating the artifact -->         <!-- For example, maven-jar-plugin or maven-war-plugin -->     </plugins> </build> Depend

Maven Repositories (local, central, global)

Maven relies on repositories to manage dependencies, plugins, and other artifacts required for a project. There are typically three types of repositories in Maven: local, central, and remote/global repositories. Local Repository: Location: The local repository is located on your local development machine. By default, it's in the .m2 directory within your user home directory (e.g., C:\Users\<username>\.m2\repository on Windows or /Users/<username>/.m2/repository on macOS and Linux). Purpose: The local repository is used to store artifacts (JARs, POMs, and other files) that your machine has downloaded or built during previous Maven builds. These artifacts are specific to your local development environment. Benefits: Using a local repository improves build performance since it caches dependencies locally, reducing the need to download them repeatedly. It also ensures reproducibility by maintaining a local copy of dependencies. Central Repository: Location: The central repo