Bird
Raised Fist0
Kubernetesdevops~5 mins

Secret types (Opaque, docker-registry, TLS) in Kubernetes - Commands & Configuration

Choose your learning style10 modes available
Introduction
Kubernetes secrets store sensitive information like passwords or certificates safely. Different secret types help Kubernetes understand how to use the stored data correctly.
When you need to store a username and password for accessing a private Docker registry.
When you want to keep TLS certificates safe for your web server inside the cluster.
When you have custom sensitive data like API keys or tokens that your app needs.
When you want to avoid putting sensitive info directly in your pod configuration files.
When you want Kubernetes to handle secret data securely and inject it into your containers.
Config File - secrets.yaml
secrets.yaml
apiVersion: v1
kind: Secret
metadata:
  name: my-opaque-secret
type: Opaque
data:
  api-key: YXBpa2V5MTIz
---
apiVersion: v1
kind: Secret
metadata:
  name: my-docker-secret
type: kubernetes.io/dockerconfigjson
data:
  .dockerconfigjson: eyJhdXRocyI6eyJodHRwczovL2luZGV4LmRvY2tlci5pby92MS8iOnsidXNlcm5hbWUiOiJteXVzZXIiLCJwYXNzd29yZCI6Im15cGFzc3dvcmQiLCJhdXRoIjoiTXlBdXRoVG9rZW4ifX19
---
apiVersion: v1
kind: Secret
metadata:
  name: my-tls-secret
type: kubernetes.io/tls
data:
  tls.crt: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCg==
  tls.key: LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQo=

This file defines three secrets:

  • Opaque: Stores generic base64-encoded data like API keys.
  • docker-registry: Stores Docker registry credentials in a JSON format.
  • TLS: Stores TLS certificate and private key for secure communication.

Kubernetes uses the type field to know how to handle each secret.

Commands
This command creates the three secrets in Kubernetes from the secrets.yaml file.
Terminal
kubectl apply -f secrets.yaml
Expected OutputExpected
secret/my-opaque-secret created secret/my-docker-secret created secret/my-tls-secret created
This command lists all secrets in the current namespace to verify they were created.
Terminal
kubectl get secrets
Expected OutputExpected
NAME TYPE DATA AGE my-opaque-secret Opaque 1 10s my-docker-secret kubernetes.io/dockerconfigjson 1 10s my-tls-secret kubernetes.io/tls 2 10s
This command shows detailed info about the docker-registry secret, confirming its type and data keys.
Terminal
kubectl describe secret my-docker-secret
Expected OutputExpected
Name: my-docker-secret Namespace: default Labels: <none> Annotations: <none> Type: kubernetes.io/dockerconfigjson Data ==== .dockerconfigjson: 223 bytes
Key Concept

If you remember nothing else from this pattern, remember: Kubernetes secret types tell the system how to use and interpret your sensitive data correctly.

Common Mistakes
Creating a secret with type Opaque but storing Docker registry credentials without proper JSON format.
Kubernetes won't recognize the data as Docker credentials, so image pulls from private registries will fail.
Use type kubernetes.io/dockerconfigjson and encode the Docker config JSON correctly.
Storing TLS certificates in a secret with type Opaque instead of kubernetes.io/tls.
Kubernetes and some tools expect TLS secrets to have specific keys and type to work properly.
Use type kubernetes.io/tls and include tls.crt and tls.key keys with base64-encoded data.
Not base64 encoding secret data before applying the secret manifest.
Kubernetes requires secret data to be base64 encoded; otherwise, it rejects the secret or stores invalid data.
Always base64 encode your secret values before adding them to the manifest.
Summary
Create secrets with the correct type to match the kind of sensitive data you store.
Use kubectl apply to create secrets from YAML files and kubectl get/describe to verify them.
Base64 encode all secret data values before adding them to the secret manifest.

Practice

(1/5)
1. Which Kubernetes secret type is best suited for storing general sensitive data like passwords or tokens?
easy
A. Opaque
B. docker-registry
C. TLS
D. ConfigMap

Solution

  1. Step 1: Understand secret types

    Opaque secrets are designed to store arbitrary user-defined data such as passwords or tokens.
  2. Step 2: Compare with other types

    docker-registry is for Docker credentials, TLS is for certificates, and ConfigMap is not a secret type.
  3. Final Answer:

    Opaque -> Option A
  4. Quick Check:

    General sensitive data = Opaque [OK]
Hint: Opaque = general secret data, docker-registry = Docker creds [OK]
Common Mistakes:
  • Confusing TLS secrets with general data storage
  • Using docker-registry for non-Docker secrets
  • Thinking ConfigMap is a secret type
2. Which kubectl command correctly creates a docker-registry secret named myregistrykey with username user1 and password pass123?
easy
A. kubectl create secret docker-registry myregistrykey --docker-server=https://myregistry.com --docker-username=user1 --docker-password=pass123
B. kubectl create secret generic myregistrykey --docker-username=user1 --docker-password=pass123
C. kubectl create secret tls myregistrykey --docker-username=user1 --docker-password=pass123
D. kubectl create secret docker-registry myregistrykey --username=user1 --password=pass123

Solution

  1. Step 1: Identify correct secret type and flags

    docker-registry secrets require the type 'docker-registry' and flags --docker-server, --docker-username and --docker-password.
  2. Step 2: Check command syntax

    kubectl create secret docker-registry myregistrykey --docker-server=https://myregistry.com --docker-username=user1 --docker-password=pass123 uses correct type and flags; others use wrong types or flag names.
  3. Final Answer:

    kubectl create secret docker-registry myregistrykey --docker-server=https://myregistry.com --docker-username=user1 --docker-password=pass123 -> Option A
  4. Quick Check:

    docker-registry + --docker-server/--docker-username/password = correct [OK]
Hint: Use --docker-server, --docker-username and --docker-password with docker-registry type [OK]
Common Mistakes:
  • Using generic secret type for docker-registry secrets
  • Wrong flag names like --username instead of --docker-username
  • Using TLS secret type for docker credentials
3. Given the command kubectl create secret tls mytlssecret --cert=cert.pem --key=key.pem, what will be the type of the created secret?
medium
A. Opaque
B. docker-registry
C. TLS
D. generic

Solution

  1. Step 1: Analyze the command

    The command uses 'create secret tls' with certificate and key files specified.
  2. Step 2: Match secret type

    Secrets created with 'tls' type store TLS certificates and keys, so the type is TLS.
  3. Final Answer:

    TLS -> Option C
  4. Quick Check:

    kubectl create secret tls = TLS type secret [OK]
Hint: TLS secrets use 'kubectl create secret tls' with cert and key [OK]
Common Mistakes:
  • Assuming TLS secrets are Opaque
  • Confusing generic secret type with TLS
  • Not specifying cert and key files properly
4. You run kubectl create secret tls mytlssecret --cert=cert.pem but get an error. What is the likely cause?
medium
A. Using wrong secret type, should be opaque
B. Missing --key flag with private key file
C. Certificate file is empty
D. Secret name contains invalid characters

Solution

  1. Step 1: Check required flags for TLS secret

    Creating TLS secrets requires both --cert and --key flags to specify certificate and private key files.
  2. Step 2: Identify missing flag

    The command only has --cert but lacks --key, causing the error.
  3. Final Answer:

    Missing --key flag with private key file -> Option B
  4. Quick Check:

    Both --cert and --key required for TLS secret [OK]
Hint: TLS secrets need both --cert and --key flags [OK]
Common Mistakes:
  • Omitting the --key flag when creating TLS secrets
  • Using opaque type instead of TLS for certs
  • Assuming secret name format causes error
5. You want to pull images from a private Docker registry in your Kubernetes cluster. Which secret type should you create and how should you use it in a Pod spec?
hard
A. Create a TLS secret and add it to the Pod's environment variables
B. Create an opaque secret and mount it as a volume in the Pod
C. Create a generic secret and specify it in the Pod's volumes section
D. Create a docker-registry secret and reference it in imagePullSecrets of the Pod

Solution

  1. Step 1: Identify secret type for private Docker registry

    docker-registry secret stores Docker credentials for private registries.
  2. Step 2: Use secret in Pod spec

    To allow image pull, reference the secret in the Pod's imagePullSecrets field.
  3. Final Answer:

    Create a docker-registry secret and reference it in imagePullSecrets of the Pod -> Option D
  4. Quick Check:

    Private Docker registry = docker-registry secret + imagePullSecrets [OK]
Hint: Use docker-registry secret with imagePullSecrets in Pod spec [OK]
Common Mistakes:
  • Mounting docker-registry secrets as volumes instead of imagePullSecrets
  • Using opaque or TLS secrets for Docker registry auth
  • Not referencing the secret in Pod spec
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard