---
title: 'Kubernetes'
description: "How to use Infisical to inject secrets into Kubernetes clusters."
---

![title](../../images/k8-diagram.png)


The Infisical Secrets Operator is a Kubernetes controller that retrieves secrets from Infisical and stores them in a designated cluster. 
It uses an `InfisicalSecret` resource to specify authentication and storage methods.
The operator continuously updates secrets and can also reload dependent deployments automatically.

## Install Operator

The operator can be install via [Helm](helm.sh) or [kubectl](https://github.com/kubernetes/kubectl)

<Tabs>
	 <Tab title="Helm">
		Install Infisical Helm repository 
    ```bash
    helm repo add infisical-helm-charts 'https://dl.cloudsmith.io/public/infisical/helm-charts/helm/charts/' 
      
    helm repo update
    ```

    Install the Helm chart
    ```bash
    helm install --generate-name infisical-helm-charts/secrets-operator
    ```

   </Tab>
	 <Tab title="Kubectl">
   The operator will be installed in `infisical-operator-system` namespace
		```
    kubectl apply -f https://raw.githubusercontent.com/Infisical/infisical/main/k8-operator/kubectl-install/install-secrets-operator.yaml
		```
   </Tab>
</Tabs>

## Sync Infisical Secrets to your cluster
To retrieve secrets from an Infisical project and save them as native Kubernetes secrets within a specific namespace, utilize the `InfisicalSecret` custom resource definition (CRD). 
This resource can be created after installing the Infisical operator. For each new managed secret, you will need to create a new InfisicalSecret CRD.

```yaml
apiVersion: secrets.infisical.com/v1alpha1
kind: InfisicalSecret
metadata:
  # Name of of this InfisicalSecret resource
  name: infisicalsecret-sample
spec:
  # The host that should be used to pull secrets from. If left empty, the value specified in Global configuration will be used
  hostAPI: https://app.infisical.com/api
  authentication:
    serviceToken: # <-- option 1
      serviceTokenSecretReference:
        secretName: service-token
        secretNamespace: option
    serviceAccount: # <-- method 2
      serviceAccountSecretReference:
        secretName: service-account
        secretNamespace: default
      projectId: "6439ec224cfbf7ea2a95b651"
      environmentName: "dev"
  managedSecretReference:
    secretName: managed-secret # <-- the name of kubernetes secret that will be created
    secretNamespace: default # <-- where the kubernetes secret that will be created
```
### InfisicalSecret CRD properties

<Accordion title="hostAPI">
  If you are fetching secrets from a self hosted instance of Infisical set the value of `hostAPI` to 
  ` https://your-self-hosted-instace.com/api`

  When `hostAPI` is not defined the operator fetches secrets from Infisical Cloud. 

  <Accordion title="Advanced use case">
    If you have installed your Infisical instance within the same cluster as the Infisical operator, you can optionally access the Infisical backend's service directly without having to route through the public internet. 
    To achieve this, use the following address for the hostAPI field:
    
    ``` bash
    http://<backend-svc-name>.<namespace>.svc.cluster.local:4000/api
    ```

    Make sure to replace `<backend-svc-name>` and `<namespace>` with the appropriate values for your backend service and namespace.
  </Accordion>
</Accordion>

<Accordion title="authentication">
  The `authentication` property tells the operator where it should look to find credentials needed to fetch secrets from Infisical. You can authenticate via two methods as described below.

  <Tabs>
    <Tab title="Service Token">
      Authenticating with service tokens is a great option when you have a small number of services you'd like to fetch secrets for and are looking for the least amount of setup. 
      
      #### 1. Generate service token 

      You can generate a service token for an Infisical project by heading over to the Infisical dashboard then to Project Settings.

      #### 2. Create Kubernetes secret containing service token 

      Once you have generated the service token, you will need to create a Kubernetes secret containing the service token you generated. 
      To quickly create a Kubernetes secret containing the generated service token, you can run the command below.

      ``` bash
      kubectl create secret generic service-token --from-literal=infisicalToken=<your-service-token-here> 
      ```

      #### 3. Add reference for the Kubernetes secret containing service token 

      Once the secret is created, add the name and namespace of the secret that was just created under `authentication.serviceToken.serviceTokenSecretReference` field in the InfisicalSecret resource.

      ## Example
      ```yaml
      apiVersion: secrets.infisical.com/v1alpha1
      kind: InfisicalSecret
      metadata:
        name: infisicalsecret-sample-crd
      spec:
        authentication:
          serviceToken: 
            serviceTokenSecretReference:
              secretName: service-token # <-- name of the Kubernetes secret that stores our service token
              secretNamespace: option # <-- namespace of the Kubernetes secret that stores our service token
        ...
      ```

    </Tab>
    <Tab title="Service Account">
      We recommend authenticating with service account credentials when you have a large number of services. With this method, instead of creating a service token for each Infisical project you'd like to 
      fetch secrets from, you can fetch secrets from a number of Infisical projects with just one set of credentials.
      
      #### 1. Generate service account

      You can generate a service account by heading over to the organization settings. Once you create the service account, keep the credentials at hand for the next steps.

      #### 2. Grant service account access to Infisical projects

      Click on the pencil icon on the service account you just created and add the projects you'd like to be accessible via that service account.

      #### 3. Store service account credentials in K8 secret  
      Next, we'll need to store the service account credentials in a kubernetes secret so that we can reference it in our InfisicalSecret CRD. 

      We recommend you create this kubernetes secret in a new namespace since you may need to reference it many times for each InfisicalSecret CRD you create.

      To quickly create a Kubernetes secret containing the service account details, you can execute the command below after replacing it with your own service account credentials.

      ```
      kubectl create secret generic service-token --from-literal=serviceAccountAccessKey=[REPLACE] --from-literal=serviceAccountPrivateKey=[REPLACE]  --from-literal=serviceAccountPublicKey=[REPLACE] 
      ```

      Regardless of how you create the kubernetes secret containing the service account credentials, you will need to define values for the following keys in the secret: `serviceAccountAccessKey`, `serviceAccountPrivateKey`, and `serviceAccountPublicKey` 

      Once the secret is created, add the name and namespace of the secret that was just created under `authentication.serviceAccount.serviceAccountSecretReference` field in the InfisicalSecret CRD.

      #### 4. Add projectId and environment from which to fetch secrets from
      Add the Infisical project id and environment from which to fetch secrets for by providing values under `authentication.serviceAccount.projectId` and `authentication.serviceAccount.environmentName`.

      ## Example
      ```yaml
      apiVersion: secrets.infisical.com/v1alpha1
      kind: InfisicalSecret
      metadata:
        name: infisicalsecret-sample-crd
      spec:
        serviceAccount:
        serviceAccountSecretReference:
          secretName: service-account
          secretNamespace: default
        projectId: "6439ec224cfbf7ea2a95b651"
        environmentName: "dev"
      ```
    </Tab>
  </Tabs>
</Accordion>

<Accordion title="managedSecretReference">
The `managedSecretReference` field in the InfisicalSecret resource is used to specify the location where secrets retrieved from an Infisical project should be stored. 
You should specify the name and namespace of the Kubernetes secret that will hold these secrets. The operator will create the secret for you, you just need to provide its name and namespace.

The managed secret be should be created in the same namespace as the deployment that will use it.

</Accordion>

### Verify managed secret creation 

To verify that the operator has successfully created the managed secret, you can check the secrets in the namespace that was specified. 

```bash
# Verify managed secret is created
kubectl get secrets -n <namespace of managed secret>
```

<Info>
  The Infisical secrets will be synced and stored into the managed secret every
  1 minutes.
</Info>

### Using managed secret in your deployment 
Incorporating the managed secret created by the operator into your deployment can be achieved through several methods. 
Here, we will highlight three of the most common ways to utilize it. Learn more about Kubernetes secrets [here](https://kubernetes.io/docs/concepts/configuration/secret/)

<Accordion title="envFrom">
  This will take all the secrets from your managed secret and expose them to your container 

  ```yaml
    envFrom:
      - secretRef:
          name: managed-secret # managed secret name
    ```

    Example usage in a deployment
    ```yaml 
    apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: nginx-deployment
    labels:
      app: nginx
  spec:
    replicas: 1
    selector:
      matchLabels:
        app: nginx
    template:
      metadata:
        labels:
          app: nginx
      spec:
        containers:
        - name: nginx
          image: nginx:1.14.2
          envFrom:
          - secretRef:
              name: managed-secret # <- name of managed secret
          ports:
          - containerPort: 80
  ```
</Accordion>


<Accordion title="env"> 
  This will allow you to select individual secrets by key name from your managed secret and expose them to your container 
  
  ```yaml
  env:
    - name: SECRET_NAME # The environment variable's name which is made available in the container
      valueFrom:
        secretKeyRef:
          name: managed-secret # managed secret name
          key: SOME_SECRET_KEY # The name of the key which  exists in the managed secret
  ```

  Example usage in a deployment
    ```yaml 
  apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: nginx-deployment
    labels:
      app: nginx
  spec:
    replicas: 1
    selector:
      matchLabels:
        app: nginx
    template:
      metadata:
        labels:
          app: nginx
      spec:
        containers:
        - name: nginx
          image: nginx:1.14.2
          env:
            - name: STRIPE_API_SECRET 
              valueFrom:
                secretKeyRef:
                  name: managed-secret # <- name of managed secret
                  key: STRIPE_API_SECRET
          ports:
          - containerPort: 80
  ```
</Accordion>

<Accordion title="volumes">
  This will allow you to create a volume on your container which comprises of files holding the secrets in your managed kubernetes secret
  ```yaml
  volumes:
    - name: secrets-volume-name # The name of the volume under which secrets will be stored
      secret:
        secretName: managed-secret # managed secret name
  ```

  You can then mount this volume to the container's filesystem so that your deployment can access the files containing the managed secrets
  ```yaml
  volumeMounts:
    - name: secrets-volume-name
      mountPath: /etc/secrets
      readOnly: true
  ```

  Example usage in a deployment
  ```yaml 
  apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: nginx-deployment
    labels:
      app: nginx
  spec:
    replicas: 1
    selector:
      matchLabels:
        app: nginx
    template:
      metadata:
        labels:
          app: nginx
      spec:
        containers:
        - name: nginx
          image: nginx:1.14.2
          volumeMounts:
          - name: secrets-volume-name
            mountPath: /etc/secrets
            readOnly: true
          ports:
          - containerPort: 80
        volumes:
        - name: secrets-volume-name
          secret:
            secretName: managed-secret # <- managed secrets
  ```
</Accordion>

## Auto redeployment 
Deployments using managed secrets don't reload automatically on updates, so they may use outdated secrets unless manually redeployed. 
To address this, we added functionality to automatically redeploy your deployment when its managed secret updates.

### Enabling auto redeploy 
To enable auto redeployment you simply have to add the following annotation to the deployment that consumes a managed secret
```yaml
secrets.infisical.com/auto-reload: "true"
```

<Accordion title="Deployment example with auto redeploy enabled">
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
  annotations: 
    secrets.infisical.com/auto-reload: "true" # <- redeployment annotation
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        envFrom:
        - secretRef:
            name: managed-secret
        ports:
        - containerPort: 80
```
</Accordion>

## Global configuration 
To configure global settings that will apply to all instances of `InfisicalSecret`, you can define these configurations in a Kubernetes ConfigMap. 
For example, you can configure all `InfisicalSecret` instances to fetch secrets from a single backend API without specifying the `hostAPI` parameter for each instance.

### Available global properties
| Property | Description                           | Default value
| -------- | ------------------------------------- |------------------------
| hostAPI  | If `hostAPI` in `InfisicalSecret` instance is left empty, this value will be used                    | https://app.infisical.com/api


### Applying global configurations
All global configurations must reside in a Kubernetes ConfigMap named `infisical-config` in the namespace `infisical-operator-system`. 
To apply global configuration to the operator, copy the following yaml into `infisical-config.yaml` file.

```yaml infisical-config.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: infisical-operator-system
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: infisical-config
  namespace: infisical-operator-system
data:
  hostAPI: https://example.com/api # <-- global hostAPI
```

Then apply this change via kubectl by running the following 

```bash 
kubectl apply -f infisical-config.yaml 
```


## Troubleshoot operator

If the operator is unable to fetch secrets from the API, it will not affect the managed Kubernetes secret.
It will continue attempting to reconnect to the API indefinitely.
The InfisicalSecret resource uses the `status.conditions` field to report its current state and any errors encountered.

```yaml
$ kubectl get infisicalSecrets
NAME                     AGE
infisicalsecret-sample   12s

$ kubectl describe infisicalSecret infisicalsecret-sample
...
Spec:
...
Status:
  Conditions:
    Last Transition Time:  2022-12-18T04:29:09Z
    Message:               Infisical controller has located the Infisical token in provided Kubernetes secret
    Reason:                OK
    Status:                True
    Type:                  secrets.infisical.com/LoadedInfisicalToken
    Last Transition Time:  2022-12-18T04:29:10Z
    Message:               Failed to update secret because: 400 Bad Request
    Reason:                Error
    Status:                False
    Type:                  secrets.infisical.com/ReadyToSyncSecrets
Events:                    <none>
```

## Uninstall Operator

The managed secret created by the operator will not be deleted when the operator is uninstalled.

<Tabs>
	 <Tab title="Helm">
		Install Infisical Helm repository 
    ```bash
    helm uninstall <release name>
    ```
   </Tab>
	 <Tab title="Kubectl">
		```
    kubectl delete -f https://raw.githubusercontent.com/Infisical/infisical/main/k8-operator/kubectl-install/install-secrets-operator.yaml
		```
   </Tab>
</Tabs>
