Providing an answer after I've wasted some good hour pulling my hair out, it is extremely important to create secret in k8s namespace
where your deployment is running as secrets are tied to namespaces and all examples just use default namespace but your deployments are likely not!
Secret can be created in various ways, I'll show two common ones:
- From literal string
kubectl create secret generic my-secret --from-literal some_key='some_value' --namespace my-namespace
- From file content
kubectl create secret generic my-secret --from-file myfile --namespace my-namespace
Note that file name essentially becomes what "some_key" is with --from-literal
and it's important to get it right because it will appear in your k8s configs!
You can now debug how it inflated:
kubectl describe secrets my-secret --namespace my-namespace
Note how secret can store multiple key value pairs, and in the Deployment example above I'm only going to mount one individual key.
Now that we have secret created in the right namespace, we can mount it as file.
apiVersion: apps/v1
kind: Deployment
metadata:
name: test
namespace: my-namespace
spec:
selector:
matchLabels:
app: test
replicas: 1
template:
metadata:
labels:
app: test
spec:
volumes:
- name: my-secret
secret:
secretName: my-secret
items:
- key: my_key
path: my_key
containers:
- name: test
image: ubuntu:jammy
volumeMounts:
- name: my-secret
mountPath: "/tmp/my_key"
subPath: my_key
readOnly: true
This will mount the secret as a single file to /tmp/my_key
without overriding entire dir.