k8s命令行web代理神器gotty

一、介绍

GoTTY是一个能将你的命令行搬到网页上来的工具。你可以代理shell、docker、ps各种你用黑窗口操作的命令。

话不多说,先上图,我们一般使用kubectl都是在命令行,下面展示一下网页版kubectl get pod

image

二、安装

官方文档:yudai/gotty

  • 方式一
go get github.com/yudai/gotty
  • 三、方式二
brew install yudai/gotty/gotty
  • 方式三

直接下载你系统的二进制文件:yudai/gotty/releases

我是源码自己编译的

go build *.go

安装完成运行gotty会出现以下界面
image

三、使用示例

  • 启动一个top终端
gotty top

默认是8080端口
image

访问如下
image

-p 指定端口

gotty -p 8089 top

-c 指定账号密码

gotty -c abc:123 top

image

-w 支持tty交互

gotty -w bash

image
image

--permit-arguments 支持get传参

参数通过arg传递,比如要传递多个参数ls -l

http://127.0.0.1:8080/?arg=ls&arg=-l

最后会变成空格分隔的形式传你启动的命令

比如,我们可以动态调用shell命令

gotty --permit-arguments bash -c

image

--random-url 生成随机地址

我们运行一下

 gotty --random-url -w bash

image

我们现在再访问得通过地址http://192.168.203.73:8080/fla589kv,而不再是一个简单的8080端口地址http://192.168.203.73:8080

--reconnet 支持重新链接

gotty -w --reconnect bash

简单说你操作一会儿后,刷新浏览器内容还保持着

可以配合--reconnect-time指定最大超时时间--reconnect-time

--title-format 指定终端名字

例如

gotty -w bash --title-format chenqionghe

image

访问如下,可以看到title变成了chenqionghe

image

四、K8S终端代理示例

启动kubectl终端

gotty --permit-arguments kubectl --kubeconfig ./test

这里我们试一下

kubectl get po

可以这样访问

http://127.0.0.1:8080/?arg=get&arg=po

image

查看pod日志

如果我们要想看pod nginx-demo-5b79dc546f-cs6f的日志,命令行是这样的

kubectl --kubeconfig ./test logs -f nginx-demo-5b79dc546f-cs6fz -n web

地址就可以这样访问

http://127.0.0.1:8080/?arg=logs&arg=-f&arg=nginx-demo-5b79dc546f-cs6fz&arg=-n&arg=web

image

进入pod执行命令

命令行命令如下

kubectl --kubeconfig ./test exec -it nginx-demo-5b79dc546f-cs6fz -n web bash

我们换成地址访问

http://127.0.0.1:8080/?arg=exec&arg=-it&arg=nginx-demo-5b79dc546f-cs6fz&arg=-n&arg=web&arg=bash

访问如下
image

可以这里有个问题,不能执行命令!
这是为啥呢?
因为我们启动kubectl的时候没有加入交互参数-w,我们停止之前的命令加入-w再重新执行一遍

gotty -w --permit-arguments kubectl --kubeconfig ./test

再访问可以正常执行shell了
image

以上内容由chenqionghe倾情提供,转载别忘了标明出处哦~

posted @ 2022-03-15 11:43  雪山飞猪  阅读(1467)  评论(1编辑  收藏  举报