visit
The main purpose of using a ConfigMap is to decouple the application configuration data from the application itself. This separation improves the reusability of the container as a decoupled architecture enables developers to use and test containerized applications under different configurations.
On the other hand, secrets provide the solution to decouple sensitive data from containers or pod configurations. This disconnect essentially prevents the need to include confidential data within the application code.
The primary difference between these two is that while ConfigMaps are designed to store any type of non-sensitive application data, Secrets are designed to store sensitive application data such as passwords, tokens, etc. Moreover, Secrets must adhere to the specific secret types that determine which kind of data can be handled by the secret.
Both these K8s objects can be created either via kubectl or a YAML file independently of any other process or object. Now, let’s look at creating YAML-based configuration files to host our database connectivity data and authentication information.
These ConfigMaps can also be created such as files, Envfiles, etc. In this example, we will create a simple YAML that contains the data.
apiVersion: v1
kind: ConfigMap
# Metadata of the ConfigMap
metadata:
name: app-database-config
namespace: default
# The Configuration Data
data:
server.host: "10.10.10.245"
server.port: "3660"
db.name: web-application
As secrets are defined by type, let’s create a basic-auth type Secret that contains the database username and password.
apiVersion: v1
kind: Secret
# Metadata of the Secret
metadata:
name: app-database-auth
namespace: default
# Secret Type
type: kubernetes.io/basic-auth
# Secret Data (This changes depending on the type)
stringData:
username: admin
password: admin
kubectl apply -f .\\db-configmap.yaml
kubectl apply -f .\\db-secret.yaml
Now you know how to create ConfigMaps and Secrets. Yet, how can you read these objects? We can simply use the kubectl describe command to view our objects.
Reading a ConfigMap
kubectl describe configmap app-database-config
kubectl describe secret app-database-auth
How to use ConfigMaps and Secrets in Kubernetes?
In this section, we will see how to use these K8s objects in our containerized applications.
Using ConfigMaps
apiVersion: v1
kind: Pod
metadata:
name: test-web-app
labels:
app: test-web-app
spec:
# Specify the Container Details
containers:
- name: ubuntu-app
image: ubuntu:latest
command: ["/bin/sleep", "3650d"]
# Setup the volumeMounts
volumeMounts:
- name: database-config
mountPath: /etc/config
resources:
requests:
memory: "250Mi"
cpu: "0.5"
limits:
memory: "1024Mi"
cpu: "1"
# Create a volume pointing to the ConfigMap
volumes:
- name: database-config
configMap:
name: app-database-config
restartPolicy: Always
apiVersion: v1
kind: Pod
metadata:
name: test-web-app
labels:
app: test-web-app
spec:
# Specify the Container Details
containers:
- name: ubuntu-app
image: ubuntu:latest
command: ["/bin/sleep", "3650d"]
# Setup the volumeMounts
volumeMounts:
- name: database-authentication
mountPath: /etc/authentication
resources:
requests:
memory: "250Mi"
cpu: "0.5"
limits:
memory: "1024Mi"
cpu: "1"
# Create a volume pointing to the Secret
volumes:
- name: database-authentication
secret:
secretName: app-database-auth
restartPolicy: Always
apiVersion: v1
kind: Pod
metadata:
name: test-web-app
labels:
app: test-web-app
spec:
containers:
- name: ubuntu-app
image: ubuntu:latest
command: ["/bin/sleep", "3650d"]
# Setup the volumeMounts
volumeMounts:
# ConfigMap Mount
- name: database-config
mountPath: /etc/config
# Secret Mount
- name: database-authentication
mountPath: /etc/authentication
resources:
requests:
memory: "250Mi"
cpu: "0.5"
limits:
memory: "1024Mi"
cpu: "1"
volumes:
# ConfigMap Volume
- name: database-config
configMap:
name: app-database-config
# Secret Volume
- name: database-authentication
secret:
secretName: app-database-auth
restartPolicy: Always
Asad Faizi
Founder CEO
CloudPlex.io, Inc