Post

쿠버네티스 ingress에 https certification 적용하기

Kubernetes를 이용해 서비스를 하기 위해서는 도메인주소와 연결시키고, https를 적용시켜서 보안을 강화시키는 것이 매우 중요하다. Kubernetes에서 이를 이용하기 위해서는, cert-manager을 이용하면 된다.

쿠버네티스 환경에서 cert-manager를 활용하여 NGINX Ingress에 HTTPS를 적용하는 방법을 안내합니다. 이 과정은 SSL/TLS 인증서의 자동 발급 및 갱신을 통해 보안 통신을 구현하는 데 중점을 둡니다. cert-manager와 관련해서 더 자세한 내용은 링크를 참고해 주세요.

1. NGINX Ingress Controller 설치

먼저, Helm을 사용하여 NGINX Ingress Controller를 설치합니다:

1
2
3
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
helm install nginx ingress-nginx/ingress-nginx

설치 시, values.yaml 파일을 수정하여 필요한 설정을 적용할 수 있습니다. 특히, 고정 IP를 사용할 경우 loadBalancerIP 항목을 지정해야 합니다. 이 부분은 이미 서비스가 돌아가고 있다면, 수행할 필요가 없는 부분 입니다.

2. cert-manager 설치

다음으로, 인증서 관리를 자동화하는 cert-manager를 설치합니다:

1
2
3
helm repo add jetstack https://charts.jetstack.io
helm repo update
sudo helm install cert-manager jetstack/cert-manager --namespace cert-manager --create-namespace --version v1.16.1 -f values.yaml

cert-manager는 쿠버네티스 클러스터 내에서 인증 기관에 인증서 발급을 요청하고, 도메인 소유권 확인을 위한 챌린지에 응답하는 역할을 합니다. Version의 경우, 상황에 따라 달라질 수 있다. 위 예시에서 values.yaml파일 내용은 아래와 같다.

1
2
crds:
  enabled: true

3. Issuer 리소스 생성

인증서를 발급받기 위해, Issuer 리소스를 생성하여 어떤 인증 기관을 통해 인증서를 발급받을지 정의합니다. 여기서는 Let’s Encrypt를 사용합니다:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
  name: letsencrypt-production
spec:
  acme:
    # ACME 서버 URL
    server: https://acme-v02.api.letsencrypt.org/directory
    # ACME 등록을 위한 이메일 주소
    email: user@example.com
    # ACME 계정 비밀키를 저장할 Secret 이름
    privateKeySecretRef:
      name: letsencrypt-production
    solvers:
    - http01:
        ingress:
          class: nginx

또는

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
 name: letsencrypt-prod
 namespace: apps
spec:
 acme:
   # ACME 서버 URL
   server: https://acme-v02.api.letsencrypt.org/directory
   # ACMD 등록을 위한 이메일 주소
   email: user@example.com
   # ACME 계정 비밀키를 저장할 Secret 이름
   privateKeySecretRef:
     name: letsencrypt-prod
   solvers:
   - http01:
       ingress:
         class: traefik

여기서 ACME는 자동화된 인증서 관리를 위한 프로토콜로, 도메인 소유권 확인을 위해 HTTP-01 챌린지를 사용합니다. 이는 지정된 경로에 토큰 파일을 업로드하여 인증 기관이 이를 확인하는 방식입니다.

4. Certificate 생성

1
2
3
4
5
6
7
8
9
10
11
12
13
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: syn-tara-cert
spec:
  secretName: syn-tara-cert
  issuerRef:
    name: letsencrypt-prod
    kind: Issuer
  commonName: syn-tara.com
  dnsNames:
  - some-where-my-domain.com
  - some-where-sub-domain.some-where-my-domain.com

5. Ingress 리소스 생성 및 인증서 적용

이제, 애플리케이션에 대한 Ingress 리소스를 생성하고, TLS 설정을 추가하여 HTTPS를 적용합니다:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    cert-manager.io/issuer: "letsencrypt-production"
    cert-manager.io/acme-challenge-type: http01
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: example-service
            port:
              number: 80
  tls:
  - hosts:
    - example.com
    secretName: example-com-tls

위 설정에서 cert-manager.io/issuer 어노테이션을 통해 앞서 생성한 Issuer를 지정하고, tls 섹션에 도메인과 사용할 시크릿의 이름을 명시합니다. cert-manager는 이 정보를 기반으로 인증서를 발급하고, 해당 시크릿에 저장합니다.

6. 인증서 발급 및 자동 갱신 확인

설정이 완료되면, cert-manager는 자동으로 인증서를 발급하고 Ingress에 적용합니다. 인증서의 상태는 다음 명령어로 확인할 수 있습니다:

1
kubectl describe certificate example-com-tls

또한, cert-manager는 인증서의 만료 기간을 모니터링하며, 자동으로 갱신을 수행합니다.

이러한 과정을 통해 쿠버네티스 환경에서 cert-manager와 NGINX Ingress를 활용하여 HTTPS를 적용하고, 인증서 관리를 자동화할 수 있습니다.

This post is licensed under CC BY 4.0 by the author.