Show List

Deploying Applications on Kubernetes

To deploy an application on Kubernetes using containers and YAML configuration files, follow these steps:

  • Create a Docker image of your application: First, you need to create a Docker image of your application. This involves creating a Dockerfile that specifies the base image, adding your application code and dependencies, and building the image. For example, a Dockerfile for a simple Python web application might look like this:
Dockerfile
Copy code
FROM python:3.9 WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY . . CMD ["python", "app.py"]

You can build the Docker image using the docker build command:

bash
Copy code
docker build -t my-app:1.0 .
  • Create a Kubernetes deployment object: Next, you need to create a Kubernetes deployment object that defines the desired state of your application. This includes specifying the Docker image, the number of replicas, and any other configuration options. For example, a deployment object for the Python web application might look like this:
yaml
Copy code
apiVersion: apps/v1 kind: Deployment metadata: name: my-app spec: replicas: 3 selector: matchLabels: app: my-app template: metadata: labels: app: my-app spec: containers: - name: my-app image: my-app:1.0 ports: - containerPort: 5000

This YAML configuration specifies that we want to deploy three replicas of our my-app container, using the Docker image my-app:1.0, and exposing port 5000. You can create the deployment object using the kubectl apply command:

bash
Copy code
kubectl apply -f deployment.yaml
  • Expose the application with a Kubernetes service: Finally, you need to expose your application with a Kubernetes service, which provides a stable IP address and DNS name for your application. For example, a service object for the Python web application might look like this:
yaml
Copy code
apiVersion: v1 kind: Service metadata: name: my-app spec: selector: app: my-app ports: - name: http port: 80 targetPort: 5000 type: LoadBalancer

This YAML configuration specifies that we want to expose our my-app deployment using a load-balanced service that maps port 80 to port 5000 on the container. You can create the service object using the kubectl apply command:

bash
Copy code
kubectl apply -f service.yaml

Once you have deployed your application, you can access it using the IP address or DNS name of the service. For example, if you're running Kubernetes on a local machine using Minikube, you can use the minikube service command to open the application in your web browser:

bash
Copy code
minikube service my-app

This will open your application in a new browser tab, using the IP address and port provided by the service object.


    Leave a Comment


  • captcha text