<p>I am deploying php and redis to a local minikube cluster but getting below error related to name resolution.</p>
<pre class="lang-sh prettyprint-override"><code>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-&gt;ping() #1 {main} thrown in /app/redis.php on line 5
</code></pre>
<p>I am using below configurations files:</p>
<p>apache-php.yaml</p>
<pre class="lang-yaml prettyprint-override"><code>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
</code></pre>
<p>redis.yaml</p>
<pre class="lang-yaml prettyprint-override"><code>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
</code></pre>
<p>And I am using the below PHP code to access Redis, I have mounted below code into the apache-php deployment.</p>
<pre class="lang-php prettyprint-override"><code>&lt;?php
ini_set('display_errors', 1);
$redis = new Redis();
$redis-&gt;connect(&quot;redis-service&quot;, 6379);
echo &quot;Server is running: &quot;.$redis-&gt;ping();
</code></pre>
<p>Cluster dashboard view for the services is given below:</p>
<p><a href="https://i.stack.imgur.com/nBT5a.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nBT5a.png" alt="img1" /></a>
<a href="https://i.stack.imgur.com/dGdbT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dGdbT.png" alt="img2" /></a></p>
<p>Thanks in advance.</p>
<p>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.</p>
<pre><code>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```
</code></pre>

<p>Consider using K8S <code>liveliness</code> and <code>readiness</code> probes here, to automatically recover from errors. You can find more related information <a href="https://cloud.google.com/blog/products/gcp/kubernetes-best-practices-setting-up-health-checks-with-readiness-and-liveness-probes" rel="nofollow noreferrer">here</a>.</p>
<p>And you can use an <code>initContainer</code> that check for availability of redis-server using bash <code>while</code> loop with <code>break</code> and then let php-apache to start. For more information, check <strong>Scenario 2</strong> in <a href="https://www.magalix.com/blog/kubernetes-patterns-the-init-container-pattern" rel="nofollow noreferrer">here</a>.</p>
<hr />
<h3>Redis Service as Cluster IP</h3>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: redis-service
spec:
  type: clusterIP
  ports:
    - port: 6379
      targetPort: 6379
  selector:
    app: redis 
</code></pre>
