简体   繁体   English

使用PostgreSQL将Django应用程序部署到Kubernetes Google Cloud集群

[英]Deploying Django Application with PostgreSQL to Kubernetes Google Cloud cluster

I am having trouble trying to deploy my Django Application and PostgreSQL database to Kubernetes Google Cloud cluster that I've already configured. 我在尝试将Django应用程序和PostgreSQL数据库部署到已经配置的Kubernetes Google Cloud集群时遇到麻烦。

I have successfully created Docker containers for my Django Application and PostgreSQL database. 我已经为Django应用程序和PostgreSQL数据库成功创建了Docker容器。 Here is what my docker-compose.yml file looks like: 这是我的docker-compose.yml文件的样子:

version: '3'

services:
  db:
    image: postgres
    environment:
      - POSTGRES_USER=stefan_radonjic
      - POSTGRES_PASSWORD=cepajecar995
      - POSTGRES_DB=agent_technologies_db
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000 --settings=agents.config.docker-settings
    volumes: 
      - .:/agent-technologies
    ports: 
      - "8000:8000"
    links:
      - db
    depends_on:
      - db

I have already build the images, and tried sudo docker-compose up command, and the application works perfectly fine. 我已经构建了映像,并尝试了sudo docker-compose up命令,该应用程序运行正常。

After successfully dockerizing Django Application and PostgreSQL, I have tried to configure Deployment / Service YML files required by Kubernetes, but I am having trouble doing so. 在成功地对Django Application和PostgreSQL进行docker化之后,我尝试配置Kubernetes所需的Deployment / Service YML文件,但是这样做很麻烦。 For example: 例如:

deployment-definition.yml - File for deploying Django application: deployment-definition.yml-用于部署Django应用程序的文件:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: agent-technologies-deployment
      labels:
        app: agent-technologies
        tier: backend
    spec:
      template:
        metadata:
          name: agent-technologies-pod
          labels:
            app: agent-technologies
            tier: backend
        spec:
          containers:
            - name: 
              image:
              ports:
                - containerPort: 8000
        replicas:
        selector:
          matchLabels:
            tier: backend

Inside container list of dictionaries, I know that my container name should be web , but I am not sure where the image of that container is located so I do not know what should i specify as container image. 在字典的容器列表中,我知道我的容器名称应该是web ,但是我不确定该容器的图像位于何处,所以我不知道我应该指定什么作为容器图像。

Another problem lies in postgres/deployment-definition.yml : 另一个问题在于postgres / deployment-definition.yml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres-container
  template:
    metadata:
      labels:
        app: postgres-container
        tier: backend
    spec:
      containers:
        - name: postgres-container
          image: postgres:9.6.6
          env:
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: postgres-credentials
                  key: user

            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: postgres-credentials
                  key: password

            - name: POSTGRES_DB
              value: agent_technologies_db
          ports:
            - containerPort: 5432
          volumeMounts:
            - name: postgres-volume-mount
              mountPath: /var/lib/postgresql/data

      volumes:
        - name: postgres-volume-mount
          persistentVolumeClaim:
            claimName: postgres-pvc

I do not understand what volumeMounts and volumes are for, and if i even specified them correctly. 我不明白什么是volumeMountsvolumes ,以及是否正确指定了它们。

Here is my secret-definition.yml file: 这是我的secret-definition.yml文件:

apiVersion: v1
kind: Secret
metadata:
  name: postgres-credentials
type: Opaque
data:
  user: stefan_radonjic
  passowrd: cepajecar995

My postgres/service-definition.yml file: 我的postgres / service-definition.yml文件:

apiVersion: v1
kind: Service
metadata:
  name: postgres-service
spec:
  selector:
    app: postgres-container
  ports:
    - protocol: TCP
      port: 5432
      targetPort: 5432

My postgres/volume-definition.yml file: 我的postgres / volume-definition.yml文件:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: postgres-pv
  labels:
    type: local
spec:
  capacity:
    storage: 2Gi
  storageClassName: standard
  accessModes:
    - ReadWriteMany
  hostPath:
    path: /data/postgres-pv

And my postgres/volume-claim-definitono.yml file: 还有我的postgres / volume-claim-definitono.yml文件:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: postgres-pv
  labels:
    type: local
spec:
  capacity:
    storage: 2Gi
  storageClassName: standard
  accessModes:
    - ReadWriteMany
  hostPath:
    path: /data/postgres-pv

Last but not least, my service-definition.yml file - for Django application 最后但并非最不重要的一点是,我的service-definition.yml文件-适用于Django应用程序

apiVersion: v1
kind: Service
metadata:
  name: agent-technologies-service
spec:
  selector:
    app: agent-technologies
  ports:
    - protocol: TCP
      port: 8000
      targetPort: 8000
  type: NodePort

Besides the questions I have already asked above, I also want to ask am I doing this right? 除了上面已经问过的问题之外,我还想问一下我在做这个对吗? If not, what can I do to fix this. 如果没有,我该怎么做才能解决此问题。

Inside container list of dictionaries, I know that my container name should be web, but I am not sure where the image of that container is located so I do not know what should i specify as container image. 在字典的容器列表中,我知道我的容器名称应该是web,但是我不确定该容器的图像位于何处,所以我不知道我应该指定什么作为容器图像。

  • Name for container is local to the pod (you can have several containers sharing same pod). 容器的名称是容器的本地名称(您可以有多个容器共享同一容器)。 Container name (web in your case) is for your files given under deployment: 容器名称(在您的情况下为Web)是针对在部署下给出的文件的:

     # setting name of first container within pod to web spec: containers: - name: web 
  • Image for container has to be in some available docker container registry. 容器的映像必须位于某些可用的Docker容器注册表中。 There are multiple options from hosting own docker registry to use publicly available ones. 托管自己的Docker注册表可以使用多个选项,以使用公开可用的选项。 In any case you have to be able to push in your build phase to that docker container registry (be it amazon ECR, Docker, Gitlab, self hosted...) and to pull from that registry from within kubernetes (security settings, pull secrets etc...). 无论如何,您都必须能够将构建阶段推送到该Docker容器注册表(亚马逊ECR,Docker,Gitlab,自托管...)并从kubernetes中从该注册表中提取信息(安全设置,提取秘密)等等...)。 In your docker-compose file you use two containers. 在docker-compose文件中,您使用两个容器。 For db you use public postgres image, and for web you use build command and image is stored to local docker registry on that host only (you have to push it to public repository for k8s to be able to pull from it during deployment). 对于db,您使用公共postgres映像;对于Web,您使用构建命令,并且映像仅存储到该主机上的本地docker注册表中(您必须将其推送到公共存储库中,以便k8s能够在部署期间从中取出)。

I do not understand what volumeMounts and volumes are for, and if i even specified them correctly. 我不明白什么是volumeMount和卷,以及是否正确指定了它们。

  • In a nutshell, volumes are for attaching volumes to containers. 简而言之,卷用于将卷附加到容器。 Depending on your use case and decided architecture there are several approaches to volumes, but all in all they boil down to ephemeral, constant and persistent. 根据您的用例和确定的体系结构,有几种卷处理方法,但是总而言之,它们可以归结为短暂的,持续的和持久的。 Ephemeral will be lost on container termination or restart, constant (such as from configMaps) are used for passing configuration files to containers and persistent are most interesting for stateful applications (databases among other things). 临时性将在容器终止或重新启动时丢失,常量(例如来自configMaps的常量)用于将配置文件传递到容器,而持久性对于有状态应用程序(数据库等)最为有趣。 You can specify volumes in several ways, all volume have to have name (to be referenced by volumeMount) and either direct volume specification or volume claim specification (latter is advised for persistent volume since you can benefit from automatic provisioning that way). 您可以通过几种方式指定卷,所有卷都必须具有名称(由volumeMount引用)以及直接卷规范或卷声明规范(建议使用持久卷,因为您可以从这种自动配置中受益)。
  • VolumeMounts are for defining where on container file system predefined volume should be mounted. VolumeMounts用于定义预定义卷应在容器文件系统上的何处安装。 They reference volume to be mounted by name, provide mount point on container filesystem by mountPath and can have subpaths to specific files in some cases. 它们按名称引用要挂载的卷,通过mountPath在容器文件系统上提供挂载点,并且在某些情况下可以具有特定文件的子路径。
  • In your example you tied persistent volume claim obtained volume to data path of postgres (/var/lib/postgresql/data). 在您的示例中,您将获得的永久卷声明绑定到postgres的数据路径(/ var / lib / postgresql / data)。 Althought you use storage class that you didn't specify, interesting part is that your Persistent volume is defined as localpath on host. 尽管您使用未指定的存储类,但有趣的是,您的Persistent卷被定义为主机上的localpath。 That means that on each node you have this database pod started you will end up pointing /var/lib/postgresql/data of that pod's db container to /data/postgres-pv on that specific node. 这意味着,在启动了该数据库容器的每个节点上,最终将指向该容器的db容器的/ var / lib / postgresql / data到该特定节点上的/ data / postgres-pv。 This opens up you to following issue: say you have 3 nodes (A, B and C) and your database pod is started on A, uses A's /data/postgres-pv folder as own /var/lib/postrgresql/data. 这使您面临以下问题:假设您有3个节点(A,B和C),并且数据库Pod在A上启动,使用A的/ data / postgres-pv文件夹作为自己的/ var / lib / postrgresql / data。 And then you restart it, it gets terminated and rescheduled to node B. All of the sudden, it uses B's /data/postgres-pv local folder (empty) and you end up with empty database. 然后重新启动它,它终止并重新安排到节点B。突然,它使用了B的/ data / postgres-pv本地文件夹(空),最后得到的是空数据库。 If you use host's local filesystem for persisntence you need to tie such pods with node (or better yet with affinity) selectors. 如果将主机的本地文件系统用于持久性,则需要将此类Pod与节点选择器(或更好的是具有亲和力)选择器绑定在一起。 It is advisable for performance reasons to run database volumes of local filesystem, but hose pods lose ability to be rescheduled easily. 出于性能考虑,建议运行本地文件系统的数据库卷,但是软管吊舱失去了易于重新安排的功能。 Another approach is to have some truly persistent volume that can be mounted independently of node (Amazon EBS for example) and they require different PVC (or provisioner to be used). 另一种方法是拥有一些真正持久的卷,这些卷可以独立于节点(例如Amazon EBS)安装,并且它们需要不同的PVC(或使用预配器)。

Besides the questions I have already asked above, I also want to ask am I doing this right? 除了上面已经问过的问题之外,我还想问一下我在做这个对吗? If not, what can I do to fix this. 如果没有,我该怎么做才能解决此问题。

  • As stated above, define storage class and either lock db pod to specific node or apply some kind of dynamic provisioning so volume will follow pod's placement on nods. 如上所述,定义存储类,或者将db pod锁定到特定节点,或者应用某种动态配置,以便卷将遵循pod在nod上的放置。
  • Oppiniated preference: don't place everything in default namespace, use separate namespace for handling k8s manifests, later on it is much harder to move everything, and harder to accidentally delete wrong thingie... 优先选择:不要将所有内容放置在默认名称空间中,使用单独的名称空间来处理k8s清单,稍后移动所有内容将更加困难,并且意外删除错误的东西也将更加困难...
  • Also personal preference: database is stateful application and as such it is advised to use statefulset instead of deployment. 还有个人喜好:数据库是有状态的应用程序,因此建议使用statefulset而不是部署。
  • There are tools to help you out when you start from docker-compose files and want to convert to kubernetes manifests, worth checking. 当您从docker-compose文件开始并且想要转换为kubernetes清单时,有一些工具可以帮助您,值得检查。
  • Documentation on kubernetes is a bit outdated but quite good and you can have some nice read on volumes and volumeClaims there, there is also active slack channel. kubernetes的文档有些过时了,但是还不错,您可以在其中阅读有关volume和volumeClaims的内容,还有一个活跃的松弛通道。
  • Oh, and mock user/pass when posting files here, we know now about cepa... 哦,在此处发布文件时模拟用户/密码,我们现在知道关于cepa ...
  • Lastly, you are doing great job! 最后,您做得很好!

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

相关问题 将Django应用程序重新部署到Google Kubernetes Engine(GKS) - Re-deploying a Django application to Google Kubernetes Engine (GKS) 将 Django 部署到谷歌云平台 - Deploying Django to Google cloud platform 使用Kubernetes部署PostgreSQL数据库 - Deploying PostgreSQL database with Kubernetes 数字海洋Kubernetes + Django / PostgreSQL在集群内部出现数据库错误 - Digital ocean Kubernetes + Django / PostgreSQL getting an database error inside cluster 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 将 django 部署到谷歌云引擎服务器错误 (500) - deploying django to google cloud engine Server Error (500) 在Google Cloud Platform上不允许使用Django,Kubernetes和负载均衡器的主机 - Disallowed host with Django, Kubernetes and a Load Balancer on Google Cloud Platform 如何通过Django框架连接到Google Cloud Postgresql? - How do you connect to Google Cloud Postgresql via Django framework? 在Openshift中部署Django应用程序 - Deploying a Django application in Openshift 部署Django应用程序时出现问题 - Issue in deploying django application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM