Ingress란?
쿠버네티스 Ingress란 HTTP(S) 기반의 L7 로드밸런싱 기능을 제공하는 컴포넌트이다. Ingress는 외부에서 쿠버네티스 내부로 들어오는 네트워크 요청을 어떻게 처리할지 결정하며, 쉽게 말해 외부에서 쿠버네티스에서 실행중인 Deployment와 Service에 접근하기 위한 관문과 같은 역할을 담당한다.
Ingress 는 LoadBalancer, SSL Terminated, Named 의 가상호스팅을 지원하기도 한다.
즉, Ingress 가 서비스 앞에서 L7 로드밸런서 역할을 하고, URL에 따라서 라우팅을 하게 된다.
Ingress-Nginx 환경세팅
https://kubernetes.github.io/ingress-nginx/deploy/
Installation Guide - Ingress-Nginx Controller
Installation Guide There are multiple ways to install the Ingress-Nginx Controller: with Helm, using the project repository chart; with kubectl apply, using YAML manifests; with specific addons (e.g. for minikube or MicroK8s). On most Kubernetes clusters,
kubernetes.github.io
위 사이트에서 Ingress-Nginx 설치 방법이 나와 있다.
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.8.2/deploy/static/provider/baremetal/deploy.yaml
설치를 하고 네임스페이스를 확인해보면
URL 테이블 구성
이제 위 의 구성도와 같이 /user , /proudct로 url로 라우팅할 수 있도록 yml 파일을 작성해보자 추가로 기본 / 인 메인 페이지 까지 작성하겠다.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- http:
paths:
- path: / #URL 주소
pathType: Prefix
backend:
service:
name: svc1 # serivce이름을 만들게 되면 일치시켜야함
port:
number: 80
- path: /user #URL 주소
pathType: Prefix
backend:
service:
name: svc2 # serivce이름을 만들게 되면 일치시켜야함
port:
number: 80
- path: /product #URL 주소
pathType: Prefix
backend:
service:
name: svc3 # serivce이름을 만들게 되면 일치시켜야함
port:
number: 80
그리고 주의할점은 이 서비스는 항상 ingress-nginx 라는 네임스페이스에서 실행해야한다.
여러가지 이유가 있겠지만 해당 네임스페이스에 ingress-nginx-contraller 가 있기 때문.
kubectl apply -f ingress_test.yml -n ingress-nginx
Deploy 생성 및 Expose
이제 서비스별 deploy을 생성하고 expose 해보자
# URL/ 메인 페이지 deploy
kubectl create deployment nginx1 --image nginx --replicas 1 --namespace ingress-nginx
kubectl expose deployment nginx1 --name svc1 --port 80 --namespace ingress-nginx
# URL/user user 페이지 depoly
kubectl create deployment nginx2 --image nginx --replicas 1 --namespace ingress-nginx
kubectl expose deployment nginx2 --name svc2 --port 80 --namespace ingress-nginx
# URL/product 페이지 deploy
kubectl create deployment nginx3 --image nginx --replicas 1 --namespace ingress-nginx
kubectl expose deployment nginx3 --name svc3 --port 80 --namespace ingress-nginx
ingress-nginx-contraller의 포트번호를 통해 접속이 가능해진다.
MAIN - PAGE
USER - PAGE
PRODUCT - PAGE
이렇게 k8s의 워커노드 ip로도 서비스가 잘 된다!
yml 파일 작성
위에 deployment하고 service을 yml 파일로 작성해보자
main - svc1
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: dep-nginx1
labels:
app: nginx
namespace: ingress-nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
name: temp
labels:
app: nginx
spec:
containers:
- name: nginx1
image: nginx
imagePullPolicy: Never
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: svc1
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
selector:
app: nginx
/user - svc2
piVersion: apps/v1
kind: Deployment
metadata:
name: dep-nginx2
labels:
app: nginx
namespace: ingress-nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx2
template:
metadata:
name: temp
labels:
app: nginx2
spec:
containers:
- name: nginx2
image: nginx
imagePullPolicy: Never
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: svc2
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
selector:
app: nginx2
/product svc3
apiVersion: apps/v1
kind: Deployment
metadata:
name: dep-nginx3
labels:
app: nginx3
namespace: ingress-nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx3
template:
metadata:
name: temp
labels:
app: nginx3
spec:
containers:
- name: nginx3
image: nginx
imagePullPolicy: Never
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: svc3
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
selector:
app: nginx3
'Docker, k8s' 카테고리의 다른 글
k8s_wordpress + mysql 연동 (0) | 2023.09.17 |
---|---|
k8s_Volume (1) EmptyDir , hostPath (0) | 2023.09.16 |
k8s_helm Prometheus Grafana 연동 (0) | 2023.09.15 |
k8s_ Replica, Depolyment (0) | 2023.09.13 |
k8s_yml 파일을 통한 Pod 실행 (0) | 2023.09.13 |