top of page

Lab 12 - Getting Started with Kubernetes - Day 85

Writer's picture: vPvP

Welcome back to the #90DaysOfDevOps series! Today, we're stepping into the world of Kubernetes, a powerful container orchestration tool. If you're new to Kubernetes, don't worry; we'll take it one step at a time. By the end of this lab, you'll have deployed a simple application on your local Kubernetes cluster.


Setting Up Your Environment

Before we dive into Kubernetes, make sure you have it installed on your machine. You can follow the installation guide on the official Kubernetes website for your specific operating system. Once installed, you're ready to roll!


Creating a Simple Application

For this lab, let's build a straightforward Python web application. Start by creating a new directory for your project. Inside that directory, create two files: app.py for your application code and requirements.txt for the dependencies.

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
    return 'Hello, Kubernetes!'

requirements.txt:

Flask==2.1.1

Dockerizing Your Application

Now, let's package our application into a Docker container. Create a new file named Dockerfile in the same directory as your application code.

Dockerfile:

# Use the official Python image
FROM python:3.9

# Set the working directory
WORKDIR /app

# Copy the dependencies file to the working directory
COPY requirements.txt .

# Install any dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy the content of the local src directory to the working directory
COPY . .

# Specify the command to run on container start
CMD ["python", "./app.py"]

This Dockerfile does the following:

  • Uses the official Python image as the base.

  • Sets the working directory to /app.

  • Copies requirements.txt and installs the dependencies.

  • Copies the application code to the container.

  • Specifies the command to run our application.


Building the Docker Image

Open a terminal, navigate to your project directory, and run the following command to build your Docker image:

docker build -t my-python-app .

This command tells Docker to build an image with the tag my-python-app using the current directory as the build context.


Running Your Dockerized Application

Now that we have our Docker image, let's run it locally to make sure everything works as expected:

docker run -p 5000:5000 my-python-app

This command maps port 5000 from your local machine to port 5000 in the Docker container.


Visit http://localhost:5000 in your browser, and you should see your simple web application greeting you.


Deploying to Kubernetes

It's time to deploy our application to Kubernetes! Create a new file named deployment.yaml:

deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-python-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-python-app
  template:
    metadata:
      labels:
        app: my-python-app
    spec:
      containers:
      - name: my-python-app
        image: my-python-app:latest
        ports:
        - containerPort: 5000
---
apiVersion: v1
kind: Service
metadata:
  name: my-python-app
spec:
  selector:
    app: my-python-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 5000
  type: LoadBalancer

This YAML file defines a Kubernetes Deployment and Service for our application.


Apply the configuration to your Kubernetes cluster:

kubectl apply -f deployment.yaml

Wait a moment for the resources to be created. You can check the status using:

kubectl get pods
kubectl get services

Once the external IP is available for the service, open your browser and visit the external IP to see your application running on Kubernetes!


Congratulations! You've successfully deployed a simple application on your local Kubernetes cluster.


In the next few days, we'll explore more advanced Kubernetes concepts, so stay tuned!


Thank you for reading!


*** Explore | Share | Grow ***


5 views0 comments

Comentários

Avaliado com 0 de 5 estrelas.
Ainda sem avaliações

Adicione uma avaliação
vp1.2_23.png

vPundit

Explore | Share | Grow

Thanks for submitting!

vPundit

bottom of page