Kubernetes: Getting name resolution error

7/20/2020

I am deploying php and redis to a local minikube cluster but getting below error related to name resolution.

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

I am using below configurations files:

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

And I am using the below PHP code to access Redis, I have mounted below code into the apache-php deployment.

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

Cluster dashboard view for the services is given below:

img1 img2

Thanks in advance.

When I run env command getting below values related to redis and when I use the IP:10.104.115.148 to access redis then it is working fine.

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```
-- Saifullah khan
kubernetes
minikube

1 Answer

7/20/2020

Consider using K8S liveliness and readiness probes here, to automatically recover from errors. You can find more related information here.

And you can use an initContainer that check for availability of redis-server using bash while loop with break and then let php-apache to start. For more information, check Scenario 2 in here.


Redis Service as Cluster IP

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