Day 35: Mastering ConfigMaps and Secrets in Kubernetesπππ‘οΈ
#90daysofdevopschallenge
Table of contents
- π What are ConfigMaps and Secrets in k8s
- π Task -1 Create a ConfigMap for your Deployment
- π Update the deployment.yml file to include the ConfigMap
- π 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
- π Update the deployment.yml file to include the Secret
- π 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
π 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
Β