Understanding Parameters
TOC
Overview
Parameters in Kubernetes refer to command-line arguments passed to containers at runtime. They correspond to the args
field in Kubernetes Pod specifications and override the default CMD arguments defined in container images. Parameters provide a flexible way to configure application behavior without rebuilding images.
Core Concepts
What are Parameters?
Parameters are runtime arguments that:
- Override the default CMD instruction in Docker images
- Are passed to the container's main process as command-line arguments
- Allow dynamic configuration of application behavior
- Enable reuse of the same image with different configurations
Relationship with Docker
In Docker terminology:
- ENTRYPOINT: Defines the executable (maps to Kubernetes
command
)
- CMD: Provides default arguments (maps to Kubernetes
args
)
- Parameters: Override CMD arguments while preserving ENTRYPOINT
# Dockerfile example
FROM nginx:alpine
ENTRYPOINT ["nginx"]
CMD ["-g", "daemon off;"]
# Kubernetes override
apiVersion: v1
kind: Pod
spec:
containers:
- name: nginx
image: nginx:alpine
args: ["-g", "daemon off;", "-c", "/custom/nginx.conf"]
Use Cases and Scenarios
1. Application Configuration
Pass configuration options to applications:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-server
spec:
template:
spec:
containers:
- name: app
image: myapp:latest
args:
- "--port=8080"
- "--log-level=info"
- "--config=/etc/app/config.yaml"
2. Environment-Specific Deployment
Different parameters for different environments:
# Development
args: ["--debug", "--reload", "--port=3000"]
# Production
args: ["--optimize", "--port=80", "--workers=4"]
3. Database Connection Configuration
apiVersion: v1
kind: Pod
spec:
containers:
- name: db-client
image: postgres:13
args:
- "psql"
- "-h"
- "postgres.example.com"
- "-p"
- "5432"
- "-U"
- "myuser"
- "-d"
- "mydb"
CLI Examples and Practical Usage
Using kubectl run
# Basic parameter passing
kubectl run nginx --image=nginx:alpine --restart=Never -- -g "daemon off;" -c "/custom/nginx.conf"
# Multiple parameters
kubectl run myapp --image=myapp:latest --restart=Never -- --port=8080 --log-level=debug
# Interactive debugging
kubectl run debug --image=ubuntu:20.04 --restart=Never -it -- /bin/bash
Using kubectl create
# Create deployment with parameters
kubectl create deployment web --image=nginx:alpine --dry-run=client -o yaml > deployment.yaml
# Edit the generated YAML to add args:
# spec:
# template:
# spec:
# containers:
# - name: nginx
# image: nginx:alpine
# args: ["-g", "daemon off;", "-c", "/custom/nginx.conf"]
kubectl apply -f deployment.yaml
Complex Parameter Examples
Web Server with Custom Configuration
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-custom
spec:
replicas: 3
selector:
matchLabels:
app: nginx-custom
template:
metadata:
labels:
app: nginx-custom
spec:
containers:
- name: nginx
image: nginx:1.21-alpine
args:
- "-g"
- "daemon off;"
- "-c"
- "/etc/nginx/custom.conf"
ports:
- containerPort: 80
volumeMounts:
- name: config
mountPath: /etc/nginx/custom.conf
subPath: nginx.conf
volumes:
- name: config
configMap:
name: nginx-config
Application with Multiple Parameters
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
containers:
- name: app
image: mycompany/myapp:v1.2.3
args:
- "--server-port=8080"
- "--database-url=postgresql://db:5432/mydb"
- "--log-level=info"
- "--metrics-enabled=true"
- "--cache-size=256MB"
- "--worker-threads=4"
Best Practices
1. Parameter Design Principles
- Use meaningful parameter names:
--port=8080
instead of -p 8080
- Provide sensible defaults: Ensure applications work without parameters
- Document all parameters: Include help text and examples
- Validate input: Check parameter values and provide error messages
2. Security Considerations
# ❌ Avoid sensitive data in parameters
args: ["--api-key=secret123", "--password=mypass"]
# ✅ Use environment variables for secrets
env:
- name: API_KEY
valueFrom:
secretKeyRef:
name: app-secrets
key: api-key
args: ["--config-from-env"]
3. Configuration Management
# ✅ Combine parameters with ConfigMaps
apiVersion: v1
kind: Pod
spec:
containers:
- name: app
image: myapp:latest
args:
- "--config=/etc/config/app.yaml"
- "--log-level=info"
volumeMounts:
- name: config
mountPath: /etc/config
volumes:
- name: config
configMap:
name: app-config
Troubleshooting Common Issues
1. Parameter Not Recognized
# Check container logs
kubectl logs pod-name
# Common error: unknown flag
# Solution: Verify parameter syntax and application documentation
2. Parameter Override Not Working
# ❌ Incorrect: mixing command and args
command: ["myapp", "--port=8080"]
args: ["--log-level=debug"]
# ✅ Correct: use args only to override CMD
args: ["--port=8080", "--log-level=debug"]
3. Debugging Parameter Issues
# Run container interactively to test parameters
kubectl run debug --image=myapp:latest -it --rm --restart=Never -- /bin/sh
# Inside container, test the command manually
/app/myapp --port=8080 --log-level=debug
Advanced Usage Patterns
1. Conditional Parameters with Init Containers
apiVersion: v1
kind: Pod
spec:
initContainers:
- name: config-generator
image: busybox
command: ['sh', '-c']
args:
- |
if [ "$ENVIRONMENT" = "production" ]; then
echo "--optimize --workers=8" > /shared/args
else
echo "--debug --reload" > /shared/args
fi
volumeMounts:
- name: shared
mountPath: /shared
containers:
- name: app
image: myapp:latest
command: ['sh', '-c']
args: ['exec myapp $(cat /shared/args)']
volumeMounts:
- name: shared
mountPath: /shared
volumes:
- name: shared
emptyDir: {}
2. Parameter Templating with Helm
# values.yaml
app:
parameters:
port: 8080
logLevel: info
workers: 4
# deployment.yaml template
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
containers:
- name: app
image: myapp:latest
args:
- "--port={{ .Values.app.parameters.port }}"
- "--log-level={{ .Values.app.parameters.logLevel }}"
- "--workers={{ .Values.app.parameters.workers }}"
Parameters provide a powerful mechanism for configuring containerized applications in Kubernetes. By understanding how to properly use parameters, you can create flexible, reusable, and maintainable deployments that adapt to different environments and requirements.