what is Minkube?
Minikube is a tool that allows you to run a Kubernetes cluster on your local machine.
With Minikube, you can quickly and easily set up a Kubernetes environment on your laptop, allowing you to experiment with Kubernetes features and deploy applications without needing a remote server or cloud service. It's a simple and convenient way to get hands-on experience with Kubernetes.
Prerequisites:
ubuntu system with Python and Flask installed
Docker
kubernetes(Kubectl and Minikube)
Implementation:
# create directory and move inside it
mkdir flask-app
cd flask-app
# create a python application called app.py
nano app.py
# copy and paste this inisde app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True)
Inside the flask-app directory itself execute,
nano Dockerfile
This will create a file where we will specify the details for our python app image
FROM python:3.9-slim
WORKDIR /app
COPY . /app
RUN pip install flask
EXPOSE 5000
CMD ["flask", "run", "--host=0.0.0.0"]
This Dockerfile uses a lightweight Python 3.9 environment to run a Flask application. It starts by using a slim version of the Python 3.9 image. It sets the working directory to /app
and copies the current directory’s contents into it. It installs Flask using pip and exposes port 5000, the default port for Flask applications. Finally, it specifies the command to run the Flask application, ensuring it listens on all network interfaces by using --host=0.0.0.0
.
Then build the image using,
docker build -t flask-app .
Run this image as a container and check whether it works or not,
docker run -p 5000:5000 --name flask-app -d flask-hello-app
You can then access your application through your browser at localhost:5000.
Now push the image into your DockerHub registry(you should use 'docker login' first if you have not yet logged in)using this command
docker push your-dockerhub-username/flask-app
If everything is going well we can now start with our deployment procedures
I) Create a deployment.yaml file and in that write:
apiVersion: apps/v1
kind: Deployment
metadata:
name: flask-app
labels:
app: hello
spec:
replicas: 3
selector:
matchLabels:
app: hello
template:
metadata:
labels:
app: hello
spec:
containers:
- name: hello
image: rois2003/flask-hello #specify the image name based on the image you pushed
ports:
- containerPort: 5000
the image will be pulled based on the image you specify in 'image:'
ii) Now create a service.yaml file
apiVersion: v1
kind: Service
metadata:
name: flask-app
labels:
app: hello
spec:
ports:
- port: 5000
protocol: TCP
name: hello
selector:
app: hello
type: LoadBalancer
iii) Use 'minikube ssh' to quickly create and initialize a local Kubernetes cluster on our machine
minikube start
iv) Apply both the deployment and service
kubectl apply -f deployment.yaml
#Then use
kubectl apply -f service.yaml
We use kubectl apply -f deployment.yaml
to create and manage the deployment of our application pods, ensuring they are running and automatically managed by Kubernetes. We then use kubectl apply -f service.yaml
to create a service that exposes these pods, providing a stable IP address and port to access the application, and enabling communication within and outside the Kubernetes cluster.
v) Get the IP of your minkube cluster using
minikube service flask-app --url
Now you can access your application with the provided URL inside your local machine.