Bird
Raised Fist0
Kubernetesdevops~10 mins

Creating Secrets in Kubernetes - Visual Walkthrough

Choose your learning style10 modes available
Process Flow - Creating Secrets
Write secret data
↓
Encode data in base64
↓
Create Secret YAML manifest
↓
Apply manifest with kubectl
↓
Secret stored in cluster
↓
Use secret in pods or configs
This flow shows how secret data is prepared, encoded, saved in a YAML file, applied to the cluster, and then used securely.
Execution Sample
Kubernetes
kubectl create secret generic mysecret --from-literal=password=Pa$$w0rd
kubectl get secret mysecret -o yaml
Creates a secret named 'mysecret' with a password, then shows the secret details in YAML format.
Process Table
StepCommandActionResult
1kubectl create secret generic mysecret --from-literal=password=Pa$$w0rdCreate secret named 'mysecret' with passwordSecret 'mysecret' created
2kubectl get secret mysecret -o yamlRetrieve secret details in YAMLYAML output with base64 encoded password
3kubectl describe secret mysecretShow secret summaryShows metadata and data keys (password)
4kubectl delete secret mysecretDelete the secretSecret 'mysecret' deleted
5-No secret exists nowkubectl get secret mysecret returns 'NotFound' error
💡 Secret deleted and no longer exists in cluster
Status Tracker
VariableStartAfter Step 1After Step 4Final
Secret 'mysecret'NoneExists with password keyDeletedNone
Key Moments - 3 Insights
Why is the password stored in base64 format in the secret YAML?
Base64 encoding is used to safely store binary or special characters in YAML. It is not encryption, just encoding. See execution_table step 2 where the password appears encoded.
What happens if you try to get a secret after deleting it?
kubectl returns a 'NotFound' error because the secret no longer exists. This is shown in execution_table step 5.
Can you see the actual password in plain text by running 'kubectl get secret'?
No, the password is base64 encoded in the output. You must decode it manually to see the plain text. This is shown in execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of step 1?
ASecret 'mysecret' deleted
BSecret 'mysecret' created
CYAML output with base64 encoded password
DNotFound error
💡 Hint
Check the 'Result' column for step 1 in the execution_table.
At which step does the secret get deleted?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for the 'Action' column mentioning deletion in the execution_table.
If you want to see the secret password in plain text, what must you do after step 2?
ARun 'kubectl delete secret mysecret'
BRun 'kubectl describe secret mysecret'
CDecode the base64 encoded password from the YAML output
DNothing, it is already in plain text
💡 Hint
Refer to the key_moments about base64 encoding and step 2 output.
Concept Snapshot
Creating Secrets in Kubernetes:
- Use 'kubectl create secret generic NAME --from-literal=key=value'
- Secret data is base64 encoded in YAML
- Apply secret with kubectl to store in cluster
- Use 'kubectl get secret NAME -o yaml' to view encoded data
- Delete secret with 'kubectl delete secret NAME'
Full Transcript
Creating secrets in Kubernetes involves writing secret data, encoding it in base64, creating a YAML manifest, and applying it to the cluster using kubectl. The secret is stored securely and can be used by pods or configurations. The password or secret data is encoded in base64, not encrypted, so it must be decoded to read the original value. Secrets can be deleted, after which they no longer exist in the cluster.

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

  1. Step 1: Understand what Secrets are for

    Secrets in Kubernetes are designed to hold sensitive data such as passwords, tokens, or keys safely.
  2. 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.
  3. Final Answer:

    To store sensitive information like passwords and tokens securely -> Option A
  4. 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

  1. Step 1: Recall the correct syntax for creating a generic secret

    The correct command uses kubectl create secret generic [name] --from-literal=key=value.
  2. 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.
  3. Final Answer:

    kubectl create secret generic mysecret --from-literal=username=admin -> Option A
  4. 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

  1. Step 1: Understand how Kubernetes stores secret data

    Kubernetes stores secret values base64 encoded in the data field when viewed in YAML or JSON.
  2. Step 2: Analyze the command output

    The command shows the secret in YAML format with the password base64 encoded, not plain text.
  3. Final Answer:

    The password will be base64 encoded under data field -> Option C
  4. 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

  1. Step 1: Check the syntax of --from-literal flag

    The correct syntax requires an '=' sign between key and value, like --from-literal=key=value.
  2. Step 2: Identify the error in the command

    The command uses a space instead of '=', causing a syntax error.
  3. Final Answer:

    Missing '=' sign between key and value in --from-literal -> Option D
  4. 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

  1. 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.
  2. 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 '='.
  3. Final Answer:

    kubectl create secret generic appsecret --from-file=config.txt --from-literal=env=prod -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard