英文:
For Kubernetes deployment in which file should I keep my environment variables, docker-compose.yml or *-deployment.yaml?
问题
為了Kubernetes部署,我應該將我的環境變數保存在哪個文件中,docker-compose.yml還是*-deployment.yaml中?
我讀到我們在docker-compose文件中傳遞環境變數(例如LOG_LEVEL=development npm run start),但我也在*-deployment.yaml中看到分配的環境變數值。
在docker-compose.yml中,我在“environment”標籤下看到環境變數,如下所示:
environment:
NODE_ENV: development
LOG_LEVEL: debug
而在*-deployment.yaml中,我在“env”標籤下看到相同的環境變數,如下所示:
env:
- name: NODE_ENV
value: 'production'
- name: LOG_LEVEL
value: 'info'
我想知道:
1)我是否需要在兩個文件中都分配環境變數的值?如果不需要
2)應該在哪個文件中編寫我的變數,哪種方法更好?
英文:
For Kubernetes deployment in which file should I keep my environment variables, docker-compose.yml or *-deployment.yaml?
I read that we pass environment variables (like LOG_LEVEL=development npm run start) in the docker-compose file but I also saw the environment variable values assigned in *-deployment.yaml as well.
In docker-compose.yml I see environment variables under the 'environment' tag
environment:
NODE_ENV: development
LOG_LEVEL: debug
while in *-deployment.yaml I see the same environment variables under the 'env' tag like
env:
- name: NODE_ENV
value: 'production'
- name: LOG_LEVEL
value: 'info'
I want to know
- Do I need to assign value to an environment variable in both files? and if not
- In which file I should write my variables and which approach is better?
答案1
得分: 2
我不太确定 docker-compose.yaml 与 Kubernetes 有什么关系,我认为 docker-compose
的 environment
部分是用于与本地设置中的 docker-compose 配合使用的,与应该与 Kubernetes 分开使用,Kubernetes 应该与部署一起使用。但是你可以在 Kubernetes 中使用 ConfigMap。ConfigMap 是用于以键值对形式存储非机密数据的 API 对象。Pods 可以将 ConfigMaps 作为环境变量、命令行参数或者卷中的配置文件来使用。
例如,postgres-config.yaml
文件用于配置 postgres 的配置:
apiVersion: v1
kind: ConfigMap
metadata:
name: postgres-config
labels:
app: postgres
data:
POSTGRES_DB: postgresdb
POSTGRES_USER: admin
POSTGRES_PASSWORD: psltest
然后你可以使用以下命令应用该文件:
kubectl apply -f postgres-config.yaml
然后在所有的 *-deployment.yaml 文件中可以使用 envFrom
来引用它:
envFrom:
- configMapRef:
name: postgres-config
有关 ConfigMap 的更多信息,请查看这里。
英文:
Am not really sure what docker-compose.yaml has to do with Kubernetes, I think the environment
of docker-compse` is to be used with docker-compose as a local setup, which is separated from Kubernetes which should be used with deployment
but you can use configMap with Kubernetes. A ConfigMap is an API object used to store non-confidential data in key-value pairs. Pods can consume ConfigMaps as environment variables, command-line arguments, or as configuration files in a volume
For example, postgres-config.yaml
file for postgres configuration
apiVersion: v1
kind: ConfigMap
metadata:
name: postgres-config
labels:
app: postgres
data:
POSTGRES_DB: postgresdb
POSTGRES_USER: admin
POSTGRES_PASSWORD: psltest
Then you can apply the file like
kubectl apply -f postgres-config.yaml
And this can be used in all the *-deployment.yaml using envFrom
envFrom:
- configMapRef:
name: postgres-config
For more information about ConfigMap Check here
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论