简体   繁体   English

我制作了 django 应用程序,但无法在 GKE 中编排

[英]I made django application but can't orchestration in GKE

I made django appilcation in reference to official manual.我参考官方手册做了 django 应用程序。

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

After pushing the Docker image, I got bellow message in EXTERNAL-IP page.推送 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")

When I develop in local, I can connect to Cloud SQL instance.当我在本地开发时,我可以连接到 Cloud SQL 实例。 But, it seems that GKE resource can't connect to Cloud SQL instance.但是,似乎 GKE 资源无法连接到 Cloud SQL 实例。

I think I made secret for instance-level access and database access correctly according to manual.我想我根据手册正确地为实例级访问和数据库访问保密。

When I create secret for instance-level access, I type bellow command.当我为实例级访问创建密钥时,我输入了 bellow 命令。

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

When I create secret for database access, I type bellow command.当我为数据库访问创建秘密时,我输入了下面的命令。

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

I edited in polls.yaml.我在 polls.yaml 中进行了编辑。

In polls.yaml, I edited like bellow.在 polls.yaml 中,我进行了如下编辑。

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

In settings.py, I coded about DATABASES like bellow.在 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',
    }
}

What can cause this error?什么会导致此错误? Please let me know the solution!请告诉我解决方案!

When I enter当我进入

ss -ntlp | grep 5432

I get bellow.我得到了吼叫。

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                     *:*

Unfortunately it is not helpful.不幸的是,它没有帮助。

I output pod's log.我输出pod的日志。

kubectl describe pods polls-55456ddbbb-6vg8g 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 describe pods polls-55456ddbbb-r9l9x kubectl 描述 pods 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 describe pods polls-55456ddbbb-wz4ml 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>

I can connect to Cloud SQL instance after some process.经过一些过程,我可以连接到 Cloud SQL 实例。

1.I changed setting.py like below. 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',
    }
}

And I changed polls.yaml like below.我改变了 polls.yaml 如下。

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

Port number was changed from 5432 to 3306. When I use mysql instance, I must change port number.端口号从 5432 改为 3306。当我使用 mysql 实例时,我必须更改端口号。

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

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