2025.03.11 - [인프라] - [쿠버네티스] Kubernetes에서 가장 이해하기 어려운 Ingress와 Nginx의 수 많은 기능들
[쿠버네티스] Kubernetes에서 가장 이해하기 어려운 Ingress와 Nginx의 수 많은 기능들
Ingress의 개념 및 기본 사용 네임스페이스에 프론트엔드 서버 portal과 백엔드 서버 core 2개의 app이 있다고 하자.보통 웹 서비스를 구현할 때 쿠버네티스 클러스터 상에서 각 app 서버는 서비스(Servi
kangth97.tistory.com
이전 글에서 분량이 너무 많아져 이어서 작성한다.
Nginx의 로드밸런싱, 로그 포맷 변경과 타임존 변경하는 방법에 대해 다룬다.
Nginx 로드밸런싱 방식 변경
https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/#load-balance
ConfigMap - Ingress-Nginx Controller
ConfigMaps ConfigMaps allow you to decouple configuration artifacts from image content to keep containerized applications portable. The ConfigMap API resource stores configuration data as key-value pairs. The data provides the configurations for system com
kubernetes.github.io
#3-1-1. Portal Pod내에서 API 테스트 (균등분산)
kubectl exec -n anotherclass-322 portal-3222-<tab> -it -- /bin/sh
while true; do curl ingress-nginx-controller.ingress-nginx/core/hostname; sleep 0.5; echo ''; done;
#3-1-2. Core Pod 한곳에 들어가서 부하 생성 후 API 모니터링
kubectl exec -n anotherclass-322 core-3222-<tab> -it -- /bin/sh
curl localhost:8080/cpu-load
# 3-1-3. 부하 분산 알고리즘 변경 (configmap 변경시 Nginx에서 바로 적용됨)
# ewma : Pod의 Response 평균치를 기준으로 응답시간이 빠른 곳에 트래픽을 우선으로 보내줌 (default : round_robin)
kubectl edit -n ingress-nginx configmap ingress-nginx-controller
---
data:
load-balance: "ewma"
nginx의 기본 로드밸런싱 방식은 라운드로빈이다.
이를 ewma로 바꾸면Pod의 Response 평균치를 기준으로 응답시간이 빠른 곳에 트래픽을 우선으로 보내준다.
Nginx 로그 포맷 변경
https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/log-format/
Log format - Ingress-Nginx Controller
Log format The default configuration uses a custom logging format to add additional information about upstreams, response time and status. log_format upstreaminfo '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_refe
kubernetes.github.io
먼저 기본 Nginx 로그 포맷은 다음과 같다.
vagrant@personal:~/tls$ k logs -n ingress-nginx ingress-nginx-controller-6dc8c8fdf4-9g9l5 --tail 10
192.168.13.246 - - [23/Feb/2025:02:22:17 +0000] "GET /core/hostname HTTP/1.1" 200 25 "-" "curl/7.61.1" 115 0.020 [anotherclass-322-core-3222-80] [] 192.168.13.245:8080 25 0.020 200 c4a240714530528aab762f70950618e3
192.168.13.246 - - [23/Feb/2025:02:22:18 +0000] "GET /core/hostname HTTP/1.1" 200 25 "-" "curl/7.61.1" 115 0.001 [anotherclass-322-core-3222-80] [] 192.168.13.244:8080 25 0.001 200 20d599a839807cc0bbf1faf209b8525a
192.168.13.246 - - [23/Feb/2025:02:22:19 +0000] "GET /core/hostname HTTP/1.1" 200 25 "-" "curl/7.61.1" 115 0.012 [anotherclass-322-core-3222-80] [] 192.168.13.245:8080 25 0.013 200 1550b87698db610fcee69cd81fa45d81
다음과 같이 로그 포맷을 변경할 수 있다.
kubectl edit -n ingress-nginx configmap ingress-nginx-controller
---
data:
log-format-upstream: '[$time_local] Remote[$remote_addr] $http_user_agent -> Service[$service_name] -> Pod[$upstream_addr] : $request $content_type Sta[$upstream_status] ReqLen[$request_length] ResLen[$upstream_response_length] Dur[$upstream_response_time]'
'$변수명'으로 기록하고 싶은 요소를 포함하여 로그 포맷을 구성하면 된다.
변경된 로그 포맷을 확인하면 다음과 같다.
vagrant@personal:~/tls$ k logs -n ingress-nginx ingress-nginx-controller-6dc8c8fdf4-9g9l5 --tail 10
[23/Feb/2025:02:24:59 +0000] Remote[192.168.13.246] curl/7.61.1 -> Service[core-3222] -> Pod[192.168.13.244:8080] : GET /core/hostname HTTP/1.1 - Sta[200] ReqLen[115] ResLen[25] Dur[0.008]
[23/Feb/2025:02:24:59 +0000] Remote[192.168.13.246] curl/7.61.1 -> Service[core-3222] -> Pod[192.168.13.244:8080] : GET /core/hostname HTTP/1.1 - Sta[200] ReqLen[115] ResLen[25] Dur[0.006]
[23/Feb/2025:02:25:00 +0000] Remote[192.168.13.246] curl/7.61.1 -> Service[core-3222] -> Pod[192.168.13.244:8080] : GET /core/hostname HTTP/1.1 - Sta[200] ReqLen[115] ResLen[25] Dur[0.015]
Nginx 서버 시간 변경 (타임존 변경)
기본적인 nginx 타임존은 글로벌 프로젝트일 경우에 UTC를 사용한다. 로그에도 UTC 기준으로 시간이 찍히기 때문에 이를 스크립트를 사용하여 Asia/Seoul로 타임존을 변경해야 한다. 로그를 확인하면 UTC 타임존이다. (+0000)
# UTC로 시간이 찍힌다
vagrant@personal:~/tls$ k logs -n ingress-nginx ingress-nginx-controller-6dc8c8fdf4-9g9l5 --tail 10
[23/Feb/2025:02:24:59 +0000] Remote[192.168.13.246] curl/7.61.1 -> Service[core-3222] -> Pod[192.168.13.244:8080] : GET /core/hostname HTTP/1.1 - Sta[200] ReqLen[115] ResLen[25] Dur[0.008]
[23/Feb/2025:02:24:59 +0000] Remote[192.168.13.246] curl/7.61.1 -> Service[core-3222] -> Pod[192.168.13.244:8080] : GET /core/hostname HTTP/1.1 - Sta[200] ReqLen[115] ResLen[25] Dur[0.006]
[23/Feb/2025:02:25:00 +0000] Remote[192.168.13.246] curl/7.61.1 -> Service[core-3222] -> Pod[192.168.13.244:8080] : GET /core/hostname HTTP/1.1 - Sta[200] ReqLen[115] ResLen[25] Dur[0.015]
타임존을 변경하는 방법은 다음과 같다. Helm의 values.yaml 파일을 수정한다.
// Helm Values.yaml 파일 수정
[root@k8s-master ~]# cd ~/ingress-nginx
[root@k8s-master ~]# vi values-dev.yaml
---
# Timezone 설정
extraEnvs:
- name: TZ
value: Asia/Seoul
---
// Nginx 재배포
[root@k8s-master ~]# helm upgrade ingress-nginx . -f ./values-dev.yaml -n ingress-nginx --install
다음과 같이 Asia/Seoul 타임존(+0900)으로 시간이 변경된 것을 확인할 수 있다.
vagrant@personal:~/ingress-nginx$ k logs -n ingress-nginx ingress-nginx-controller-57c68475f4-ss89j --tail 4
[23/Feb/2025:11:29:07 +0900] Remote[192.168.13.246] curl/7.61.1 -> Service[core-3222] -> Pod[192.168.13.245:8080] : GET /core/hostname HTTP/1.1 - Sta[200] ReqLen[115] ResLen[25] Dur[0.001]
[23/Feb/2025:11:29:07 +0900] Remote[192.168.13.246] curl/7.61.1 -> Service[core-3222] -> Pod[192.168.13.245:8080] : GET /core/hostname HTTP/1.1 - Sta[200] ReqLen[115] ResLen[25] Dur[0.001]
[23/Feb/2025:11:29:08 +0900] Remote[192.168.13.246] curl/7.61.1 -> Service[core-3222] -> Pod[192.168.13.245:8080] : GET /core/hostname HTTP/1.1 - Sta[200] ReqLen[115] ResLen[25] Dur[0.000]
[23/Feb/2025:11:29:09 +0900] Remote[192.168.13.246] curl/7.61.1 -> Service[core-3222] -> Pod[192.168.13.245:8080] : GET /core/hostname HTTP/1.1 - Sta[200] ReqLen[115] ResLen[25] Dur[0.001]
'인프라' 카테고리의 다른 글
[쿠버네티스] Kubernetes에서 가장 이해하기 어려운 Ingress와 Nginx의 수 많은 기능들 (0) | 2025.03.11 |
---|---|
[쿠버네티스] 인프라 구성으로 배우는 Kubernetes Service의 거의 모든 기능들 (0) | 2025.03.09 |
[쿠버네티스] Application 개발자가 꼭 알아야하는 Kubernetes Pod 기능 (2) - Pod 종료 시 안정적으로 Application 종료하기 (0) | 2025.03.08 |
[쿠버네티스] Application 개발자가 꼭 알아야하는 Kubernetes Pod 기능 (1) - Pod 정보 조회하기 (1) | 2025.03.08 |
개발자 쿠버네티스 개발/테스트 환경 구축하기 (0) | 2025.03.07 |