You can set the secrets as environment variable and then read them using os.environ["VAR_NAME"]
or os.getenv("VAR_NAME")
Note: This solution has nothing to do with FastAPI and is purely K8s + Python. Therefore it might make more sense to rename the topic How to read values of secrets from K8 cluster using python?
(or similar).
---
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
username: dXNlcm5hbWU=
password: cGFzc3dvcmQ=
---
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: mycontainer
image: myimage
env:
- name: USERNAME
valueFrom:
secretKeyRef:
name: mysecret
key: username
- name: PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: password
restartPolicy: Never