The Nautilus DevOps team is working on to setup some pre-requisites for an application that will send the greetings to different users. There is a sample deployment, that needs to be tested. Below is a scenario which needs to be configured on Kubernetes cluster. Please find below more details about it.
- Create a
podnamedprint-envars-greeting. - Configure spec as, the container name should be
print-env-containerand usebashimage. Create three environment variables:
a.GREETINGand its value should beWelcome to
b.COMPANYand its value should beNautilus
c.GROUPand its value should beGroupUse command
["/bin/sh", "-c", 'echo "$(GREETING) $(COMPANY) $(GROUP)"'](please use this exact command), also set itsrestartPolicypolicy toNeverto avoid crash loop back.You can check the output using
kubectl logs -f print-envars-greetingcommand.
Note: The kubectl utility on the jump-host has been configured to work with the Kubernetes cluster.
What We Will Build
We are going to create a Kubernetes Pod that:
- Runs a container using the
bashimage - Defines three environment variables
- Executes a command that prints these variables
- Exits after completing its task
Technical Details
| Component | Specification |
|---|---|
| Pod Name | print-envars-greeting |
| Container Name | print-env-container |
| Image | bash |
| Restart Policy | Never |
| Environment Variables |
GREETING, COMPANY, GROUP
|
Understanding Environment Variables in Kubernetes
Environment variables are key-value pairs that provide configuration data to containers. They are commonly used for:
- Application configuration
- Database connection strings
- API endpoints
- Feature flags
- Credentials (though Secrets are recommended for sensitive data)
In Kubernetes, environment variables can be defined directly in the Pod specification using the env field.
Step-by-Step Implementation
Step 1: Create the Pod YAML File
Create a file named print-envars-greeting.yaml with the following content:
apiVersion: v1
kind: Pod
metadata:
name: print-envars-greeting
spec:
restartPolicy: Never
containers:
- name: print-env-container
image: bash
command:
- "/bin/sh"
- "-c"
- 'echo "$(GREETING) $(COMPANY) $(GROUP)"'
env:
- name: GREETING
value: "Welcome to"
- name: COMPANY
value: "Nautilus"
- name: GROUP
value: "Group"
YAML Breakdown
Pod Metadata
metadata:
name: print-envars-greeting
This defines the name of the Pod.
Restart Policy
spec:
restartPolicy: Never
Never ensures the Pod runs once and does not restart. Since this is a one-time task, this policy is appropriate.
Container Definition
containers:
- name: print-env-container
image: bash
The container is named print-env-container and uses the bash image, which provides a lightweight shell environment.
Command Definition
command:
- "/bin/sh"
- "-c"
- 'echo "$(GREETING) $(COMPANY) $(GROUP)"'
The command executes a shell script that echoes the values of the three environment variables.
Environment Variables
env:
- name: GREETING
value: "Welcome to"
- name: COMPANY
value: "Nautilus"
- name: GROUP
value: "Group"
Three environment variables are defined with their respective values.
Step 2: Create the Pod
Apply the YAML configuration to create the Pod:
kubectl apply -f print-envars-greeting.yaml
Output:
pod/print-envars-greeting created
Step 3: Verify Pod Status
Check the status of the Pod:
kubectl get pods
Output:
NAME READY STATUS RESTARTS AGE
print-envars-greeting 0/1 Completed 0 11s
The Completed status indicates that the container ran successfully and exited. Since the restart policy is Never, the Pod remains in this completed state.
Step 4: View the Output
View the logs to see the output of the command:
kubectl logs print-envars-greeting
Output:
Welcome to Nautilus Group
How the Command Works
The command executes as follows:
- The container starts with the
bashimage - The shell interprets the command
- The shell substitutes each
$(VARIABLE_NAME)with its value:-
$(GREETING)→Welcome to -
$(COMPANY)→Nautilus -
$(GROUP)→Group
-
- The
echocommand prints the concatenated string - The container exits successfully
The command syntax is important. The single quotes preserve the $ characters, allowing the shell to interpret them as variable references rather than treating them as literal strings.
Environment Variable Best Practices
When to Use Environment Variables
Environment variables are ideal for:
- Non-sensitive configuration
- Application settings that differ between environments
- Feature toggles
- Service discovery information
When Not to Use Environment Variables
Consider alternatives for:
- Sensitive data (use Secrets instead)
- Large configuration files (use ConfigMaps)
- Complex nested configurations (use ConfigMaps or external configuration management)
Best Practices
- Use descriptive names: Make variable names meaningful and consistent
- Document your variables: Maintain a reference of what each variable does
- Use Secrets for sensitive data: Never store passwords or tokens in plain text
- Keep it organized: Group related variables together
- Validate variables: Ensure required variables are set before the application starts
Common Use Cases
Application Configuration
Environment variables are commonly used to configure applications:
env:
- name: DATABASE_URL
value: "postgres://user:pass@host:5432/db"
- name: LOG_LEVEL
value: "debug"
- name: API_KEY
value: "abcdef123456"
Feature Flags
Control application features:
env:
- name: FEATURE_NEW_UI
value: "true"
- name: FEATURE_BETA
value: "false"
Environment Identification
Identify the deployment environment:
env:
- name: ENVIRONMENT
value: "production"
- name: REGION
value: "us-east-1"
Troubleshooting
Pod Does Not Start
# Check the Pod status
kubectl get pods
# Get detailed information
kubectl describe pod print-envars-greeting
# Check logs for errors
kubectl logs print-envars-greeting
Variables Not Substituted
Ensure you are using the correct syntax:
- Use
$(VAR_NAME)for variable substitution - Use single quotes to preserve
$characters - Verify that the variable names match exactly
Pod Is in CrashLoopBackOff
# Check the pod status
kubectl get pods
# Check logs
kubectl logs print-envars-greeting --previous
# Verify the command syntax
kubectl describe pod print-envars-greeting
Key Takeaways
What We Learned
-
Environment variables in Kubernetes: Defined in the
envfield of a container specification -
Variable substitution: Use
$(VAR_NAME)to reference environment variables in commands -
Restart policies:
Neveris suitable for one-time tasks -
Pod lifecycle: A Pod can have a
Completedstatus after successful execution -
Command execution: The
bashimage provides a lightweight shell environment
Top comments (0)