简体   繁体   English

单节点 Kubernetes 集群 - 不同 pod 中的容器无法相互通信

[英]Single node Kubernetes cluster - Containers in different pods can't communicate with each other

I have a reccuring problem with container in different pods can't communicate with each other.我有一个反复出现的问题,不同 pod 中的容器无法相互通信。 To make things simple, I created a cluster with only 2 containers in different pods:为简单起见,我在不同的 pod 中创建了一个只有 2 个容器的集群:

  1. app that does only one thing: connecting to redis server.只做一件事的应用程序:连接到 redis 服务器。
  2. redis-server container redis 服务器容器

To make long story short: I'm keep getting 'connection refused' when trying to connect from the app to redis:长话短说:尝试从应用程序连接到 redis 时,我不断收到“连接被拒绝”:

$ kubectl logs app-deployment-86f848b46f-n7672

> app@1.0.0 start
> node ./app.js

LATEST
Error: connect ECONNREFUSED 10.104.95.63:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1133:16) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '10.104.95.63',
  port: 6379
}

the app identidfy the redis-service successfully but fails to connect该应用程序成功识别redis服务但无法连接

$ kubectl get services
NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
app-service     ClusterIP   10.107.18.112   <none>        4000/TCP   2m42s
kubernetes      ClusterIP   10.96.0.1       <none>        443/TCP    29h
redis-service   ClusterIP   10.104.95.63    <none>        6379/TCP   29h

the app code:应用程序代码:

const redis = require("redis");
const bluebird = require("bluebird");
bluebird.promisifyAll(redis);
console.log('LATEST');
const host = process.env.HOST;
const port = process.env.PORT;
const client = redis.createClient({ host, port });

client.on("error", function (error) {
    console.error(error);
}); 

app's docker file:应用程序的 docker 文件:

FROM node
WORKDIR "/app"
COPY ./package.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]

for the redis server I tried the default image of redis, and when it didn't work, I used a custome-made image without any bind to a specific ip and no protected-mode.对于 redis 服务器,我尝试了 redis 的默认图像,当它不起作用时,我使用了一个定制的图像,没有绑定到特定的 Z957B527BCFBAD2E80F58D20683931435。

redis dockerfile: redis dockerfile:

FROM redis:latest
COPY redis.conf /usr/local/etc/redis/redis.conf
CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]

Finally, I've created 2 deployments with respected ClusterIP services:最后,我使用受人尊敬的 ClusterIP 服务创建了 2 个部署:

app deployment:应用部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: app
  template:
    metadata:
      labels:
        component: app
    spec: 
      containers:
      - name: app
        image: user/redis-app:latest
        ports:
          - containerPort: 4000
        env:
          - name: HOST
            valueFrom:
              configMapKeyRef:
                name: app-env
                key: HOST
          - name: PORT
            valueFrom:
              configMapKeyRef:
                name: app-env
                key: PORT

app service:应用服务:

apiVersion: v1
kind: Service
metadata:
  name: app-service
spec:
  type: ClusterIP
  selector:
    component: app
  ports:
    - port: 4000
      targetPort: 4000

env file: .env 文件:

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-env
data:
   PORT: "6379"
   HOST: "redis-service.default"

redis deployment: redis部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      db: redis
  template:
    metadata:
      labels:
        db: redis
    spec: 
      containers:
        - name: redis
          image: user/custome-redis:latest
          ports:
            - containerPort: 6379

redis service: redis 服务:

apiVersion: v1
kind: Service
metadata:
  name: redis-service
spec:
  type: ClusterIP
  selector:
    component: redis
  ports:
  - protocol: TCP
    port: 6379
    targetPort: 6379

Originally, I used Windows enviorment with WSL2 and Kubernetes running over docker with Docker Desktop installed.最初,我使用 Windows 环境和 WSL2 和 Kubernetes 在 docker 上运行并安装 ZC5FD214CDD0D2B3B4B427 桌面。 when it failed, I've provisioned a centos8 vm over virtualbox and installed kubernets with minikube - got the same results..当它失败时,我在 virtualbox 上配置了一个 centos8 vm,并用 minikube 安装了 kubernets - 得到了相同的结果..

any ideas?....有任何想法吗?....

Posting an answer out of comments since David Maze found the issue (added as a community wiki, feel free to edit)自从 David Maze 发现问题后,在评论中发布答案(添加为社区 wiki,随时编辑)

It's very important to match labels between pods, deployments, services and other elements.在 pod、部署、服务和其他元素之间匹配标签非常重要。

In the example above, there are different labels used for redis service:在上面的示例中, redis服务使用了不同的标签:

component: redis and db: redis which caused this issue. component: redisdb: redis导致此问题。

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

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