2017-04-13 56 views
1

我需要在運行我們的應用程序之前更新我的數據庫模式。對於基於this threadthis answer的我已經決定使用init容器來完成這項工作。Kubernetes:initContainer與gce-proxy?

由於我的SQL實例是託管的Google Cloud SQL實例,因此我需要gce-proxy才能連接到數據庫。我initContainers看起來是這樣的:

initContainers: 
    - name: cloudsql-proxy-init 
     image: gcr.io/cloudsql-docker/gce-proxy:1.09 
     command: ["/cloud_sql_proxy"] 
     args: 
     - --dir=/cloudsql 
     - -instances=xxxx:europe-west1:yyyyy=tcp:5432 
     - -credential_file=/secrets/cloudsql/credentials.json 
     volumeMounts: 
     - name: dev-db-instance-credentials 
      mountPath: /secrets/cloudsql 
      readOnly: true 
     - name: ssl-certs 
      mountPath: /etc/ssl/certs 
     - name: cloudsql 
      mountPath: /cloudsql 
    - name: liquibase 
     image: eu.gcr.io/xxxxx/liquibase:v1 
     imagePullPolicy: Always 
     command: ["./liquibase.sh"] 
     env: 
     - name: DB_TYPE 
      value: postgresql 
     - name: DB_URL 
      value: jdbc:postgresql://localhost/test 
     - name: DB_PASSWORD 
      valueFrom: 
      secretKeyRef: 
       name: db-credentials 
       key: password 
     - name: DB_USER 
      valueFrom: 
      secretKeyRef: 
       name: db-credentials 
       key: username 

但我的匣被卡住:

containers with incomplete status: [cloudsql-proxy-init liquibase]

如果我看莢描述:

Init Containers: 
    cloudsql-proxy-init: 
    Container ID: docker://0373fa6528ec3768d46a1c59ca45f12d9fc46d1f0d199b7eb3772545701e1b1d 
    Image:  gcr.io/cloudsql-docker/gce-proxy:1.09 
    Image ID:  docker://sha256:66c58ef63dbfe239ff95416d62635559498ebb395abb8a4b1edee78e48e05fe4 
    Port: 
    Command: 
     /cloud_sql_proxy 
    Args: 
     --dir=/cloudsql 
     -instances=xxxxx:europe-west1:yyyyyy=tcp:5432 
     -credential_file=/secrets/cloudsql/credentials.json 
    State:  Running 
     Started:  Thu, 13 Apr 2017 17:40:02 +0300 
    Ready:  False 
    Restart Count: 0 
    Mounts: 
     /cloudsql from cloudsql (rw) 
     /etc/ssl/certs from ssl-certs (rw) 
     /secrets/cloudsql from dev-db-instance-credentials (ro) 
     /var/run/secrets/kubernetes.io/serviceaccount from default-token-th58c (ro) 
liquibase: 
    Container ID: 
    Image:  eu.gcr.io/xxxxxx/liquibase:v1 
    Image ID: 
    Port: 
    Command: 
     ./liquibase.sh 
    State:  Waiting 
     Reason:  PodInitializing 
    Ready:  False 
    Restart Count: 0 
    Environment: 
     DB_TYPE:  postgresql 
     DB_URL:  jdbc:postgresql://localhost/test 
     DB_PASSWORD: <set to the key 'password' in secret 'db-credentials'> Optional: false 
     DB_USER:  <set to the key 'username' in secret 'db-credentials'> Optional: false 
    Mounts: 
     /var/run/secrets/kubernetes.io/serviceaccount from default-token-th58c (ro) 

它似乎雲SQL- proxy-init正在運行:

2017/04/13 14:40:02 using credential file for authentication; [email protected] 
2017/04/13 14:40:02 Listening on 127.0.0.1:5432 for xxxxx:europe-west1:yyyyy 
2017/04/13 14:40:02 Ready for new connections 

這可能是問題所在,因爲init容器應該退出以便初始化可以繼續?那麼我怎樣才能從liquibase連接到Google Cloud SQL實例?

回答

1

您希望init容器都像一個容器中的普通容器一樣運行。

但不幸的是,init容器會在前一個完成時一個接一個地啓動。見 https://kubernetes.io/docs/concepts/workloads/pods/init-containers/#understanding-init-containers

初始化容器是完全一樣定期集裝箱,除了:

  • 他們一直運行到結束。
  • 每個人都必須在下一個開始之前成功完成。

因此,您將無法與應用容器一起運行代理容器。

解決方案是構建一個容器,該容器中同時包含二進制文件,然後使用shell腳本來背景代理並運行應用程序以完成操作。

+0

咄,被跳躍以避免耦合... – gerasalus

+0

您還可以在運行過程中的另一盤上的代理,但我假設你有沒有做,在一個原因第一名 –

0

您正在使用需要運行完成的init容器。在查詢數據庫時,雲SQL代理需要始終運行。爲此,建議使用第二個容器,並將其作爲邊車容器放入您的容器中。

您可以在這裏找到一個例子:https://github.com/GoogleCloudPlatform/container-engine-samples/tree/master/cloudsql

+0

我不認爲你完全理解這個問題:)。我有這個設置爲我的常規容器。但是我需要在我的常規容器啓動之前更新數據庫。 – gerasalus