> For the complete documentation index, see [llms.txt](https://kubernetes.youngwjung.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kubernetes.youngwjung.com/core-concepts/configmap/lab.md).

# 실습

### Environment Variable

1. ConfigMap 생성&#x20;

   ```
   kubectl create configmap echo --from-literal=message="hello kubernetes"
   ```
2. 생성된 ConfigMap 확인&#x20;

   ```
   kubectl get cm echo -o yaml
   ```
3. Pod 생성&#x20;

   ```
   cat <<EOF | kubectl apply -f -
   apiVersion: v1
   kind: Pod
   metadata:
     name: echo
   spec:
     containers:
     - image: alpine
       name: echo
       command: [ "/bin/sh", "-c" ]
       args: [ "while true; echo \$MESSAGE; do sleep 5; done;" ]
       env:
         - name: MESSAGE
           valueFrom:
             configMapKeyRef:
               name: echo
               key: message
   EOF
   ```
4. Pod 로그 확인&#x20;

   ```
   kubectl logs echo
   ```
5. ConfigMap 수정&#x20;

   ```
   cat <<EOF | kubectl apply -f -
   apiVersion: v1
   kind: ConfigMap
   metadata:
     name: echo
   data:
     message: goodbye kubernetes
   EOF
   ```
6. 수정한 ConfigMap 확인&#x20;

   ```
   kubectl get cm echo -o yaml
   ```
7. Pod 로그 확인&#x20;

   ```
   kubectl logs echo
   ```
8. Pod 재생성&#x20;

   ```
   cat <<EOF | kubectl replace --force -f -
   apiVersion: v1
   kind: Pod
   metadata:
     name: echo
   spec:
     containers:
     - image: alpine
       name: echo
       command: [ "/bin/sh", "-c" ]
       args: [ "while true; echo \$MESSAGE; do sleep 5; done;" ]
       env:
         - name: MESSAGE
           valueFrom:
             configMapKeyRef:
               name: echo
               key: message
   EOF
   ```
9. Pod 로그 확인&#x20;

   ```
   kubectl logs echo
   ```
10. ConfigMap 생성&#x20;

    ```
    cat <<EOF | kubectl apply -f -
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: env
    data:
      DB_URL: db.example.com
      DB_NAME: mydb
      DB_USER: admin
    EOF
    ```
11. Pod 생성&#x20;

    ```
    cat <<EOF | kubectl apply -f -
    apiVersion: v1
    kind: Pod
    metadata:
      name: env
    spec:
      containers:
      - image: alpine
        name: env
        command: [ "env" ]
        envFrom:
          - configMapRef:
              name: env
      restartPolicy: Never
    EOF
    ```
12. Pod 로그 확인&#x20;

    ```
    kubectl logs env
    ```
13. 생성한 리소스 삭제&#x20;

    ```
    {
        kubectl delete pod echo env
        kubectl delete cm echo env
    }
    ```

### **Volume**

1. HTML 파일 생성&#x20;

   ```
   cat > index.html <<EOF
   hello world
   EOF

   cat > home.html <<EOF
   hello home
   EOF
   ```
2. ConfigMap 생성&#x20;

   ```
   kubectl create configmap www --from-file=index.html --from-file=home.html
   ```
3. 생성된 ConfigMap 확인&#x20;

   ```
   kubectl get cm www -o yaml
   ```
4. Pod 생성&#x20;

   ```
   cat <<EOF | kubectl apply -f -
   apiVersion: v1
   kind: Pod
   metadata:
     name: nginx
   spec:
     containers:
     - image: nginx
       name: nginx
       volumeMounts:
         - name: html
           mountPath: /usr/share/nginx/html
     volumes:
       - name: html
         configMap:
           name: www
   EOF
   ```
5. ConfigMap을 마운트한 경로에 있는 파일 확인&#x20;

   ```
   kubectl exec nginx -- ls /usr/share/nginx/html
   ```
6. 배포한 웹서버를 호출해서 인덱스 페이지 확인 &#x20;

   ```
   kubectl exec nginx -- curl -s localhost
   ```
7. ConfigMap에 데이터 추가&#x20;

   ```
   cat <<EOF | kubectl replace -f -
   apiVersion: v1
   kind: ConfigMap
   metadata:
     name: www
   data:
     home.html: |
       hello home
     index.html: |
       hello world
     404.html: |
       Not Found
   EOF
   ```
8. 업데이트된 ConfigMap 확인&#x20;

   ```
   kubectl get cm www -o yaml
   ```
9. ConfigMap을 마운트한 경로에 있는 파일 확인 - *수정분이 반영되는지 시간이 걸릴수 있음*

   ```
   kubectl exec nginx -- ls /usr/share/nginx/html
   ```
10. ConfigMap에 데이터 변경&#x20;

    ```
    cat <<EOF | kubectl replace -f -
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: www
    data:
      home.html: |
        hello home
      index.html: |
        hello kubernetes
      404.html: |
        Not Found
    EOF
    ```
11. 업데이트된 ConfigMap 확인&#x20;

    ```
    kubectl get cm www -o yaml
    ```
12. 배포한 웹서버를 호출해서 인덱스 페이지 확인 &#x20;

    ```
    kubectl exec nginx -- curl -s localhost
    ```
13. Pod 재생성&#x20;

    ```
    cat <<EOF | kubectl replace --force -f -
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        volumeMounts:
          - name: html
            mountPath: /usr/share/nginx/html
      volumes:
        - name: html
          configMap:
            name: www
            items:
            - key: 404.html
              path: index.html
    EOF
    ```
14. ConfigMap을 마운트한 경로에 있는 파일 확인&#x20;

    ```
    kubectl exec nginx -- ls /usr/share/nginx/html
    ```
15. 배포한 웹서버를 호출해서 인덱스 페이지 확인 &#x20;

    ```
    kubectl exec nginx -- curl -s localhost
    ```
16. 생성한 리소스 삭제&#x20;

    ```
    {
        kubectl delete pod nginx
        kubectl delete cm www
        rm index.html home.html
    }
    ```
