【发布时间】: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