Skip to content

How to Create a Secret Volume in Kubernetes

homepage-banner

Create a Secret Volume

From command line

kubectl create secret generic my-secret-1 --from-literal=username='admin' --from-literal=password='B3684A64'

## or
echo -n 'admin' > username.txt
echo -n 'B3684A64' > password.txt
kubectl create secret generic my-secret-2 --from-file=./username.txt --from-file=./password.txt

From yaml file

apiVersion: v1
kind: Secret
metadata:
  name: my-secret
type: Opaque
data:
  username: admin
  password: B3684A64

Check the Secret

kubectl get secret
kubectl describe secrets/my-secret-1

Mounting a Secret Volume

Mount a Secret Volume to a Pod

apiVersion: v1
kind: Pod
metadata:
    name: my-pod
spec:
  containers:
  - name: my-container
    image: nginx
    volumeMounts:
    - name: my-secret-volume
      mountPath: /etc/my-secret
      readOnly: true
  volumes:
  - name: my-secret-volume
    secret:
      secretName: my-secret-1
kubectl create -f secret.yaml

Verify the Secret Volume

kubectl exec -it my-pod -- ls /etc/my-secret
Leave a message







Disclaimer
  • Welcome to visit the knowledge base of SRE and DevOps!
  • License under CC BY-NC 4.0
  • Made with Material for MkDocs and improve writing by generative AI tools
  • Copyright issue feedback me#imzye.com, replace # with @