kubernetes
k8s
jq

Get K8s secrets


In this guide, I assume the following:

  • You have using Amazon Elastic Kubernetes Service (EKS) to manage K8s clusters
    • Although the steps to fetch the secrets would be same regardless of the technology behind your K8s clusters
  • You have the following CLI tools installed:
    • AWS CLI v2 (if you are using EKS)
    • kubectl
    • jq

Here are the steps to fetch secrets from a K8s cluster:

  1. Update kube.config file with the appropriate cluster name. The <profile-name> would be the name of the local aws profile. If you do not have a specific profile, you may skip it, and aws will pick up the default profile.

    aws cli
    aws --profile <profile-name> eks update-kubeconfig --name <cluster-name>
  2. Verify if the k8s context was correctly updated. The above command should output the ARN of the EKS cluster

    Terminal window
    kubectl config current-context
  3. Fetch desired k8s secret and output in the form of JSON

    Terminal window
    kubectl get secret <secret-name> -n <namespace> -o json
  4. Pass the above output to jq for better deserialization

    Terminal window
    kubectl get secret <secret-name> -n <namespace> -o json | jq -r .
  5. Pass the encoded secret value to base64 to get the decoded value

    Terminal window
    kubectl get secret <secret-name> -n <namespace> -o json | jq -r ."<key-1>"."<key-2>" | base64 -d