我正在尝试将django应用程序迁移到Google kubernetes引擎,但无法正常工作。
应用程序的响应为:
异常值:
(1045,“拒绝用户'root'@'cloudsqlproxy〜[cloudsql实例ip]的访问(使用密码:否)”)
如果我将bash shell启动到django应用所在的运行容器中,则可以连接到云sql实例。
我在环境变量中拥有数据库凭据,但django似乎并不特别在乎用户名和密码。
容器内部的django连接设置:
from django.db import connection
connection.settings_dict
{'ENGINE': 'django.db.backends.mysql', 'NAME': 'aesh_db', 'USER': None, 'PASSWORD': None, 'HOST': '127.0.0.1', 'PORT': '3306', 'OPTIONS': {'charset': 'utf8mb4'}, 'TEST': {'CHARSET': 'utf8mb4', 'COLLATION': 'utf8mb4_unicode_ci', 'NAME': None, 'MIRROR': None}, 'ATOMIC_REQUESTS': False, 'AUTOCOMMIT': True, 'CONN_MAX_AGE': 0, 'TIME_ZONE': None}
在容器中正确设置了环境变量,这有效:
>> import _mysql
>> import os
>> conn = _mysql.connect(db=os.getenv('DB_NAME'), user=os.getenv('DB_USER'), passwd=os.getenv('DB_PASS'), host=os.getenv('DB_HOST'))
django数据库设置:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.getenv('DB_NAME'),
'USER': os.getenv('DB_USER'),
'PASSWORD': os.getenv('DB_PASS'),
'HOST': os.getenv('DB_HOST'),
'PORT': os.getenv('DB_PORT'),
'OPTIONS': {
# Tell MySQLdb to connect with 'utf8mb4' character set
'charset': 'utf8mb4',
},
'TEST': {
'CHARSET': 'utf8mb4',
'COLLATION': 'utf8mb4_unicode_ci',
}
}
}
deployment.yaml:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: aesh-web
labels:
app: aesh-web
spec:
replicas: 1
template:
metadata:
labels:
app: aesh-web
spec:
containers:
- name: aesh-web
image: gcr.io/[my-project]/aesh_web:1.0.0
ports:
- containerPort: 8000
env:
- name: LOAD_BALANCER_IP
value: [service ip]
- name: DB_HOST
value: 127.0.0.1
- name: DB_PORT
value: "3306"
- name: DB_NAME
value: aesh_db
- name: DB_USER
valueFrom:
secretKeyRef:
name: cloudsql-db-credentials
key: username
- name: DB_PASS
valueFrom:
secretKeyRef:
name: cloudsql-db-credentials
key: password
- name: cloudsql-proxy
image: gcr.io/cloudsql-docker/gce-proxy:1.11
command: ["/cloud_sql_proxy",
"-instances=[instance-connection-name]:aesh-web-db=tcp:3306",
"-credential_file=/secrets/cloudsql/credentials.json"]
volumeMounts:
- name: cloudsql-instance-credentials
mountPath: /secrets/cloudsql
readOnly: true
volumes:
- name: cloudsql-instance-credentials
secret:
secretName: cloudsql-instance-credentials
答案 0 :(得分:0)
mistery解决了,部署所基于的docker映像具有一个启动Web服务器的入口点脚本,在入口点脚本启动后设置了Deployment.yaml中指定的env变量,因此django不知道它们