简体   繁体   English

如何将运行 python 程序的 pod 与另一个运行数据库的 pod 连接起来

[英]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 编写的简单程序,它与 redis 数据库交互,获取存储在我的数据库中的元素列表并对它们进行排序。

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 :我使用以下 Docker 文件创建了一个图像:

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 :同样对于 redis 部署和服务,我有以下 yaml 文件:

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:对于 python 程序部署和服务,我有以下内容:

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.我的 redis pod 似乎工作正常,但是当我尝试运行我的排序应用程序时,它会创建 pod,但状态是 CrashLoopBackOff。 I tried to show the logs and it shows the prints of my python program我试图显示日志,它显示了我的 python 程序的打印

The sorted array :
[]

So as I understand something is wrong with the connection between the app pod and the redis pod.因此,据我了解,app pod 和 redis pod 之间的连接有问题。 Any suggestion about what I am doing wrong?关于我做错了什么有什么建议吗?

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.你这样做是正确的,我在本地测试了你的代码,当你将错误的数据库主机名传递给你的 python 脚本时,它会失败,所以既然你有输出The sorted array : []这意味着与数据库的连接已正确制作。

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.但是,你要知道,通过在 Kubernetes(或 docker)中部署这种单执行脚本,容器会一次又一次地重启,因为它只运行一次就停止了。

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.因此,如果您不希望出现此错误,只需使您的脚本成为一个连续运行的应用程序或使用 kubernetes 作业,例如,如果您想在需要时手动运行它。

Another thing: Since redis is a stateful application, consider using a StatefulSet object in Kubernetes instead of a Deployment .另一件事:由于 redis 是一个有状态的应用程序,请考虑在 Kubernetes 中使用StatefulSet对象而不是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.一点建议:您应该在环境变量中的python代码中传递redis数据库的host配置,如果有一天您需要在其他地方执行容器,您只需修改环境变量而不是重建您的docker镜像会更好.

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未来的一个大问题:看看你的 Python Kubernetes 服务,它包含选择器: app: go-redis-app而不是app: sorting-app

Basically it's a python problem, not a Kubernetes database connection problem, so good job.基本上这是一个 python 问题,而不是 Kubernetes 数据库连接问题,干得好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM