Kubernetes
YAML
dynamic values
DevOps
configuration management

How to set dynamic values with Kubernetes yaml file

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Kubernetes is a powerful container orchestration platform, and the YAML files used for resource configurations enable a declarative approach to specify the desired state of your applications. One advanced feature is the ability to set dynamic values in these YAML files, allowing for adaptability and scalability in highly dynamic environments. This article discusses various techniques and tools for setting dynamic values in Kubernetes YAML files.

Understanding Kubernetes YAML Files

Kubernetes YAML files are configuration files used to define desired states for various Kubernetes objects such as Pods, Deployments, and Services. They are structured in a hierarchical format, which is both human-readable and machine-parseable. A typical YAML file would include metadata, specification of resources, and configuration details.

Example YAML:

yaml
1apiVersion: v1
2kind: Pod
3metadata:
4  name: mypod
5spec:
6  containers:
7  - name: mycontainer
8    image: nginx
9    ports:
10    - containerPort: 80

Why Set Dynamic Values?

Dynamic values in Kubernetes YAML files allow for greater flexibility, enabling configurations to adapt based on environment or other external parameters. Whether through CI/CD pipelines or directly in cluster configurations, dynamic values help automate and optimize resource management, thus enhancing scalability and efficiency.

Techniques for Setting Dynamic Values

1. Environment Variables

Environment variables can be defined in YAML files to dynamically pass information to containerized applications. They allow for easy configuration adjustments without the need to modify the application code directly.

Example:

yaml
1apiVersion: v1
2kind: Pod
3metadata:
4  name: mypod
5spec:
6  containers:
7  - name: mycontainer
8    image: nginx
9    env:
10    - name: DATABASE_HOST
11      valueFrom:
12        configMapKeyRef:
13          name: myconfig
14          key: database_host

2. ConfigMaps and Secrets

ConfigMaps and Secrets are Kubernetes resources used to store non-sensitive and sensitive information, respectively. They allow applications to consume dynamic configuration data and credentials without embedding them directly into container images.

Using ConfigMaps:

yaml
1apiVersion: v1
2kind: ConfigMap
3metadata:
4  name: myconfig
5data:
6  app_name: "DynamicApp"
7  log_level: "INFO"

Using Secrets:

yaml
1apiVersion: v1
2kind: Secret
3metadata:
4  name: mysecret
5type: Opaque
6data:
7  password: cGFzc3dvcmQ=  # Base64 encoded

3. Helm Charts

Helm is a package manager for Kubernetes that facilitates application deployment using Helm charts. These charts support templates using the Go templating language, allowing for defining dynamic values that can be modified using values.yaml.

Example template:

yaml
1apiVersion: v1
2kind: ConfigMap
3metadata:
4  name: {{ .Values.appName }}-config
5data:
6  log_level: {{ .Values.logLevel | quote }}

values.yaml:

yaml
appName: MyDynamicApp
logLevel: INFO

4. Kustomize

Kustomize is a Kubernetes native configuration management tool that allows for the customization of YAML files. It supports patches and overlays, enabling dynamic modifications without needing to template.

Example of a kustomization.yaml file:

yaml
1bases:
2- ../base
3patchesStrategicMerge:
4- patch.yaml

patch.yaml can dynamically modify existing setups:

yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4  name: myapp
5spec:
6  replicas: 3

Comparison Table

TechniqueUse CaseProsCons
Environment VariablesSimple environment configurationEasy to implement FlexibleLimited to container-level variables
ConfigMaps/SecretsExternalize config and credentialsSecure with Secrets Decouples config from codeMay require additional setup
Helm ChartsApplication deployment with complex templatesVersatile Integrated with CI/CDLearning curve Requires Helm client
KustomizePatching and overlaying configurationsNative Kubernetes Simplifies multi-environment setupsLess template flexibility

Conclusion

Setting dynamic values in Kubernetes YAML files enhances the flexibility and manageability of your applications running on Kubernetes. By leveraging environment variables, ConfigMaps, Secrets, Helm charts, and Kustomize, you can effectively manage dynamic configurations across different environments and use cases. These techniques ensure that you can keep up with evolving requirements and optimize the utilization of resources in your Kubernetes ecosystem.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.