티스토리 뷰

nginx-ingress를 사용하면서 ingress에 nginx 설정을 좀더 추가하고 싶고 때에 따라서는 nginx-ingress에 글로벌하게 설정이 필요한 경우도 있다. 하여 이를 설정하는 방법에 대하여 알아보도록 하겠다.
여기서는 log_format을 custom한 설정추가의 하나의 예로 기술하였다.

기본적인 custom 설정 추가

먼저 기본적으로 custom 설정을 추가하기 위한 기본 작업을 알아보면,
nginx-ingress deployment를 확인해보면 다음과 같은 container 실행시 추가될 argument가 정의되어 있다.

...
spec:
  containers:
  - args:
    - /nginx-ingress-controller
    - --election-id=ingress-controller-leader
    - --controller-class=k8s.io/ingress-nginx
    - --configmap=$(POD_NAMESPACE)/ingress-nginx-controller
    - --validating-webhook=:8443
    - --validating-webhook-certificate=/usr/local/certificates/cert
    - --validating-webhook-key=/usr/local/certificates/key
    env:
...

여기서 정의된 configmap을 참고해두어야한다.

custom 한 log_format 설정

앞서 참고해둔 configmap의 이름으로 생성한 namespace에서 확인해보면 아래와 같이 확인이 된다.

apiVersion: v1
data:
  allow-snippet-annotations: "true"
...
    }
  log-format-upstream: $remote_addr - $request_id - [$proxy_add_x_forwarded_for] -
    $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer"
    "$http_user_agent" $request_length $request_time [$proxy_upstream_name] $upstream_addr
    $upstream_response_length $upstream_response_time $upstream_status $uri
kind: ConfigMap
...

위와 같이 설정을 하게되면 upstreaminfo 형태로 access_log 생성이 가능해진다.
이제 ingress를 생성시 다음과 같이 access_log를 추가하여 생성하게 되면

    nginx.ingress.kubernetes.io/server-snippet: |
      location ~ / {
        ...
          access_log /var/log/nginx/test-access.log upstreaminfo;
      }

nginx-ingress controller 가 동작되는 pod로 접근해보면 위에 정의한 /var/log/nginx에 test-access.log 파일이
생성되어 있고 로그가 쌓여가는것을 확인할 수 있다.

여기서 /var/log/nginx 외의 다른 디렉토리를 사용하고자 하는 경우는 volume mount가 필요하다.
nginx-ingress deployment를 수정하여 volume을 추가로 생성하도록 해야 한다.

custom header

위와 별개로 customer header를 configmap으로 별도 관리할수도 있다.

기본으로 생성되는 configmap인 ingress-nginx-controller에 proxy-set-headers를 추가한다.
(여기서 생성될 configmap의 namespace를 필수로 입력해주어야한다.)

apiVersion: v1
data:
  proxy-set-headers: "ingress-nginx/custom-headers"
kind: ConfigMap
metadata:
  name: ingress-nginx-controller
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx

아래와 같이 별도의 configmap을 아래와 같이 생성하면 아래 configmap에 선언된 header 정보가 ingress 를 생성하게 되면
추가되게 된다.

apiVersion: v1
data:
  X-Different-Name: "true"
  X-Request-Start: t=${msec}
  X-Using-Nginx-Controller: "true"
kind: ConfigMap
metadata:
  name: custom-headers
  namespace: ingress-nginx

위 내용은 기본 ingress-nginx 에서 참고하였다.

참고사이트

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함