두 손끝의 창조자

NCP Kubernetes 서비스에 HTTPS 적용 본문

DevOps

NCP Kubernetes 서비스에 HTTPS 적용

codinglog 2023. 8. 29. 14:22

네이버 클라우드 플랫폼에서 Kubernetes을 사용한다면 인그래스를 이용하여 https를 적용할 수 있다.

적용하려면 pod를 NodePort으로 노출시키고 인그래스 리소스를 이용해서 로드밸랜서를 생성한다.

인그래스 컨트롤러 설치

인그래스를 사용하려면 인그래스 콘트롤러(파드와 유사함)를 설치하고 인그래스 리소스로 노드포트 서비스와 연결한다.

kubectl --kubeconfig=$KUBE_CONFIG apply -f https://raw.githubusercontent.com/NaverCloudPlatform/nks-alb-ingress-controller/main/docs/install/pub/install.yaml

위 구문을 실행하면 그냥 설치된다.

노드포트 서비스 생성

apiVersion: v1
kind: Service
metadata: 
  name: sample-np-service 
spec:
  type: NodePort
  selector: 
    app: sample
  ports: 
    - name: http
      protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 30000

sample Pod를 포트 30000 으로 노출한다. 이렇게 설정하면 노드의 공인IP:30000으로 해당 POD 에 접근이 가능하다. 브라우저에서 접속하려면 ACG 설정에서 포트 30000을 추가해줘야 한다.

인그래스 생성

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: sample-alb-ingress
  annotations:
    alb.ingress.kubernetes.io/listen-ports: '[{"HTTP": 80},{"HTTPS":443}]'  
    alb.ingress.kubernetes.io/ssl-certificate-no: "[인증서번호]"
    alb.ingress.kubernetes.io/actions.ssl-redirect: |
      {"type":"redirection","redirection":{"port": "443","protocol":"HTTPS","statusCode":301}}    
  labels:
    app: sample-alb-ingress
spec:
  ingressClassName: alb
  defaultBackend:
    service:
      name: sample-np-service
      port:
        number: 80 
  rules:
  - http:
      paths:
      - path: /*
        pathType: Prefix
        backend:
          service:
            name: ssl-redirect
            port:
              name: use-annotation
      - path: /*
        pathType: Prefix
        backend:
          service:
            name: sample-np-service
            port:
              number: 80

인증서 번호는 콘솔의 Certificate Manager에서 찾을 수 있다.

 

반응형
Comments