How to Create ConfigMap from Properties File Using K8s Client


How to Create ConfigMap from Properties File Using K8s Client

In the dynamic world of Kubernetes (K8s), efficient configuration management is crucial for maintaining the flexibility and scalability of applications. ConfigMaps play a pivotal role in this regard, allowing you to decouple configuration artifacts from the application code. In this tutorial, we will explore the process of creating a ConfigMap from a properties file using the Kubernetes Client.

Prerequisites:

Before diving into the tutorial, ensure that you have the following prerequisites:

  1. A running Kubernetes cluster.
  2. kubectl command-line tool installed.
  3. A properties file containing the configuration details.

Step 1: Create a Properties File

Start by creating a properties file that encapsulates the configuration parameters for your application. For example, let's name it app-config.properties:

# app-config.properties

database.url=jdbc:mysql://localhost:3306/mydatabase
database.username=admin
database.password=secretpassword

Step 2: Verify Kubernetes Cluster Connection

Ensure that your kubectl is configured to connect to your Kubernetes cluster. You can verify this by running:

kubectl cluster-info

Step 3: Create a ConfigMap

Now, let's create a ConfigMap using the kubectl create configmap command. Execute the following command in your terminal:

kubectl create configmap app-config --from-file=app-config.properties

This command instructs Kubernetes to create a ConfigMap named app-config using the data from the app-config.properties file.

Step 4: Verify ConfigMap Creation

To verify the successful creation of the ConfigMap, run the following command:

kubectl get configmap app-config

This command should display details about the newly created ConfigMap.

Step 5: Mount ConfigMap in Pod

To utilize the ConfigMap in a Pod, you need to mount it as a volume. Update your Pod manifest or Deployment YAML file to include a volume and volumeMount for the ConfigMap. Here is a snippet as an example:

apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: mycontainer
image: myimage
volumeMounts:
- name: config-volume
mountPath: /path/to/config
volumes:
- name: config-volume
configMap:
name: app-config

Replace myimage with your actual container image and adjust the paths accordingly.

Step 6: Verify ConfigMap Usage

After applying the updated Pod manifest, verify that the ConfigMap is correctly mounted in the Pod:

kubectl exec -it mypod -- cat /path/to/config/app-config.properties

This command allows you to view the contents of the ConfigMap within the running Pod.

More Examples:

  • Update ConfigMap:
    To update the ConfigMap after modifying the properties file, use the following command:
kubectl create configmap app-config --from-file=app-config.properties --dry-run=client -o yaml | kubectl apply -f -

This command ensures that the ConfigMap is updated without restarting the associated Pods.

  • Delete ConfigMap:
    To delete the ConfigMap, use the following command:
kubectl delete configmap app-config

ConfigMaps simplify the management of configuration data in Kubernetes, providing a clean separation between application code and configuration settings. By following the steps outlined in this tutorial, you can effortlessly create a ConfigMap from a properties file and integrate it into your Kubernetes deployments.

Related Searches and Questions asked:

  • How To Setup Kube State Metrics on Kubernetes
  • Pods in Kubernetes, Nearly Identically Deployed, One Isn't Visible
  • How to Setup Nginx Ingress Controller On Kubernetes
  • How To Troubleshoot Kubernetes Pods
  • That's it for this topic, Hope this article is useful. Thanks for Visiting us.