SSH (and git+ssh) has very particular opinions about file permissions. In Kubernetes you can set secret file permissions, but not ownership: (see the "Secret files permissions" section) https://kubernetes.io/docs/concepts/configuration/secret/#using-secrets
apiVersion: v1
kind: Pod
metadata:
name: security-context-demo
spec:
securityContext:
runAsUser: 1000
runAsGroup: 3000
fsGroup: 2000
There is no way to specify the fsOwner, so you need to workaround this somehow. One way is an init container.
Another workaround is to use a postStart
lifecycle hook to copy it into a different place:
lifecycle:
postStart:
exec:
command:
- /bin/sh
- -c
- cp /var/my-app-secrets/id_rsa /var/my-app/id_rsa
Create a secret containing some ssh keys:
kubectl create secret generic ssh-key-secret --from-file=ssh-privatekey=/path/to/.ssh/id_rsa --from-file=ssh-publickey=/path/to/.ssh/id_rsa.pub
The output is similar to:
secret "ssh-key-secret" created
Note that the filenames will be as follows:
ssh-privatekey ssh-publickey
You can also create a kustomization.yaml with a secretGenerator field containing ssh keys.
You can access the secrets this way:
kubectl get secret -n demographics ssh-key-secret -o jsonpath="{.data.ssh-privatekey}" | base64 --decode
If you want the files to use the standard names, since ssh-privatekey
is required, you need do something like this:
apiVersion: v1
kind: Secret
metadata:
name: secret-ssh-auth
type: kubernetes.io/ssh-auth
stringData:
ssh-privatekey: |
-
data:
id_rsa: |
SEVMTE9PT09PT09PT09PT09PT09PCg==
You can also mount the secret volume with specific key paths (filenames) for the items (keys).