Day 35 Task: Mastering ConfigMaps and Secrets in Kubernetes

Day 35 Task: Mastering ConfigMaps and Secrets in Kubernetes

Unlocking the Power of 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 'cdac123' | base64
      #Y2RhYzEyMw==
      echo -n 'Y2RhYzEyMw==' | base64 --decode
      #cdac123
    

  •   apiVersion: v1
      kind: Secret
      metadata:   
        name: my-secret
        namespace: devops
      type: Opaque
      data:
        password: Y2RhYzEyMw==
    

  • 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 :)

Stay in the loop with my latest insights and articles on cloud โ˜๏ธ and DevOps โ™พ๏ธ by following me on Hashnode, LinkedIn (https://www.linkedin.com/in/chandreshpatle28/), and GitHub (https://github.com/Chandreshpatle28).

Thank you for reading! Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.

#Git #Linux Devops #Devopscommunity #90daysofdevopschallenge #python #docker #Jenkins #Kubernetes

Did you find this article valuable?

Support Chandresh Patle's Blog by becoming a sponsor. Any amount is appreciated!

ย