Run The Bridge

Secert과 tls 타입의 시크릿 생성 후 mysql, wordpress, nginx 적용 본문

Cloud/k8s

Secert과 tls 타입의 시크릿 생성 후 mysql, wordpress, nginx 적용

anfrhrl5555 2021. 8. 11. 22:37
728x90

0. 실습과제


1. mysql 패스워드를 시크릿으로 생성하여 wordpress - mysql pod에 적용하기, Mysql의 두 환경변수 중, 패스워드는 시크릿으로, 나머지는 컨피그맵으로 적용

 

Secret으로 루트 패스워드를 먼저 만들어준다.

kubectl create secret generic mysql-root-password --from-literal MYSQL_ROOT_PASSWORD=password

 

나머지는 지난시간에 쓴 configMap를 가져와서 수정 후 configMap을 생성하면 된다.

# vi mysql-configmap

MYSQL_DATABASE=my-db
MYSQL_USER=test
MYSQL_PASSWORD=test
k create configmap mysql-envfile --from-env-file mysql-configmap

 

이제 mysql.yaml 파일에서 secert에 해당하는 부분을 추가해주면 된다.

apiVersion: v1
kind: Pod
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  containers:
  - name: my-mysql-container
    image: mysql:5.7
    envFrom:
    - configMapRef:
        name: mysql-envfile
    - secretRef:
        name: mysql-root-password

    ports:
    - containerPort: 3306
      protocol: TCP

 

이제 mysql, wordpress를 올려주면 된다.(성공!)

 


2. tls인증서를 tls 타입의 시크릿으로 생성 후 nginx pod에 적용하기

- 포트 443
- 인증서 생성 명령어 참고
- openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=alicek106.example.com/O=alicek106“​

 

먼저 위에 명시되어 있는 명령어를 통해 'tls.crt'와 tls.key'를 먼저 만들어준다.(인증서와 키)

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=anfrhrl5555.example.com/O=organiztion"

 

그리고 만들어진 인증서와 키를 Secert으로 만들어준다.(나는 이름을 mv로 이름을 바꿨다)

kubectl create secret tls my-tls-secret --cert cert.crt --key cert.key

 

 

그다음 2장에서 쓴 SSL를 이용하기 위해 nginx.conf 파일을 가져온다.

 

이제 default.conf 파일을 수정해야 한다.

server {
    listen       443;
    server_name  localhost;

    ssl on;
    ssl_certificate /cert/tls.crt;
    ssl_certificate_key /cert/tls.key;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
 	#
    #location ~ /\.ht {
    #    deny  all;
    #}
}

default.conf는 configMap으로 생성하면 된다.

kubectl create configmap nginx-default-conf --from-file default.conf

 

마지막으로 nginx-test.yaml 파일을 수정하면 된다.

apiVersion: v1
kind: Pod
metadata:
  name: nginx-volume-pod
  labels:
    app: nginx-test
spec:
  containers:
    - name: my-nginx-container
      image: nginx:latest
      volumeMounts:
      - name: nginx-page-docs
        mountPath: /usr/share/nginx/html

      - name: nginx-default-conf
        mountPath: /etc/nginx/conf.d

      - name: nginx-ssl
        mountPath: /cert

      ports:
      - containerPort: 443

  volumes:
  - name: nginx-page-docs
    configMap:
      name: nginx-page-docs

  - name: nginx-default-conf
    configMap:
      name: nginx-default-conf

  - name: nginx-ssl
    secret:
      secretName: my-tls-secret

이제 nginx.yaml, service.yaml을 올리면 되는데 nginx-page-docs가 없어서 오류가 뜬다.

 

그래서 어제 docs 파일을 다시 configMap으로 만들어서 정의해주어야 한다.

 

이 부분은 다른부분에 내용을 기재해두었기 때문에 pass~ 바로 실행해보겠다

 

k apply -f nginx-test.yaml  # pod 실행

k apply -f nginx-test-service.yaml  # service 실행
curl -k -v https://10.97.150.216:8080  # 호출방법

성공!

주의 요함이 뜨는 이유는, 우리가 만든 인증서이기 떄문이다

 

공인된 기관에 인증받은 인증서가 아니기 때문이다.

 

naver.com

naver.com 같은 사이트는 상위 기관에 인증을 받았으며, 최상위 Root CA가 존재한다.


3. 소감

어려웠던 점: 처음에 키랑 인증서 경로가 '/etc/nginx/conf.d'로 잡혀있어서 default.conf와 경로가 똑같다. 그래서 mount 할 때 마다 오류가 났는데 어떻게하면 경로가 같은곳에 mount를 두번할까 고민했다.

 

해결방안: 간단하게 default.conf에적힌 키랑 인증서 경로를 새롭게 생성해주거나, 겹치지 않게 생성해주면 되었다.(매우간단)

 

느낀 점: 역시 문제를 맞이했을 때는 고민하면서 끝내 해결하는 것이 재밌게 느껴진다.


728x90
Comments