Home > front end >  How can I connect a pod which runs a python program with another pod which runs a database
How can I connect a pod which runs a python program with another pod which runs a database

Time:05-26

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?

CodePudding user response:

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 The sorted array : [] That means that the connection to the database has been made properly.

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.

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.

Another thing: Since redis is a stateful application, consider using a StatefulSet object in Kubernetes instead of a Deployment. But since its not related to the problem, you can do it whenever you want.

A little advice: you should pass the host 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.

A big problem for the future: look at your Python Kubernetes service, it contains the selector: app: go-redis-app instead of app: sorting-app

Basically it's a python problem, not a Kubernetes database connection problem, so good job.

  • Related