Skip to content

Latest commit

 

History

History
166 lines (129 loc) · 5.03 KB

K8s-Secret.md

File metadata and controls

166 lines (129 loc) · 5.03 KB

LAB: K8s Secret

This scenario shows:

  • how to create secrets with file,
  • how to use secrets: volume and environment variable,
  • how to create secrets with command,
  • how to get/delete secrets

Steps

  • Run minikube (in this scenario, K8s runs on WSL2- Ubuntu 20.04) ("minikube start")

image

# Secret Object Creation  
apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
stringData:
  db_server: db.example.com
  db_username: admin
  db_password: P@ssw0rd!

image

apiVersion: v1
kind: Pod
metadata:
  name: secretvolumepod
spec:
  containers:
  - name: secretcontainer
    image: nginx
    volumeMounts:
    - name: secret-vol
      mountPath: /secret
  volumes:
  - name: secret-vol
    secret:
      secretName: mysecret
---
apiVersion: v1
kind: Pod
metadata:
  name: secretenvpod
spec:
  containers:
  - name: secretcontainer
    image: nginx
    env:
      - name: username
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: db_username
      - name: password
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: db_password
      - name: server
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: db_server
---
apiVersion: v1
kind: Pod
metadata:
  name: secretenvallpod
spec:
  containers:
  - name: secretcontainer
    image: nginx
    envFrom:
    - secretRef:
        name: mysecret

image

image

image

  • Create secret object:

image

  • Create pods:

image

  • Describe secret to see details:

image

  • Run bash in the secretvolumepod (1st pod):

image

  • Run "printenv" command in the secretenvpod (2nd pod):

image

  • Run "printenv" command in the secretenvallpod (3rd pod):

image

  • Create new secret with imperative way:
kubectl create secret generic mysecret2 --from-literal=db_server=db.example.com --from-literal=db_username=admin --from-literal=db_password=P@ssw0rd!

image

kubectl create secret generic mysecret3 --from-file=db_server=server.txt --from-file=db_username=username.txt --from-file=db_password=password.txt

image

{
    "apiKey": "7ac4108d4b2212f2c30c71dfa279e1f77dd12356",
}
kubectl create secret generic mysecret4 --from-file=config.json

image

  • Delete mysecret4:

image