본문 바로가기
MLOps/Doker & Kubernetes

Udemy CKA 강의 정리 103: Configure Secrets in Applications

by 공부하는 무니 2023. 1. 12.
반응형

해당 내용은 Udemy의 Certified Kubernetes Administrator (CKA) with Practice Tests 강의를 공부한 내용입니다. 내용을 그대로 번역하기보다는, 제가 이해하기 쉬운 대로 수정한 부분들이 있습니다.

⚠️ 영어 독해가 많이 부족합니다. 틀린 내용이 있으면 알려주시면 감사하겠습니다.


(보충 필요)

이번 강의에서는 쿠버네티스의 secrets 에 대해 살펴보겠습니다.

Web-Mysql Application

한 가지 방법은 app properties/envs을 configmap으로 이동하는 것입니다. 그러나 configmap은 데이터를 일반 텍스트 형식으로 저장합니다. 확실히 암호를 저장하기에 적합한 장소가 아닙니다.

 

  • apiVersion: v1
    kind: ConfigMap
    metadata:
     name: app-config
    data:
      DB_Host: mysql
      DB_User: root
      DB_Password: paswrd
    
  •  

Secrets은 민감한 정보를 저장하는 데 사용됩니다. configmap과 유사하지만 암호화된 형식 또는 해시 형식으로 저장됩니다.

There are 2 steps involved with secrets

  • First, Create a secret
  • Second, Inject the secret into a pod.

 

  •  

There are 2 ways of creating a secret

  • The Imperative way
  • $ kubectl create secret generic app-secret --from-literal=DB_Host=mysql --from-literal=DB_User=root --from-literal=DB_Password=paswrd
    $ kubectl create secret generic app-secret --from-file=app_secret.properties
    
  • Declarative way로는, secret definition file을 만들고  kubectl create 커맨드를 실행하여 배포합니다.
    $ kubectl create -f secret-data.yaml
    
  • apiVersion: v1
    kind: Secret
    metadata:
     name: app-secret
    data:
      DB_Host: bX1zcWw=
      DB_User: cm9vdA==
      DB_Password: cGFzd3Jk
    
  • Generate a hash value of the password and pass it to secret-data.yaml definition value as a value to DB_Password variable.
    $ echo -n "mysql" | base64
    $ echo -n "root" | base64
    $ echo -n "paswrd"| base64
    

Encode Secrets

View Secrets

  • To view secrets
  • $ kubectl get secrets
    
  • To describe secret
  • $ kubectl describe secret
    
  • To view the values of the secret
  • $ kubectl get secret app-secret -o yaml
    

Decode Secrets

  • To decode secrets
    $ echo -n "bX1zcWw=" | base64 --decode
    $ echo -n "cm9vdA==" | base64 --decode
    $ echo -n "cGFzd3Jk" | base64 --decode
    

Configuring secret with a pod

  • To inject a secret to a pod add a new property envFrom followed by secretRef name and then create the pod-definition
     apiVersion: v1
     kind: Pod
     metadata:
       name: simple-webapp-color
     spec:
      containers:
      - name: simple-webapp-color
        image: simple-webapp-color
        ports:
        - containerPort: 8080
        envFrom:
        - secretRef:
            name: app-secret
    
    $ kubectl create -f pod-definition.yaml
    
  • apiVersion: v1
    kind: Secret
    metadata:
     name: app-secret
    data:
      DB_Host: bX1zcWw=
      DB_User: cm9vdA==
      DB_Password: cGFzd3Jk
    

There are other ways to inject secrets into pods.

  • You can inject as Single ENV variable
  • You can inject as whole secret as files in a Volume

Secrets in pods as volume

  • Each attribute in the secret is created as a file with the value of the secret as its content.
반응형

댓글