본문 바로가기
Kubernetes

[Kubernetes] Pod 또는 Deployment 생성 시 주의 사항

by worldcenter 2025. 1. 20.

장애 상황

1. 다음 명령어를 사용하여 Deployment와 Service를 생성합니다.

// Deployment 생성
kubectl create deploy <Deployment Name> --image=nginx:latest --port=8080 --replicas=1
// Service 생성
kubectl expose deploy <Deployment Name> --name=<Service Name> --port=80 --target-port=8080

 

2. Nginx Ingress Controller를 설치하고 Ingress를 생성합니다.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nuri-ingress
  namespace: nuri
spec:
  ingressClassName: nginx
  rules:
  - host: balance.gaonnuri.site
    http:
      paths:
      - backend:
          service:
            name: nuri-service
            port:
              number: 80
        path: /
        pathType: Prefix

 

3. balance.gaonnuri.site 접속 시 502 Bad Gateway가 발생하면서 Nginx Controller에서는 다음의 에러 로그가 발생하는 것을 볼 수 있습니다.

// 해당 에러는 Nginx Controller가 업스트림 서버(백엔드 서버), 즉 Pod로 통신을 할 수 없다는 에러
2023/05/22 05:51:06 [error] 98#98: *13328 connect() failed (111: Connection refused) while connecting to upstream, client: 10.224.0.4, server: balance.gaonnuri.site, request: "GET / HTTP/1.1", upstream: "http://10.224.0.143:8080/", host: "balance.gaonnuri.site"
502 Bad Gateway는 프록시 역할을 하는 서버가 다른 서버로부터 유효하지 않은 응답을 받았을 때 나타납니다. 

 

해결 방법

nginx의 경우에는 설정 파일에 listen 포트가 지정되어 있습니다. 예를 들어 80 포트가 지정되어 있는데 컨테이너 포트를 8080으로 할 경우 nginx가 응답을 할 수 없는 것 입니다.

 

다음의 2가지 방법 중 하나를 택하여 이슈를 해결할 수 있습니다.

  1. Container Port를 80으로 변경
  2. Nginx 설정 파일에서 Listen 포트를 8080으로 변경