Kubernetes에서 외부 트래픽을 클러스터 내부 서비스로 전달하려면 Ingress와 Ingress Controller가 필요하다.
Ingress는 단순히 설정(규칙)을 정의하는 리소스이고, 실제 트래픽을 처리하려면 반드시 Ingress Controller가 필요하다.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
spec:
rules:
- host: example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-service
port:
number: 80
| 항목 | Ingress | Ingress Controller |
|---|---|---|
| 역할 | 트래픽 라우팅 규칙 정의 | 실제 트래픽을 처리하는 컨트롤러 |
| 동작 방식 | Ingress 리소스를 생성하면 끝 | Ingress를 감시하고 라우팅 수행 |
| 단독 실행 가능 여부 | ❌ 불가능 (컨트롤러 필요) | ✅ 가능 |
| L7 트래픽 처리 | ✅ 가능 (HTTP/HTTPS 라우팅) | ✅ 가능 (컨트롤러에 따라 지원 기능 다름) |
| 로드 밸런싱 | ❌ 직접 수행하지 않음 | ✅ 가능 (Ingress Controller 내부에서 처리) |
| 대표적인 구현체 | Kubernetes 기본 리소스 | NGINX, Traefik, Istio Gateway 등 |
vagrant@master:~$ helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
"ingress-nginx" has been added to your repositories
vagrant@master:~$ helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "ingress-nginx" chart repository
...Successfully got an update from the "istio" chart repository
Update Complete. ⎈Happy Helming!⎈
vagrant@master:~$ helm install nginx-ingress ingress-nginx/ingress-nginx -n nginx-controller --create-namespace
NAME: nginx-ingress
LAST DEPLOYED: Sun Feb 16 12:12:00 2025
NAMESPACE: nginx-controller
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
The ingress-nginx controller has been installed.
It may take a few minutes for the load balancer IP to be available.
You can watch the status by running 'kubectl get service --namespace nginx-controller nginx-ingress-ingress-nginx-controller --output wide --watch'
An example Ingress that makes use of the controller:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example
namespace: foo
spec:
ingressClassName: nginx
rules:
- host: www.example.com
http:
paths:
- pathType: Prefix
backend:
service:
name: exampleService
port:
number: 80
path: /
# This section is only required if TLS is to be enabled for the Ingress
tls:
- hosts:
- www.example.com
secretName: example-tls
If TLS is enabled for the Ingress, a Secret containing the certificate and key must also be provided:
apiVersion: v1
kind: Secret
metadata:
name: example-tls
namespace: foo
data:
tls.crt: <base64 encoded cert>
tls.key: <base64 encoded key>
type: kubernetes.io/tls
vagrant@master:~$ k get po -n nginx-controller
NAME READY STATUS RESTARTS AGE
nginx-ingress-ingress-nginx-controller-6f6bf998dc-tn9bt 1/1 Running 0 89s
kubectl run nginx --image=nginx
vagrant@master:~$ kubectl run nginx --image=nginx
pod/nginx created
vagrant@master:~$ k get po
NAME READY STATUS RESTARTS AGE
nginx 2/2 Running 0 34s
k run mynginx --image nginx --expose --port 80
vagrant@master:~$ k run mynginx --image nginx --expose --port 80
service/mynginx created
pod/mynginx created
vagrant@master:~$ k get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 10h
mynginx ClusterIP 10.105.27.85 <none> 80/TCP 3s
//helm upgrade --install nginx-ingress ingress-nginx/ingress-nginx -n nginx
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: mynginx
spec:
ingressClassName: nginx
rules:
- host: localhost
http:
paths:
- pathType: Prefix
backend:
service:
name: mynginx
port:
number: 80
path: /
vagrant@master:~$ k apply -f ing-mynginx.yml
ingress.networking.k8s.io/mynginx created
vagrant@master:~$ k get ing
NAME CLASS HOSTS ADDRESS PORTS AGE
mynginx nginx localhost 80 4s
vagrant@master:~$ k get po
NAME READY STATUS RESTARTS AGE
mynginx 2/2 Running 0 8m38s
vagrant@master:~$ k exec -it mynginx -- curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>