DEV Community

DEV-AI
DEV-AI

Posted on

Generic Environment Variables for Multiple Java Services in Kubernetes

There are several effective approaches to share generic environment variables across multiple Java services using ConfigMaps in Kubernetes. Here are the most practical methods:

Method 1: Single Generic ConfigMap with envFrom

Create a shared ConfigMap containing common environment variables that all your Java services need.[1][2]

Create the Generic ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: generic-config
  namespace: default
data:
  JAVA_OPTS: "-Xmx512m -Xms256m"
  LOG_LEVEL: "INFO"
  TIMEZONE: "UTC"
  DATABASE_POOL_SIZE: "10"
  CACHE_TTL: "3600"
Enter fullscreen mode Exit fullscreen mode

Reference in Service Deployments

Use envFrom to automatically load all key-value pairs from the ConfigMap as environment variables:[2]

apiVersion: apps/v1
kind: Deployment
metadata:
  name: java-service-1
spec:
  template:
    spec:
      containers:
      - name: java-app
        image: your-java-service:latest
        envFrom:
        - configMapRef:
            name: generic-config
Enter fullscreen mode Exit fullscreen mode

This approach automatically creates environment variables for all keys in the ConfigMap, making them available to your Java applications.[2]

Method 2: Multiple ConfigMaps for Different Contexts

Organize your configuration into multiple ConfigMaps based on context:[3][2]

# Database configuration
apiVersion: v1
kind: ConfigMap
metadata:
  name: database-config
data:
  DB_HOST: "postgresql.default.svc.cluster.local"
  DB_PORT: "5432"
---
# Application configuration  
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  LOG_LEVEL: "INFO"
  CACHE_ENABLED: "true"
Enter fullscreen mode Exit fullscreen mode

Reference Multiple ConfigMaps

apiVersion: apps/v1
kind: Deployment
metadata:
  name: java-service
spec:
  template:
    spec:
      containers:
      - name: java-app
        image: your-service:latest
        envFrom:
        - configMapRef:
            name: database-config
        - configMapRef:
            name: app-config
Enter fullscreen mode Exit fullscreen mode

Method 3: Environment-Specific ConfigMaps

For different environments (dev, staging, prod), create separate ConfigMaps with the same structure:[3]

# dev-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: generic-config
  namespace: app-dev
data:
  LOG_LEVEL: "DEBUG"
  DATABASE_URL: "dev-database"
---
# prod-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: generic-config
  namespace: app-prod
data:
  LOG_LEVEL: "WARN"
  DATABASE_URL: "prod-database"
Enter fullscreen mode Exit fullscreen mode

Method 4: Selective Environment Variables

If you need only specific variables from a ConfigMap, use the env field with valueFrom:[1][2]

apiVersion: apps/v1
kind: Deployment
metadata:
  name: java-service
spec:
  template:
    spec:
      containers:
      - name: java-app
        image: your-service:latest
        env:
        - name: LOG_LEVEL
          valueFrom:
            configMapKeyRef:
              name: generic-config
              key: LOG_LEVEL
        - name: JAVA_OPTS
          valueFrom:
            configMapKeyRef:
              name: generic-config
              key: JAVA_OPTS
Enter fullscreen mode Exit fullscreen mode

Best Practices

1. Namespace Organization

Create separate namespaces for different environments and deploy the same ConfigMap name in each:[3]

  • app-dev namespace
  • app-staging namespace
  • app-prod namespace

2. ConfigMap Naming Convention

Use descriptive names for different types of configuration:

  • generic-config - Common application settings
  • database-config - Database connection settings
  • monitoring-config - Monitoring and logging settings

3. Spring Boot Integration

For Spring Boot applications, you can use the SPRING_APPLICATION_JSON environment variable to provide JSON configuration:[4]

apiVersion: v1
kind: ConfigMap
metadata:
  name: spring-config
data:
  SPRING_APPLICATION_JSON: |
    {
      "spring.datasource.url": "jdbc:postgresql://db:5432/mydb",
      "spring.jpa.hibernate.ddl-auto": "validate",
      "logging.level.com.mycompany": "DEBUG"
    }
Enter fullscreen mode Exit fullscreen mode

4. Deployment Template

Create a base deployment template that all your Java services can inherit:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: REPLACE_SERVICE_NAME
spec:
  template:
    spec:
      containers:
      - name: java-app
        image: REPLACE_IMAGE
        envFrom:
        - configMapRef:
            name: generic-config
        - configMapRef:
            name: database-config
        env:
        - name: SERVICE_NAME
          value: REPLACE_SERVICE_NAME
Enter fullscreen mode Exit fullscreen mode

Top comments (0)