인그레스(Ingress) 오브젝트의 기본 기능
외부 요청의 라우팅
: 특정 경로로 들어온 요청을 어떤 서비스로 전달할지 정의하는 라우팅 규칙 설정 가능
가상 호스트 기반의 요청 처리
: 같은 IP에 대해 다른 도메인 이름으로 요청이 오면, 어떻게 처리할 것인지 정의 가능
SSL/TLS 보안 연결 처리
: 여러 서비스로 요청을 라우팅할 때, 인증서를 쉽게 적용할 수 있음
사용하는 이유
- 3개의 디플로이먼트를 외부에 노출해야 한다면?
- NodePort, LoadBalancer 타입 서비스 3개를 생성하는 방법이 있다.
- 위 방식은 서비스마다 디플로이먼트에 일일이 설정을 해야만 한다.
- 이때 인그레스를 사용하면 URL 엔드포인트를 하나만 생성하면 된다.
- 따라서 클라이언트는 인그레스의 URL로만 접근하고, 그 요청은 적절히 처리되어 디플로이먼트의 파드로 전달된다.
- 외부 요청에 대한 처리 규칙을 쿠버네티스 기능으로 관리할 수 있는 것이 핵심이다.
예제
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-example
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: alicek106.example.com # [1]
http:
paths:
- path: /echo-hostname # [2]
pathType: Prefix
backend:
service:
name: hostname-service # [3]
port:
number: 80
$ kubectl apply -f ingress-example.yaml
$ kubectl get ing
- 인그레스를 정의하는 YAML 파일 중에서 annotation 항목을 통해 인그레스의 추가 기능을 사용할 수 있다.
- host: 해당 도메인 이름으로 접근하는 요청에 대해서 처리 규칙을 적용한다.
- path: 해당 경로에 들어온 요청을 어느 서비스로 전달할 것인지 정의한다.
- name, port: path로 들어온 요청이 전달될 서비스와 포트이다.
- 인그레스는 요청을 처리하는 규칙을 정의하는 오브젝트일 뿐이므로, 인그레스 컨트롤러라는 특수한 서버가 필요하다.
인그레스 컨트롤러 (ingress controller)
$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.2.0/deploy/static/provider/aws/deploy.yaml
# ingress-nginx 네임스페이스의 디플로이먼트와 파드를
# 확인해보면 nginx 웹서버가 생성됨
$ kubectl get pods,deployment -n ingress-nginx
# 외부에서 nginx 인그레스 컨트롤러에 접근하기 위한 서비스도 생성됨
$ kubectl get svc -n ingress-nginx
- ex. nginx 웹서버 인그레스 컨트롤러
- 쿠버네티스에서 공식 개발되고 있어서 컨트롤러와 관련된 모든 리소스를 한번에 설치할 수 있다.
- 설치하면 자동으로 생성되는 서비스는 LoadBalancer 타입이다.
- 실제 환경에서는 LoadBalancer 타입에 DNS 이름을 할당해 컨트롤러에 접근한다.
- 지금은 자동으로 부여된 DNS 이름(a20..2.elb.amazonaws.com)을 사용한다.
참고로 저자는 aws 인스턴스에서 해서 저렇게 부여된 것임
이런 식으로 외부에서 접근 가능한 공인 IP가 필요함
- 온프레미스 환경이면 NodePort 타입의 서비스를 생성해 사용한다.
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/component: controller
app.kubernetes.io/instance: ingress-nginx
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: ingress-nginx
name: ingress-nginx-controller-nodeport
namespace: ingress-nginx
spec:
ports:
- name: http
nodePort: 31000
port: 80
protocol: TCP
targetPort: http
- name: https
nodePort: 32000
port: 443
protocol: TCP
targetPort: https
selector:
app.kubernetes.io/component: controller
app.kubernetes.io/instance: ingress-nginx
app.kubernetes.io/name: ingress-nginx
type: NodePort
$ kubectl apply -f ingress-nginx-svc-nodeport.yaml
$ kubectl apply -f hostname-deployment.yaml
$ kubectl apply -f hostname-service.yaml
- 인그레스의 종착점이 될 테스트용 디플로이먼트와 서비스를 생성했다.
- 이제 nginx 인그레스 컨트롤러로 들어오는 요청은 이 디플로이먼트 파드들로 분산될 것이다.
인그레스 컨트롤러의 동작 원리
- 공식 깃허브에서 제공되는 YAML 파일로 nginx 인그레스 컨트롤러를 생성한다.
- nginx 인그레스 컨트롤러를 외부로 노출하기 위한 서비스를 생성한다.
- 요청 처리 규칙을 정의하는 인그레스 오브젝트를 생성한다.
- nginx 인그레스 컨트롤러로 들어온 요청은 인그레스 규칙에 따라 적절한 서비스로 전달된다.
# ENDPOINTS 항목에 출력된 지점으로 요청이 전달된다.
$ kubectl get endpoints
- nginx 인그레스 컨트롤러는 서비스에 의해 생성된 엔드포인트로 요청을 직접 전달한다.
- 즉, 서비스의 ClusterIP가 아닌 엔드포인트의 종착 지점들로 요청이 전달된다.
- 서비스를 거치지 않고 파드로 직접 요청이 전달되므로 바이패스(bypass)라고 부른다.
Azure
subscription
- 돈내는 계정
- 이 계정에 청구가 다 붙음
- 내 계정에 이 섭스크립션 계정이 여러 개 있을 수 있음
resource group
- 섭스크립션 계정 하나에 여러 리소스 그룹이 있을 수 있음
- 애져에서 실제로 서비스해주는 인스턴스들이 있음
- 애져 서비스들~
Azure kubernetes tutorial
# 그전에 띄워놨던 것들 yaml파일 기반으로 다 제거
$ kubectl delete -f ./
# brew 최신화
$ brew update
# kubectl처럼 애져명령어 치기 위한 의존 설치
$ brew install azure-cli
# 튜토리얼 진행
$ git clone https://github.com/Azure-Samples/azure-voting-app-redis.git
$ cd azure-voting-app-redis
$ docker-compose up -d
# `todorepo`는 애져에 만든 컨테이너 레지스트리명
$ docker tag mcr.microsoft.com/azuredocs/azure-vote-front:v1 todorepo.azurecr.io/azure-vote-front:v1
$ az acr login --name todoRepo
$ docker push todorepo.azurecr.io/azure-vote-front:v1
$ az acr repository list --name todoRepo --output table
# kubectl을 만든 쿠버네티스 클러스터를 바라보도록 연결
$ az aks get-credentials --resource-group study --name todoCluster
$ kubectl apply -f azure-vote-all-in-one-redis.yaml
$ kubectl get service azure-vote-front --watch
# 이후 EXTERNAL IP로 접근하면 튜토리얼 화면이 뜬다.
애져 리소스그룹 삭제한 후의 로컬에서 컨텍스트 스위칭하기
$ kubectl config get-contexts
$ kubectl config unset clusters.todoCluster
$ kubectl config unset contexts.todoCluster
$ kubectl config unset users.clusterUser_study_todoCluster
$ kubectl config use-context docker-desktop
$ kubectl config view
# 이제 config를 보면 현재 컨텍스트가 docker-desktop이다.
Unable to connect to the server: dial tcp i/o time out~
- 내가 연결해놨던 애져 클러스터가 없어져서 이런 에러가 발생했다.
- 이때는 컨텍스트를 바꿔주어야 한다.
- 내 로컬 도커 데스크탑 쿠버네티스 클러스터로 바꿔주었다.
쿠버네티스 yaml 파일들 있을 때 전체 삭제하기
~ kubectl delete -f ./
deployment.apps "hostname-deployment" deleted
service "hostname-service" deleted
ingress.networking.k8s.io "ingress-example" deleted
deployment.apps "todo-deployment" deleted
Error from server (NotFound): error when deleting "ingress-nginx-svc-nodeport.yaml": services "ingress-nginx-controller-nodeport" not found
Error from server (NotFound): error when deleting "todo-pod.yaml": pods "todo-pod" not found
Error from server (NotFound): error when deleting "todo-replicaset.yaml": replicasets.apps "todo-replicaset" not found
unable to decode ".angular-config.json": Object 'Kind' is missing in '{
"version": 1,
"cli": {
"analytics": false
}
}'
Cannot ls ~/.Trash in the Terminal~
- 위 메시지와 함께 권한 문제가 발생한다면?
- 이때는 터미널에
설정 > 개인 정보 보호 > 전체 디스크 접근 권한
을 주고 실행하자.