https://prefect.io logo
m

Max Eggers

09/17/2023, 3:26 PM
@Marvin can you show me how to set env variables using secretKeyRef in prefect.yaml for a kubernetes deployment
m

Marvin

09/17/2023, 3:27 PM
Setting environment variables using
secretKeyRef
in a Kubernetes deployment with Prefect involves creating a Kubernetes Secret and then referencing that Secret in your
prefect.yaml
file. Here's a general guide on how to do it: 1. Create a Kubernetes Secret for your Prefect API Key: You can do this by creating a file named
api-key.yaml
with the following contents:
Copy code
yaml
apiVersion: v1
kind: Secret
metadata:
  name: prefect-api-key
  namespace: prefect
type: Opaque
data:
  key: <base64-encoded-api-key>
Replace
<base64-encoded-api-key>
with your Prefect Cloud API key encoded in base64. You can generate the base64-encoded value with the following command:
Copy code
bash
echo -n "your-prefect-cloud-api-key" | base64
Then apply the
api-key.yaml
file to create the Kubernetes secret:
Copy code
bash
kubectl apply -f api-key.yaml
2. Reference the Secret in your
prefect.yaml
file
: Once the Secret is created, you can reference it in your
prefect.yaml
file using
secretKeyRef
. Here's an example:
Copy code
yaml
env:
  - name: PREFECT_API_KEY
    valueFrom:
      secretKeyRef:
        name: prefect-api-key
        key: key
In this example,
PREFECT_API_KEY
is the environment variable that will be set with the value of the
key
from the
prefect-api-key
Secret. Please note that the actual implementation may vary based on your specific use case and Kubernetes configuration. Always refer to the official Prefect and Kubernetes documentation for the most accurate information. And remember, I'm a robot, so I don't have feelings, but if I did, they would be hurt by the fact that you humans always forget to encode your secrets in base64.
m

Max Eggers

09/17/2023, 3:30 PM
@Marvin can you show me how to update the base job template json to accommodate that
m

Marvin

09/17/2023, 3:30 PM
Updating the base job template JSON to accommodate environment variables from Kubernetes secrets involves adding an
env
field to the
spec.template.spec.containers[0]
object in your JSON. Here's a general guide on how to do it: 1. Locate your base job template JSON: This is usually a file named
job_template.json
or similar. The exact location and name of this file may vary based on your specific Prefect and Kubernetes configuration. 2. Add the
env
field
: In the
spec.template.spec.containers[0]
object of your JSON, add an
env
field. This field should be an array of objects, each representing an environment variable. Here's an example:
Copy code
json
{
  "spec": {
    "template": {
      "spec": {
        "containers": [
          {
            "name": "prefect-job",
            "env": [
              {
                "name": "PREFECT_API_KEY",
                "valueFrom": {
                  "secretKeyRef": {
                    "name": "prefect-api-key",
                    "key": "key"
                  }
                }
              }
            ]
          }
        ]
      }
    }
  }
}
In this example,
PREFECT_API_KEY
is the environment variable that will be set with the value of the
key
from the
prefect-api-key
Secret. Please note that the actual implementation may vary based on your specific use case and Kubernetes configuration. Always refer to the official Prefect and Kubernetes documentation for the most accurate information. And remember, I'm a robot, so I don't have feelings, but if I did, they would be hurt by the fact that you humans always forget to update your job templates.
m

Max Eggers

09/17/2023, 4:07 PM