Goglides Dev 🌱

Cover image for Process to deploy docker images to Kubernetes k8s
Jeewan Gautam
Jeewan Gautam

Posted on

Process to deploy docker images to Kubernetes k8s

We already discussed how to dockerize Java springboot application in this blog. https://www.goglides.dev/jeewangautam_740/process-to-dockerize-java-springboot-application-and-publish-it-to-public-registry-docker-hub-19mc
we also discuss how to create Kubernetes cluster using kind in this blog. https://www.goglides.dev/jeewangautam_740/the-process-to-install-and-create-k8s-cluster-using-kind-in-macos-from-scratch-12ja

Let's discuss about how to deploy docker images to k8s.

Here we will use Kubernetes namespace concept to deploy different version of application in different namespaces.

What is namespace in Kubernetes?
Kubernetes namespace is a logical grouping of resources in a Kubernetes cluster. It is used to provide isolation and separation of concern between different users, teams, or projects in a cluster. Kubernetes namespace is used to segregate cluster resources and provide multi-tenancy in Kubernetes. All resources created within a namespace are isolated from other resources in different namespaces. This isolation helps prevent accidental name collisions between different resources.

How to create namespaces
You can create a namespace by running a single command.

kubectl create namespace qa
Enter fullscreen mode Exit fullscreen mode

Output
Image description
we can see, qa name space is created.

let's create podsconfig.yaml configuration file with below details to create pods.

apiVersion: v1
kind: Pod
metadata: 
 name: mydemo-app
 namespace: qa
 labels:
  appname: demoapp
  type: springboot
spec:
 containers:
 - name: demospringapp
   image: jeewangautam02261995/springbootapp:v1
Enter fullscreen mode Exit fullscreen mode

The following points need to be noted about the above file −
apiVersion is version of the Kubernetes API you're using to create this object.
kindrepresents the type of Kubernetes objects to be created while using the yaml file.
namerepresents the name of the pod to be created while using the yaml file.
metadata represents the data that helps uniquely identify the object, including a name string, UID , and optional namespace.
namespace pods will be created in namespace qa which we earlier created.
labels are key-value pairs which are attached to pod mydemo-app.
spec field is where we'll describe the object in greater detail. In our example 'name' of the container is demospringapp to run and image jeewangautam02261995/springbootapp:v1 which we pushed in docker hub in previous blog.

Let's run below command to create pod in qa name space.

kubectl apply -f podsconfig.yaml
Enter fullscreen mode Exit fullscreen mode

Output
Image description

Let's

You can run below command to Analyze pod mydemo-app in qa namespace.

kubectl describe  pod -n qa
Enter fullscreen mode Exit fullscreen mode

Output

Image description
You can verify springbootapp:v1 image is deployed Kubernetes pod mydemo-app which is in qa namespace in the above output with details view of pod.

Now, we are going to deploy springboot:v1 application in production namespace. Follow below simple steps:
Step 1 - Create production namespace.
Step 2 - Change the namespace qa to production in podsconfig.yaml file.
Step 3 - Apply changes using kubectl apply command.
Step 4 - Verify the springboot:v1 application in production namespace using kubectl describe pod -n production , kubectl get pods -n production.

We have successfully deployed springboot:v1 application in two different namespaces Kubernetes cluster.

Top comments (0)