简体   繁体   中英

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 ). With Docker compose I'd simply do the following...

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 . I came across a bit on ConfigMap , but was a little resistant when immediately met with the following...

Use the kubectl create configmap command to create configmaps from directories, files, or literal values

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. 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. You can create a configmap using a spec like the following:

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:

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.

A lot more details on configmaps here: https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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