Kubernetes: ConfigMaps, Secrets & Spring Boot Property Sources
Spring Boot reads configuration from many sources: environment variables, property files, and system properties. On Kubernetes, ConfigMaps provide non-sensitive config and Secrets provide sensitive values (passwords, API keys, TLS certificates). This article shows how to create both, inject them into your Spring Boot pods, enable live refresh without restarting, and manage secrets securely in production.
Contents
Where Spring Boot gets its configuration Creating a ConfigMap ConfigMap values as environment variables ConfigMap values as mounted files Kubernetes Secrets Reading Secrets as files with configtree Spring Cloud Kubernetes Config Reloading changes with @RefreshScope External Secrets Operator Which method to use Secrets best practices
Where Spring Boot gets its configuration
Spring Boot collects configuration from many sources into one Environment
| Source | Typical use on Kubernetes |
|---|---|
Config files inside the JAR: application.ymlapplication-prod.yml | Defaults, and settings for a profile chosen with SPRING_PROFILES_ACTIVE |
Config files outside the JAR, found with spring.config.additional-locationspring.config.import | A ConfigMap or Secret mounted as files |
| Operating system environment variables | ConfigMap and Secret keys injected with envenvFrom |
Java system properties (-D | Rare in containers; JVM flags go in JAVA_TOOL_OPTIONS |
Command-line arguments (--name=value | The container's args |
Two consequences follow. An environment variable overrides any file, so a value set with env
Kubernetes offers two objects to supply those values. A ConfigMap holds ordinary settings, a Secret holds passwords, keys and certificates, and either can reach the pod as environment variables or as files. The sections below show each combination, then two ways to manage them from outside the pod.
Creating a ConfigMap
A ConfigMap holds configuration as key-value pairs. A pod can receive it in two ways: each key as an environment variable, or each key as a file. The two ways suit different kinds of settings, so this article uses one ConfigMap for each:
# configmap.yaml
# Settings passed as environment variables
apiVersion: v1
kind: ConfigMap
metadata:
name: payment-service-env
namespace: payments
data:
LOGGING_LEVEL_ROOT: "INFO"
FEATURE_NEW_CHECKOUT: "true"
APP_RETRY_MAX_ATTEMPTS: "3"
---
# A whole properties file, passed as a file
apiVersion: v1
kind: ConfigMap
metadata:
name: payment-service-config
namespace: payments
data:
application.properties: |
spring.jpa.show-sql=false
app.payment.timeout-seconds=30
You can also create ConfigMaps from the command line:
# From individual values
kubectl create configmap payment-service-env \
--from-literal=LOGGING_LEVEL_ROOT=INFO \
--from-literal=APP_RETRY_MAX_ATTEMPTS=3 -n payments
# From an existing file; the key becomes the file name
kubectl create configmap payment-service-config \
--from-file=application.properties=./k8s/application-prod.properties -n payments
# Show what is stored
kubectl get configmap payment-service-config -n payments -o yaml
ConfigMap values as environment variables
envFromenvconfigMapKeyRef
# In the Deployment's pod template
spec:
containers:
- name: payment-service
image: myorg/payment-service:1.0.0
# Every key of the ConfigMap as an environment variable
envFrom:
- configMapRef:
name: payment-service-env
# Or one key, under a name of your choice
env:
- name: LOGGING_LEVEL_COM_EXAMPLE
valueFrom:
configMapKeyRef:
name: payment-service-env
key: LOGGING_LEVEL_ROOT
Spring Boot reads environment variables as properties. To find the variable for a property, replace dots and dashes with underscores and write it in capitals: spring.datasource.urlSPRING_DATASOURCE_URLfeature.new-checkoutFEATURE_NEW_CHECKOUTLOG_LEVEL
ConfigMap values as mounted files
Mounting a ConfigMap as a volume creates one file per key. Point Spring Boot at the folder with SPRING_CONFIG_ADDITIONAL_LOCATIONapplication.properties
spec:
containers:
- name: payment-service
image: myorg/payment-service:1.0.0
env:
- name: SPRING_CONFIG_ADDITIONAL_LOCATION
value: "optional:file:/etc/config/"
volumeMounts:
- name: app-config
mountPath: /etc/config
readOnly: true
volumes:
- name: app-config
configMap:
name: payment-service-config
subPathKubernetes Secrets
A Secret works like a ConfigMap but is meant for passwords, keys and certificates. Values under datastringData
# secret.yaml: base64-encoded values (use Sealed Secrets or ESO in production)
apiVersion: v1
kind: Secret
metadata:
name: payment-service-secrets
namespace: payments
type: Opaque
data:
# Values must be base64-encoded
DB_PASSWORD: cGFzc3dvcmQxMjM= # echo -n "password123" | base64
STRIPE_API_KEY: c2tfbGl2ZV8uLi4=
stringData:
# stringData is plain text: Kubernetes base64-encodes it
REDIS_PASSWORD: "my-redis-password"
# Create from literals (Kubernetes does the base64 encoding)
kubectl create secret generic payment-service-secrets \
--from-literal=DB_PASSWORD=password123 \
--from-literal=STRIPE_API_KEY=sk_live_... \
-n payments
# Create TLS secret
kubectl create secret tls api-tls-cert \
--cert=tls.crt \
--key=tls.key \
-n payments
# Mount secrets as environment variables
spec:
containers:
- name: payment-service
envFrom:
- secretRef:
name: payment-service-secrets
# Or individual secret keys
env:
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: payment-service-secrets
key: DB_PASSWORD
Reading Secrets as files with configtree
Spring Boot can read a mounted Secret directly, without any environment variables. A configtree:
# Secret keys named after the properties they set
apiVersion: v1
kind: Secret
metadata:
name: payment-db
namespace: payments
type: Opaque
stringData:
spring.datasource.username: payments
spring.datasource.password: "change-me"
# In the Deployment's pod template: mount the Secret as files
spec:
containers:
- name: payment-service
image: myorg/payment-service:1.0.0
volumeMounts:
- name: db-secret
mountPath: /etc/secrets/db
readOnly: true
volumes:
- name: db-secret
secret:
secretName: payment-db
# application.yml: read every file under /etc/secrets/db as a property
spring:
config:
import: "optional:configtree:/etc/secrets/db/"
- The pod gets two files,
and/etc/secrets/db/spring.datasource.username , and Spring Boot binds them to the datasource like any other property./etc/secrets/db/spring.datasource.password - The
prefix lets the application start on a laptop, where the folder does not exist.optional: - No password appears in an environment variable, so it does not show up in
, child processes or diagnostic dumps. This is the file-based approach the best practices at the end of this page recommend./proc - Like other files, the import is read at startup. A rotated Secret updates the files in place, but the application sees the new value only after a restart or a reload.
The configtree:
Spring Cloud Kubernetes Config
Spring Cloud Kubernetes reads ConfigMaps and Secrets directly from the Kubernetes API: no environment variable mapping needed. The ConfigMap name matches spring.application.name
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes-client-config</artifactId>
</dependency>
# application.yml
spring:
application:
name: payment-service # without "name" below, a ConfigMap named "payment-service" is loaded
config:
import: "kubernetes:" # needed since Spring Cloud 2020.0, which no longer uses bootstrap.yml
cloud:
kubernetes:
config:
enabled: true
name: payment-service-config # override name
namespace: payments
secrets:
enabled: true
name: payment-service-secrets
namespace: payments
# RBAC: give the pod permission to read ConfigMaps and Secrets
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: config-reader
namespace: payments
rules:
- apiGroups: [""]
resources: ["configmaps", "secrets", "pods"]
verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: config-reader-binding
namespace: payments
subjects:
- kind: ServiceAccount
name: default
namespace: payments
roleRef:
kind: Role
name: config-reader
apiGroup: rbac.authorization.k8s.io
This Role lets the pod read every ConfigMap and Secret in the namespace. To follow the least-privilege advice at the end of this article, list the exact objects under resourceNamessecrets
Reloading changes with @RefreshScope
Spring Cloud Kubernetes can watch ConfigMaps for changes and trigger a context refresh without restarting the pod.
# application.yml
spring:
cloud:
kubernetes:
reload:
enabled: true
mode: polling # or "event" for watch-based (requires RBAC watch verb)
period: 15000 # poll every 15 seconds
strategy: refresh # refresh vs restart_context vs shutdown
With the refresh@RefreshScope@ConfigurationProperties@Value
// Beans annotated with @RefreshScope are recreated after a reload
@RefreshScope
@Service
public class FeatureFlagService {
@Value("${feature.new-checkout:false}")
private boolean newCheckoutEnabled;
public boolean isNewCheckoutEnabled() { return newCheckoutEnabled; }
}
External Secrets Operator
The External Secrets Operator syncs secrets from AWS Secrets Manager, HashiCorp Vault, GCP Secret Manager, or Azure Key Vault into Kubernetes Secrets, keeping the source of truth outside the cluster. The examples use the current v1v1beta1
# SecretStore: points to AWS Secrets Manager
apiVersion: external-secrets.io/v1
kind: SecretStore
metadata:
name: aws-secrets-manager
namespace: payments
spec:
provider:
aws:
service: SecretsManager
region: us-east-1
auth:
serviceAccount: # use IRSA for RBAC
name: external-secrets-sa
---
# ExternalSecret: defines which secrets to sync
apiVersion: external-secrets.io/v1
kind: ExternalSecret
metadata:
name: payment-service-secrets
namespace: payments
spec:
refreshInterval: 1h
secretStoreRef:
name: aws-secrets-manager
kind: SecretStore
target:
name: payment-service-secrets # resulting K8s Secret name
creationPolicy: Owner
data:
- secretKey: DB_PASSWORD # key in the K8s Secret
remoteRef:
key: payment-service/prod # path in AWS Secrets Manager
property: db_password # field within the secret JSON
- secretKey: STRIPE_API_KEY
remoteRef:
key: payment-service/prod
property: stripe_api_key
Which method to use
| Method | Changes picked up | Best for |
|---|---|---|
| Environment variables from a ConfigMap | On pod restart | A few simple settings, such as log levels and feature flags |
| Mounted ConfigMap file | Files update within about a minute; the application on restart or reload | Whole property files, and many related settings |
| Secret as environment variables | On pod restart | Simple setups, when the exposure of environment variables is acceptable |
Secret as files, with configtree: | Files update in place; the application on restart or reload | Passwords and keys in production |
| Spring Cloud Kubernetes | While running, with reload enabled | Settings that must change without a restart; needs RBAC to read the API |
| External Secrets Operator | At each refreshInterval | Secrets whose source of truth is a vault or a cloud secret manager |
A common production setup combines three of them: defaults in the JAR, environment-specific settings from a ConfigMap, and secrets synced by the External Secrets Operator and read as files with configtree:
Secrets best practices
- Never commit secrets to Git: even base64-encoded K8s secret YAML. Use
Sealed Secrets or the External Secrets Operator. - Enable encryption at rest: configure
in your cluster to encrypt Secrets in etcd.EncryptionConfiguration - Least privilege RBAC: only grant
on specific secrets; neverget orlist all secrets cluster-wide.watch - Prefer files to environment variables: environment variables are passed on to every child process, readable in
, and often end up in error reports and diagnostic dumps. Files mounted from a Secret are less exposed, and are updated when the Secret is rotated./proc - Rotate regularly: use ESO's
to pull rotated secrets into Kubernetes automatically. The application still has to pick up the new value: files are updated in place, but environment variables only change when the pod restarts.refreshInterval - Audit access: enable Kubernetes audit logging for
secrets operations to detect unexpected access.get