简体   繁体   English

Kubernetes-以声明方式包含配置文件的简便方法?

[英]Kubernetes - easy way to declaratively include a config file?

Trying to copy over a nginx conf file for my pod to use (lives at <project>/nginx.conf ). 尝试复制一个Nginx conf文件供我的pod使用(位于<project>/nginx.conf )。 With Docker compose I'd simply do the following... 使用Docker compose我可以简单地执行以下操作...

image: "nginx:alpine"
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/conf.d/default.conf

Best practice? 最佳实践? Unsure as this is new-ish to me. 不确定,因为这对我来说是新事物。 It did seemingly work well. 看起来效果不错。 I'd like to accomplish the same with a kubernetes deployment but am not finding a straightforward way to do this declaratively . 我想通过kubernetes部署实现相同的目的,但是没有找到一种直接的方式来声明性地做到这一点。 I came across a bit on ConfigMap , but was a little resistant when immediately met with the following... 我在ConfigMap上遇到了一些问题,但是当遇到以下问题时有点耐心 ...

Use the kubectl create configmap command to create configmaps from directories, files, or literal values 使用kubectl create configmap命令从目录,文件或文字值创建configmap

I don't want a lot of remember-these-commands-down-the-roads' scattered in text files and much prefer the file approach as shown with compose. 我不希望在文本文件中分散记住很多这些命令,而更喜欢使用compose所示的文件方法。 Is this achievable? 这可以实现吗? My spec so far looks as such... 到目前为止我的规格看起来像这样...

spec:
  containers:
  - name: nginx
    image: nginx:alpine
    ports:
    - containerPort: 80
      name: http

Help me the get config in here? 帮助我在这里获取配置?

ConfigMaps are absolutely the way to go. ConfigMap是绝对可行的方法。 You can create a configmap using a spec like the following: 您可以使用如下规格创建configmap:

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx
  namespace: default
data:
  nginx.conf: |-
     user www;
     worker_processes 5; 
     ...
     # all nginx.conf file contents goes here

Once you applied your configmap using kubectl, you can mount it in your container, by specifying the following in the PodSpec: 使用kubectl应用configmap后,可以通过在PodSpec中指定以下内容将其挂载到容器中:

containers:
- name: ...
  ...
  volumeMounts:
  - name: nginx-volume
    mountPath: <path where you want to put nginx.conf>
volumes:
- name: nginx-volume
  configMap:
    name: nginx

Please note that the directory you will choose as a mount point for your nginx.conf file will contain only that file (hiding all other files already present in that directory), exactly as it happens when mounting a device on a non empty directory in linux. 请注意,您将选择作为nginx.conf文件安装点的目录仅包含该文件(隐藏该目录中已经存在的所有其他文件),与在Linux中将设备安装在非空目录上时的情况完全相同。

A lot more details on configmaps here: https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/ 有关configmap的更多详细信息,请参见: https ://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM