본문 바로가기
개발 이야기/Kubernetes

telepresence intercept 사용해서 로컬머신에서 디버깅

by 농개 2024. 11. 3.
반응형

쿠버네티스 클러스터를 개발환경으로 사용하고 있다면

telepresence intercept 기능을 한번쯤 들어봤을 것입니다.

이번 포스팅에서는 intercept 기능을 간단하게 사용해보고 정리합니다.

 

목차

    1. Intercept?

    telepresence intercept는 쿠버네티스 클러스터에서 실행 중인 서비스의 트래픽을

    로컬 개발 환경으로 리다이렉션하는 기능입니다.

    특정 서비스만 선택적으로 가로챌 수 있고, 로컬에서 디버깅 및 수정 테스트를 실시간에 가깝게 해볼수 있습니다.

    서비스 간 의존성을 감안해서 개발했다면, MSA 환경에서 이만한 개발도구가 없을 것으로 보입니다.

     

    2. CLI

    먼저 대상 서비스를 확인하고

    아래 명령어를 통해 트래픽을 로컬 환경으로 가로챌 수 있습니다.

    telepresence intercept <service-name> --port <local-port>[:<remote-port>] --env-file <path-to-env-file>

    예를 들면 아래와 같이 작성하면 됩니다.

     

    telepresence intercept example-service --port 8080:http --env-file ~/example-service-intercept.env

    여기서 --env-file은 별도 설정안해줘도 되더군요. 정확히 어떤 설정에 필요한지...모르겠네요.🥲

     

    3. 서비스 확인

     ~ kubectl get svc -n myproject
    NAME                         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
    myproject-api-gateway        ClusterIP   172.20.231.158   <none>        80/TCP,443/TCP   13d
    myproject-member-api         ClusterIP   172.20.35.27     <none>        80/TCP           11d
    ...

    연결할 서비스 대상을 확인 합니다.

    여기서는 myproject-member-api로 해보겠습니다.

     

    4. intercept 실행

     ~ telepresence intercept myproject-member-api --port="8080:80"
    Using Deployment myproject-member-api
       Intercept name         : myproject-member-api
       State                  : ACTIVE
       Workload kind          : Deployment
       Destination            : 127.0.0.1:8080
       Service Port Identifier: http
       Volume Mount Error     : sshfs is not installed on your local machine
       Intercepting           : all TCP connections

     

    이 후, 로컬 머신에 8080포트로 myproject-member-api 애플리케이션을 띄우고

    실제 kubernetes 클러스터에 구축된 서비스 환경에 접속해보면

    myproject-member-api로 전달되는 트래픽을 가로채 local 환경에서 캐치해볼수 있습니다.

     

    myproject-member-api 서비스에 intercept 동작 예시

     

    5. namespace 관련 이슈😯

     ~ telepresence intercept myproject-member-api --port="80:8080" -n myproject
    Flag --namespace has been deprecated, use telepresence connect to set the namespace
    telepresence intercept: error: connector.CreateIntercept: workload "myproject-member-api.default" not found

    namespace 설정 부분이 deprecated 되었다고 뜹니다.

    만약 telepresence connect 시 namespace를 설정하지 않는다면 아래와 같이 default 네임스페이스로 연결됩니다.

     ~ telepresence status
    OSS User Daemon: Running
      Version           : 2.19.0
      Executable        : /opt/homebrew/bin/telepresence
      Install ID        : 9b92aace-9a38-4847-bde3-83b2044b2990
      Status            : Connected
      Kubernetes server : https://***.***.com/k8s/clusters/local
      Kubernetes context: local
      Namespace         : default ⬅️⬅️⬅️⬅️
      ...(생략)

     

    이 경우, 아래와 같이 종료하고 다시 namespace를 설정하여 연결해줍니다.

     ~ telepresence quit
    Disconnected
    
     ~ telepresence connect -n myproject
    Connected to context local, namespace myproject
    
     ~ telepresence status
    ...(중략)
      Namespace         : myproject  ⬅️⬅️⬅️⬅️⬅️
    반응형