Hi all in the blog we ‘ll learn about Kubernetes deployment and service relationship. As we all know that deployment and service is the heart of Kubernetes. And with these two we can deploy any containerize application. Deployment is use to manage a group of pods, and service is use to provide a stable static address for these group of pods. So that any end-user can access your application.
Do you know how these are connected to each other
So let’s see when we have 100 of deployments and 100 of services in our Kubernetes cluster, How Kubernetes know which service is use to expose which deployment pods.
The answer is deployment and service are connecting via selectors and labels. Labels we define in the deployment file. So when we create the deployment our pods are labels with a particular string. Say i am working on a order microservice so when i ‘ll create the deployment of my order microservice i ‘ll label my pods with order-deployment. It’s totally depend upon you what name you want to give but make sure you give name that is easy to understand and give reference of the project requirement.
Selectors we use in service file. We have to make sure that our selector use the same value that we define for labels in deployment file. So using this selector value Kubernetes service know to which deployment pods it needs to provide a stable network.
Let’s see it with some example it helps us to understand it better.
Example of Kubernetes Deployment and Service
Let’s take an example i have a spring boot application and i have a docker image of this project. So to deploy this application to Kubernetes i need to create deployment and service with this project docker image.
Say my docker image name is order-docker-image. Let’s create the deployment file with this image. You can create the deployment file by yourself or can use the below command to generate the deployment file for you.
kubectl create deployment order-deployment --image=order-docker-image --dry-run=client -o yaml > order-deployment.yaml
Your deployment file looks like below:
order-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: order-deployment
name: order-deployment
spec:
replicas: 1
selector:
matchLabels:
app: order-deployment
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: order-deployment
spec:
containers:
- image: order-docker-image
name: order-docker-image
imagePullPolicy: Never
resources: {}
status: {}Code language: JavaScript (javascript)

This is your deployment file here just look at labels inside template property. So when we create the deployment with this file our pods ‘ll label with order-deployment name. And other thing to notice in this file is macthLabels in selector property. It’s app property name must be same as labels in template, Because this deployment ‘ll manage pods with define label. So in this case our deployment only manage pods who are label with order-deployment.
Use below command to create the deployment using order-deployment.yaml file
kubectl apply -f order-deployment.yamlCode language: CSS (css)
Above command ‘ll create the deployment. Now use below command to check deployment is created or not.
kubectl get deploymentCode language: JavaScript (javascript)

Use below command to see the pods and their labels for this deployment. Use below command.
kubectl get pods -l app=order-deployment --show-labelsCode language: JavaScript (javascript)
In above command app=order-deployment is the name of deployment. You can see this name in your deployment file inside metadata property.

So we have our deployment ready now. It’s time to create a service and give our deployment pods a static address.
Create Kubernetes service
Now we have to create the service to expose this pods to the end-user to access the application. You can create service yaml file manually or can use below command to create the service file.
kubectl expose deployment order-deployment --type=LoadBalancer --port=80 --target-port=8080 --dry-run=client -o yaml > order-service.yaml
After running above command your service yaml file ‘ll be like below.
order-service.yaml
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: order-deployment
name: order-deployment
spec:
ports:
- port: 80
protocol: TCP
targetPort: 8080
selector:
app: order-deployment
type: LoadBalancer
status:
loadBalancer: {}Code language: JavaScript (javascript)

You can see in selector property we are using the same string order-deployment. Using this selector property it knows that which pods needs to be expose. So it just look at the pods that are label with order-deployment.
Use below command to create the service using above service yaml file.
kubectl apply -f order-service.yamlCode language: CSS (css)
Run below command to check if service created or not. and look for external-ip column using this column you can access your application.
kubectl get serviceCode language: JavaScript (javascript)


Hope you like this blog, If you like please share this with others and help us to grow. You can give your feedback in our comment section.
If you need blog on any specific topic you can mention that in our comment section we ‘ll try to cover that in our upcoming blog.
Other blogs you may like.
How to deploy spring boot application on Kubernetes step by step guide