Formation DevOps | Formation kubernetes​ : ConfigMaps

www.itgalaxy.io

ConfigMaps

Les ConfigMaps sont des objets qui permettent de gérer des fichiers de configuration.

Une fois les fichiers importés dans Kubernetes, on peut les utiliser comme variable d’environnement ou comme volume (fichier monté).

Création d’une configMap en CLI :

mkdir mysql

echo "[mysqld]" > mysql/my.cnf

echo "[client]" > mysql/client.cnf

kubectl create configmap mysql --from-file=mysql

kubectl get configmap mysql

Création d’un ConfigMap via un fichier YAML :

kubectl create -f configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
data:
  special.how: very
  special.type: charm

kubectl create -f pod-configmap.yaml

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: registry.k8s.io/busybox
      command: [ "/bin/sh", "-c", "env" ]
      env:
        # Définie la variable d'environnement
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              # La ConfigMap contenant la valeur que vous voulez attribuer à SPECIAL_LEVEL_KEY
              name: special-config
              # Spécifier la clé associée à la valeur
              key: special.how
  restartPolicy: Never

Utilisation comme volume :

Créez le pod:

kubectl create -f pod-configmap-volume.yaml

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: registry.k8s.io/busybox
      command: [ "/bin/sh", "-c", "ls /etc/config/" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        # Indiquez le nom de la ConfigMap contenant les fichiers que vous souhaitez ajouter au conteneur
        name: special-config
  restartPolicy: Never

Lorsque le pod s’exécute, la commande ls /etc/config/ produit la sortie ci-dessous:

SPECIAL_LEVEL
SPECIAL_TYPE





1. Nous contactez


2. Infra as a Service

  • Description: Infrastructure cloud évolutive et sécurisée
  • Links:

3. Projets Développeurs


4. Développeurs


5. Formations Complètes


6. Marketplace

7. Blogs


This website is powered by ItGalaxy.io