Its very necessary that we specify a proper selector while creating a kubernetes service. The selectors should be unique. So, that they can easily discover the pods.
If you specify a common selectors for all the services, then it may occur that it will point to a multiple pods. And then it will be very difficult to identify the real cause. I was getting a connection-refused error. Sometimes, when i was accessing pod using a node port service.
In my case i was having two pods (named backend-manager and the other one was named engine).
And i have created two node port services for it (bm and engine).
And here are the service yamls :
1. Engine Service :
2. BM SERVICE :
As, you can see i have mistakenly mentioned the common selector in both the service.
And lets see, the endpoints for the service, using the below url.
http://localhost:8000/api/v1/namespaces/myapps-fv92n/endpoints/bm
And here is the output :
As, you can clearly see that different pods (both BM and Engine) are listing under the service endpoint (under subsets->addresses highlighted above ). Actually, only BM should be listed.
I have then modified both the service yaml and added one more custom selector:
a. "tier:bm" in BM service.
b. "tier:engine" in Engine Service.
And here is the output for the service endpoint, Only BM is listing under the bm service endpoint.
Sometimes, i was getting the connection refused error while accessing the pods using the node port service. And after updating the service yaml's this issue has been resolved.
Below is the steps for creating the end-point URL :
We can get the endpoints by sending a GET request to the following URL.
The URL contains the following parts :
1. localhost:8000 : As, i have created the proxy (kubectl proxy --port=8000).
2. api/v1 : version
3. namespaces : keyword
4. {{namespace-name}} : Your namespace name, if you have created services under the default namespace, then mentioned it default.
5. endpoints : keyword.
6. {{service-name}} : In my case it is bm
URL : http://localhost:8000/api/v1/namespaces/myapps-fv92n/endpoints/bm
If you specify a common selectors for all the services, then it may occur that it will point to a multiple pods. And then it will be very difficult to identify the real cause. I was getting a connection-refused error. Sometimes, when i was accessing pod using a node port service.
In my case i was having two pods (named backend-manager and the other one was named engine).
And i have created two node port services for it (bm and engine).
And here are the service yamls :
1. Engine Service :
"engine": { //Engine /
"apiVersion":"v1",
"kind":"Service",
"metadata":{
"name":"engine",
"namespace":`${tennantId}-${salt}`,
"labels":{
"app":"backend",
"tier":"engine"
}
},
"spec":{
"type":"NodePort",
"selector":{
"app":"backend"
},
"ports":[{
"port":3000,
"targetPort": 3000
}]
}
}
2. BM SERVICE :
"bm": { //Backend Manager
"apiVersion":"v1",
"kind":"Service",
"metadata":{
"name":"bm",
"namespace":`${tennantId}-${salt}`,
"labels":{
"app":"backend",
"tier":"bm"
}
},
"spec":{
"type":"NodePort",
"selector":{
"app":"backend"
},
"ports":[{
"port":3001,
"targetPort": 3001
}]
}
}
As, you can see i have mistakenly mentioned the common selector in both the service.
And lets see, the endpoints for the service, using the below url.
http://localhost:8000/api/v1/namespaces/myapps-fv92n/endpoints/bm
And here is the output :
As, you can clearly see that different pods (both BM and Engine) are listing under the service endpoint (under subsets->addresses highlighted above ). Actually, only BM should be listed.
I have then modified both the service yaml and added one more custom selector:
a. "tier:bm" in BM service.
b. "tier:engine" in Engine Service.
And here is the output for the service endpoint, Only BM is listing under the bm service endpoint.
Sometimes, i was getting the connection refused error while accessing the pods using the node port service. And after updating the service yaml's this issue has been resolved.
Below is the steps for creating the end-point URL :
We can get the endpoints by sending a GET request to the following URL.
The URL contains the following parts :
1. localhost:8000 : As, i have created the proxy (kubectl proxy --port=8000).
2. api/v1 : version
3. namespaces : keyword
4. {{namespace-name}} : Your namespace name, if you have created services under the default namespace, then mentioned it default.
5. endpoints : keyword.
6. {{service-name}} : In my case it is bm
URL : http://localhost:8000/api/v1/namespaces/myapps-fv92n/endpoints/bm