将数据库公开以在GKE上进行部署 [英] Expose database to deployment on GKE

查看:98
本文介绍了将数据库公开以在GKE上进行部署的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a deployment running a pod that needs access to a postgres database I am running in the same cluster as the kubernetes cluster. How do I create a service that selects the deployment such that it has access. My pods keep restarting as the connection times out. I have created firewall rules in the vpc subnet to allow internal communication and have modified pg_hba.conf and postgresql.conf My deployment definition is given below:

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

这是我公开数据库的服务定义,但我认为我没有选择部署.我已在此处的示例中进行操作

This is my service definition to expose the database but I don't think I am selecting the deployment. I have followed the example 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

推荐答案

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

You can use the following to expose database to deployment on GKE:

$ kubectl expose deployment name-of-db --type=LoadBalancer --port 80 --target-port 8080

这篇关于将数据库公开以在GKE上进行部署的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆