How to inject kubernetes secret into configuration file
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Kubernetes Secrets are designed to store and manage sensitive information like passwords, OAuth tokens, ssh keys, etc. Kubernetes provides a way to make sensitive data accessible to pods but without exposing this data in your application source code or configuration files.
Injecting a Kubernetes Secret into a configuration file inside a pod can be done in multiple ways. This article outlines the methods and examples for achieving this.
Prerequisites
- Basic knowledge of Kubernetes.
- A functional Kubernetes cluster.
kubectlcommand-line tool set up and working with your Kubernetes cluster.- A Kubernetes Secret already created in your namespace.
Methods for Injecting Kubernetes Secrets
There are multiple methods to inject Kubernetes Secrets into a configuration file in a pod:
- Using Environment Variables
- Mounting as a Volume
- Using Tools within the Pod (e.g.,
envsubst)
1. Using Environment Variables
Injecting secrets as environment variables is one of the simplest ways. Here’s an example YAML configuration for a pod:
- name: mycontainer
- name: SECRET_USERNAME
- name: SECRET_PASSWORD
- name: mycontainer
- name: secret-volume
- name: secret-volume
- Security: Be cautious when handling secrets. Avoid logging secrets and consider using RBAC to limit access.
- Performance: Using environment variables might be simpler but mounting a secret as a volume is generally preferred for large configurations due to environment size limits.
- Applications: Not all applications can be configured to read secrets from environment variables; in such cases, volume mounting is more appropriate.
- HashiCorp Vault: A tool for secrets management, encryption as a service, and privileged access management.
- Sealed Secrets: Encrypts your secrets into Kubernetes resources that are safe to store publicly.
Related reading
- How to install a specific Chart version
- How to install Hive Metastore in Kubernetes?
- How to install Kubernetes cluster behind proxy with Kubeadm?
- How to install specific version of Kubernetes?
- How to install docker on Amazon Linux2
- How to install git on a docker ubuntu image?
- How to integrate Kubernetes with Gitlab
- How to invoke the Pod proxy verb using the Kubernetes Go client?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.