Wednesday, September 2, 2026

Configure Environment Variables in Kubernetes

 

What are Environment Variables?


Environment variables are key-value pairs which is used to pass the configuration values to an application without changing the application code.


Example:


Database URL

Username

Application Mode (dev, test, prod)




Why Use Environment Variables?


Instead of hardcoding values in the application code, We can pass them from Kubernetes.


DB_HOST=localhost

DB_HOST=mysql-service


This makes the application more flexible and easier to manage.




Example Deployment YAML


apiVersion: apps/v1

kind: Deployment

metadata:

  name: myapp


spec:

  replicas: 2


  selector:

    matchLabels:

      app: myapp


  template:

    metadata:

      labels:

        app: myapp


    spec:

      containers:

      - name: myapp

        image: nginx


        env:

        - name: APP_ENV

          value: "production"


        - name: COMPANY

          value: "HCL Pvt Ltd"




Understanding the Configuration


env:

- name: APP_ENV

  value: "production"


- name: DB_HOST

  value: "mysql-service"


Creates an environment variable.


APP_ENV=production

DB_HOST="HCL Pvt Ltd"





Verify Environment Variables



Find the Pod - kubectl get pods

Access the Pod - kubectl exec -it <pod-name> -- /bin/bash


View environment variables command:


env Or printenv


env APP_ENV Or printenv APP_ENV





No comments:

Post a Comment

testing