Exercise 6.2: Create and consume Secrets

  1. 비밀번호로 사용할 문자열을 Base64로 인코딩

    echo -n asdf1234 | base64
  2. Secret 생성

    kubectl create secret generic mysql-credentials --from-literal=password=asdf1234
  3. 생성된 Secret 확인

    kubectl get secrets mysql-credentials -o yaml
  4. Pod 생성

    kubectl run mysql --image=mysql
  5. Pod가 생성되었는지 확인

    kubectl get pod mysql -w
  6. Ctrl+C 입력

  7. 해당 링크를 통해서 mysql 컨테이너 이미지 리뷰

  8. Pod 삭제

    kubectl delete pod mysql
  9. Pod 생성

    cat <<EOF | kubectl create -f -
    apiVersion: v1
    kind: Pod
    metadata:
      name: mysql
    spec:
      containers:
      - name: mysql
        image: mysql
        env:
          - name: MYSQL_ROOT_PASSWORD
            valueFrom:
              secretKeyRef:
                name: mysql-credentials
                key: password
        volumeMounts:
          - name: mysql
            mountPath: /opt/mysql
      volumes:
      - name: mysql
        secret:
          secretName: mysql-credentials
    EOF
  10. Pod가 생성 되었는지 확인

    kubectl get pod mysql
  11. MYSQL_ROOT_PASSWORD 환경변수가 설정 되었는지 확인

    kubectl exec -it mysql -- /bin/bash -c 'echo $MYSQL_ROOT_PASSWORD'
  12. /opt/mysql 디렉토리에 파일이 생성됐는지 확인

    kubectl exec -it mysql -- ls /opt/mysql
  13. /opt/mysql 디렉토리에 생성된 파일 내용 확인

    kubectl exec -it mysql -- cat /opt/mysql/password
  14. 리소스 삭제

    {
        kubectl delete pod mysql
        kubectl delete secret mysql-credentials
    }

Last updated