📖 Scenario: You are managing a Kubernetes cluster for a small company. You need to securely store different types of sensitive data such as generic secrets, Docker registry credentials, and TLS certificates. Kubernetes provides different secret types for these use cases.In this project, you will create three Kubernetes secrets of types Opaque, kubernetes.io/dockerconfigjson, and kubernetes.io/tls to understand how to handle each type.
🎯 Goal: Create three Kubernetes secrets using YAML files: one Opaque secret with a username and password, one docker-registry secret with Docker registry credentials, and one TLS secret with certificate and key files. Then, verify the secrets are created correctly.
📋 What You'll Learn
Create an Opaque secret named my-opaque-secret with username and password data.
Create a docker-registry secret named my-docker-secret with Docker registry credentials.
Create a TLS secret named my-tls-secret with certificate and key files.
Use kubectl commands to apply the secrets and verify their creation.
💡 Why This Matters
🌍 Real World
Kubernetes secrets are used to store sensitive information like passwords, tokens, and certificates securely, preventing them from being exposed in plain text in configuration files or container images.
💼 Career
Knowing how to create and manage different Kubernetes secret types is essential for DevOps engineers and cloud administrators to secure applications running in Kubernetes clusters.
Progress0 / 4 steps
1
Create an Opaque secret YAML
Create a YAML file named opaque-secret.yaml that defines a Kubernetes secret of type Opaque named my-opaque-secret. Include the data fields username with value admin and password with value secret123. Use base64 encoding for the values.
Kubernetes
Hint
Use echo -n 'admin' | base64 and echo -n 'secret123' | base64 to get base64 encoded values.
2
Create a docker-registry secret YAML
Create a YAML file named docker-secret.yaml that defines a Kubernetes secret of type kubernetes.io/dockerconfigjson named my-docker-secret. Use the following Docker registry credentials encoded in base64 inside the .dockerconfigjson key: server https://index.docker.io/v1/, username dockeruser, password dockersecret, email user@example.com. The .dockerconfigjson value must be a base64 encoded JSON string with these credentials.
Kubernetes
Hint
Prepare a JSON like {"auths":{"https://index.docker.io/v1/":{"username":"dockeruser","password":"dockersecret","email":"user@example.com","auth":"ZG9ja2VydXNlcjpkb2NrZXJzZWNyZXQ="}}} and base64 encode it.
Replace dockerauth_token with base64 of dockeruser:dockersecret.
3
Create a TLS secret YAML
Create a YAML file named tls-secret.yaml that defines a Kubernetes secret of type kubernetes.io/tls named my-tls-secret. Use the keys tls.crt and tls.key with base64 encoded dummy certificate and key values: dummycert and dummykey respectively.
Kubernetes
Hint
Base64 encode dummycert and dummykey using echo -n 'dummycert' | base64 and echo -n 'dummykey' | base64.
4
Apply and verify the secrets
Use kubectl apply -f to create the secrets from the YAML files opaque-secret.yaml, docker-secret.yaml, and tls-secret.yaml. Then, run kubectl get secrets my-opaque-secret my-docker-secret my-tls-secret -o yaml to display the secrets and verify their types and data keys.
Kubernetes
Hint
Run kubectl apply -f for each YAML file, then kubectl get secrets with -o yaml to see details.
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
Step 1: Understand secret types
Opaque secrets are designed to store arbitrary user-defined data such as passwords or tokens.
Step 2: Compare with other types
docker-registry is for Docker credentials, TLS is for certificates, and ConfigMap is not 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
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.
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.
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
Step 1: Analyze the command
The command uses 'create secret tls' with certificate and key files specified.
Step 2: Match secret type
Secrets created with 'tls' type store TLS certificates and keys, so the type is TLS.
Final Answer:
TLS -> Option C
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
Step 1: Check required flags for TLS secret
Creating TLS secrets requires both --cert and --key flags to specify certificate and private key files.
Step 2: Identify missing flag
The command only has --cert but lacks --key, causing the error.
Final Answer:
Missing --key flag with private key file -> Option B
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
Step 1: Identify secret type for private Docker registry
docker-registry secret stores Docker credentials for private registries.
Step 2: Use secret in Pod spec
To allow image pull, reference the secret in the Pod's imagePullSecrets field.
Final Answer:
Create a docker-registry secret and reference it in imagePullSecrets of the Pod -> Option D