Skip to main content
Learn how to encrypt and manage your Kubernetes secrets in Git using Mozilla SOPS with a PGP key, then let FluxCD decrypt them automatically on apply.

Prerequisites

  • A Git repository with your application code checked out.
  • Administrator-generated PGP keypair (public key committed in infrastructure/SOPS/).
  • FluxCD installed in your cluster.
  • gpg, git, wget, and kubectl available on your machine.

Table of Contents

  1. Prepare the Repository
  2. Import the Public PGP Key
  3. Install SOPS
  4. Encrypt the Secret with SOPS
  5. Commit and Push
  6. Configure FluxCD Decryption
  7. Verify Decrypted Secret in Cluster

1. Prepare the Repository

Switch to your infrastructure branch and restore the plaintext secret for re-encryption.
Always back up existing sealed or encrypted secrets before modifying them.
Verify the plaintext Secret at database/secret-mysql.yaml:

2. Import the Public PGP Key

On a fresh developer machine, confirm you have no existing public keys:
Import the administrator’s public key:
Validate the import and note the fingerprint (e.g., CE284BB236654E42A):
You will use the PGP fingerprint with the sops CLI to encrypt your secret.

3. Install SOPS

Install the SOPS binary if it’s not already present:
Confirm the installation:

4. Encrypt the Secret with SOPS

Navigate to the directory containing your plaintext secret:
Encrypt only the data and stringData sections in place:
After encryption, secret-mysql.yaml will include an sops: block:

Encryption Backends Supported by SOPS


5. Commit and Push

Add the encrypted secret to your Git repository and push:

6. Configure FluxCD Decryption

FluxCD needs the private key stored in a Kubernetes Secret (e.g., sops-gpg) and decryption enabled in the Kustomization manifest. Edit infrastructure/flux/kustomization-database.yaml:
Commit and push the FluxCD configuration:
For details, see FluxCD Kustomization Documentation.

7. Verify Decrypted Secret in Cluster

Trigger reconciliation and inspect the applied secret:
You should see the original plaintext password, confirming that FluxCD decrypted the secret before applying it.

Watch Video

Practice Lab