使用 Telepresence 2 本地调试(开发)Kubernetes 应用

Telepresence 2 可以形象地比喻为: Kubernetes 项目开发的瑞士军刀!它能够将你的本地环境和远程的 Kubernetes 集群打通,让你的本地应用就像运行在集群内的 Pod 一样访问集群内的其他服务以及被集群内的其他服务访问
在这里插入图片描述
Telepresence 2 会在远端集群中运行一个 agent 程序,同时修改本地的网络设置,通过一个加密隧道转发你本地计算机和远端集群的流量,让你的本地计算机就像运行在集群内的 Pod 一样,能够访问集群内的其他服务,也能够让你的本地应用被集群内的其他应用访问。

使用方法

测试本地访问远端服务
# 1. 连接远端集群 (失败请重试)
~ % telepresence connect --kubeconfig=xxx.kubeconfig
Launching Telepresence Root Daemon
Launching Telepresence User Daemon
Connected to context kubernetes-admin@kubernetes (https://x.x.x.x:6443)
# 2. 启动本地程序(此时就可以直接使用集群内的地址连接其他服务,这里以连接集群内的 mysql 服务为例)
~ % mysql -h mysql.default.svc.cluster.local -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 189970
Server version: 5.7.33-36-log Percona Server (GPL), Release 36, Revision 7e403c5

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

# 3. 退出(清理环境)
~ % telepresence quit
Telepresence Root Daemon quitting... done
Telepresence User Daemon quitting... done
测试远端访问本地服务
# 1. 创建一个 deployment (已存在可跳过)
~ % kubectl create deployment example --image=datawire/hello-world
~ % kubectl expose deployment example --port=3000

# 2. swap deploymet 为本地服务,使用本地 3000 端口
~ % telepresence intercept example --port 3000
Using Deployment example
intercepted
    Intercept name    : example
    State             : ACTIVE
    Workload kind     : Deployment
    Destination       : 127.0.0.1:3000
    Volume Mount Error: sshfs is not installed on your local machine
    Intercepting      : all TCP connections

# 3. 启动本地 3000 服务(假设本地有 react 项目监听 3000 端口)
~ % yarn start

# 4. 通过 service 访问本地服务
~ % curl example.default.svc.cluster.local:3000
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="/logo192.png" />
    <!--
      manifest.json provides metadata used when your web app is installed on a
      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
    -->
    <link rel="manifest" href="/manifest.json" />
    <!--
      Notice the use of  in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <title>React App</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
  <script src="/static/js/bundle.js"></script><script src="/static/js/vendors~main.chunk.js"></script><script src="/static/js/main.chunk.js"></script></body>
</html>

注意:服务互访都需要通过 Service ,而不能直接访问 Endpoints 端点。

猜你喜欢

转载自blog.csdn.net/shida_csdn/article/details/120881554
今日推荐