Kubernetes

쿠버네티스 ( secret)

law and security 2024. 10. 7. 11:36

 

* MYSQL_ADDR / MYSQL_PORT / MYSQL_DATABASE 설정정보를 위한 ConfigMap 생성

 

[root@k8smaster 07]# cd ~/lab/07/guestbook


[root@k8smaster guestbook]# ls
guestbook-config.yaml  guestbook-pod-env-cm-secret-all.yaml  guestbook-pod-env-cm-secret.yaml

[root@k8smaster guestbook]# cat guestbook-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: guestbook-config
data:
  MYSQL_ADDR: "192.168.56.100"
  MYSQL_PORT: "3306"
  MYSQL_DATABASE: "guestbook"

#  guestbook-config 컨피그맵 생성

[root@k8smaster guestbook]# k create -f guestbook-config.yaml
configmap/guestbook-config created

[root@k8smaster guestbook]# k get cm
NAME                     DATA   AGE
cowsay-config            5      2d18h
cowsay-config-fromfile   5      2d18h
guestbook-config         3      5s
kube-root-ca.crt         1      285d

#생성된ConfigMap 데이터 확인
 
[root@k8smaster guestbook]# k get cm guestbook-config  -o yaml
apiVersion: v1
data:
  MYSQL_ADDR: 192.168.56.100
  MYSQL_DATABASE: guestbook
  MYSQL_PORT: "3306"
kind: ConfigMap
metadata:
  creationTimestamp: "2024-10-07T02:12:08Z"
  name: guestbook-config
  namespace: default
  resourceVersion: "286357"
  uid: 3d56c5ec-a4ea-4c0a-ad89-e19add533af6

 

 

 

* guestbook-secret 시크릿 생성

[root@k8smaster guestbook]# echo -n 'root' > ./MYSQL_USER
[root@k8smaster guestbook]# ls
guestbook-config.yaml  guestbook-pod-env-cm-secret-all.yaml  guestbook-pod-env-cm-secret.yaml  MYSQL_USER
[root@k8smaster guestbook]# cat MYSQL_USER
root[root@k8smaster guestbook]# echo -n 'edu' > ./MYSQL_USER_PASSWORD
[root@k8smaster guestbook]# ls
guestbook-config.yaml  guestbook-pod-env-cm-secret-all.yaml  guestbook-pod-env-cm-secret.yaml  MYSQL_USER  MYSQL_USER_PASSWORD
[root@k8smaster guestbook]# cat MYSQL_USER_PASSWORD
edu[root@k8smaster guestbook]#

[root@k8smaster guestbook]# kubectl create secret generic guestbook-secret --from-file=./MYSQL_USER --from-file=./MYSQL_USER_PASSWORD secret/guestbook-secret created
[root@k8smaster guestbook]# k get secret
NAME               TYPE     DATA   AGE
guestbook-secret   Opaque   2      11s

# 생성된Secret 데이터 확인

[root@k8smaster guestbook]# kubectl get secret guestbook-secret -o yaml
apiVersion: v1
data:
  MYSQL_USER: cm9vdA==        # 해시값으로 만들어짐
  MYSQL_USER_PASSWORD: ZWR1   # 해시값으로 만들어짐
kind: Secret
metadata:
  creationTimestamp: "2024-10-07T02:19:20Z"
  name: guestbook-secret
  namespace: default
  resourceVersion: "287026"
  uid: 7a699b1f-f598-4a5f-aa0f-c3a7eddcc270
type: Opaque

 

 

[root@k8smaster guestbook]# cat guestbook-pod-env-cm-secret.yaml
apiVersion: v1
kind: Pod
metadata:
  name: guestbook-pod-env-cm-secret
spec:
  containers:
  - image: yu3papa/k8s_guestbook:env-secret
    name: guestbook
    envFrom:
    - configMapRef:
        name: guestbook-config
    env:
    - name: MYSQL_USER
      valueFrom:
        secretKeyRef:
          name: guestbook-secret
          key: MYSQL_USER
    - name: MYSQL_USER_PASSWORD
      valueFrom:
        secretKeyRef:
          name: guestbook-secret
          key: MYSQL_USER_PASSWORD
    ports:
    - containerPort: 8080

 

 

 

* ConfigMap 과 Secret을 사용하는 guestbook POD 만들기

# ConfigMap 과 Secret을 사용하는 guestbook POD 생성

[root@k8smaster guestbook]# k create -f guestbook-pod-env-cm-secret.yaml
pod/guestbook-pod-env-cm-secret created
[root@k8smaster guestbook]# k get pods
NAME                                 READY   STATUS              RESTARTS       AGE
calculator-qd85p                     1/1     Running             2 (113m ago)   5d
cowsay-args-from-configmap           2/2     Running             0              48m
cowsay-env                           2/2     Running             2 (113m ago)   2d21h
cowsay-env-from-configmap            2/2     Running             0              80m
cowsay-env-from-configmap-allentry   1/1     Running             0              42m
guestbook-7pfq2                      1/1     Running             2 (113m ago)   5d
guestbook-959db856c-bnbct            1/1     Running             1 (113m ago)   2d21h
guestbook-959db856c-hqdsg            1/1     Running             1 (113m ago)   2d21h
guestbook-959db856c-nvt8s            1/1     Running             1 (113m ago)   2d21h
guestbook-pod-env-cm-secret          0/1     ContainerCreating   0              4s
[root@k8smaster guestbook]# k get pods guestbook-pod-env-cm-secret -o wide
NAME                          READY   STATUS    RESTARTS   AGE   IP              NODE       NOMINATED NODE   READINESS GATES
guestbook-pod-env-cm-secret   1/1     Running   0          17s   10.102.249.45   k8snode1   <none>           <none>

# 실행중인guestbook 컨테이너에서 env 명령으로 환경변수를 조회

[root@k8smaster guestbook]# kubectl exec guestbook-pod-env-cm-secret -- env | sort
APP_HOME=/app
CALCULATOR_PORT_80_TCP_ADDR=10.101.244.101
CALCULATOR_PORT_80_TCP_PORT=80
CALCULATOR_PORT_80_TCP_PROTO=tcp
CALCULATOR_PORT_80_TCP=tcp://10.101.244.101:80
CALCULATOR_PORT=tcp://10.101.244.101:80
CALCULATOR_SERVICE_HOST=10.101.244.101
CALCULATOR_SERVICE_PORT=80
GUESTBOOK_HTTP_PORT_80_TCP_ADDR=10.111.55.145
GUESTBOOK_HTTP_PORT_80_TCP_PORT=80
GUESTBOOK_HTTP_PORT_80_TCP_PROTO=tcp
GUESTBOOK_HTTP_PORT_80_TCP=tcp://10.111.55.145:80
GUESTBOOK_HTTP_PORT=tcp://10.111.55.145:80
GUESTBOOK_HTTP_SERVICE_HOST=10.111.55.145
GUESTBOOK_HTTP_SERVICE_PORT=80
GUESTBOOK_PORT_80_TCP_ADDR=10.105.210.38
GUESTBOOK_PORT_80_TCP_PORT=80
GUESTBOOK_PORT_80_TCP_PROTO=tcp
GUESTBOOK_PORT_80_TCP=tcp://10.105.210.38:80
GUESTBOOK_PORT=tcp://10.105.210.38:80
GUESTBOOK_SERVICE_HOST=10.105.210.38
GUESTBOOK_SERVICE_PORT=80
HOME=/root
HOSTNAME=guestbook-pod-env-cm-secret
JAVA_BASE_URL=https://github.com/AdoptOpenJDK/openjdk8-upstream-binaries/releases/download/jdk8u222-b10/OpenJDK8U-jdk_
JAVA_HOME=/usr/local/openjdk-8
JAVA_URL_VERSION=8u222b10
JAVA_VERSION=8u222
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_SERVICE_HOST=10.96.0.1
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_PORT_HTTPS=443
LANG=C.UTF-8
MYSQL_ADDR=192.168.56.100
MYSQL_DATABASE=guestbook
MYSQL_PORT=3306
MYSQL_USER_PASSWORD=edu
MYSQL_USER=root
PATH=/usr/local/openjdk-8/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

 

 

[root@k8smaster guestbook]# kubectl port-forward guestbook-pod-env-cm-secret 80:8080
Forwarding from 127.0.0.1:80 -> 8080
Forwarding from [::1]:80 -> 8080
Handling connection for 80


<새로운 창>

[root@k8smaster ~]# curl http://localhost
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="/common.css" rel="stylesheet">
<link href="/guestbook.css" rel="stylesheet">

<title>방명록 (K8S)</title>
</head>
<body>
        <div class="guestbook list displaynone" style="display: block;">
                <ul class="cont">
                        <li>
                                <p class="memo">
HealthCheck : This is 1.0 running in pod guestbook-pod-env-cm-secret (10.102.249.45)
                                </p>
                        </li>
                </ul>
        </div>

        <h4 class="g-top">방명록 1.0</h4>
        <div class="top-img">
                <img src="/bg-guestbook.jpg">
        </div>
        <form action="/" method="post" id="frm" enctype="multipart/form-data">
                <div class="g-cont">
                        <ul>
                                <li class="name"><input name="name" type="text" maxlength="20" placeholder="이름" class="form-control"></li>
                                <li class="msg"><textarea name="content" maxlength="200" placeholder="내용을 입력해주세요" class="form-control" rows="3"></textarea></li>
                                <li>파일첨부<input type="file" name="uploadingFile"></li>
                        </ul>
                        <p class="btn btn-lg" onclick="document.getElementById('frm').submit();">방명록 남기기</p>
                </div>
        </form>

        <div class="guestbook list displaynone" style="display: block;">
                <ul class="cont">
                        <li>
                                <p class="name">최유진</p>
                                <p class="date">2024-09-25 03:43:41</p>
                                <p class="memo">방명록</p>
                                첨부파일&nbsp;:&nbsp;<a href="/downloadFile/null"></a>
                        </li>
                </ul>
        </div>
</body>

 

 

[root@k8smaster guestbook]# k get pods guestbook-pod-env-cm-secret -o wide
NAME                          READY   STATUS    RESTARTS   AGE     IP              NODE       NOMINATED NODE   READINESS GATES
guestbook-pod-env-cm-secret   1/1     Running   0          6m40s   10.102.249.45   k8snode1   <none>           <none>

 

 

 

* 다음 실습을 위해 default 네임스페이스의 모든 리소스 삭제 *

 

[root@k8smaster guestbook]# k get all
NAME                                     READY   STATUS    RESTARTS       AGE
pod/calculator-qd85p                     1/1     Running   2 (121m ago)   5d
pod/cowsay-args-from-configmap           2/2     Running   0              57m
pod/cowsay-env                           2/2     Running   2 (121m ago)   2d21h
pod/cowsay-env-from-configmap            2/2     Running   0              89m
pod/cowsay-env-from-configmap-allentry   1/1     Running   0              50m
pod/guestbook-7pfq2                      1/1     Running   2 (121m ago)   5d
pod/guestbook-959db856c-bnbct            1/1     Running   1 (121m ago)   2d21h
pod/guestbook-959db856c-hqdsg            1/1     Running   1 (122m ago)   2d21h
pod/guestbook-959db856c-nvt8s            1/1     Running   1 (121m ago)   2d21h
pod/guestbook-pod-env-cm-secret          1/1     Running   0              8m36s

NAME                     TYPE           CLUSTER-IP       EXTERNAL-IP      PORT(S)        AGE
service/calculator       ClusterIP      10.101.244.101   <none>           80/TCP         5d
service/guestbook        ClusterIP      10.105.210.38    <none>           80/TCP         5d
service/guestbook-http   LoadBalancer   10.111.55.145    192.168.56.201   80:32290/TCP   2d21h
service/kubernetes       ClusterIP      10.96.0.1        <none>           443/TCP        5d

NAME                        READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/guestbook   3/3     3            3           2d21h

NAME                                   DESIRED   CURRENT   READY   AGE
replicaset.apps/calculator             1         1         1       5d
replicaset.apps/guestbook              1         1         1       5d
replicaset.apps/guestbook-6f5b54f576   0         0         0       2d21h
replicaset.apps/guestbook-959db856c    3         3         3       2d21h


[root@k8smaster guestbook]# k delete all --all
pod "calculator-qd85p" deleted
pod "cowsay-args-from-configmap" deleted
pod "cowsay-env" deleted
pod "cowsay-env-from-configmap" deleted
pod "cowsay-env-from-configmap-allentry" deleted
pod "guestbook-7pfq2" deleted
pod "guestbook-959db856c-bnbct" deleted
pod "guestbook-959db856c-hqdsg" deleted
pod "guestbook-959db856c-nvt8s" deleted
pod "guestbook-pod-env-cm-secret" deleted
service "calculator" deleted
service "guestbook" deleted
service "guestbook-http" deleted
service "kubernetes" deleted
deployment.apps "guestbook" deleted
replicaset.apps "calculator" deleted
replicaset.apps "guestbook" deleted

[root@k8smaster guestbook]# k delete cm --all
configmap "cowsay-config" deleted
configmap "cowsay-config-fromfile" deleted
configmap "guestbook-config" deleted
configmap "kube-root-ca.crt" deleted

[root@k8smaster guestbook]# k delete secrets --all
secret "guestbook-secret" deleted

[root@k8smaster guestbook]# k get all
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   56s