brunch

You can make anything
by writing

C.S.Lewis

by Master Seo Jul 11. 2021

쿠버1탄-20. 쿠버네티스 - 인그레스를 통한 내부접속


다음은 쿠버네티스 스터디 자료를 참고해  정리한 내용입니다.


쿠버네티스-도메인 없이 kops로 aws쿠버설치

https://brunch.co.kr/@topasvga/1681


<1>  인그레스를 통한 내부 접속 흐름

<2> 인그레스  상세 확인

<3> 삭제

<4> 다음 과정



<1>  인그레스를 통한 내부 접속 흐름


1

내부 접속을 테스트하는것이다.

외부가 아니다.

Master node에  Ingress Nginx pod생성 - 172.16.29.21

Ngix pod에 접속하기 위해 서비스를 NodePort로 만듬

외부에서 31660으로 들어오면, Dnat로 80매칭

목적지인 서비스 10.97.1.1로 가지 않고 , 바로 Pod로 감


2

Ingress Nginx가 어떻게 Pod정보를 알까요?

별도 컨트롤러 Pod가 관리한다.


3

웹브라우저에서  접속하기 ?


도메인 : 포트 로 접속

/   = nginx 홈페이지 , pod 1개

/guest  = Hello Kubernetes bootcamp! | deploy2-guestsrv-6f7f766988-kn4bt | v=1 ,   pod 2개

/admin  =  xforward-for 정보 , pod 3개




<2> 인그레스  상세 확인


alias k=kubectl

complete -F __start_kubectl k

k get ns,no,po,svc,deploy,rs,ing,ep


1

로그 모니터링 

kubectl ingress-nginx logs -n ingress-nginx -f 


watch로 모니터링

watch -d 'kubectl get pod -n ingress-nginx -o wide -l app.kubernetes.io/component=controller; echo; kubectl get svc -n ingress-nginx; echo; kubectl get ingress,ep;'





2

//  접속경로  확인


kubectl describe ingress ingress-1

Name:             ingress-1

Namespace:        default

Address:

Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)

Rules:

  Host        Path  Backends

  ----        ----  --------

  *

              /        svc1-web:80 ()

              /guest   svc2-guest:8080 ()

              /admin   svc3-admin:8080 ()

Annotations:  kubernetes.io/ingress.class: nginx

Events:       <none>



// 기 설치한 유틸로  접속경로  확인


kubectl ingress-nginx ingress  

INGRESS NAME   HOST+PATH   ADDRESSES        TLS   SERVICE      SERVICE PORT   ENDPOINTS

ingress-1      /           13.114.225.167   NO    svc1-web     80             1

ingress-1      /guest      13.114.225.167   NO    svc2-guest   8080           2

ingress-1      /admin      13.114.225.167   NO    svc3-admin   8080           3



3

Master node 에 있는 Pod로 접속 테스트


Master node에서 작업

svc1-web로  접속테스트 


k get nodes -o wide

NAME                                               STATUS   ROLES                  AGE   VERSION    INTERNAL-IP     EXTERNAL-IP      OS-IMAGE             KERNEL-VERSION    CONTAINER-RUNTIME

ip-172-20-43-172.ap-northeast-1.compute.internal   Ready    node                   4d    v1.20.11   172.20.43.172   18.183.238.226   Ubuntu 20.04.3 LTS   5.11.0-1017-aws   containerd://1.4.4

ip-172-20-46-12.ap-northeast-1.compute.internal    Ready    node                   4d    v1.20.11   172.20.46.12    13.112.170.254   Ubuntu 20.04.3 LTS   5.11.0-1017-aws   containerd://1.4.4

ip-172-20-60-173.ap-northeast-1.compute.internal   Ready    control-plane,master   4d    v1.20.11   172.20.60.173   18.183.248.245   Ubuntu 20.04.3 LTS   5.11.0-1017-aws   containerd://1.4.4



k get nodes

NAME                                               STATUS   ROLES                  AGE   VERSION

ip-172-20-56-239.ap-northeast-1.compute.internal   Ready    node                   95m   v1.20.8

ip-172-20-60-135.ap-northeast-1.compute.internal   Ready    control-plane,master   98m   v1.20

ip-172-20-62-175.ap-northeast-1.compute.internal   Ready    node                   95m   v1.20.8


dig +short ip-172-20-60-135.ap-northeast-1.compute.internal

 172.20.60.173


MASTER=172.20.60.173


curl <마스터노드 IP>:<Nginx Controller 의 Service 의 NodePort 중 HTTP 접속용>



echo $MASTER

echo $IngHttp


master node에서 테스트 ?

curl $MASTER:$IngHttp


curl $MASTER:32588

<!DOCTYPE html>

<html>

<head>

<title>Welcome to nginx!</title>

<style>

html { color-scheme: light dark; }

body { width: 35em; margin: 0 auto;



4

//  svvc2-guest 접속 테스트

curl -s $MASTER:$IngHttp/guest

for i in {1..100}; do curl -s $MASTER:$IngHttp/guest ; done | sort | uniq -c | sort -nr

     50 Hello Kubernetes bootcamp! | Running on: deploy2-guestsrv-6f7f766988-wnzbq | v=1

     50 Hello Kubernetes bootcamp! | Running on: deploy2-guestsrv-6f7f766988-p8xrc | v=1

50 vs 50으로 연결됨.


5

//  svc3-admin 접속   /admin 

> 기본적으로 Nginx 는 라운드로빈 부하분산 알고리즘을 사용


curl $MASTER:$IngHttp/admin 

curl -s $MASTER:$IngHttp/admin | grep Hostname

curl -s $MASTER:$IngHttp/admin | egrep '(client_address|x-forwarded-for)'


for i in {1..100}; do curl -s $MASTER:$IngHttp/admin | grep Hostname ; done | sort | uniq -c | sort -nr

     34 Hostname: deploy3-adminsrv-7bd5d589b9-9wb7s

     33 Hostname: deploy3-adminsrv-7bd5d589b9-gwhsc

     33 Hostname: deploy3-adminsrv-7bd5d589b9-2gkmt


while true; do curl -s --connect-timeout 1 $MASTER:$IngHttp/admin | grep Hostname ; echo "--------------" ; date "+%Y-%m-%d %H:%M:%S" ; sleep 1; done


6

// log로 확인

kubectl ingress-nginx logs -n ingress-nginx -f


kubectl logs -l 'app in (websrv,guestsrv,adminsrv)' -f --max-log-requests 8


7

Pod 1개일때


로그 확인

kubectl ingress-nginx logs -n ingress-nginx -f


3개 Pod를 1개로 변경하기

kubectl scale deployment deploy3-adminsrv --replicas=1


확인

watch -d kubectl get pods,ep

NAME                                    READY   STATUS    RESTARTS   AGE

pod/deploy1-websrv-574449b8df-wccbq     1/1     Running   0          19h

pod/deploy2-guestsrv-6f7f766988-gwg26   1/1     Running   0          19h

pod/deploy2-guestsrv-6f7f766988-z9mlw   1/1     Running   0          19h

pod/deploy3-adminsrv-7bd5d589b9-4zwnd   1/1     Running   0          2m58s


pod명으로 pod수 변경 가능

kubectl scale deployment deploy3-adminsrv --replicas=1

kubectl scale deployment deploy2-guestsrv --replicas=3

kubectl scale deployment deploy2-guestsrv --replicas=10

kubectl scale deployment deploy1-websrv --replicas=10


8

파드 3개로 변경

kubectl scale deployment deploy3-adminsrv --replicas=3


9

엔드 포인트로 3개로 바로 전달 된다.


kubectl get endpoints

NAME         ENDPOINTS                                                  AGE

kubernetes   172.20.60.135:443                                          113m

svc1-web     100.119.191.130:80                                         94m

svc2-guest   100.119.191.131:8080,100.99.251.3:8080                     94m

svc3-admin   100.119.191.132:8080,100.99.251.7:8080,100.99.251.8:8080   94m




10

인그레스는 L7이라 소스 IP를 확인 가능하도록 할수 있다.

X-Forwarded-for 로  Client IP를 알수 있도록  해더 내용을 추가해서 전달 할수도 있다.


Load Balancer 타입은 L4라 clinet-ip를 알기 힘들다.





<3> 삭제


1

kubectl delete deployments,svc,ingress --all


2

확인

k get ns,no,po,svc,deploy,rs,ing,ep


NAME                        STATUS   AGE

namespace/default           Active   115m

namespace/ingress-nginx     Active   94m

namespace/kube-node-lease   Active   115m

namespace/kube-public       Active   115m

namespace/kube-system       Active   115m


NAME                                                    STATUS   ROLES                  AGE    VERSION

node/ip-172-20-56-239.ap-northeast-1.compute.internal   Ready    node                   112m v1.20.8

node/ip-172-20-60-135.ap-northeast-1.compute.internal   Ready   control-plane,master  115mv1.20.8

node/ip-172-20-62-175.ap-northeast-1.compute.internal   Ready    node              112m   v1.20.8


NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE

service/kubernetes   ClusterIP   100.64.0.1   <none>        443/TCP   2s


NAME                   ENDPOINTS           AGE

endpoints/kubernetes   172.20.60.135:443   2s




<4> 다음 과정


https://brunch.co.kr/@topasvga/1709



https://brunch.co.kr/@topasvga/1656




감사합니다.

매거진의 이전글 쿠버1탄-19. 쿠버네티스 - 인그레스=ALB=L7
작품 선택
키워드 선택 0 / 3 0
댓글여부
afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari