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
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
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
spec field is where we'll describe the object in greater detail. In our example 'name' of the container is
demospringapp to run and
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
You can run below command to Analyze pod
kubectl describe pod -n qa
Now, we are going to deploy
springboot:v1 application in
production namespace. Follow below simple steps:
Step 1 - Create
Step 2 - Change the namespace
qa to production in
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.