将数据库公开以部署到GKE

时间:2018-08-24 14:27:50

标签: kubernetes google-kubernetes-engine

我有一个运行Pod的部署,该Pod需要访问与kubernetes集群相同的集群中运行的postgres数据库。如何创建选择部署的服务,使其具有访问权限。连接超时后,我的Pod会继续重新启动。我已经在vpc子网中创建了防火墙规则以允许内部通信,并修改了pg_hba.conf和postgresql.conf 我的部署定义如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: api
  labels: 
    name: server
    app: api
spec:
  replicas: 3
  selector:
    matchLabels:
      app: api
  template:
    metadata:
      labels:
        app: api
    spec:
      containers:
        - name: api
          image: gcr.io/api:v1
          ports:
            - containerPort: 80
          env:
            - name: DB_HOSTNAME
              valueFrom:
                secretKeyRef:
                  name: api-config
                  key: hostname
            - name: DB_USERNAME
              valueFrom:
                secretKeyRef:
                  name: api-config
                  key: username
            - name: DB_NAME
              valueFrom:
                secretKeyRef:
                  name: api-config
                  key: name
            - name: DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: api-config
                  key: password

这是我公开数据库的服务定义,但我认为我没有选择部署。我遵循了示例here

kind: Service
apiVersion: v1
metadata:
  name: postgres
  label:
spec:
  type: ClusterIP
  ports:
  - port: 5432
    targetPort: 5432
---
kind: Endpoints
apiVersion: v1
metadata:
  name: postgres
subsets:
  - addresses:
      - ip: 10.0.0.50
    ports:
      - port: 5432

1 个答案:

答案 0 :(得分:0)

您可以使用以下方法将数据库公开以在GKE上进行部署:

\xc3