📖 Scenario: You are managing a Kubernetes cluster and need to store sensitive information like passwords securely. Kubernetes Secrets require data to be base64 encoded before storing.
🎯 Goal: Learn how to create a Kubernetes Secret by base64 encoding the data values manually and then defining the Secret YAML file correctly.
📋 What You'll Learn
Create a plain text file with the secret data
Encode the secret data using base64
Create a Kubernetes Secret YAML file using the encoded data
Verify the Secret contains the correct base64 encoded values
💡 Why This Matters
🌍 Real World
Kubernetes Secrets store sensitive information like passwords, tokens, and keys securely in clusters. Base64 encoding is required to safely store binary or text data in YAML files.
💼 Career
Understanding how to create and manage Kubernetes Secrets is essential for DevOps engineers and cloud administrators to protect sensitive data in production environments.
Progress0 / 4 steps
1
Create a plain text file with the secret data
Create a file named password.txt containing the exact text myS3cretPass.
Kubernetes
Hint
Use the echo command with -n to avoid adding a newline.
2
Encode the secret data using base64
Use the base64 command to encode the contents of password.txt and save the output to a file named password.b64.
Kubernetes
Hint
Use base64 command and redirect output to password.b64.
3
Create a Kubernetes Secret YAML file using the encoded data
Create a file named secret.yaml with the following content exactly:
The password value is the base64 encoded string of myS3cretPass.
Kubernetes
Hint
Use a here document or any text editor to create secret.yaml with the exact content.
4
Verify the Secret contains the correct base64 encoded values
Run kubectl apply -f secret.yaml to create the Secret, then run kubectl get secret mysecret -o yaml and print the output.
Kubernetes
Hint
Use kubectl apply -f secret.yaml to create the secret and kubectl get secret mysecret -o yaml to view it.
Practice
(1/5)
1. What is the main reason Kubernetes Secrets use Base64 encoding for their data?
easy
A. To compress the secret data to save space
B. To encrypt the secret data for security
C. To convert binary data into text format for safe storage
D. To convert text data into binary format for faster access
Solution
Step 1: Understand Base64 encoding purpose
Base64 encoding changes binary data into readable text characters so it can be stored in text files or systems.
Step 2: Apply to Kubernetes Secrets
Kubernetes Secrets store sensitive data as text in YAML files, so Base64 encoding is used to safely represent binary or special characters as text.
Final Answer:
To convert binary data into text format for safe storage -> Option C
Quick Check:
Base64 encoding = text format for storage [OK]
Hint: Base64 makes binary data safe as text in Secrets [OK]
Common Mistakes:
Thinking Base64 encrypts data
Confusing Base64 with compression
Assuming Base64 changes data speed
2. Which of the following is the correct YAML snippet for a Kubernetes Secret with a key named password and value cGFzc3dvcmQ= (Base64 encoded)?
easy
A. apiVersion: v1
kind: Secret
stringData:
password: cGFzc3dvcmQ=
B. apiVersion: v1
kind: Secret
data:
password: cGFzc3dvcmQ=
C. apiVersion: v1
kind: Secret
secrets:
password: cGFzc3dvcmQ=
D. apiVersion: v1
kind: Secret
values:
password: cGFzc3dvcmQ=
Solution
Step 1: Identify correct field for Base64 data
Kubernetes Secrets use the data field to store Base64 encoded values.
Step 2: Check YAML structure
The correct YAML must have apiVersion, kind, and data with the key and Base64 value.
Final Answer:
apiVersion: v1
kind: Secret
data:
password: cGFzc3dvcmQ= -> Option B
Quick Check:
Base64 data goes under data field [OK]
Hint: Base64 values go under 'data', not 'stringData' or others [OK]
Common Mistakes:
Using stringData for Base64 encoded values
Wrong field names like secrets or values
Missing apiVersion or kind fields
3. Given this command: kubectl create secret generic mysecret --from-literal=username=admin --from-literal=password=pass123 What will be the Base64 encoded value of the password key in the Secret?
medium
A. cGFzczEyMw==
B. cGFzc3dvcmQ=
C. cGFzc3dvcmQxMjM=
D. cGFzc3dvcmQxMjM0
Solution
Step 1: Identify the literal value for password
The password literal is pass123.
Step 2: Base64 encode 'pass123'
Encoding 'pass123' in Base64 results in cGFzczEyMw==.
Final Answer:
cGFzczEyMw== -> Option A
Quick Check:
Base64('pass123') = cGFzczEyMw== [OK]
Hint: Encode exact literal value, not similar words [OK]
Common Mistakes:
Encoding 'password' instead of 'pass123'
Missing padding characters '=='
Confusing similar Base64 strings
4. You created a Secret YAML with this data field: data:
apiKey: mysecretkey What is the problem with this Secret definition?
medium
A. The value 'mysecretkey' is not Base64 encoded
B. The key 'apiKey' is invalid in Secrets
C. The data field should be named stringData
D. The Secret kind is missing
Solution
Step 1: Check the data field requirements
The data field requires Base64 encoded values, not plain text.
Step 2: Identify the error in the value
The value 'mysecretkey' is plain text and must be Base64 encoded before placing under data.
Final Answer:
The value 'mysecretkey' is not Base64 encoded -> Option A
Quick Check:
data values must be Base64 encoded [OK]
Hint: data field values must be Base64, else error [OK]
Common Mistakes:
Putting plain text in data instead of Base64
Confusing data with stringData field
Ignoring required apiVersion and kind fields
5. You want to create a Kubernetes Secret from a file config.json containing JSON data. Which command correctly creates the Secret with Base64 encoding handled automatically?
hard
A. kubectl create secret generic myconfig --from-file=config.json --encode
B. kubectl create secret generic myconfig --from-literal=config.json=$(cat config.json | base64)
C. kubectl create secret generic myconfig --from-literal=config.json=config.json
D. kubectl create secret generic myconfig --from-file=config.json