How to use ENV vars in golang to pass DB connection values dynamically when deployed in kubernetes

huangapple go评论95阅读模式
英文:

How to use ENV vars in golang to pass DB connection values dynamically when deployed in kubernetes

问题

我目前正在开发一个使用golang编写的小型API,它将连接到一个SQL数据库。下面是我在代码中静态传递数据库连接字符串详细信息的片段(硬编码在代码中)。

const (
    DB_USER     = "username"
    DB_PASSWORD = "password"
    DB_NAME     = "db_name"
    DB_HOST     = "db_server_name"
    DB_PORT     = db_port
)

现在,我想使用环境变量使这些静态硬编码的值变得动态,这样任何人都可以使用环境变量在运行时设置这些详细信息。有人可以帮助我如何实现这一点吗?如何从代码中设置它们,以便可以在k8的部署中动态传递为环境变量?

我的当前deployment.yaml文件如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: go-demo-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: go-demo-app
  template:
    metadata:
      labels:
        app: go-demo-app
    spec:
      containers:
        - name: go-demo-app
          image: dockerhub/go-api:latest
          resources: {}
          ports:
            - containerPort: 8001

上述代码正常工作,因为数据库服务器名称在代码中是硬编码的,现在我希望通过代码动态地使用环境变量来传递它,这样我可以在k8的部署中引用它。

英文:

I am currently developing a small API in golang which will connect to a sql database. Below is the snip where I am passing the database connection string details statically ( hardcoded in the code ) .

const (
DB_USER     = "username"
DB_PASSWORD = "password"
DB_NAME     = "db_name"
DB_HOST     = "db_server_name"
DB_PORT     = db_port )

Now I want to use ENV variables to make this static hardcoded values to become dynamic, so anyone on the fly can set these details using ENV vars. Can someone help me how I can achieve this. How to set them in such way from code so it can be dynamically passed as ENV vars in k8's deployment ?

My current deployment.yaml file as below

apiVersion: apps/v1
kind: Deployment
metadata:
  name: go-demo-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: go-demo-app
  template:
    metadata:
      labels:
        app: go-demo-app
    spec:
      containers:
        - name: go-demo-app
          image: dockerhub/go-api:latest
          resources: {}
          ports:
            - containerPort: 8001

The above is working fine as the db server name is hardcoded in the code and now I can looking to pass it dynamically using ENV vars via code which I can refer in k8's deployment

答案1

得分: 2

将.yaml文件更改为

apiVersion: apps/v1
kind: Deployment
metadata:
  name: go-demo-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: go-demo-app
  template:
    metadata:
      labels:
        app: go-demo-app
    spec:
      containers:
        - name: go-demo-app
          image: dockerhub/go-api:latest
          resources: {}
          ports:
            - containerPort: 8001
          env:
            - name: DB_USER
              value: 用户名
            - name: DB_PASSWORD 
              value: 密码
            - name: DB_NAME     
              value: 数据库名称
            - name: DB_HOST     
              value: 数据库服务器名称
            - name: DB_PORT     
              value: "3306"

kubectl apply -f /path/to/file

在你的代码中,将初始化更改为

var (
	DB_USER     = os.Getenv("DB_USER")
	DB_PASSWORD = os.Getenv("DB_PASSWORD")
	DB_NAME     = os.Getenv("DB_NAME")
	DB_HOST     = os.Getenv("DB_HOST")
	DB_PORT     = os.Getenv("DB_PORT")
)

我将const更改为var,因为不允许使用const。

英文:

Change the .yaml to

apiVersion: apps/v1
kind: Deployment
metadata:
  name: go-demo-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: go-demo-app
  template:
    metadata:
      labels:
        app: go-demo-app
    spec:
      containers:
        - name: go-demo-app
          image: dockerhub/go-api:latest
          resources: {}
          ports:
            - containerPort: 8001
          env:
            - name: DB_USER
              value: username
            - name: DB_PASSWORD 
              value: password
            - name: DB_NAME     
              value: db_name
            - name: DB_HOST     
              value: db_server_name
            - name: DB_PORT     
              value: "3306"

kubectl apply -f /path/to/file

on your code, change the initialization into

var (
	DB_USER     = os.Getenv("DB_USER")
	DB_PASSWORD = os.Getenv("DB_PASSWORD")
	DB_NAME     = os.Getenv("DB_NAME")
	DB_HOST     = os.Getenv("DB_HOST")
	DB_PORT     = os.Getenv("DB_PORT")
)

I change const to var because it is not allowed

huangapple
  • 本文由 发表于 2022年1月16日 16:28:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/70728451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定