我参考官方手册做了django的应用。
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")当我在本地开发时,可以连接到云SQL实例。但是,GKE资源似乎无法连接到云SQL实例。
我想我根据手册正确地对实例级访问和数据库访问进行了保密。
当我为实例级访问创建secret时,我输入以下命令。
kubectl create secret generic cloudsql-oauth-credentials --from-file=credentials.json=appname-0000-*******.json当我为数据库访问创建secret时,我输入以下命令。
kubectl create secret generic cloudsql --from-literal=username=user --from-literal=password=***我是在polls.yaml中编辑的。
在polls.yaml中,我像bellow一样编辑。
# [START proxy_container]
- image: gcr.io/cloudsql-docker/gce-proxy:1.05
name: cloudsql-proxy
command: ***在settings.py中,我编写了关于数据库的代码,比如bellow。
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的log。
kubectl描述pods投票-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描述pods投票-55456ddbbb-r919x
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投票-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>发布于 2020-03-28 17:33:41
经过一定处理后,即可连接到云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实例时,必须更改端口号。
https://stackoverflow.com/questions/60795905
复制相似问题