How to deploy a dockerized application to Kubernetes

Kubernetes is a popular platform for orchestrating and maintaining containerized applications. We can easily deploy, scale, and manage applications by combining the power of Docker containers and Kubernetes. Here, we’ll deploy a dockerized application to a Kubernetes cluster.

Prerequisites:

  • Docker installed on your machine.

  • Kubernetes is installed and running.

  • A working application that is to be pushed to Docker hub.

To deploy a dockerized application via Kubernetes, we should keep the following steps in mind:

Step 1: Containerize the application before deploying it to Kubernetes. We must make sure that the application is kept as a Docker container. The application must contain a Dockerfile and must be built and tested locally.

Step 2: Push the Docker image to a registry to make it available for the Kubernetes cluster. Push it to a container registry like Docker Hub or Google Container Registry. Use the command docker push to upload the image.

docker login
docker push yourusername/my-app:version
docker tag my-app:latest yourusername/my-app:version

We login to docker using our username and password and push the image to docker hub. Replace yourusername with docker username and my-app with the name of app we are pushing. Image tag is create using tag command.

Step 3: Define Kubernetes files like, deployment.yaml and service.yaml. These are configuration files used in Kubernetes to manage applications in a cluster.

The deployment.yaml file is responsible for deployment and scaling of identical pods. It makes sure that the required pod instances are running continuously. It allows easy updates and rollbacks of application.

apiVersion: apps/v1
kind: Deployment
metadata:
name: flask-app-deployment
spec:
replicas: 1
selector:
matchLabels:
app: flask-app
template:
metadata:
labels:
app: flask-app
spec:
containers:
- name: flask-app-container
image: educativeanswers/simple-flask-app:latest #Replace with image name
ports:
- containerPort: 80
deployment.yaml to help in app deployment

In this file:

  • apiVersion: Specifies API version for Kubernetes. Here, it is using the apps/v1 API version, which points to Kubernetes Apps API group and version 1.

  • kind: Specifies the resource type, which is "Deployment" in here.

  • metadata: Includes metadata about Deployment. The name of Deployment is set as "flask-app-deployment".

  • spec: Describes the required state of the Deployment.

  • replicas: Specifies the required amount pod instances that should be running.

  • selector: Defines how Deployment selects the pods to manage. The matchLabels field defines that the pods managed by this Deployment will have a label app and value "flask-app".

  • template: Defines pod template that Deployment uses to create new pods.

  • metadata: Are the labels applied to pods created. The label app: flask-app is applied to pods.

  • spec: Describes the pod specifications.

  • containers: Specifies the containers within this pod, sets name of the container, specifies image to pull, and defines ports to listen on.

The service.yaml file defines a Kubernetes Service, that is an abstraction to enables network access to pods. Services provide load balancing, service discovery, and a stable IP address for the application.

apiVersion: v1
kind: Service
metadata:
name: flask-app-service
spec:
selector:
app: flask-app
ports:
- protocol: TCP
port: 8080
targetPort: 80
type: LoadBalancer
service.yaml to configure app services

In this file:

  • apiVersion: Specifies the API version used for this Kubernetes.

  • kind: Specifies the resource type, which is “Service” here.

  • metadata: Includes metadata about the Service.

  • spec: Describes the required state of the Service.

  • selector: Defines labels that the Service uses to target pods. The selector defines that the Service should target pods with the label app set to “flask-app.”

  • ports: Specifies the ports that the Service will expose.

  • type: Specifies the Service type.

Step 4: Deploy to application to Kubernetes by following commands in the terminal:

kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
Terminal commands to apply configuration files

Step 5: To check if the app is running properly, we can use the command:

kubectl get services flask-app-service #replace with your app service name
Teminal command to get app service

Step 6: Monitor and Troubleshoot Kubernetes provides monitoring and logging tools to help us keep track of our application’s health and performance. We can do that by command:

kubectl logs
Terminal command to monitor app

To practice running a dockerized application via Kubernetes, press “Run” below and in the terminal, use the following commands in sequence:

kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl get services flask-app-service
Commands for practice playground

Use these commands in the terminal below:

By running these commands in the terminal below, we can see the app status as "pending". It means that the app is working but will take a while to move to "running" state.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: flask-app-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: flask-app
  template:
    metadata:
      labels:
        app: flask-app
    spec:
      containers:
        - name: flask-app-container
          image: educativeanswers/simple-flask-app:latest  # Change this to your image name
          ports:
            - containerPort: 80
Deploying a dockerized application to Kubernetes

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved