CodeBlanks Logo Icon CodeBlanks

Summary - Secret

Combine YAML and mount to use sensitive data safely.

Summary: Secrets

In this group:

  • A Secret holds secret data; values are base64, not encrypted. They aren't put in Git and are protected with RBAC and etcd encryption.
  • Types: Opaque, kubernetes.io/tls, dockerconfigjson (imagePullSecrets).
  • In YAML, stringData accepts plain text; echo -n prevents the trailing newline.
  • Usage: secretKeyRef/secretRef or a secret.secretName volume (defaultMode).
  • Rotation: --dry-run=client -o yaml | kubectl apply -f - and rollout restart.
kubectl create secret generic db-gizli --from-file=DB_PASSWORD=./parola.txt
kubectl get secret db-gizli -o jsonpath='{.data.DB_PASSWORD}' | base64 -d

Practice Mount the TLS secret

Write a pod's spec section (starting with spec:): a tls volume from the web-tls Secret with defaultMode: 0400 under volumes; container named nginx, image nginx:1.27; mount tls at /etc/tls with readOnly: true.

Starter code

terminal
# buraya yaz

Sample solution

terminal
spec:
  volumes:
    - name: tls
      secret:
        secretName: web-tls
        defaultMode: 0400
  containers:
    - name: nginx
      image: nginx:1.27
      volumeMounts:
        - name: tls
          mountPath: /etc/tls
          readOnly: true

Expected output

pod/web configured
$ kubectl exec web -- ls /etc/tls
tls.crt
tls.key

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which is used to keep a Secret YAML safely in a Git repository?

  • Writing type: Opaque
  • A tool like Sealed Secrets or External Secrets Correct answer
  • Encoding with base64 once more
  • Using stringData

Explanation: Sealed Secrets produces an encrypted object that only the cluster can decrypt.

2 Mount the Secret as a volume.

Question
      volumes:
        - name: tls
          secret:
            ____: web-tls
Answer
      volumes:
        - name: tls
          secret:
            secretName: web-tls

Explanation: A Secret volume uses secretName.

3 Match.

Term Meaning
Secret Secret
ConfigMap Not secret
base64 -d Decode the value
imagePullSecrets Private registry

All lessons in Intermediate