본문 바로가기
카테고리 없음

istio 실습해보기-1

by BTC_XOXO 2024. 1. 5.

베하~안녕하세요 

3대 5000의 man, xoxo 입니다

이번 시간에는 저번 시간에 이어 istio 실습을 해보도록 하겠습니다. 

글이 길어질 것 같아 세션을 나눠서 진행하도록 하겠습니다. 오늘은 istio 테스트 환경을 구축을 해보도록 하겠습니다. 

 

1. 테스트용 이미지 생성 (클라우드 쉘에서 진행했음)

$ dotnet --version # 닷넷 기반 테스트 이미지 생성을 위한 설치 확인
$ dotnet new mvc -o HelloWorldAspNetCore # 닷넷 기반 웹 응용 프로그램 생성
$ vim Dockerfile # 도커 파일 생성
# Use Microsoft's official build .NET image.
# https://hub.docker.com/_/microsoft-dotnet-core-sdk/
FROM mcr.microsoft.com/dotnet/sdk:7.0-alpine AS build
WORKDIR /app

# Install production dependencies.
# Copy csproj and restore as distinct layers.
COPY *.csproj ./
RUN dotnet restore

# Copy local code to the container image.
COPY . ./
WORKDIR /app

# Build a release artifact.
RUN dotnet publish -c Release -o out

# Use Microsoft's official runtime .NET image.
# https://hub.docker.com/_/microsoft-dotnet-core-aspnet/
FROM mcr.microsoft.com/dotnet/aspnet:7.0-alpine AS runtime
WORKDIR /app
COPY --from=build /app/out ./

# Make sure the app binds to port 8080
ENV ASPNETCORE_URLS http://*:8080

# Run the web service on container startup.
ENTRYPOINT ["dotnet", "HelloWorldAspNetCore.dll"]

# 아래 코드 실행하기 전 Artifact Registry 저장소 생성 
$ docker build -t asia-northeast3-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/<생성한 저장소 이름>/hello-dotnet:v1 .
$ docker push asia-northeast3-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/<생성한 저장소 이름>/hello-dotnet:v1

 

2. GKE 환경 구축

- 테스트 GKE 생성 

# GKE API 설정
gcloud services enable container.googleapis.com

# 테스트 GKE 생성 
gcloud container clusters create istio-test \
  --cluster-version=latest \
  --machine-type=n1-standard-2 \
  --num-nodes=1 \
  --region asia-northeast3

 

- Istio 클라이언트 다운

$ curl -L https://istio.io/downloadIstio | sh -
# cli 창에서 뜬 istio 버전의 디렉토리가 생성됨 (테스트 현재 기준 istio-1.20.0)
$ cd istio-1.20.0
# istioctl을 사용하기 위해 환경 변수 지정 
$ export PATH="$PATH:/home/<YOURHOMEID>/istio-1.20.0/bin"

 

- Istio 클라이언트 정상 설치 확인

# 생성한 gke 연결 후 
$ gcloud container clusters get-credentials istio-test --region asia-northeast3 --project iistio
$ istioctl x precheck # 환경 확인 (정상이면 아래와 같이 나옴)
No issues found when checking the cluster. Istio is safe to install or upgrade!

$ istioctl install --set profile=demo # 테스트를 위한 demo 프로필 사용 후 설치
$ kubectl label namespace default istio-injection=enabled # 자동 사이드카 매칭
$ kubectl get namespace -L istio-injection # 사이드카 확인

# 정상 설치 확인 (아래 3개 존재 확인)
$ kubectl get svc -n istio-system
istio-egressgateway    ClusterIP      10.104.3.223    <none>         80/TCP,443/TCP                                                               102s
istio-ingressgateway   LoadBalancer   10.104.9.211    34.64.230.54   15021:32493/TCP,80:31798/TCP,443:32556/TCP,31400:30436/TCP,15443:30945/TCP   102s
istiod                 ClusterIP      10.104.14.127   <none>         15010/TCP,15012/TCP,443/TCP,15014/TCP                                        103s

$ kubectl get pods -n istio-system (아래 3개 존재 확인)
istio-egressgateway-56c5f9b7cc-s2xbk    1/1     Running   0          6m25s
istio-ingressgateway-678fd6f6b9-xv8js   1/1     Running   0          6m25s
istiod-664dc95b55-dfpv4                 1/1     Running   0          6m25s

 

3. 테스트 어플리케이션 배포

 

- 테스트용 서비스 배포

aspnetcore.yaml

apiVersion: v1
kind: Service
metadata:
  name: aspnetcore-service
  labels:
    app: aspnetcore
spec:
  ports:
  - port: 8080
    name: http
  selector:
    app: aspnetcore
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: aspnetcore-v1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: aspnetcore
      version: v1
  template:
    metadata:
      labels:
        app: aspnetcore
        version: v1
    spec:
      containers:
      - name: aspnetcore
        image: asia-northeast3-docker.pkg.dev/<YOUR-PROJECT-ID>/hello-dotnet/hello-dotnet:v2
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080

$ kubectl apply -f aspnetcore.yaml
$ kubectl get pods

 

- 테스트용 게이트웨이 및 가상 서비스 배포

 

aspnetcore-gateway.yaml

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: aspnetcore-gateway
spec:
  selector:
    istio: ingressgateway # use istio default controller
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "*"

 

aspnetcore-virtualservice.yaml

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: aspnetcore-virtualservice
spec:
  hosts:
  - "*"
  gateways:
  - aspnetcore-gateway
  http:
  - route:
    - destination:
        host: aspnetcore-service

 

4. 정상 동작 테스트

kubectl get svc istio-ingressgateway -n istio-system

export INGRESS_HOST=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].ip}')

export INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].port}')

export GATEWAY_URL=$INGRESS_HOST:$INGRESS_PORT

curl -o /dev/null -s -w "%{http_code}\n" http://${GATEWAY_URL}/

-> 200코드 확인

 

정상 동작 화면

 

여기까지 하시면 istio 테스트 환경 구축이 완료되었습니다~ 다음 시간에는 이를 활용하여 모니터링 및 배포 방법에 대해서 설명드리겠습니다~ 

 

댓글