'How can I connect a pod which runs a python program with another pod which runs a database

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.

PYTHON 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("The sorted array : ");
print(sorted_array);

I have created an image with the following Docker file :

FROM python:3
WORKDIR /sorting
COPY sorting.py ./
RUN apt-get update
RUN pip3 install numpy
RUN pip3 install redis
CMD python3 sorting.py

Also for the redis deployment and service I have the following yaml file :

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: "redis-server"
          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

and for the python programm deployment and service I have the following:

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

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

The sorted array :
[]

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?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source