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 logindocker push yourusername/my-app:versiondocker 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/v1kind: Deploymentmetadata:name: flask-app-deploymentspec:replicas: 1selector:matchLabels:app: flask-apptemplate:metadata:labels:app: flask-appspec:containers:- name: flask-app-containerimage: educativeanswers/simple-flask-app:latest #Replace with image nameports:- containerPort: 80
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: v1kind: Servicemetadata:name: flask-app-servicespec:selector:app: flask-appports:- protocol: TCPport: 8080targetPort: 80type: LoadBalancer
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.yamlkubectl apply -f service.yaml
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
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
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.yamlkubectl apply -f service.yamlkubectl get services flask-app-service
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
Free Resources