I created a redis deployment and service in kubernetes,
I can access redis from another pod by service ip, but I can’t access it by service name
the redis yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-deployment
namespace: myapp-ns
spec:
replicas: 1
selector:
matchLabels:
component: redis
template:
metadata:
labels:
component: redis
spec:
containers:
- name: redis
image: redis
ports:
- containerPort: 6379
---
apiVersion: v1
kind: Service
metadata:
name: redis
namespace: myapp-ns
spec:
type: ClusterIP
selector:
component: redis
ports:
- port: 6379
targetPort: 6379
2
Answers
I applied your file, and I am able to ping and telnet to the service both from within the same namespace and from a different namespace. To test this, I created pods in the same namespace and in a different namespace and installed telnet and ping. Then I exec’ed into them and did the below tests:
Same Namespace
Different Namespace
If you are not able to do that due to dns resolution issues, you could look at your
/etc/resolv.conf
in your pod to make sure it has the search prefixessvc.cluster.local
andcluster.local
Keep in mind that you can use the
Service
name to access the backendPods
it exposes only within the same namespace. Looking at yourDeployment
andService
yaml manifests, we can see they’re deployed withinmyapp-ns
namespace. It means that only from aPod
which is deployed within this namespace you can access yourService
by using it’s name.So if you deploy the following
Pod
:you will be able to access your
Service
by its name, so the following commands (provided you’ve installed all required tools) will work:However if your redis-cliet
Pod
is deployed to completely different namespace, you will need to use fully qualified domain name (FQDN) which is build according to the rule described here: