web-dev-qa-db-fra.com

Ajouter nginx.conf au cluster Kubernetes

Quelles sont les meilleures pratiques pour passer le fichier de configuration à un NGINX à l'intérieur d'un cluster k8s?

18
xechelonx

Vous pouvez créer un objet ConfigMap puis monter les valeurs sous forme de fichiers là où vous en avez besoin:

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  nginx.conf: |
    your config
    comes here
    like this
  other.conf: |
    second file
    contents

Et dans votre pod spec:

spec:
  containers:
    - name: nginx
      image: nginx
      volumeMounts:
        - name: nginx-config
          mountPath: /etc/nginx/nginx.conf
          subPath: nginx.conf
        - name: nginx-config
          mountPath: /etc/nginx/other.conf
          subPath: other.conf
  volumes:
    - name: nginx-config
      configMap:
        name: nginx-config

(Prenez note de la duplication du nom de fichier dans mountPath et en utilisant exactement le même sous-chemin; identique à la liaison des fichiers de montage.)

Pour plus d'informations sur ConfigMap, voir: https://kubernetes.io/docs/user-guide/configmap/

33
Janos Lenart

Je n'ai pas trouvé un bon moyen d'échapper au contenu de la configuration nginx dans ConfigMap. Le meilleur recours pour moi était d'utiliser la création de ConfigMap à l'aide de fichiers

Enregistrez les éléments suivants sous ./data/nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

Créez maintenant le configMap kubectl create configmap confnginx --from-file=./data/nginx.conf

Enregistrez le déploiement et le pod yaml suivants sous nginx.yaml

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: nginx
  labels:
    app: nginx     
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1 # tells deployment to run 2 pods matching the template
  template: # create pods using pod definition in this template
    metadata:
      # unlike pod-nginx.yaml, the name is not included in the meta data as a unique name is
      # generated from the deployment name
      labels:
        app: nginx     
    spec:
      containers:
        - name: nginx
          image: nginx:Alpine
          ports:
          - containerPort: 80        
          volumeMounts:
            - name: nginx-config
              mountPath: /etc/nginx/nginx.conf
              subPath: nginx.conf
      volumes:
        - name: nginx-config
          configMap:
            name: confnginx

Maintenant, créez-le dans k8 kubectl apply -f nginx.yaml

6
ice.nicer