ConfigMaps and Secrets in k8s

ConfigMaps and Secrets in k8s

Tasks 1:

  • Create a ConfigMap for your Deployment

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

  • Update the deployment.yml file to include the ConfigMap

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

  • 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

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

  • Update the deployment.yml file to include the Secret

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


Before we begin creating ConfigMap and Secret, create a namespace.

kubectl create namespace mysql-ns

Yes, you guessed right. We are going to create MySQL deployment and pass the sensitive information such as username and password.

Implementing the Task 1

Step 1: Create a YAML file for ConfigMap.

kind: ConfigMap
apiVersion: v1
metadata:
  name: mysql-config
  labels:
    app: mysql
  namespace: mysql-ns
data:
  MYSQL_DB: "todo-db"

Step 2: Apply the file.

kubectl apply -f configMap.yml

ConfigMap-created

Step 3: Verify

kubectl get ConfigMap -n mysql-ns

kubectl get

Step 4: Use data in deployment.

  • Follow Step 4 of Task 2.

Implementing the Task 2

Step 1: Create a YAML file for Secret.

apiVersion: v1
kind: Secret
metadata:
  name: mysql-secret
  namespace: mysql-ns
type: Opaque
data:
  password: dHJhaW53aXRoc2h1YmhhbQ==

Step 2: Apply the file.

kubectl apply -f secret.yml

Step 3: Verify

kubectl get secret -n mysql-ns

Secret get

Step 4: Add to the Deployment file.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
  labels:
    app: mysql
  namespace: mysql-ns
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_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: password
        - name: MYSQL_DATABASE
          valueFrom:
            configMapKeyRef:
              name: mysql-config
              key: MYSQL_DB

Step 5: Apply the file.

kubectl apply -f deployment.yml

Step 3: Verify

kubectl get pods -n mysql-ns

verify

You can check that configured deployment's Pod is working with the status of "Running".