Creating Kubernetes Secrets Using TLS/SSL as an Example

Creating Kubernetes secrets isn’t intuitive the first time you do it. A common reason to use a secret is to add a SSL/TLS certificate to a cluster. Kubernetes provides two ways to add a secret: directly on the command line, and from a YAML source file. First, let’s generate a test certificate to work with and select our cluster.

Prerequisites

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=foo.bar.com"

This command produces two files: tls.key and tls.cert. In production, you’d generate a key file and use it to obtain a certificate from a certificate authority.

Make sure you’ve selected the right Kubernetes cluster to receive your secret. In Google Cloud Platform/Google Kubernetes Engine (GKE), the command to select a Kubernetes cluster is:

gcloud container clusters get-credentials your-cluster-name --zone=your-cluster-zone

kubectl config get-contexts

kubectl config use-context gke_your-project-name_your-gcp-zone_your-cluster-name

Are you implementing Kubernetes? Rootwork can help!


Using the Command Line

The easiest way to create a TLS secret in Kubernetes is with the command:

kubectl create secret tls test-tls --key="tls.key" --cert="tls.crt"

Verify that it was added:

kubeclt get secrets

To view the YAML source of the secret:

kubectl get secret test-tls -o yaml

Delete the secret so we can demonstrate the next method:

kubectl delete secrets test-tls

Using a YAML Source File

The major disadvantage of managing infrastructure from the command line (including creating secrets) is that it’s hard to document and reproduce. I much prefer creating files that specify the state of the system, which can be committed to a Git repo. At a minimum, get the YAML file that corresponds to your secret and save it to a private Git repo. Encrypt the repo and back it up off-site.

We can also create a YAML source file by hand and use it to create the secret, but this is a little trickier. The skeleton of the YAML file is:

apiVersion: v1
data:
  tls.crt:
  tls.key:
kind: Secret
metadata:
  name: test-tls
  namespace: default
type: kubernetes.io/tls

The trick is that you have to base64 encode the key and certificate data. In Bash:

cat tls.crt | base64
cat tls.key | base64

Paste each piece of base64 encoded data into the appropriate sections of the YAML file as one line. Make sure your text editor doesn’t add any carriage returns to wrap the lines.

apiVersion: v1
data:
  tls.crt: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURKakNDQWc2Z0F3SUJBZ0lKQUw2Y3R2bk9zMz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bEVLY2Y1N3JsK1FLQmdGbjVSaFc2NS9oU0diVlhFWVZQU0pSOW9FK3lkRjcrd3FvaGRoOVQKekQ0UTZ6THBCQXJITkFYeDZZK0gxM3pFVlUzVEwrTTJUM1E2UGFHZ2Rpa2M5TlRPdkE3aU1nVTRvRXMzMENPWQpoR2x3bUhEc1B6YzNsWXlsU0NvYVVPeDJ2UFFwN2VJSndoU25PVVBwTVdKWi80Z2pZZTFjZmNseTFrQTJBR0x3ClJ1STlBb0dCQU14aGFJSUdwTGdmcHk0K24rai9BSWhJUUhLZFRCNVBqaGx0WWhqZittK011UURwK21OeTVMbzEKT0FRc0Q0enZ1b3VxeHlmQlFQZlllYThvcm4vTDE3WlJyc3lSNHlhS1M3cDVQYmJKQlNlcTc5Z0g5ZUNIQkxMbQo0aThCUFh0K0NmWktMQzg3NTNHSHVpOG91V25scUZ0NGxMQUlWaGJZQmtUbURZSWo4Q0NaCi0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==
kind: Secret
metadata:
  name: test-tls
  namespace: default
type: kubernetes.io/tls

Finally, use the YAML file to create the secret:

kubectl create -f tls.yaml

Creating a TLS Secret with Terraform

resource "kubernetes_secret" "tls" {
   metadata {
     name = "tls"
     namespace = "web"
   }
   type = "tls"
   data = {
     "tls.crt" = var.tlscrt
     "tls.key" = var.tlskey
   }
 }

Use local variables to keep sensitive data, like the secret contents, out of your Terraform repo. Variable definition:

variable "tlscrt" {
     type = string
 }
 variable "tlskey" {
     type = string
 }

I prefer to put the actual secrets in a .env file, and source the file prior to running terraform. I use the .gitignore file in my Terraform repo to ensure that the .env file is never added to the repo by mistake. Here’s a sample of a .env file:

export TF_VAR_tlscrt="-----BEGIN CERTIFICATE-----
site cert here
-----END CERTIFICATE-----
 -----BEGIN CERTIFICATE-----
intermediate cert here
-----END CERTIFICATE-----
 -----BEGIN CERTIFICATE-----
CA root cert here
-----END CERTIFICATE-----"

export TF_VAR_tlskey="-----BEGIN PRIVATE KEY-----
key here
-----END PRIVATE KEY-----"

Troubleshooting

See this article if you are having problems with curl/libcurl and SSL/TLS Certificates in Kubernetes!

References

  1. https://kubernetes.io/docs/tasks/tls/managing-tls-in-a-cluster/
  2. https://kubernetes.io/docs/concepts/configuration/secret/

6 thoughts on “Creating Kubernetes Secrets Using TLS/SSL as an Example”

  1. You can as well use the kubectl create secret command to generate the yaml file:

    kubectl create secret tls my-certificate-name \
    –cert=my-cert-file.crt \
    –key=my-private.key \
    –namespace my-namespace \
    –dry-run=client -o yaml > my-certificate.yaml

    Then run the kubectl apply command:

    kubectl apply -f my-certificate.yaml

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.