skip to Main Content

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


  1. 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

    kubectl exec -it <same-namespace-pod> /bin/bash
    # ping redis
    PING redis.<redis-namespace>.svc.cluster.local (172.20.211.84) 56(84) bytes of data.
    
    # telnet redis 6379
    Trying 172.20.211.84...
    Connected to redis.<redis-namespace>.svc.cluster.local.
    Escape character is '^]'.
    

    Different Namespace

    kubectl exec -it <different-namespace-pod> /bin/bash
    # ping redis.<redis-namespace>.svc.cluster.local
    PING redis.test.svc.cluster.local (172.20.211.84) 56(84) bytes of data.
    
    # telnet redis.<redis-namespace>.svc.cluster.local 6379
    Trying 172.20.211.84...
    Connected to redis.<redis-namespace>.svc.cluster.local.
    Escape character is '^]'.
    

    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 prefixes svc.cluster.local and cluster.local

    Login or Signup to reply.
  2. 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

    Keep in mind that you can use the Service name to access the backend Pods it exposes only within the same namespace. Looking at your Deployment and Service yaml manifests, we can see they’re deployed within myapp-ns namespace. It means that only from a Pod which is deployed within this namespace you can access your Service by using it’s name.

    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
    

    So if you deploy the following Pod:

    apiVersion: v1
    kind: Pod
    metadata:
      name: redis-client
      namespace: myapp-ns ### 👈
    spec:
      containers:
        - name: redis-client
          image: debian
    

    you will be able to access your Service by its name, so the following commands (provided you’ve installed all required tools) will work:

    redis-cli -h redis
    telnet redis 6379
    

    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:

    redis-cli -h redis.myapp-ns.svc.cluster.local
    telnet redis.myapp-ns.svc.cluster.local 6379
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search