<p>I have created a simple program written in Python which interacts with a redis database take a list of elements which is stored in my db and sort them.</p>
<p>PYTHON CODE :</p>
<pre><code>import redis
import numpy as np

r = redis.Redis(host='redis-master', port=6379, db=9, socket_connect_timeout=2, socket_timeout=2)

array = np.array([]);

vector = np.vectorize(int);

while(r.llen('Numbers')!=0):
    array = vector(np.append(array, r.lpop('Numbers').decode('utf8')))

sorted_array = np.sort(array);

print(&quot;The sorted array : &quot;);
print(sorted_array);
</code></pre>
<p>I have created an image with the following Docker file :</p>
<pre><code>FROM python:3
WORKDIR /sorting
COPY sorting.py ./
RUN apt-get update
RUN pip3 install numpy
RUN pip3 install redis
CMD python3 sorting.py
</code></pre>
<p>Also for the redis deployment and service I have the following yaml file :</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis-master
  labels:
    app: redis
spec:
  selector:
    matchLabels:
      app: redis
      role: master
      tier: backend
  replicas: 1
  template:
    metadata:
      labels:
        app: redis
        role: master
        tier: backend
    spec:
      containers:
      - name: master
        image: redis
        ports:
        - name: &quot;redis-server&quot;
          containerPort: 6379

---
apiVersion: v1
kind: Service
metadata: 
  name: redis-master
  labels:
    app: redis
    role: master
    tier: backend
spec:
  ports:
  - port: 6379
    targetPort: 6379
  selector:
    app: redis
    role: master
    tier: backend
</code></pre>
<p>and for the python programm deployment and service I have the following:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: sortingapp
  labels:
    app: sortingapp
spec:
  selector:
    matchLabels:
      app: sortingapp
  replicas: 1
  template:
    metadata:
      labels:
        app: sortingapp
    spec:
      containers:
      - name: sortingapp
        image: sorting-app:latest
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080

---
apiVersion: v1
kind: Service
metadata:
  name: sorting-app
spec:
  type: NodePort
  ports:
  - name: http
    port: 9090
    targetPort: 8080
  selector:
    app: go-redis-app
</code></pre>
<p>My redis pod seems to work properly, but when I try to run my sortingapp it creates the pod but the status is CrashLoopBackOff. I tried to show the logs and it shows the prints of my python program</p>
<pre><code>The sorted array :
[]
</code></pre>
<p>So as I understand something is wrong with the connection between the app pod and the redis pod.
Any suggestion about what I am doing wrong?</p>

Rodr
<p>You are doing it the right way, I tested your code locally and when you pass a wrong database host name to your python script, it fails, so since you have the output <code>The sorted array : []</code> That means that the connection to the database has been made properly.</p>
<p>However, you have to know that by deploying this kind of one-executing script in Kubernetes (or docker), the container will restart again and again since it only runs one time and stops.</p>
<p>So if you don't want this error to come out, just make your script an app that runs continuously OR use kubernetes job, for example, if you want to run it manually when needed.</p>
<p>Another thing: Since redis is a stateful application, consider using a <code>StatefulSet</code> object in Kubernetes instead of a <code>Deployment</code>. But since its not related to the problem, you can do it whenever you want.</p>
<p>A little advice: you should pass the <code>host</code> configuration of your redis database in your python code in the environment variable, it will be better if one day you need to execute the container elsewhere you would just modify the environment variable instead of rebuilding your docker image.</p>
<p>A big problem for the future: look at your Python Kubernetes service, it contains the selector: <code>app: go-redis-app</code> instead of <code>app: sorting-app</code></p>
<p>Basically it's a python problem, not a Kubernetes database connection problem, so good job.</p>
