Deployment with Kubernetes with sample example

Posted on January 18, 2023

Kubernetes is an open-source platform that automates the deployment, scaling, and management of containerized applications. To deploy an application with Kubernetes, you'll need to create a Kubernetes "manifest" file, which defines the desired state of your application and its resources.

Enable Kubernetes from Docker Desktop

To enable Kubernetes from Docker Desktop, follow these steps:

  • Open Docker Desktop
  • Go to the settings menu by clicking on the Docker icon in the system tray, then select Preferences.
  • In the Kubernetes tab, check the box next to "Enable Kubernetes".
  • Click on Apply & Restart to save the changes and restart Docker.
  • Once Docker has restarted, you should see a Kubernetes option in the Docker menu, where you can access Kubernetes command-line tools and manage your cluster.

Please ensure your system meets the requirement to run Kubernetes on the Docker desktop.

Enable Kubernetes From Docker

OR use Azure Kubernetes Service Follow another article for (HOW TO CREATE AZURE KUBERNETES SERVICE (AKS?)

Here is an example of a Kubernetes manifest file for a simple web application that runs in a Docker container:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-web-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-web-app
  template:
    metadata:
      labels:
        app: my-web-app
    spec:
      containers:
      - name: my-web-app
        image: my-web-app:latest
        ports:
        - containerPort: 80

This manifest file defines a Deployment named "my-web-app" that will run 3 replicas of a container based on the "my-web-app:latest" image, and that container will listen on port 80.

To deploy this application to a Kubernetes cluster, you can use the kubectl command-line tool and run the following command:

This command will create the deployment and the necessary ReplicaSet (Learn more about in my other blogs ) and Pods in the cluster.

You can also create a service to expose the application on the network using the following yaml file:

apiVersion: v1
kind: Service
metadata:
  name: my-web-app
spec:
  selector:
    app: my-web-app
  ports:
  - name: http
    port: 80
    targetPort: 80
  type: LoadBalancer

This service will create a load-balancer that will route the traffic to the pods and expose the service on port 80.

Keep in mind that this is just a simple example and you should adjust the configuration according to your needs.

Summary

To deploy an application with Kubernetes, you need to create a manifest file that defines the desired state of your application and its resources. The manifest file should include information such as the number of replicas, the container image, and the ports that the application will use. You can use the kubectl command-line tool to deploy the application to a Kubernetes cluster by running the command "kubectl apply -f my-web-app.yaml". Additionally, you can create a service to expose the application on the network by creating a service yaml file and applying it to the cluster using "kubectl apply -f my-web-app-service.yaml" which will create a load-balancer that routes the traffic to the pods.

Thanks for reading!


Posted on January 18, 2023
Profile Picture

Arun Yadav

Software Architect | Full Stack Web Developer | Cloud/Containers

Subscribe
to our Newsletter

Signup for our weekly newsletter to get the latest news, articles and update in your inbox.

More Related Articles