简体   繁体   English

Kubernetes中按服务名访问redis

[英]Access redis by service name in Kubernetes

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我在kubernetes中创建了一个redis部署和服务,我可以通过服务ip从另一个pod访问redis,但是我不能通过服务名访问它

the redis yaml 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

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.我应用了您的文件,并且能够从同一命名空间和不同的命名空间 ping 和 telnet 到该服务。 To test this, I created pods in the same namespace and in a different namespace and installed telnet and ping.为了测试这一点,我在相同的命名空间和不同的命名空间中创建了 pod,并安装了 telnet 和 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如果由于 dns 解析问题而无法执行此操作,则可以查看 pod 中的/etc/resolv.conf以确保它具有搜索前缀svc.cluster.localcluster.local

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我在kubernetes中创建了一个redis部署和服务,我可以通过服务ip从另一个pod访问redis,但是我不能通过服务名访问它

Keep in mind that you can use the Service name to access the backend Pods it exposes only within the same namespace.请记住,您只能使用Service名称访问它在同一命名空间内公开的后端Pods Looking at your Deployment and Service yaml manifests, we can see they're deployed within myapp-ns namespace .查看您的DeploymentService yaml 清单,我们可以看到它们部署在myapp-ns命名空间中 It means that only from a Pod which is deployed within this namespace you can access your Service by using it's name.这意味着只有从部署在此命名空间中的Pod ,您才能使用它的名称访问您的Service

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 :因此,如果您部署以下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:您将能够通过其名称访问您的Service ,因此以下命令(前提是您已安装所有必需的工具)将起作用:

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-cliet Pod部署到完全不同的命名空间,您将需要使用根据此处描述的规则构建的完全限定域名 ( FQDN ):

redis-cli -h redis.myapp-ns.svc.cluster.local
telnet redis.myapp-ns.svc.cluster.local 6379

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM