Minikube Service: Direct Access
Minikube, a tool designed to run Kubernetes locally, simplifies the process of learning and developing applications on Kubernetes. One of its notable features is the minikube service
command, which provides direct access to services running in your Minikube cluster. This feature is particularly useful for developers who want to quickly and easily access their applications without the hassle of configuring external load balancers or ingress controllers. In this article, we’ll explore how to configure Minikube to use the minikube service
command for direct access to a Python application, ensuring a straightforward path to testing and development.
Understanding minikube service
The minikube service
command is designed to open a browser window or tab directly to the Kubernetes service running in your Minikube cluster. It works by forwarding a request from your local machine to a service within Minikube, allowing you to access the service as if it were exposed on your local network. This bypasses the need for manual configuration of port forwarding or external IP addresses, streamlining the development workflow.
Setting Up Your Python Application
Before we dive into the Minikube configuration, let’s set up a simple Python application using Flask, a lightweight WSGI web application framework. This example will demonstrate a basic “Hello, World” application.
Step 1: Create Your Python Flask Application
First, create a file named app.py
in your project directory with the following content:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World from Flask!'
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8080)
Step 2: Dockerize the Application
To run the application in Minikube, you need to containerize it. Create a Dockerfile
in the same directory as your app.py
file:
# Use an official Python runtime as a parent image
FROM python:3.8
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . .
# Install Flask
RUN pip install flask
# Make port 8080 available to the world outside this container
EXPOSE 8080
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
Also, ensure you have a requirements.txt
file in the same directory, listing Flask:
flask
Step 3: Build and Deploy the Application in Minikube
With Minikube running, switch to using Minikube’s Docker daemon:
eval $(minikube docker-env)
Build your Docker image:
docker build -t python-flask-app .
Deploy your application to Minikube by creating a Kubernetes deployment. Here’s a simple deployment configuration in deployment.yaml
:
apiVersion: apps/v1
kind: Deployment
metadata:
name: python-flask-app
spec:
replicas: 2
selector:
matchLabels:
app: python-flask-app
template:
metadata:
labels:
app: python-flask-app
spec:
containers:
- name: python-flask-app
image: python-flask-app
ports:
- containerPort: 8080