본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Kubernetes에서 cert-manager로 PKI 하이브리드 인스턴스 설정하는 방법

원문 보기 위키 갱신

Kubernetes에서 cert-manager로 PKI 하이브리드 인스턴스 설정하는 방법 (PKI Hybrid)

Kong Gateway를 하이브리드 모드로 운영할 때 공인 CA가 아닌 사설 CA의 PKI 인증서를 적용하는 방법을 다뤄요 cert-manager로 루트 CA에서 중간 CA 두

출처: 문서

본문

TL;DR

Kubernetes에서 cert-manager를 사용해 프라이빗 CA가 발급한 PKI 인증서를 쓰도록 Kong Gateway 하이브리드 모드를 구성하려면 어떻게 해야 하나요? cert-manager를 사용해 프라이빗 루트 CA에서 두 개의 중간 CA를 거쳐 인증서 체인을 만들고, 최종 중간 issuer에서 컨트롤 플레인용(server auth) 인증서와 데이터 플레인용(client auth) 인증서를 각각 발급받아요. 그런 다음 만들어진 시크릿을 Kong Helm 차트 값(cluster_ca_cert, cluster_cert, cluster_cert_key)에 마운트하고, 양쪽 노드 모두에 cluster_mtls: pki를 설정한 뒤, lua_ssl_verify_depth를 전체 체인을 커버할 수 있을 만큼 넉넉하게 설정해요.

개요

PKI 인증서를 사용하는 하이브리드 모드로 Kong을 구성할 때 cert-manager와 프라이빗 CA를 사용해야 하는 요구 사항이 있어요. 인증서가 신뢰할 수 있는 CA에서 발급된 것이 아니기 때문에, Kong이 프라이빗 CA 인증서를 사용할 수 있으려면 어떤 설정이 필요할까요?

단계

소개

아래 단계에서는 필요한 설정 과정을 하나씩 살펴봐요. 이 문서에서는 프라이빗 루트 CA와 중간 인증서 2개를 사용하고, 루트 CA의 프라이빗 키와 공개 인증서에 접근할 수 있다고 가정해요. 이 문서를 진행하면서 아래 인증서를 사용할 수 있다고 가정할게요:

rootCA.key - 루트 CA 프라이빗 키

rootCA.pem - 루트 CA 공개 인증서

환경에 따라서는 이 인증서 쌍에 접근할 수 없고, 대신 cert-manager에서 사용할 수 있는 ClusterIssuer가 있을 수도 있어요. 그런 경우에는 자신의 환경에 맞게 지침을 적절히 조정해야 해요.

cert-manager 설치

Kubernetes 문서를 따라 cert-manager를 설치하고 테스트해 보세요.

인증서 체인 만들기

RootCA용 시크릿 만들기

루트 CA의 프라이빗 키와 공개 인증서가 담긴 tls 시크릿을 만들어요


kubectl create secret tls rootca-key-pair --key="rootCA-noenc.key" --cert="rootCA.pem" -n kong

루트 CA Issuer 만들기


cat <<EOF | kubectl apply -f -
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
  name: rootca-issuer
  namespace: kong
spec:
  ca:
    secretName: rootca-key-pair
EOF

루트 CA Issuer에서 Intermediate 1 CA 만들기


cat <<EOF | kubectl apply -f -
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: intermediate-ca1
  namespace: kong
spec:
  # Secret names are always required.
  secretName: intermediate-ca1-key-pair
  duration: 1440h # 60d
  renewBefore: 240h # 10d
  commonName: Kong Intermediate 1 CA
  isCA: true
  privateKey:
    algorithm: RSA
    encoding: PKCS1
    size: 2048
  # Issuer references are always required.
  issuerRef:
    name: rootca-issuer
    # We can reference ClusterIssuers by changing the kind here.
    # The default value is Issuer (i.e. a locally namespaced Issuer)
    kind: Issuer
    # This is optional since cert-manager will default to this value however
    # if you are using an external issuer, change this to that issuer group.
    group: cert-manager.io
EOF

Intermediate 1 CA Issuer 만들기


cat <<EOF | kubectl apply -f -
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
  name: intermediate1ca-issuer
  namespace: kong
spec:
  ca:
    secretName: intermediate-ca1-key-pair
EOF

Intermediate 1 CA Issuer에서 Intermediate 2 CA 만들기


cat <<EOF | kubectl apply -f -
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: intermediate-ca2
  namespace: kong
spec:
  # Secret names are always required.
  secretName: intermediate-ca2-key-pair
  duration: 720h # 30d
  renewBefore: 120h # 5d
  commonName: Kong Intermediate 2 CA
  isCA: true
  privateKey:
    algorithm: RSA
    encoding: PKCS1
    size: 2048
  # Issuer references are always required.
  issuerRef:
    name: intermediate1ca-issuer
    # We can reference ClusterIssuers by changing the kind here.
    # The default value is Issuer (i.e. a locally namespaced Issuer)
    kind: Issuer
    # This is optional since cert-manager will default to this value however
    # if you are using an external issuer, change this to that issuer group.
    group: cert-manager.io
EOF

Intermediate 2 CA Issuer 만들기


cat <<EOF | kubectl apply -f -
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
  name: intermediate2ca-issuer
  namespace: kong
spec:
  ca:
    secretName: intermediate-ca2-key-pair
EOF

전체 인증서 체인이 담긴 시크릿 만들기

인증서 검증을 위해서는 rootCA --> Intermediate1CA --> Intermediate2CA로 이어지는 전체 체인을 제공해야 해요. 이 시크릿은 generic 타입으로 만들어야 해요


kubectl get secret rootca-key-pair -n kong -o jsonpath='{.data.tls\.crt}'|base64 -d > chain.pem
kubectl get secret intermediate-ca1-key-pair -n kong -o jsonpath='{.data.tls\.crt}'|base64 -d >> chain.pem
kubectl get secret intermediate-ca2-key-pair -n kong -o jsonpath='{.data.tls\.crt}'|base64 -d >> chain.pem
kubectl -n kong create secret generic cluster-fullchain --from-file=./chain.pem

Kong 클러스터 인증서 만들기

PKI 모드에서는 컨트롤 플레인과 데이터 플레인이 같은 CA로 서명된 각자의 인증서를 필요로 해요.

컨트롤 플레인 인증서 쌍 만들기

컨트롤 플레인 인증서를 생성하는 cert-manager 설정을 작성해요. 이때 컨트롤 플레인에는 "Web Server Authentication" 용도가 필요하다는 점에 유의하세요:


cat <<EOF | kubectl apply -f -
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: kong-control-plane-kong-cluster-inter
  namespace: kong
spec:
  # Secret names are always required.
  secretName: kong-control-plane-kong-cluster-inter
  duration: 240h # 10d
  renewBefore: 48h # 2d
  commonName: kong-cp
  subject:
    organizations:
    - kong-cp
  isCA: false
  privateKey:
    algorithm: RSA
    encoding: PKCS1
    size: 2048
  usages:
    - server auth
  # At least one of a DNS Name, URI, or IP address is required.
  dnsNames:
  - kong-control-plane-kong-cluster.kong.svc.cluster.local
  # Issuer references are always required.
  issuerRef:
    name: intermediate2ca-issuer
    kind: Issuer
    group: cert-manager.io
EOF

다음과 같은 명령어로 인증서의 세부 정보를 확인할 수 있어요:


kubectl get secret kong-control-plane-kong-cluster-inter -n kong -o jsonpath='{.data.tls\.crt}'|base64 -d|openssl x509 -text

데이터 플레인 인증서 쌍 만들기

데이터 플레인 인증서를 생성하는 cert-manager 설정을 작성해요. 이때 데이터 플레인에는 "Web Client Authentication" 용도가 필요하다는 점에 유의하세요:


cat <<EOF | kubectl apply -f -
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: kong-data-plane-kong-cluster-inter
  namespace: kong
spec:
  # Secret names are always required.
  secretName: kong-data-plane-kong-cluster-inter
  duration: 240h # 10d
  renewBefore: 48h # 2d
  commonName: kong-dp
  subject:
    organizations:
    - kong-dp
  isCA: false
  privateKey:
    algorithm: RSA
    encoding: PKCS1
    size: 2048
  usages:
    - client auth
  # At least one of a DNS Name, URI, or IP address is required.
  dnsNames:
  - kong-data-plane-kong-cluster.kong.svc.cluster.local
  # Issuer references are always required.
  issuerRef:
    name: intermediate2ca-issuer
    kind: Issuer
    group: cert-manager.io
EOF

다음과 같은 명령어로 인증서의 세부 정보를 확인할 수 있어요:


kubectl get secret kong-data-plane-kong-cluster-inter -n kong -o jsonpath='{.data.tls\.crt}'|base64 -d|openssl x509 -text

Kong 인스턴스 배포하기

중간 인증서를 사용할 때는 Kong이 검증 과정에서 전체 인증서 체인을 끝까지 순회할 수 있도록 lua_ssl_verify_depth 파라미터를 적절하게 설정하는 게 중요해요. 이 값이 너무 작으면 인증서 체인이 너무 길다는 오류가 표시돼요.

컨트롤 플레인 배포하기

minimal-kong-hybrid-control.yaml이라는 이름의 최소 컨트롤 플레인 Helm 차트를 만들어요


# Basic configuration for Kong without the ingress controller, using the Postgres subchart
# This installation does not create an Ingress or LoadBalancer Service for
# the Admin API. It requires port-forwards to access without further
# configuration to add them, e.g.:
# kubectl port-forward deploy/your-deployment-kong 8001:8001

image:
  repository: kong/kong-gateway
  tag: "3.14.0.0"

enterprise:
  enabled: true

env:
  prefix: /kong_prefix/
  anonymous_reports: off

  database: postgres
  pg_username: kong
  pg_password: password
  pg_database: kong

  role: control_plane
  cluster_mtls: pki
  cluster_telemetry_listen: 0.0.0.0:8006
  cluster_ca_cert: /etc/secrets/cluster-fullchain/chain.pem
  cluster_cert: /etc/secrets/kong-control-plane-kong-cluster-inter/tls.crt
  cluster_cert_key: /etc/secrets/kong-control-plane-kong-cluster-inter/tls.key
  lua_ssl_verify_depth: 5

admin:
  enabled: true
  http:
    enabled: true
    servicePort: 8001
    containerPort: 8001

cluster:
  enabled: true
  tls:
    enabled: true
    servicePort: 8005
    containerPort: 8005

proxy:
  enabled: false

secretVolumes:
  - kong-control-plane-kong-cluster-inter
  - cluster-fullchain

postgresql:
  enabled: true
  # The Kong Helm chart's bundled postgresql subchart no longer ships a default image
  # (the plain "bitnami/postgresql" repository stopped publishing this tag). Use
  # "bitnamilegacy/postgresql", or any other Postgres image you host yourself.
  image:
    registry: docker.io
    repository: bitnamilegacy/postgresql
    tag: "13"
  postgresqlUsername: kong
  postgresqlPassword: password
  postgresqlDatabase: kong
  service:
    port: 5432

ingressController:
  enabled: false

PKI 인증서가 담긴 시크릿은 secretVolumes로 마운트돼요. 이렇게 하면 설정에서 경로 이름으로 인증서를 참조할 수 있어요.

Helm으로 컨트롤 플레인을 배포해요


helm install kong-control-plane kong/kong -f minimal-kong-hybrid-control.yaml -n kong

데이터 플레인 배포하기

minimal-kong-hybrid-data.yaml이라는 이름의 최소 데이터 플레인 Helm 차트를 만들어요


# Basic configuration for Kong as a hybrid mode data plane node.
# It depends on the presence of a control plane release, as shown in
# https://github.com/Kong/charts/blob/main/charts/kong/example-values/minimal-kong-hybrid-control.yaml
#
# The "env.cluster_control_plane" value must be changed to your control plane
# instance's cluster Service hostname. Search "CHANGEME" to find it in this
# example.
#
# Hybrid mode requires a certificate. See https://github.com/Kong/charts/blob/main/charts/kong/README.md#certificates
# to create one.

image:
  repository: kong/kong-gateway
  tag: "3.14.0.0"

env:
  prefix: /kong_prefix/
  database: "off"
  log_level: debug

  role: data_plane
  cluster_mtls: pki
  cluster_control_plane: kong-control-plane-kong-cluster.kong.svc.cluster.local:8005
  cluster_ca_cert: /etc/secrets/cluster-fullchain/chain.pem
  cluster_cert: /etc/secrets/kong-data-plane-kong-cluster-inter/tls.crt
  cluster_cert_key: /etc/secrets/kong-data-plane-kong-cluster-inter/tls.key
  lua_ssl_trusted_certificate: /etc/secrets/cluster-fullchain/chain.pem
  lua_ssl_verify_depth: 5

admin:
  enabled: false

proxy:
  enabled: true

secretVolumes:
  - kong-data-plane-kong-cluster-inter
  - cluster-fullchain

ingressController:
  enabled: false

PKI 인증서가 담긴 시크릿은 secretVolumes로 마운트돼요. 이렇게 하면 설정에서 경로 이름으로 인증서를 참조할 수 있어요.

Helm으로 데이터 플레인을 배포해요


helm install kong-data-plane kong/kong -f minimal-kong-hybrid-data.yaml -n kong

이제 cert-manager가 생성한 인증서를 PKI 하이브리드 모드에 사용하는 컨트롤 플레인 pod와 데이터 플레인 pod가 준비됐어요.

관련 리소스

더 알아보기 (Learn more)

이 문서에는 원문에서 추출한 별도의 관련 문서 링크가 없어요.