Day 35: Mastering ConfigMaps and Secrets in Kubernetes๐Ÿ”’๐Ÿ”‘๐Ÿ›ก๏ธ

Day 35: Mastering ConfigMaps and Secrets in Kubernetes๐Ÿ”’๐Ÿ”‘๐Ÿ›ก๏ธ

ยท

3 min read

๐Ÿ”ถ What are ConfigMaps and Secrets in K8s

In Kubernetes, ConfigMaps and Secrets are used to store configuration data and secrets, respectively. ConfigMaps store configuration data as key-value pairs, while Secrets store sensitive data in an encrypted form.

  • Example:- Imagine you're in charge of a big spaceship (Kubernetes cluster) with lots of different parts (containers) that need information to function properly. ConfigMaps are like a file cabinet where you store all the information each part needs in simple, labeled folders (key-value pairs). Secrets, on the other hand, are like a safe where you keep important, sensitive information that shouldn't be accessible to just anyone (encrypted data). So, using ConfigMaps and Secrets, you can ensure each part of your spaceship (Kubernetes cluster) has the information it needs to work properly and keep sensitive information secure!

  • Read more about ConfigMap & Secret.

  • Prerequisites Make instances of t2.medium.

          # First all update system and install Docker 
          sudo apt-get update
          sudo apt-get install docker.io -y
          sudo usermod -aG docker $USER && newgrp docker
    
          # To install Minikube & Kubectl
          curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
          sudo install minikube-linux-amd64 /usr/local/bin/minikube 
    
          sudo snap install kubectl --classic
          minikube start --driver=docker
    

๐Ÿ”ถ Task 1: Create a ConfigMap for your Deployment

  • Create a ConfigMap for your Deployment using a file or the command line

        apiVersion: v1
        kind: ConfigMap
        metadata:
          name: my-configmap
          labels:
            app: django-todo-app
          namespace: devops
        data:
          MYSQL_DB: "database_todo"
    

  • Update the deployment.yml file to include the ConfigMap.

  • Apply the updated deployment using the command: kubectl apply -f deployment.yml -n <namespace-name>

        apiVersion: apps/v1
         kind: Deployment
         metadata:
           name: mysql-configuration
           labels:
             app: mysql
           namespace: devops 
         spec:
           replicas: 3
           selector:
             matchLabels:
               app: mysql
           template:
             metadata:
               labels:
                 app: mysql
             spec:
               containers:
               - name: mysql-container
                 image: mysql:8
                 ports:
                 - containerPort: 3306
                 env:
                 - name: MYSQL_DATABASE
                   valueFrom:
                     configMapKeyRef:
                       name: my-configmap
                       key: MYSQL_DB
    
  • Verify that the ConfigMap has been created by checking the status of the ConfigMaps in your Namespace.

๐Ÿ”ถ Task 2: Create a Secret for your Deployment

  • First of we need to make a password

        echo -n 'cdac321' | base64
        #Y2RhYzMyMQ==
        echo -n 'Y2RhYzMyMQ==' | base64 --decode
        #cdac321
    

  •           apiVersion: v1
              kind: Secret
              metadata:   
                name: my-secret
                namespace: devops
              type: Opaque
              data:
                password: Y2RhYzMyMQ==
    
  • Create a Secret for your Deployment using a file or the command line

  •           kubectl apply -f secret.yml -n devops
              #secret/my-secret created
              kubectl get secrets -n devops
    

  • Update the deployment.yml file to include the Secret.

       apiVersion: apps/v1
       kind: Deployment
             metadata:
             name: mysql-configuration
             labels:
                 app: mysql
                 namespace: devops 
       spec:
               replicas: 3
               selector:
                 matchLabels:
                   app: mysql
               template:
                 metadata:
                   labels:
                     app: mysql
                 spec:
                   containers:
                   - name: mysql-container
                     image: mysql:8
                     ports:
                     - containerPort: 3306
                     env:
                     - name: MYSQL_ROOT_PASSWORD
                       valueFrom:
                         secretKeyRef:
                           name: my-secret
                           key: password
                     - name: MYSQL_DATABASE
                       valueFrom:
                         configMapKeyRef:
                           name: my-configmap
                           key: MYSQL_DB
    
  • Apply the updated deployment using the command: kubectl apply -f deployment.yml -n <namespace-name>

  • Verify that the Secret has been created by checking the status of the Secrets in your Namespace.

Need help with ConfigMaps and Secrets? Check out this video for assistance.

Keep learning and expanding your knowledge of Kubernetes

Happy Learning :)

If you find my blog valuable, I invite you to like, share, and join the discussion. Your feedback is immensely cherished as it fuels continuous improvement. Let's embark on this transformative DevOps adventure together! ๐Ÿš€ #devops #90daysofdevop #jenkins #k8s

ย