OpenShift v4.x – Ingress Controller 접근 로그(access log) 설정 – Yongbok Blog
Yongbok Blog

OpenShift v4.x – Ingress Controller 접근 로그(access log) 설정

OpenShift 환경에서 Ingress Controller에 Access Log를 설정하는 방법에 대해서 정리한다.

1. 개요

 

Kubernetes 환경에서 Ingress Controller의 주 역할은 내부/외부에서
Application Pod 접근에 사용되는 Software L4, L7 기능을 담당한다고 보면 된다.

OpenShift의 경우 HAProxy 기반으로 제공되는 Router Pod가 이 역할을 담당하고 있으며,
OpenShift v4.x 버전부터는 Ingress Controller Operator라는 이름으로 CRD(Custom Resource Definition)에서 제공하는
기능으로만 제어가 가능하도록 변경 되었다.

1.1. 주 목적

 

내부/외부에서 Application Pod에 접근시 해당 구간 사이에 트래픽이 전달이 되지 않거나 이슈 분석 용도로 많이 사용한다.

2. Access Log 설정

 

Ingress Controller Operator에서 제공되는 기록 방식은 크게 2가지로 나눌수 있다.

2.1. Container 방식

 

Router Pod에 Sidecar Container가 logs라는 이름으로 추가가 되고, stdout 방식으로 로그가 출력 제공되는 기능을 제공한다.
HAProxy 기반의 Custom Log를 지원하며, Conatiner에 로그가 쌓이게 되므로 디스크 사용량이 많아 지고,
OpenShift Logging(EFK Stack)을 사용중이라면 로그 수집량이 기하급수적으로 늘어날수 있으므로 주의 해야한다.

Ingress Controller의 설정을 통해 access log 설정을 추가한다.

[root@bastion ~]# oc edit ingresscontrollers/default -n openshift-ingress-operator
apiVersion: operator.openshift.io/v1
kind: IngressController
metadata:
  name: default
  namespace: openshift-ingress-operator
spec:
....
  logging:
    access:
      destination:
        type: Container
      httpCaptureHeaders:
        request:
        - maxLength: 70
          name: Host
        - maxLength: 20
          name: Server
        - maxLength: 256
          name: User-Agent
        - maxLength: 50
          name: X-Forwarded-For
      httpLogFormat: '{"timestamp": "%trl", "server": "%s", "client": "%ci", "status": "%ST", "request": "%HM %HU %HV", "size": "%B", "referrer": "%hr"}'

2.1.1. 옵션 설명

 

– httpCaptureHeaders

 

access log에 캡쳐되어야 하는 HTTP Header를 정의 할때 사용한다.
이 필드가 비어 있는경우 헤더 캡쳐가 되지 않으며, HTTPS에 이 옵션은 사용이 불가능하다.
maxLength의 최소/최대값은 1~1024를 가진다.

– httpLogFormat

 

HAProxy에서 제공하는 로그 포맷을 사용할수 있다.
본 예제 처럼 설정이 되면 아래와 같이 JSON 형식으로 로그 정제가 가능하다.

{
   "timestamp":"17/Jul/2022:05:21:05 +0000",
   "server":"pod:console-74dbf5bdb-9f69n:console:https:10.128.0.18:8443",
   "client":"192.168.0.1",
   "status":"200",
   "request":"GET /api/alertmanager/api/v2/silences HTTP/1.1",
   "size":"401",
   "referrer":"{console.apps.ocp4.poc.local||Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36|}"
}

로그 포맷은 관련 링크[1]에서 custom으로 조합하여 사용가능하다.

2.2. syslog 방식

 

중앙 로그 수집기인 syslog에 원격으로 메세지 전달하는 형태이다.
facility 기반으로 설정이 가능하며, IPv4, IPv6을 지원한다.

HAProxy 기반의 Custom Log를 지원하며, Sidecar Container 방식이 아닌,
syslog 기능을 사용하여 원격지의 서버로 로그 메세지 전달을 제공한다.

syslog 서버가 없는 경우 구성하여 설정한다.
(syslog 서버가 있는 경우 이 과정은 생략한다.)

2.2.1. Rsyslog 설치

 

[root@rsyslog ~]# yum install rsyslog

2.2.2. Rsyslog 설정

 

Ingress Controller에서 전송한 로그 메세지를 받을 서비스 포트와 Facility, 로그 파일 저장 위치를 지정한다.

[root@rsyslog ~]# vi /etc/rsyslog.d/haproxy.conf
## OpenShift 4.x - Ingress Controller ##
# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514
$template Haproxy, "%msg%\n"
# Facility
local1.* /data/logs/rsyslog/haproxy/ocp4-ingress-controller-access.log

2.2.3. 로그 저장소 디렉토리 생성

 

실제 전달 받은 로그를 저장할 디렉토리를 생성한다.

[root@rsyslog ~]# mkdir -p /data/logs/rsyslog/haproxy/

2.2.4. SELinux Context 설정

 

SELinux가 활성화 되어 있는 경우 아래와 같이 파일 및 디렉토리 권한을 부여 한다.

[root@rsyslog ~]# yum install policycoreutils-python
[root@rsyslog ~]# semanage fcontext -a -t etc_t "/etc/rsyslog.d/*"
[root@rsyslog ~]# semanage fcontext -a -t syslog_conf_t "/etc/rsyslog.d/*"
[root@rsyslog ~]# restorecon -R -v /etc/rsyslog.d/
[root@rsyslog ~]# chcon -R -t var_log_t /data/logs/

2.2.5. 방화벽 설정

 

syslog는 기본 UDP/514 포트를 사용하므로 SELinux와 방화벽에 서비스 포트를 등록 한다.

[root@rsyslog ~]# semanage port -l | grep syslog
syslog_tls_port_t              tcp      6514, 10514
syslog_tls_port_t              udp      6514, 10514
syslogd_port_t                 tcp      601, 20514
syslogd_port_t                 udp      514, 601, 20514
[root@rsyslog ~]# semanage port -m -t syslogd_port_t -p udp 514
[root@rsyslog ~]# firewall-cmd --permanent --zone=public --add-port=514/udp
[root@rsyslog ~]# firewall-cmd --reload

2.2.6. 서비스 활성화 및 구동

 

[root@rsyslog ~]# systemctl enable rsyslog
[root@rsyslog ~]# systemctl restart rsyslog

2.2.7. Log Rotate 설정

 

로그 파일의 용량이 많아지면 분석하는데 시간이 많이 소요 되므로, logrotate를 통해 매일 rotate 될수 있도록 설정한다.

[root@rsyslog ~]# yum install logrotate
[root@rsyslog ~]# vi /etc/logrotate.d/ocp4-ingress-controller-access
/data/logs/rsyslog/haproxy/*log {
    # 로그 파일 roate 이후, 원본 파일명으로 신규 파일 생성시 권한 부여
    create 0644 nobody nobody

    # 로그 파일 압축
    compress

    # 일 단위 실행
    daily

    # 회전 주기
    rotate 5

    # rotate된 파일명에 날짜를 붙임
    dateext

    # 로그 파일의 내용이 없을경우 rotate 하지 않음
    notifempty

    # 로그 파일이 없을 경우 에러 메시지를 출력하고 다음에 실행
    missingok

    # 로그 파일을 스크립트로 공유하여 실행
    sharedscripts

    # 스크립트 파일 실행
    # rotate 이후 새 로그 파일이 생성 되었을때 rsyslog가 로그를 계속 쌓을수 있도록 rsyslog를 재기동
    postrotate
        /bin/systemctl restart rsyslog.service > /dev/null 2>/dev/null || true
    endscript
}
[root@rsyslog ~]# logrotate -f /etc/logrotate.d/ocp4-ingress-controller-access

2.2.8. Ingress Controller 설정

 

[root@bastion ~]# oc edit ingresscontrollers/default -n openshift-ingress-operator
apiVersion: operator.openshift.io/v1
kind: IngressController
metadata:
  name: default
  namespace: openshift-ingress-operator
spec:
....
  logging:
    access:
      destination:
        syslog:
          address: 192.168.0.5
          facility: local1
          port: 514
        type: Syslog
      httpCaptureHeaders:
        request:
        - maxLength: 70
          name: Host
        - maxLength: 20
          name: Server
        - maxLength: 256
          name: User-Agent
        - maxLength: 50
          name: X-Forwarded-For
      httpLogFormat: '{"timestamp": "%trl", "server": "%s", "client": "%ci", "status": "%ST", "request": "%HM %HU %HV", "size": "%B", "referrer": "%hr"}'

2.2.9. 옵션 설명

 

– address

 

원격지의 syslog의 서버 IP 또는 FQDN을 지정한다.

– port

 

원격지의 syslog의 서버의 서비스 포트를 지정한다.

– facility

 

syslog에 로그를 전달할 항목을 정의한다.
지원되는 항목[2]은 아래와 같다.
kern, user, mail, daemon, auth, syslog, lpr,
news, uucp, cron, auth2, ftp, ntp, audit, alert, cron2,
local0, local1, local2, local3, local4, local5, local6, local7

– httpCaptureHeaders

 

access log에 캡쳐되어야 하는 HTTP Header를 정의 할때 사용한다.
이 필드가 비어 있는경우 헤더 캡쳐가 되지 않으며, HTTPS에 이 옵션은 사용이 불가능하다.
maxLength의 최소/최대값은 1~1024를 가진다.

– httpLogFormat

 

HAProxy에서 제공하는 로그 포맷을 사용할수 있다.
본 예제 처럼 설정이 되면 아래와 같이 JSON 형식으로 로그 정제가 가능하다.

{
   "timestamp":"17/Jul/2022:05:21:05 +0000",
   "server":"pod:console-74dbf5bdb-9f69n:console:https:10.128.0.18:8443",
   "client":"192.168.0.1",
   "status":"200",
   "request":"GET /api/alertmanager/api/v2/silences HTTP/1.1",
   "size":"401",
   "referrer":"{console.apps.ocp4.poc.local||Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36|}"
}

로그 포맷은 관련 링크[1]에서 custom으로 조합하여 사용가능하다.

3. RefURL

 

[1]: HAProxy – HTTP log format
[2]: GitHub – OpenShift Ingress Controller Operator: Facility list
[3]: OpenShift Docs – Configuring Ingress access logging

Exit mobile version