首页 > 解决方案 > 我制作了 django 应用程序,但无法在 GKE 中编排

问题描述

我参考官方手册制作了 django appilcation。

https://cloud.google.com/python/django/kubernetes-engine?hl=ja

推送 Docker 映像后,我在 EXTERNAL-IP 页面中收到以下消息。

Django Version: 2.2.5
Exception Type: OperationalError
Exception Value:     (2013, "Lost connection to MySQL server at 'reading initial communication packet', system error: 0")

当我在本地开发时,我可以连接到 Cloud SQL 实例。但是,似乎 GKE 资源无法连接到 Cloud SQL 实例。

我想我根据手册正确地对实例级访问和数据库访问保密。

当我为实例级访问创建密钥时,我键入以下命令。

kubectl create secret generic cloudsql-oauth-credentials --from-file=credentials.json=appname-0000-*******.json

当我为数据库访问创建密钥时,我输入以下命令。

kubectl create secret generic cloudsql --from-literal=username=user --from-literal=password=***

我在 polls.yaml 中编辑。

在 polls.yaml 中,我编辑如下。

  # [START proxy_container]
  - image: gcr.io/cloudsql-docker/gce-proxy:1.05
    name: cloudsql-proxy
    command: ***

在 settings.py 中,我编写了关于 DATABASES 的代码,如下所示。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'polls',
        'USER': os.getenv('DATABASE_USER'),
        'PASSWORD': os.getenv('DATABASE_PASSWORD'),
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

什么会导致此错误?请告诉我解决方案!

当我进入

ss -ntlp | grep 5432

我得到咆哮。

root@polls-55456ddbbb-6vg8g:/home/vmagent/app# ss -ntlp | grep 5432
LISTEN     0      1024   127.0.0.1:5432                     *:*

root@polls-55456ddbbb-r9l9x:/home/vmagent/app# ss -ntlp | grep 5432
LISTEN     0      1024   127.0.0.1:5432                     *:*

root@polls-55456ddbbb-wz4ml:/home/vmagent/app# ss -ntlp | grep 5432
LISTEN     0      1024   127.0.0.1:5432                     *:*

不幸的是,它没有帮助。

我输出 pod 的日志。

kubectl 描述 pod polls-55456ddbbb-6vg8g

Name:           polls-55456ddbbb-6vg8g
Namespace:      default
Priority:       0
Node:           gke-polls-default-pool-2c3ca2d7-9m9d/10.146.0.29
Start Time:     Mon, 23 Mar 2020 18:11:03 +0900
Labels:         app=polls
                pod-template-hash=55456ddbbb
Annotations:    kubernetes.io/limit-ranger: LimitRanger plugin set: cpu request for container polls-app; cpu request for container cloudsql-proxy
Status:         Running
IP:             10.60.3.4
Controlled By:  ReplicaSet/polls-55456ddbbb
Containers:
  polls-app:
    Container ID:   ***
    Image:          ***
    Image ID:       ***
    Port:           8080/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Mon, 23 Mar 2020 18:12:01 +0900
    Ready:          True
    Restart Count:  0
    Requests:
      cpu:  100m
    Environment:
      DATABASE_USER:      <set to the key 'username' in secret 'cloudsql'>  Optional: false
      DATABASE_PASSWORD:  <set to the key 'password' in secret 'cloudsql'>  Optional: false
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-vkv88 (ro)
  cloudsql-proxy:
    Container ID:  docker://d5458d325b071937d12c0cefe2daaf31630d4ab9d565bc3888686cb53127b3e6
    Image:         gcr.io/cloudsql-docker/gce-proxy:1.05
    Image ID:      docker-pullable://gcr.io/cloudsql-docker/gce-proxy@sha256:af9140a11b2593d4be77323f25c0c2abb136133d2bbef76204e7bdad82f05ee7
    Port:          <none>
    Host Port:     <none>
    Command:
      ***
    State:          Running
      Started:      Mon, 23 Mar 2020 18:12:03 +0900
    Ready:          True
    Restart Count:  0
    Requests:
      cpu:        100m
    Environment:  <none>
    Mounts:
      /cloudsql from cloudsql (rw)
      /etc/ssl/certs from ssl-certs (rw)
      /secrets/cloudsql from cloudsql-oauth-credentials (ro)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-vkv88 (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  cloudsql-oauth-credentials:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  cloudsql-oauth-credentials
    Optional:    false
  ssl-certs:
    Type:          HostPath (bare host directory volume)
    Path:          /etc/ssl/certs
    HostPathType:
  cloudsql:
    Type:       EmptyDir (a temporary directory that shares a pod's lifetime)
    Medium:
    SizeLimit:  <unset>
  default-token-vkv88:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-vkv88
    Optional:    false
QoS Class:       Burstable
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:          <none>

kubectl 描述 pod polls-55456ddbbb-r9l9x

Name:           polls-55456ddbbb-r9l9x
Namespace:      default
Priority:       0
Node:           gke-polls-default-pool-2c3ca2d7-9m9d/10.146.0.29
Start Time:     Mon, 23 Mar 2020 18:11:03 +0900
Labels:         app=polls
                pod-template-hash=55456ddbbb
Annotations:    kubernetes.io/limit-ranger: LimitRanger plugin set: cpu request for container polls-app; cpu request for container cloudsql-proxy
Status:         Running
IP:             10.60.3.5
Controlled By:  ReplicaSet/polls-55456ddbbb
Containers:
  polls-app:
    Container ID:   ***
    Image:          ***
    Image ID:       ***
    Port:           8080/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Mon, 23 Mar 2020 18:12:01 +0900
    Ready:          True
    Restart Count:  0
    Requests:
      cpu:  100m
    Environment:
      DATABASE_USER:      <set to the key 'username' in secret 'cloudsql'>  Optional: false
      DATABASE_PASSWORD:  <set to the key 'password' in secret 'cloudsql'>  Optional: false
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-vkv88 (ro)
  cloudsql-proxy:
    Container ID:  ***
    Image:         ***
    Image ID:      ***
    Port:          <none>
    Host Port:     <none>
    Command:
      ***
    State:          Running
      Started:      Mon, 23 Mar 2020 18:12:04 +0900
    Ready:          True
    Restart Count:  0
    Requests:
      cpu:        100m
    Environment:  <none>
    Mounts:
      /cloudsql from cloudsql (rw)
      /etc/ssl/certs from ssl-certs (rw)
      /secrets/cloudsql from cloudsql-oauth-credentials (ro)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-vkv88 (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  cloudsql-oauth-credentials:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  cloudsql-oauth-credentials
    Optional:    false
  ssl-certs:
    Type:          HostPath (bare host directory volume)
    Path:          /etc/ssl/certs
    HostPathType:
  cloudsql:
    Type:       EmptyDir (a temporary directory that shares a pod's lifetime)
    Medium:
    SizeLimit:  <unset>
  default-token-vkv88:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-vkv88
    Optional:    false
QoS Class:       Burstable
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:          <none>

kubectl 描述 pods polls-55456ddbbb-wz4ml

Name:           polls-55456ddbbb-wz4ml
Namespace:      default
Priority:       0
Node:           gke-polls-default-pool-2c3ca2d7-ztcd/10.146.0.27
Start Time:     Mon, 23 Mar 2020 18:11:03 +0900
Labels:         app=polls
                pod-template-hash=55456ddbbb
Annotations:    kubernetes.io/limit-ranger: LimitRanger plugin set: cpu request for container polls-app; cpu request for container cloudsql-proxy
Status:         Running
IP:             10.60.1.3
Controlled By:  ReplicaSet/polls-55456ddbbb
Containers:
  polls-app:
    Container ID:   ***
    Image:          ***
    Image ID:       ***
    Port:           8080/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Mon, 23 Mar 2020 18:11:59 +0900
    Ready:          True
    Restart Count:  0
    Requests:
      cpu:  100m
    Environment:
      DATABASE_USER:      <set to the key 'username' in secret 'cloudsql'>  Optional: false
      DATABASE_PASSWORD:  <set to the key 'password' in secret 'cloudsql'>  Optional: false
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-vkv88 (ro)
  cloudsql-proxy:
    Container ID:  docker://375a66aafccc2cab413b9eaf9b3bf18a68234c353c9e33082cc879d2edd7639f
    Image:         gcr.io/cloudsql-docker/gce-proxy:1.05
    Image ID:      docker-pullable://gcr.io/cloudsql-docker/gce-proxy@sha256:af9140a11b2593d4be77323f25c0c2abb136133d2bbef76204e7bdad82f05ee7
    Port:          <none>
    Host Port:     <none>
    Command:
      ***
    State:          Running
      Started:      Mon, 23 Mar 2020 18:12:03 +0900
    Ready:          True
    Restart Count:  0
    Requests:
      cpu:        100m
    Environment:  <none>
    Mounts:
      /cloudsql from cloudsql (rw)
      /etc/ssl/certs from ssl-certs (rw)
      /secrets/cloudsql from cloudsql-oauth-credentials (ro)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-vkv88 (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  cloudsql-oauth-credentials:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  cloudsql-oauth-credentials
    Optional:    false
  ssl-certs:
    Type:          HostPath (bare host directory volume)
    Path:          /etc/ssl/certs
    HostPathType:
  cloudsql:
    Type:       EmptyDir (a temporary directory that shares a pod's lifetime)
    Medium:
    SizeLimit:  <unset>
  default-token-vkv88:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-vkv88
    Optional:    false
QoS Class:       Burstable
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:          <none>

标签: pythonmysqldjangodeploymentgoogle-kubernetes-engine

解决方案


经过一些处理后,我可以连接到 Cloud SQL 实例。

1.我改变了setting.py,如下所示。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'polls',
        'USER': os.getenv('DATABASE_USER'),
        'PASSWORD': os.getenv('DATABASE_PASSWORD'),
        'HOST': '127.0.0.1',
        'PORT': '3306',
    }
}

我改变了 polls.yaml 如下所示。

  - image: gcr.io/cloudsql-docker/gce-proxy:1.16
    name: cloudsql-proxy
    command: ***:mysqlinstance=tcp:3306",***

端口号从 5432 改为 3306。当我使用 mysql 实例时,我必须更改端口号。


推荐阅读