我在 GKE 中部署了 Django 應用程式。(完成本教程)
我的組態檔:myapp.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
labels:
app: myapp
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp-app
image: gcr.io/myproject/myapp
imagePullPolicy: IfNotPresent
---------
- image: gcr.io/cloudsql-docker/gce-proxy:1.16
name: cloudsql-proxy
command: ["/cloud_sql_proxy", "--dir=/cloudsql",
"-instances=myproject:europe-north1:myapp=tcp:3306",
"-credential_file=/secrets/cloudsql/credentials.json"]
apiVersion: v1
kind: Service
metadata:
name: myapp
labels:
app: myapp
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 8080
selector:
app: myapp
設定.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.environ['DATABASE_NAME'],
'USER': os.environ['DATABASE_USER'],
'PASSWORD': os.environ['DATABASE_PASSWORD'],
'HOST': '127.0.0.1',
'PORT': os.getenv('DATABASE_PORT', '3306'),
}
現在,當我進行滾動更新或通過
kubectl rollout restart deployment myapp
要么
kubectl apply -f myapp.yaml
kubectl get pods處于以下狀態:
NAME READY STATUS RESTARTS AGE
myapp-8477898cff-5wztr 2/2 Terminating 0 88s
myapp-8477898cff-ndt5b 2/2 Terminating 0 85s
myapp-8477898cff-qxzsh 2/2 Terminating 0 82s
myapp-97d6ccfc4-4qmpj 2/2 Running 0 6s
myapp-97d6ccfc4-vr6mb 2/2 Running 0 4s
myapp-97d6ccfc4-xw294 2/2 Running 0 7s
在推出期間,我在一段時間內收到以下錯誤:
OperationalError at /
(2003, "Can't connect to MySQL server on '127.0.0.1' (111)")
請告知我如何調整設定以在不停機/此錯誤的情況下推出
UPD
我通過查看日志發現發生這種情況是因為cloudsql-proxy在應用程式容器仍然存在時首先關閉。
應用程式日志:
Found 3 pods, using pod/myapp-f59c686b5-6t7c4
[2022-02-27 17:39:55 0000] [7] [INFO] Starting gunicorn 20.0.4
[2022-02-27 17:39:55 0000] [7] [INFO] Listening at: http://0.0.0.0:8080 (7)
[2022-02-27 17:39:55 0000] [7] [INFO] Using worker: sync
[2022-02-27 17:39:55 0000] [10] [INFO] Booting worker with pid: 10
Internal Server Error: /api/health/ # here cloudsql-proxy died
Internal Server Error: /api/health/
Internal Server Error: /api/health/
.... here more messages of Internal Server Error ...
rpc error: code = NotFound desc = an error occurred when try to find container "ec7658770c772eff6efb544a502fcd1841d7401add6efb2b53bf264b8eca1bb6": not founde
cloudsql-proxy的日志
2022/02/28 08:17:58 New connection for "myapp:europe-north1:myapp"
2022/02/28 08:17:58 Client closed local connection on 127.0.0.1:3306
2022/02/28 08:17:58 Client closed local connection on 127.0.0.1:3306
2022/02/28 08:17:59 Received TERM signal. Waiting up to 0s before terminating.
所以我想解決方案應該是關閉命令 - 在更新 pod 時關閉 cloudsql-proxy之前以某種方式關閉應用程式。
uj5u.com熱心網友回復:
看起來代理的 sidecar 正在終止,并且不允許您在應用程式執行之前進行清理。
考慮使用-term-timeout標志給自己一些時間:https ://github.com/GoogleCloudPlatform/cloudsql-proxy#-term_timeout30s
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/434279.html
