Kubernetes 환경 만들기

Minseok Jeon·2025년 4월 13일
0

k8s

목록 보기
1/1
post-thumbnail

목표

로컬 환경에서 테스트할 수 있는 kubernetes 환경을 만들어 봅니다

참고

kubectl command 설치
istioctl command 설치
nginx 설치

Docker Desktop Kubernetes 활성화

Docker Desktop 설치 후 우측 상단 설정 버튼을 클릭하고 Kubernetes 메뉴를 선택 -> Enable Kubernetes 토글 버튼을 클릭해 활성화

cluster info command를 통해 정상적으로 Kubernetes가 실행된 상태인지 확인

kubectl cluster-info

외부 호출 테스트를 위한 Application 셋팅

kubectl create ns test
kubectl apply -f hello.yaml

hello.yaml

apiVersion: v1
kind: Service
metadata:
  name: hello-service
  namespace: test
spec:
  type: ClusterIP
  selector:
    app: hello
  ports:
    - port: 80
      targetPort: 8080
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello
  namespace: test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello
  template:
    metadata:
      annotations:
        sidecar.istio.io/inject: "true"
      labels:
        app: hello
        version: v1
    spec:
      containers:
        - name: hello
          image: hashicorp/http-echo
          args:
            - "-text=Hello, minssogi!"
            - "-listen=:8080"
          ports:
            - containerPort: 8080

외부 호출을 Cluster 내부까지 전달하기 위한 셋팅

외부에서 호출을 받아주는 리소스로 Istio Ingress Gateways를 사용

Istio를 설치하려는 Cluster를 선택(kubectl config use-context )하고, istio 설치 command 실행

istioctl install --set profile=demo -y

istio-system namespace에 새로 생성된 resource 확인

Ingress Service 설정 확인

kubectl get svc istio-ingressgateway -n istio-system -o yaml

Gateway 리소스 생성
Istio Ingress Gateway가 외부로부터 어떤 host, port, protocol 로 요청을 전달 받을지 설정

apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
  name: istio-ingressgateway
  namespace: test
spec:
  selector:
    istio: ingressgateway
  servers:
    - port:
        number: 80
        name: http
        protocol: HTTP
      hosts:
        - minssogi.test

Virtual Service 리소스 생성
Istio Ingress Gateway가 전달 받은 요청을 어디로 전달할지 설정

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: hello-virtualservice
namespace: test
spec:
hosts:
  - minssogi.test
gateways:
  - test/istio-ingressgateway
http:
  - match:
      - uri:
          prefix: /
    route:
      - destination:
          host: hello-service
          port:
            number: 80

minssogi.test host 요청을 전달 받아 Application으로 요청을 전달하기 때문에 nginx를 설치하고 Reverse Proxy 설정

nginx.conf

   server {
       listen       80;
       server_name  minssogi.test;

       #charset koi8-r;

       #access_log  logs/host.access.log  main;

       location / {
           proxy_pass http://minssogi.test:31406;

	    proxy_http_version 1.1;
           proxy_set_header Host $host;
	    proxy_set_header Connection "";
	    proxy_set_header X-Real-IP $remote_addr;
       }
   }

minssogi.test host 사용을 위한 host 설정

sudo vi /etc/hosts
 
 127.0.0.1 localhost minssogi.test

Application 호출 테스트

profile
개발 천재 밍코천

0개의 댓글