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:
curl -LO "https://storage.googleapis. com/kubernetes-release/ release/$(curl -s https://storage.googleapis. com/kubernetes-release/ release/stable.txt)/bin/linux/ amd64/kubectl"
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
Post a Comment