简体   繁体   English

Kubernetes + Django / PostgreSQL-将PostgreSQL数据库部署到Kubernetes集群时如何指定HOST

[英]Kubernetes + Django / PostgreSQL - How do I specify HOST of my PostgreSQL Database when I deploy it to Kubernetes Cluster

I am having a lot of issues configuring My Dockerized Django + PostgreSQL DB application to work on Kubernetes Cluster, which I have created using Google Cloud Platform. 我在配置Dockerized Django + PostgreSQL DB应用程序以使用我使用Google Cloud Platform创建的Kubernetes Cluster上工作时遇到很多问题。

How do I specify DATABASES.default.HOST from my settings.py file when I deploy image of PostgreSQL from Docker Hub and an image of my Django Web Application, to the Kubernetes Cluster? 当我从Docker Hub部署PostgreSQL映像和Django Web应用程序映像到Kubernetes集群时,如何从settings.py文件中指定DATABASES.default.HOST?

Here is how I want my app to work. 这就是我希望我的应用正常运行的方式。 When I run the application locally, I want to use SQLITE DB, in order to do that I have made following changes in my settings.py file: 在本地运行应用程序时,我想使用SQLITE DB,为此,我在settings.py文件中做了以下更改:

if(os.getenv('DB')==None):
    print('Development - Using "SQLITE3" Database')
    DATABASES = {
        'default':{
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR,'db.sqlite3'),
        }
    }
else:
    print('Production - Using "POSTGRESQL" Database')
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'agent_technologies_db',
            'USER': 'stefan_radonjic',
            'PASSWORD': 'cepajecar995',
            'HOST': ,  #???
            'PORT': ,  #???
            }
    }

The main idea is that when I deploy application to Kubernetes Cluster, inside of Kubernetes Pod object, a Docker container ( my Dockerized Django application ) will run. 主要思想是,当我将应用程序部署到Kubernetes Pod对象内部的Kubernetes Cluster时,将运行一个Docker容器(我的Dockerized Django应用程序)。 When creating a container I am also creating Environment Variable DB and setting it to True. 创建容器时,我还要创建环境变量DB并将其设置为True。 So when I deploy application I use PostgreSQL Database . 因此,当我部署应用程序时,我使用PostgreSQL数据库。

NOTE : If anyone has any other suggestions how I should separate Local from Production development, please leave a comment. 注意 :如果有人有其他建议我应如何将本地与生产开发区分开,请发表评论。

Here is how my Dockerfile looks like: 这是我的Dockerfile的样子:

FROM python:3.6

ENV PYTHONUNBUFFERED 1
RUN mkdir /agent-technologies
WORKDIR /agent-technologies
COPY . /agent-technologies 
RUN pip install -r requirements.txt

EXPOSE 8000

And here is how my docker-compose file looks like: 这是我的docker-compose文件的样子:

version: '3'
services:
  web:
    build: .
    command: python src/manage.py runserver --settings=agents.config.settings
    volumes: 
      - .:/agent-technologies
    ports: 
      - "8000:8000"
    environment:
      - DB=true

When running application locally it works perfectly fine. 在本地运行应用程序时,它运行良好。 But when I try to deploy it to Kubernetes cluster, Pod objects which run my application containers are crashing in an infinite loop, because I dont know how to specify the DATABASES.default.HOST when running app in production environment. 但是,当我尝试将其部署到Kubernetes集群时,运行我的应用程序容器的Pod对象会陷入无限循环,因为在生产环境中运行应用程序时,我不知道如何指定DATABASES.default.HOST。 And of course the command specified in docker-compose file ( command: python src/manage.py runserver --settings=agents.config.settings ) probably produces an exception and makes the Pods crash in infinite loop. 当然,在docker-compose文件中指定的命令( command: python src/manage.py runserver --settings=agents.config.settings )可能会产生异常,并使Pods无限循环崩溃。

NOTE: I have already created all necessary configuration files for Kubernetes ( Deployment definitions / Services / Secret / Volume files ). 注意:我已经为Kubernetes创建了所有必要的配置文件(Deployment definitions / Services / Secret / Volume files)。 Here is my github link: https://github.com/StefanCepa/agent-technologies-bachelor 这是我的github链接: https : //github.com/StefanCepa/agent-technologies-bachelor

Any help would be appreciated! 任何帮助,将不胜感激! Thank you all in advance! 谢谢大家!

You will have to create a service (cluster ip) for your postgres pod to make it "accessible". 您必须为postgres pod创建一个服务 (集群ip),以使其“可访问”。 When you create a service, you can access it via <service name>.default:<port> . 当你创建一个服务,您可以访问通过它<service name>.default:<port> However, running postgres (or any db) as a simple pod is dangerous (you will loose data as soon as you or k8s re-creates the pod or scale it up). 但是,将postgres(或任何数据库)作为简单的Pod运行是危险的(一旦您或k8s重新创建Pod或按比例放大,就会丢失数据)。 You can use a service or install it properly using statefulSets . 您可以使用服务或使用statefulSets正确安装它。

Once you have the address, you can put it in env variable and access it from your settings.py 获得地址后,可以将其放入env变量 ,然后从settings.py访问

EDIT : Put this in your deployment yaml ( example ): 编辑 :将其放在您的部署Yaml中( 示例 ):

env:
- name: POSTGRES_HOST
  value: "postgres-service.default"
- name: POSTGRES_PORT
  value: "5432"
- name: DB
  value: "DB"

And in your settings.py 并在您的settings.py中

'USER': 'stefan_radonjic',
'PASSWORD': 'cepajecar995',
'HOST': os.getenv('POSTGRES_HOST'),
'PORT': os.getenv('POSTGRES_PORT'),

Below are my findings: 以下是我的发现:

  1. The postgres instances was depending on a persistant volume. postgres实例取决于持久性卷。 I see the code for the persistent volueme claim, but not persistent volume itself. 我看到了持久性体积声明的代码,但没有持久性体积本身的代码。 So I had to create this first. 所以我必须先创建这个。
apiVersion: v1
kind: PersistentVolume
metadata:
  labels:
    type: local
  name: task-pv-volume
spec:
  accessModes:
  - ReadWriteMany
  capacity:
    storage: 1Gi
  hostPath:
    path: /tmp/data
  persistentVolumeReclaimPolicy: Retain
  1. There is a type in agent-technologies-bachelor/agents/config/kubernetes/postgres/secrets-definition.yml term password. agent-technologies-bachelor / agents / config / kubernetes / postgres / secrets-definition.yml术语密码中有一种类型。
data:
  user: c3RlZmFuX3JhZG9uamlj #stefan_radonjic
  passowrd: sdfsdfsd #cepajecar995

Because of this the postgres instance was not able to startup. 因此,postgres实例无法启动。 I found this by looking into the events by running command kubectl describe pods 我是通过运行命令kubectl describe pods来查看事件发现的

  1. The Docker image didn't have a command to execute the application in it. Docker映像中没有命令来执行应用程序。 As a result, If I ran your docker image at cepa995/agents_web it would simply exit and not run any application. 结果,如果我在cepa995 / agents_web上运行您的docker映像,它将直接退出而不运行任何应用程序。 This is why the django application was not running. 这就是django应用程序未运行的原因。 To fix this, I modified the Dockerfile to add a CMD instruction at the end. 为了解决这个问题,我修改了Dockerfile,在末尾添加了CMD指令。 I see you put this in the docker-compose to build the image, but this command has to be inside the Dockerfile itself. 我看到您将其放在docker-compose中以构建映像,但是此命令必须在Dockerfile本身内。 The Dockerfile looks like this now: Dockerfile现在看起来像这样:
FROM python:3.6
ENV PYTHONUNBUFFERED 1
RUN mkdir /agent-technologies
WORKDIR /agent-technologies
COPY . /agent-technologies
RUN pip install -r src/requirements.txt
EXPOSE 8000
CMD python src/manage.py runserver 0.0.0.0:8000 --settings=agents.config.settings

暂无
暂无

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

相关问题 数字海洋Kubernetes + Django / PostgreSQL在集群内部出现数据库错误 - Digital ocean Kubernetes + Django / PostgreSQL getting an database error inside cluster 使用Kubernetes部署PostgreSQL数据库 - Deploying PostgreSQL database with Kubernetes 使用PostgreSQL将Django应用程序部署到Kubernetes Google Cloud集群 - Deploying Django Application with PostgreSQL to Kubernetes Google Cloud cluster 尝试使用PostgreSQL DB部署Dockerized Django应用程序时,Kubernetes Pod对象在无限循环中崩溃 - Kubernetes Pod Object crashing in infinite loop when trying to deploy Dockerized Django application with PostgreSQL DB 如何将我的 Django SQLite 数据库内容放入 Heroku PostGreSQL 数据库? - How do I put my Django SQLite database contents into a Heroku PostGreSQL database? 如何使用 Django 和 postgresql 数据库中的字段填写 html 表单? - How do I fill a html form with fields from my database in Django and postgresql? 如何显示[PostgreSQL]数据库中[HTML]中[Django]模型的结果? - How do I show the results of my [Django] model in [HTML] from a [PostgreSQL] database? 如何在 docker 中为我的 postgresql 数据库创建持久卷? - How do I create a persistent volume in docker for my postgresql database? Django-将PostGIS数据库与PostgreSQL数据库一起使用,我需要2个数据库吗? - Django - Using PostGIS database with PostgreSQL database, do I need 2 databases? 如何修复 Django 中的 PostgreSQL 数据库连接问题? - What do I do to fix my PostgreSQL database connection problem in Django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM