sangw0804 / study-docker

0 stars 1 forks source link

8. 인그레스(Ingress) #7

Open jihyuunn opened 3 years ago

jihyuunn commented 3 years ago

Ingress

ingress 기능

인그레스를 사용해야하는 이유?

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-example
  annotations:
    # nginx 컨트롤러에서만 사용가능한 기능. 인그레스에 정의된 경로로 들어오는 요청을 rewrite-target에 설정된 경로로 전달
    # /echo-hostname을 hostname-service에는 / 로 전달됨
    nginx.ingress.kubernetes.io/rewrite-target: /
    # 해당 규칙을 어떤 인그레스 컨트롤러에 적용할 것인지
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
    # 해당 도메인으로 접근하는 요청에 대한 규칙 정함
    - host: alicek106.example.com
      http:
        # 해당 요청을 어느 서비스로 보낼지 정하는 것. 해당 path를 어느 backend에 보낼지
        paths:
          - path: /echo-hostname
            backend:
              serviceName: hostname-service
              # hostname-service의 80번 포트로 보낸다
              servicePort: 80
 kubectl apply -f ingress-example.yaml
 kubectl get ingress

ingress

실제로 외부 요청을 받아들이는 것은 인그레스 컨트롤러이다. 여러 종류가 있는데 엔진엑스걸 많이 사용한다. ingress controller에서 ingress의 규칙을 이용하는 것

kubectl apply -f \
  https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.35.0/deploy/static/provider/aws/deploy.yaml
kubectl get pods,deployment -n ingress-nginx
kubectl get svc -n ingress-nginx

kubectl apply -f hostname-deployment.yaml
kubectl apply -f hostname-service.yaml
kubernetes curl localhost/echo-hostname
kubectl edit ingress ingress-example
curl localhost/echo-hostname
  1. 공식 깃허브에서 제공되는 YAML파일로 nginx 인그레스 컨트롤러 생성
  2. Nginx 인그레스 컨트롤러를 외부로 노출하기 위한 서비스 생성
  3. 요청 처리 규칙을 정의하는 인그레스 오브젝트 생성 -> 인그레스 컨트롤러가 자동으로 인그레스를 로드해 nginx 웹서버에 적용(항상 인그레스 리소스 상태를 지켜본다)
  4. Nginx 인그레스 컨트롤러로 들어온 요청은 인그레스 규칙에 따라 적절한 서비스로 전달 근데 ingress-example에 작성된 것처럼 hostname-service로 전달이 되는건 아니다.. 서비스에 의해 생성된 엔드포인트로 전달됨

ingress의 annotation

SSL/TLS 적용가능

openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
  -keyout tls.key -out tls.crt -subj "/CN-alicek106.example.com/0=alicek106"
kubectl apply -f ingress-tls.yam
curl https://localhost/echo-hostname -k
curl http://localhost/echo-hostname -k