Day 35: Mastering ConfigMaps and Secrets in KubernetesπŸ”’πŸ”‘πŸ›‘οΈ

Day 35: Mastering ConfigMaps and Secrets in KubernetesπŸ”’πŸ”‘πŸ›‘οΈ

#90daysofdevopschallenge

Β·

2 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.

πŸ“Œ 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: mysql-config
  namespace: deployment
  labels:
    app: mysql
data:
  MYSQL_DATABASE: "mydb"

πŸ“Œ Update the deployment.yml file to include the ConfigMap

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-deployment
  namespace: deployment
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: mysql:8
          ports:
            - containerPort: 3306
          env:
            - name: MYSQL_DATABASE
              valueFrom:
                configMapKeyRef:
                  name: mysql-config
                  key: MYSQL_DATABASE

πŸ“Œ Apply the updated deployment using the command: kubectl apply -f deployment.yml -n

πŸ“Œ 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

apiVersion: v1
kind: Secret
metadata:
  name: my-secret
  namespace: deployment
  labels: 
    app: mysql
type: Opaque
data:
  MYSQL_PASSWORD: "cm9vdAo="

In the above key pair the value of MYSQL_PASSWORD is the encoded password so below is the command to encode or decode the password to create the secret

echo <password> | base64      ## To encode the password
echo <encoded_password> | base64 -d  ## To decode the password

πŸ“Œ Update the deployment.yml file to include the Secret

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-deployment
  namespace: deployment
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: mysql:8
          ports:
            - containerPort: 3306
          env:
            - name: MYSQL_DATABASE
              valueFrom:
                configMapKeyRef:
                  name: mysql-config
                  key: MYSQL_DATABASE
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: my-secret
                  key: MYSQL_PASSWORD

πŸ“Œ Apply the updated deployment using the command: kubectl apply -f deployment.yml -n

πŸ“Œ Verify that the Secret has been created by checking the status of the Secrets in your Namespace.

Thank You

Β