Adding .env to your .gitignore file is table stakes. It keeps your credentials out of public source code repositories, but it does almost nothing to secure your application once it reaches a live cluster.
Many engineering teams deploy applications under the assumption that default platform mechanisms keep sensitive credentials safe. In reality, environment variables are easily inspected, native Kubernetes secrets are stored unencrypted by default, and overly broad permissions make lateral movement simple for an attacker.
Here is how to move past basic secret hiding and implement proper deployment security step by step.
1. Stop Treating Base64 as Encryption
Default Kubernetes Secret objects are stored in etcd as base64-encoded strings. Base64 is an encoding format, not encryption. Anyone with access to the cluster's etcd store or cluster-wide secret read permissions can inspect those credentials instantly.
Instead of storing plain base64 manifests in source control or direct cluster apply pipelines, integrate a dedicated secrets engine (such as AWS Secrets Manager, HashiCorp Vault, or GCP Secret Manager) combined with an operator like the External Secrets Operator (ESO).
Here is how an ExternalSecret manifest safely fetches credentials dynamically at runtime:
apiVersion: external-secrets.io/v1beta1 kind: ExternalSecret metadata: name: app-db-credentials namespace: production spec: refreshInterval: 1h secretStoreRef: name: aws-secrets-backend kind: ClusterSecretStore target: name: db-credentials-target creationPolicy: Owner data: - secretKey: DB_PASSWORD remoteRef: key: production/api/db-password 2. Mount Secrets as Files Instead of Environment Variables
Injecting secrets directly as environment variables via env or envFrom in Pod manifests is common practice, but it carries notable risk:
- Environment variables appear in plaintext via process inspection tools like
ps auxor/proc/1/environ. - Application crashes or debug logs often dump the entire execution environment into stdout.
- Child processes automatically inherit all parent environment variables.
Mounting secrets as read-only volume files restricts credential access directly to the filesystem path, keeping them out of application process trees.
apiVersion: apps/v1 kind: Deployment metadata: name: api-service namespace: production spec: template: spec: containers: - name: api image: api-service:v1.2.0 volumeMounts: - name: db-secret-vol mountPath: "/etc/secrets" readOnly: true volumes: - name: db-secret-vol secret: secretName: db-credentials-target 3. Enforce Tight RBAC Scope
A common failure mode in Kubernetes deployment security is over-provisioned Service Accounts. If an attacker gains shell execution inside a Pod, they will attempt to query the API server using the mounted service account token.
Never grant wildcard permissions like verbs: ["*"] across resources: ["secrets"]. Scope role bindings strictly to specific resource names and required namespaces.
apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: namespace: production name: isolated-secret-reader rules: - apiGroups: [""] resources: ["secrets"] resourceNames: ["db-credentials-target"] verbs: ["get"] 4. Eliminate Shared Secrets Between Services
Sharing a single database password or API token across multiple microservices creates a single point of failure. If Service A suffers a compromise, the attacker immediately gains access to the datastores and APIs intended only for Service B.
- Generate isolated credentials for every distinct microservice.
- Restrict API tokens to the minimum permissions necessary for that service's workload.
- Implement automated rotation policies so that any leaked key expires quickly.
5. Enable Continuous Secret Auditing
Storing secrets securely is not a one-time configuration. Without visibility into secret access patterns, credential exposure will go unnoticed until damage occurs.
Turn on Kubernetes API server audit logging, stream cloud secret store logs to a central observability platform, and set up alert rules for unexpected secret access calls or unauthorized permission queries.
About Me
Tanmay builds AI agents and automation for businesses and is open to freelance projects. Feel free to connect on LinkedIn to discuss DevOps, backend architecture, or custom automation solutions.

