<p>Basically, i have a Deployment that creates 3 containers which scale automatically: PHP-FPM, NGINX and the container that contains the application, all set up with secrets, services and ingress. The application also share the project between PHP-FPM and NGINX, so it's all set up.</p>

<p>Since i want to explore more with K8s, i decided to create a pod with Redis that also mounts a persistent disk (but that's not important). I have also created a service for redis and all works perfectly fine if i SSH into the Redis container and run <code>redis-cli</code>.</p>

<p>The fun part is that the project can't connect to the pod on which Redis is on. I understand that the containers between pods share the same "local" network and they can be accessed using <code>localhost</code>.</p>

<p>How do i connect my project to the redis server that is running in other pod, that scales independently? What's wrong with the Redis service?</p>

<hr>

<p>My Redis service is this:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: redis-service
spec:
  ports:
  - port: 6379
    targetPort: 6379
  selector:
    app: redis
</code></pre>

<p>My Redis pod is powered by a deployment configuration file (i don't necessarily scale it, but i'll look forward into it):</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  labels:
    app: redis
spec:
  selector:
    matchLabels:
      app: redis
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: redis
    spec:
      volumes:
        - name: redis-persistent-volume
          persistentVolumeClaim:
            claimName: redis-pvc
      containers:
        - image: redis:4.0.11
          command: ['redis-server']
          name: redis
          imagePullPolicy: Always
          resources:
            limits:
              cpu: 250m
              memory: 512Mi
            requests:
              cpu: 250m
              memory: 512Mi
          ports:
            - containerPort: 6379
              name: redis
          volumeMounts:
            - name: redis-persistent-volume
              mountPath: /data
</code></pre>

<p>Also, when i tap into the <code>kubectl get service</code>, the Redis server has a Cluster IP:</p>

<pre><code>NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP   10.96.0.1       &lt;none&gt;        443/TCP        21h
nginx-service   NodePort    10.100.111.16   &lt;none&gt;        80:30312/TCP   21h
redis-service   ClusterIP   10.99.80.141    &lt;none&gt;        6379/TCP       6s
</code></pre>

<blockquote>
<p>How do I connect my project to the redis server that is running in other pod, that scales independently?</p>
</blockquote>
<p>You have three possible states here:</p>
<ul>
<li><p>To connect to Redis pod from <strong>within</strong> any other pod running <strong>in the same namespace</strong> as Redis pod is running. In this case you will use service name <code>redis-service</code> and designates service port <code>6379</code> to reach it over it's current ClusterIP (kube-dns is making DNS resolution for you there). I'm guessing that you are asking for this scenario.</p>
</li>
<li><p>Here is just an example of accessing one pod from within another pod (in your case). First run:</p>
<pre><code>     kubectl run -it --rm test --image=busybox --restart=Never -- sh
</code></pre>
<p>this will run new test pod and give you <code>sh</code> within that pod. Now if you type <code>nslookup redis-service</code> there (within test pod) you will check that DNS is working correctly between pods. You can also try to see if redis port is actually open with <code>nc -zv redis-service 6379</code>. If your kube-dns is working properly you should see that the port is opened.</p>
</li>
<li><p>To connect to Redis pod from <strong>within</strong> any other pod running in the same kubernetes cluster but <strong>in different namespace</strong>. In this case, you will use FQDN consisting of the service name and namespace name like it is given in <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/#namespaces-and-dns" rel="noreferrer">the documentation</a>.</p>
</li>
<li><p>To connect to Redis pod from <strong>outside</strong> of the kubernetes cluster. In this case, you will need some kind of ingress or nodePort of similar mechanism to expose redis service to outside world. More on this you can read in <a href="https://kubernetes.io/docs/tutorials/kubernetes-basics/expose/expose-intro/" rel="noreferrer">the official documentation</a>.</p>
</li>
</ul>
