📖 Scenario: You are managing a Kubernetes cluster for a small web application. You need to securely store sensitive information like database passwords so that your application can access them safely without exposing them in plain text.
🎯 Goal: Learn how to create a Kubernetes Secret resource using a YAML manifest. You will create a secret with a database username and password, then verify it is created correctly.
📋 What You'll Learn
Create a Kubernetes Secret manifest file named db-secret.yaml
Include exact keys username and password with base64 encoded values
Use kubectl apply -f db-secret.yaml to create the secret
Use kubectl get secret db-secret -o yaml to verify the secret
💡 Why This Matters
🌍 Real World
Kubernetes Secrets are used to store sensitive information like passwords, tokens, and keys securely so that applications can access them without exposing them in plain text.
💼 Career
Knowing how to create and manage Kubernetes Secrets is essential for DevOps engineers and cloud administrators to maintain secure application deployments.
Progress0 / 4 steps
1
Create the Secret YAML skeleton
Create a file named db-secret.yaml with the following exact content: apiVersion: v1 kind: Secret metadata: name: db-secret type: Opaque data:
Kubernetes
Hint
Start by writing the YAML keys exactly as shown. The data section will hold your encoded secrets.
2
Add base64 encoded username and password
Add two keys under data: username with value YWRtaW4= (which is base64 for 'admin') and password with value c2VjcmV0MTIz (which is base64 for 'secret123'). Indent these keys correctly under data.
Kubernetes
Hint
Remember to indent the username and password keys two spaces under data.
3
Apply the Secret manifest to the cluster
Run the exact command kubectl apply -f db-secret.yaml in your terminal to create the secret in your Kubernetes cluster.
Kubernetes
Hint
This command tells Kubernetes to create or update the secret from your YAML file.
4
Verify the Secret was created
Run the exact command kubectl get secret db-secret -o yaml to display the secret details and confirm the keys username and password exist with the correct base64 values.
Kubernetes
Hint
This command shows the secret's stored data so you can check it matches your input.
Practice
(1/5)
1. What is the main purpose of creating a Secret in Kubernetes?
easy
A. To store sensitive information like passwords and tokens securely
B. To create a backup of your application data
C. To increase the number of pods in a deployment
D. To monitor the health of your cluster nodes
Solution
Step 1: Understand what Secrets are for
Secrets in Kubernetes are designed to hold sensitive data such as passwords, tokens, or keys safely.
Step 2: Compare options with the purpose
Only To store sensitive information like passwords and tokens securely describes storing sensitive info securely, which matches the purpose of Secrets.
Final Answer:
To store sensitive information like passwords and tokens securely -> Option A
Quick Check:
Secrets store sensitive info = B [OK]
Hint: Secrets = safe storage for sensitive data [OK]
Common Mistakes:
Confusing Secrets with backups
Thinking Secrets manage pods or nodes
Assuming Secrets are for monitoring
2. Which of the following is the correct command to create a generic secret named mysecret with a literal key-value pair username=admin?
easy
A. kubectl create secret generic mysecret --from-literal=username=admin
B. kubectl secret create generic mysecret --literal=username=admin
C. kubectl create secret mysecret --literal username=admin
D. kubectl create secret generic mysecret username=admin
Solution
Step 1: Recall the correct syntax for creating a generic secret
The correct command uses kubectl create secret generic [name] --from-literal=key=value.
Step 2: Match the options with the correct syntax
Only kubectl create secret generic mysecret --from-literal=username=admin matches the exact syntax with correct flags and order.
Final Answer:
kubectl create secret generic mysecret --from-literal=username=admin -> Option A
Quick Check:
Correct syntax uses --from-literal=key=value = D [OK]
Hint: Use --from-literal=key=value with create secret generic [OK]
Common Mistakes:
Using wrong flag names like --literal
Omitting 'generic' keyword
Incorrect order of arguments
3. What will be the output of the command kubectl get secret mysecret -o yaml after creating a secret with kubectl create secret generic mysecret --from-literal=password=pass123?
medium
A. The secret will not appear in the output
B. The password will be shown in plain text under data field
C. The password will be base64 encoded under data field
D. An error will occur because secrets cannot be viewed
Solution
Step 1: Understand how Kubernetes stores secret data
Kubernetes stores secret values base64 encoded in the data field when viewed in YAML or JSON.
Step 2: Analyze the command output
The command shows the secret in YAML format with the password base64 encoded, not plain text.
Final Answer:
The password will be base64 encoded under data field -> Option C
Quick Check:
Secrets data is base64 encoded = A [OK]
Hint: Secrets data shown base64 encoded, not plain text [OK]
Common Mistakes:
Expecting plain text output
Thinking secrets are hidden completely
Assuming error when viewing secrets
4. You tried to create a secret with the command kubectl create secret generic mysecret --from-literal password pass123 but got an error. What is the likely cause?
medium
A. You need to add --type=generic flag explicitly
B. Secret name 'mysecret' is invalid
C. The password value is too short
D. Missing '=' sign between key and value in --from-literal
Solution
Step 1: Check the syntax of --from-literal flag
The correct syntax requires an '=' sign between key and value, like --from-literal=key=value.
Step 2: Identify the error in the command
The command uses a space instead of '=', causing a syntax error.
Final Answer:
Missing '=' sign between key and value in --from-literal -> Option D
Quick Check:
--from-literal needs '=' between key and value = A [OK]
Hint: Use '=' in --from-literal key=value, no spaces [OK]
Common Mistakes:
Using space instead of '=' in --from-literal
Assuming secret name rules cause error
Thinking --type=generic is mandatory
5. You want to create a secret from a file named config.txt and also add a literal key env=prod. Which command correctly does this?
hard
A. kubectl create secret generic appsecret --from-file=config.txt env=prod
B. kubectl create secret generic appsecret --from-file=config.txt --from-literal=env=prod
C. kubectl create secret generic appsecret --from-literal=config.txt --from-literal=env=prod
D. kubectl create secret generic appsecret --from-file=config.txt --literal=env=prod
Solution
Step 1: Understand how to add file and literal to secret
Use --from-file=filename to add file content and --from-literal=key=value for literals.
Step 2: Check options for correct syntax
kubectl create secret generic appsecret --from-file=config.txt --from-literal=env=prod correctly uses both flags with proper syntax; others misuse flags or omit '='.
Final Answer:
kubectl create secret generic appsecret --from-file=config.txt --from-literal=env=prod -> Option B
Quick Check:
Use --from-file and --from-literal together correctly = C [OK]
Hint: Combine --from-file and --from-literal with '=' correctly [OK]
Common Mistakes:
Using --from-literal for file instead of --from-file
Omitting '=' in --from-literal
Mixing flags incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.