admin

GoTTY – 终端工具变为 Web 应用

admin 运维技术 2022-11-12 393浏览 0

GoTTY 是一个简单的命令行工具,可将您的 CLI 工具变成 Web 应用。

GoTTY – 终端工具变为 Web 应用

快速入门

1、安装 GoTTY

#Mac版
brewinstallyudai/gotty/gotty

如果你有 GO 的环境,也可以通过如下方式安装:

gogetgithub.com/yudai/gotty

2、GoTTY 用法

Usage:gotty[options]<command>[<arguments...>]
  • options
--addressvalue,-avalueIPaddresstolisten(default:"0.0.0.0")[$GOTTY_ADDRESS]
--portvalue,-pvaluePortnumbertoliten(default:"8080")[$GOTTY_PORT]
--permit-write,-wPermitclientstowritetotheTTY(BECAREFUL)[$GOTTY_PERMIT_WRITE]
--credentialvalue,-cvalueCredentialforBasicAuthentication(ex:user:pass,defaultdisabled)[$GOTTY_CREDENTIAL]
--random-url,-rAddarandomstringtotheURL[$GOTTY_RANDOM_URL]
--random-url-lengthvalueRandomURLlength(default:8)[$GOTTY_RANDOM_URL_LENGTH]
--tls,-tEnableTLS/SSL[$GOTTY_TLS]
--tls-crtvalueTLS/SSLcertificatefilepath(default:"~/.gotty.crt")[$GOTTY_TLS_CRT]
--tls-keyvalueTLS/SSLkeyfilepath(default:"~/.gotty.key")[$GOTTY_TLS_KEY]
--tls-ca-crtvalueTLS/SSLCAcertificatefileforclientcertifications(default:"~/.gotty.ca.crt")[$GOTTY_TLS_CA_CRT]
--indexvalueCustomindex.htmlfile[$GOTTY_INDEX]
--title-formatvalueTitleformatofbrowserwindow(default:"{{.command}}@{{.hostname}}")[$GOTTY_TITLE_FORMAT]
--reconnectEnablereconnection[$GOTTY_RECONNECT]
--reconnect-timevalueTimetoreconnect(default:10)[$GOTTY_RECONNECT_TIME]
--max-connectionvalueMaximumconnectiontogotty(default:0)[$GOTTY_MAX_CONNECTION]
--onceAcceptonlyoneclientandexitondisconnection[$GOTTY_ONCE]
--timeoutvalueTimeoutsecondsforwaitingaclient(0todisable)(default:0)[$GOTTY_TIMEOUT]
--permit-argumentsPermitclientstosendcommandlineargumentsinURL(e.g.http://example.com:8080/?arg=AAA&arg=BBB)[$GOTTY_PERMIT_ARGUMENTS]
--widthvalueStaticwidthofthescreen,0(default)meansdynamicallyresize(default:0)[$GOTTY_WIDTH]
--heightvalueStaticheightofthescreen,0(default)meansdynamicallyresize(default:0)[$GOTTY_HEIGHT]
--ws-originvalueAregularexpressionthatmatchesoriginURLstobeacceptedbyWebSocket.Nocrossoriginrequestsareacceptablebydefault[$GOTTY_WS_ORIGIN]
--termvalueTerminalnametouseonthebrowser,oneofxtermorhterm.(default:"xterm")[$GOTTY_TERM]
--close-signalvalueSignalsenttothecommandprocesswhengottycloseit(default:SIGHUP)(default:1)[$GOTTY_CLOSE_SIGNAL]
--close-timeoutvalueTimeinsecondstoforcekillprocessafterclientisdisconnected(default:-1)(default:-1)[$GOTTY_CLOSE_TIMEOUT]
--configvalueConfigfilepath(default:"~/.gotty")[$GOTTY_CONFIG]
--version,-vprinttheversion

3、实践

#示例
gotty-wpython3

访问 http://127.0.0.1:8080 即可在线体验 Python3 环境。

进阶篇

容器化时代,遇到一些问题的时候,会进入容器内部排查问题,依靠命令行确实可以解决,但是效率较低,如果将容器作为 Web 可访问的应用,那么处理问题就会便捷很多。

下面将介绍如何使用 GoTTY 连接 k8s 集群中的任意容器。

1、构建 GoTTY Docker 镜像

已构建好的镜像:registry.cn-beijing.aliyuncs.com/tlab/k8s-gotty:latest

  • gotty:可运行的 gotty 程序,查看 [Releases]列表,选择合适的
  • kubernetes.repo:用于下载 kubectl
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpghttps://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
  • Dockerfile:用于构建镜像
FROMcentos:latest
RUNyuminstall-yepel-releasekde-l10n-Chineseglibc-commonwget
RUNlocaledef-c-fUTF-8-izh_CNzh_CN.utf8
ENVLC_ALLzh_CN.utf8
ADDgotty/root/
ADDkubernetes.repo/etc/yum.repos.d/
RUNyum-yinstallkubectl
WORKDIR/root
EXPOSE8080
CMD["./gotty","-w","--permit-arguments","kubectl","exec","-it","-n"]

2、在 k8s 集群中运行 GoTTY

kind:Deployment
apiVersion:apps/v1
metadata:
name:gotty
namespace:default
spec:
replicas:1
selector:
matchLabels:
k8s-app:gotty
template:
metadata:
labels:
k8s-app:gotty
spec:
serviceAccountName:<此处填具有合适权限的k8s用户名>
containers:
-name:gotty
image:registry.cn-beijing.aliyuncs.com/tlab/k8s-gotty
ports:
-containerPort:8080
protocol:TCP

3、暴露 GoTTY 服务

kind:Service
apiVersion:v1
metadata:
labels:
k8s-app:gotty
name:gotty-service
namespace:default
spec:
ports:
-port:80
targetPort:8080
nodePort:38080
selector:
k8s-app:gotty
type:NodePort

4、访问容器

在终端里,进入容器的命令是:

kubectlexec-it-n<Namespace><PodName>

那么,利用 GoTTY 访问则是 http://<ip>:38080/?arg=<Namespace>&arg=<PodName>

继续浏览有关 系统运维 的文章
发表评论