【问题标题】:Kubernetes: Getting name resolution errorKubernetes:获取名称解析错误
【发布时间】:2025-12-14 10:45:01
【问题描述】:

我正在将 php 和 redis 部署到本地 minikube 集群,但出现与名称解析相关的错误。

Warning: Redis::connect(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in /app/redis.php on line 4

Warning: Redis::connect(): connect() failed: php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in /app/redis.php on line 4

Fatal error: Uncaught RedisException: Redis server went away in /app/redis.php:5 Stack trace: #0 /app/redis.php(5): Redis->ping() #1 {main} thrown in /app/redis.php on line 5

我正在使用以下配置文件:

apache-php.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webserver
  labels:
    app: apache
spec:
  replicas: 1
  selector:
    matchLabels:
      app: apache
  template:
    metadata:
      labels:
        app: apache
    spec:
      containers:
      - name: php-apache
        image: webdevops/php-apache
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        volumeMounts:
        - name: app-code
          mountPath: /app
      volumes:
        - name: app-code
          hostPath:
            path: /minikubeMnt/src
---
apiVersion: v1
kind: Service
metadata:
  name: web-service
  labels:
    app: apache
spec:
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
  selector:
    app: apache

redis.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  labels:
    app: redis
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
        - name: redis
          image: redis:5.0.4
          imagePullPolicy: IfNotPresent
          ports:
             - containerPort: 6379
---
apiVersion: v1
kind: Service
metadata:
  name: redis-service
spec:
  type: NodePort
  ports:
    - port: 6379
      targetPort: 6379
  selector:
    app: redis

我正在使用下面的 PHP 代码访问 Redis,我已将下面的代码安装到 apache-php 部署中。

<?php
ini_set('display_errors', 1);
$redis = new Redis();
$redis->connect("redis-service", 6379);
echo "Server is running: ".$redis->ping();

服务的集群仪表板视图如下:

提前致谢。

当我运行 env 命令获取低于与 redis 相关的值时,当我使用 IP:10.104.115.148 访问 redis 时,它工作正常。

REDIS_SERVICE_PORT=tcp://10.104.115.148:6379
REDIS_SERVICE_PORT_6379_TCP=tcp://10.104.115.148:6379
REDIS_SERVICE_SERVICE_PORT=6379
REDIS_SERVICE_PORT_6379_TCP_ADDR=10.104.115.148
REDIS_SERVICE_PORT_6379_TCP_PROTO=tcp```

【问题讨论】:

  • 请确保 redis 服务器在您的网络服务器之前运行。我看到网络服务器部署比 redis 旧
  • 好像你是在 redis 之前启动你的应用程序并且它最初没有找到 redis
  • 当我 ssh 进入两个 pod 时,两者都在工作。当我 ssh 进入 php pod 并运行“php redis.php”时,也会出现名称解析错误。我确信 k8 没有按名称公开服务是一些问题。将上述配置部署到 digitalocean 集群后,甚至出现同样的错误。
  • 我没有在某些入口点或启动脚本中使用 redis。 redis 已打开,然后我使用 http 请求调用 php 文件。当我使用 cli php 运行文件时出现相同的错误。
  • 如果把服务器名换成IP,可以吗?

标签: kubernetes minikube


【解决方案1】:

考虑在此处使用 K8S livelinessreadiness 探针,以自动从错误中恢复。您可以找到更多相关信息here

您可以使用initContainer 来检查redis-server 的可用性,使用带有break 的bash while 循环,然后让php-apache 启动。如需更多信息,请查看here中的场景2


Redis 服务作为集群 IP

apiVersion: v1
kind: Service
metadata:
  name: redis-service
spec:
  type: clusterIP
  ports:
    - port: 6379
      targetPort: 6379
  selector:
    app: redis 

【讨论】:

  • anmol sharma 在上面添加了几个 cmets ^^。这可能会澄清问题。
  • @Saifullahkhan => Redis 通常用作后端服务,无需将其公开为 NodePort,请改用 clusterIP,请参阅上面答案中的部分。 => 如果将 redis 主机设置为 redis-service 不起作用,请尝试使用完整的 dns 名称,例如 redis-service.default.svc.cluster.local => 还可以使用命令 kubectl get svc --namespace=kube-system 检查 miniKube 中的 dns 服务是否正常工作。有关调试 DNS 的详细信息,请查看此 -kubernetes.io/docs/tasks/administer-cluster/…