docs/documentation/guides/kubernetes-operator.mdx
Infisical's Kubernetes Operator provides a seamless, secure, and automated way to synchronize secrets between your Infisical instance and your Kubernetes clusters. The Operator's three Custom Resource Definitions (CRDs) make this possible. In this guide, we provide the necessary CRDs and configurations for your kubernetes cluster, but you can customize them to fit your use-case.
In this guide, we'll walk through how to:
Before we begin, make sure your environment is ready
The Infisical Operator runs inside your cluster and is responsible for handling secret synchronization events.
helm repo add infisical-helm-charts 'https://dl.cloudsmith.io/public/infisical/helm-charts/helm/charts/'
helm repo update
helm install infisical-operator infisical-helm-charts/secrets-operator
Verify the operator pod is running:
kubectl get pods -n default
The operator uses a Machine Identity to authenticate with Infisical through the Kubernetes Auth Method
kubectl cluster-infoNow we will be interacting with the local repository you cloned earlier. Make sure you are in the directory that contains the yaml configurations. Assuming you are in your root user directory:
cd infisical-guides-source-code/kubernetes-operator-demo
infisical-token-reviewer service account. This Manifest creates a service account that the Infisical Operator uses to authenticate with Kubernetes for token reviews. It allows Infisical to validate Kubernetes tokens securely during the Machine Identity authentication process.kubectl apply -f infisical-reviewer-service-account.yaml
kubectl apply -f service-account-reviewer-token.yaml
infisical-token-reviewer service account to the built-in system:auth-delegator ClusterRole. That role allows the service account to perform token review and authentication delegation requests on behalf of other service accounts - a key part of Kubernetes-based identity verification. Without this binding, the Infisical Operator wouldn't have permission to validate tokens.kubectl apply -f cluster-role-binding.yaml
infisical-service-account that the Infisical Operator uses to access and sync secrets within your cluster. It operates as the Operator's working identity in your cluster, separate from the token reviewer.kubectl apply -f infisical-service-account.yaml
infisical-service-account. It allows the Infisical operator to authenticate against Infisical's API when syncing secrets. The token will then be manually patched and associated with the service account to make sure Kubernetes mains it persistently.kubectl apply -f infisical-service-account-token.yaml
kubectl patch serviceaccount infisical-service-account -p '{"secrets": [{"name": "infisical-service-account-token"}]}' -n default
kubectl get secret infisical-token-reviewer-token -n default -o jsonpath='{.data.token}' | base64 -d
kubectl get secret infisical-token-reviewer-token -n default -o jsonpath='{.data.ca\.crt}' | base64 -d
kubectl get serviceaccount -n default | grep infisical
kubectl get secrets -n default | grep infisical
The InfisicalSecret CRD tells the operator to sync secrets from Infisical to Kubernetes. By referencing your identityID, projectSlug, and envSlug, this CRD tells the Infisical Operator which Infisical secrets to fetch and how to format them into a Kubernetes Secret. Make sure to edit the provided CRD to match your specific Machine Identity ID, Project ID, and which environment your secrets are being pulled from (default is prod).
- Project Slug: Can be found when you select your project and navigate to settings
- Identity ID: Can be found when you select your machine identity from your organization's access control
example-infisical-secret-crd.yaml to contain your demo-specific values, apply the yaml in your clusterkubectl apply -f example-infisical-secret-crd.yaml
InfisicalSecret was created successfullykubectl get infisicalsecret -n default
managed-secretkubectl get secret managed-secret -n default
kubectl get secret managed-secret -n default -o jsonpath='{.data}' | jq
kubectl apply -f demo-deployment.yaml
kubectl get deployments
kubectl get pods -l app=nginx
kubectl exec -it $(kubectl get pod -l app=nginx -o jsonpath='{.items[0].metadata.name}') -- env | grep SMTP
Now that we have successfully synced secrets from Infisical to Kubernetes, lets explore how we can push Kubernetes Secrets to Infisical.
1. Either create a **Kubernetes Secret** via yaml, or use the one in the repository.
kubectl apply -f source-secret.yaml
2. Verify creation of the secret
kubectl get secret push-secret-demo -n default -o yaml
The InfisicalPushSecret CRD tells the operator to sync secrets from Kubernetes to Infisical. Make sure you edit the CRD to include the specific Project Slug, and Identity ID. The other values present in example-push-secret.yaml should be configured based on the previously committed yaml configurations.
1. Apply the InfisicalPushSecret CRD provided after making the necessary changes
kubectl apply -f example-push-secret-crd.yaml
2. Once your CRD has been configured, go back to your project within Infisical and check to see if your secrets have populated there.
The InfisicalDynamicSecret CRD allows you to sync dynamic secrets and create leases automatically in Kubernetes as native Kubernetes Secret resources Any Pod, Deployment, or other Kubernetes resource can make use of dynamic secrets from Infisical just like any other Kubernetes secret.
1. Navigate to your Infisical **Project** and click on the dropdown next to **Add Secret**. From here you will select **Add Dynamic Secret**
- Select **SQL Database** as the service you would like to connect to.
- Select **PostegreSQL** as the database service. Enter in the connection details for your database, specifically the **Host**, **Port**, **User**, **Password**, and **Database Name**.
- For the **Secret Name**, if you want to use the same name as the one in the cloned **InfisicalDynamicSecret** CRD, use the name **dynamic-secret-lease**. Otherwise you will need to change the **dynamicSecret.secretName** config in the InfisicalDynamicSecret CRD to whatever you name the secret here.
- In the CA (SSL) section, make sure to upload the **CA Certificate** for your database.
- Finally, select **Prod** as the environment (we are keeping this configuration as part of the demonstration).
2. Edit the ```dynamic-secret-crd``` with the proper machine **Identity ID**, **Project Slug**, **dynamicSecret.secretName** (same as the **Secret Name** you gave to the dynamic secret in Infisical), and managedSecretReference.secretName (name of the kubernetes secret that Infisical Operator will create/populate in the cluster).
- If you want to keep the **managedSecretReference.secretName** then you can leave it as **dynamic-secret-test**
3. Once the changes have been saved, apply the yaml:
```console
kubectl apply -f dynamic-secret-crd.yaml
```
4. After applying the CRD, you should notice that the dynamic secret lease has been created and synced with your cluster. Verify by running:
kubectl get secret dynamic-secret-test -n default -o yaml
5. Once the dynamic secret lease has been created, you should see that the secret has data that contains the lease credentials.
</Step> </Steps>
Congratulations! You successfully managed secrets with Kubernetes.