Telepresence安装调试K8s微服务

前言

  • 项目开发使用的是微服务,部署到k8s上了,出问题了只能看日志,看到k8s官方推荐的telepresence,可以将部分或者全部流量转发到本地,很是好奇准备尝试一番。
  • 我是本地创建一个拦截,然后测试服的流量被导到本地,本地可以进行调试,实时改代码等操作,再将流程转发到测试服(都是telepresence实现的)

使用环境

  • 服务器:ubuntu搭建的k8s版本1.27
  • 本地系统: windows 11
  • 开发软件: goland
  • 调试服务:微服务服务端

安装

本地连接集群

下载kubectl

获取集群配置文件

  • 要是不知道集群配置文件,可以先导出集群配置文件
bash 复制代码
(Get-Content $HOME\.kube\config)|Foreach-Object {$_ -replace ("apiserver.cluster.local","172.16.xx.xx")}|Out-File $HOME\.kube\config
  • 将k8s集群的配置文件拷贝到本地
bash 复制代码
scp root@172.16.xxx.xx:/root/.kube/config $HOME\.kube

telepresence配置

安装telepresence

  • 可以参考官网的教程进行安装telepresence官网安装教程
  • 可以下载msi安装包,也可以使用powershell 安装,我用的是power安装
bash 复制代码
# To install Telepresence, run the following commands
# from PowerShell as Administrator.

# 1. Download the latest windows zip containing telepresence.exe and its dependencies (~50 MB):
Invoke-WebRequest https://app.getambassador.io/download/tel2/windows/amd64/latest/telepresence.zip -OutFile telepresence.zip

# 2. Unzip the telepresence.zip file to the desired directory, then remove the zip file:
Expand-Archive -Path telepresence.zip -DestinationPath telepresenceInstaller/telepresence
Remove-Item 'telepresence.zip'
cd telepresenceInstaller/telepresence

# 3. Run the install-telepresence.ps1 to install telepresence's dependencies. It will install telepresence to
# C:\telepresence by default, but you can specify a custom path by passing in -Path C:\my\custom\path
powershell.exe -ExecutionPolicy bypass -c " . '.\install-telepresence.ps1';"

# 4. Remove the unzipped directory:
cd ../..
Remove-Item telepresenceInstaller -Recurse -Confirm:$false -Force

# 5. Telepresence is now installed and you can use telepresence commands in PowerShell.

拷贝kubectl

  • 将kubectl 拷贝到telepresence的程序目录也就是C:\telepresence和telepresence.exe放一起

连接

  • 连接可以直接连接,也可以指定集群的配置文件路径
bash 复制代码
telepresence connect --kubeconfig=C:\Users\user053\.kube\config
  • 这个连接会跳转telepresence的官网ambassador,需要注册登录,我第一次注册的时候不管点击哪个github 还是docker等授权没有反应,过几天就可以了,不知道是不是官网的问题,这个必须进行的
  • 登录之后回到命令行,使用 telepresence status 看看状态,文章最下面的附录有常用的telepresence命令

创建拦截

  • 新版的telepresence链接和创建拦截的时候不支持指定命名空间,你当前是哪个命名空间就连接到哪个
  • 需要连接其他命名空间,要在本地使用kubectl 切换命名空间,再连接
bash 复制代码
telepresence intercept server-name --port 20006:20006
  • 创建拦截之后可以使用telepresence list查看连接状态,也可以去官网查看
  • k8s对应的容器也会自动创建监听容器

goland添加拦截

  • 在goland中添加拦截,然后debug启动,k8s的流量就会转发到本地,再发回测试服
  • 有关goland的其他说明见附录

附录

telepresence常用命令

  • 查看状态
bash 复制代码
telepresence status
  • 查看服务列表 -n可以指定命名空间,不加就是默认当前的命名空间
bash 复制代码
telepresence list -n 
  • 创建拦截,拦截的服务名称是telepresence list查看到的自己服务的名称,端口是20005是本地监听的端口,20006是服务的端口
bash 复制代码
telepresence intercept server-name --port 20005:20006
  • 断开拦截
bash 复制代码
telepresence leave server-name
  • 断开telepresence连接
bash 复制代码
telepresence quit

goland插件telepresence

  • goland是有一个telepresence插件的可以查看集群服务情况和创建拦截,但是它默认会创建header头,如果header头不会不会转发(这个是个人指定转发部分流量功能),我把header头去掉,就无法创建拦截,所以我目前都是命令行创建的拦截
  • 先去plugin安装telepresence,然后在view->tools->telepresence
  • 配置telepresence路径
  • 创建拦截
  • 个人指定拦截这个收费的,初创可以试用,我用的是global全局拦截,全局拦截我不添加header创建按钮是不可点击的,我不想添加header 所以我用命令创建的,

一些异常情况

  • 如果遇到了拦截创建成功了,本地服务也启动了,查看状态也正常,但是就是不转发,这时候可以重启容器或者删除ambassador创建的容器tel-agent-init和traffic-agent再重新创建拦截
  • 创建拦截容器可能探针报错# : can't stat '/tmp/agent/ready',这个可以不用管
相关推荐
wn53133 分钟前
【Go - 类型断言】
服务器·开发语言·后端·golang
希冀1231 小时前
【操作系统】1.2操作系统的发展与分类
后端
GoppViper1 小时前
golang学习笔记29——golang 中如何将 GitHub 最新提交的版本设置为 v1.0.0
笔记·git·后端·学习·golang·github·源代码管理
爱上语文2 小时前
Springboot的三层架构
java·开发语言·spring boot·后端·spring
serve the people3 小时前
springboot 单独新建一个文件实时写数据,当文件大于100M时按照日期时间做文件名进行归档
java·spring boot·后端
罗政8 小时前
[附源码]超简洁个人博客网站搭建+SpringBoot+Vue前后端分离
vue.js·spring boot·后端
拾光师9 小时前
spring获取当前request
java·后端·spring
Java小白笔记11 小时前
关于使用Mybatis-Plus 自动填充功能失效问题
spring boot·后端·mybatis
JOJO___13 小时前
Spring IoC 配置类 总结
java·后端·spring·java-ee
白总Server14 小时前
MySQL在大数据场景应用
大数据·开发语言·数据库·后端·mysql·golang·php