文章首发于个人公众号:「阿拉平平」
Lens
是一个开源的管理 Kubernetes 集群的 IDE,支持 MacOS, Windows 和 Linux。通过 Lens,我们可以很方便地管理多个 Kubernetes 集群。
本文演示环境为 Windows X64,Lens 版本为 3.3.1,连接的 Kubernetes 集群托管在阿里云上。
下载安装
到 releases 下载对应的安装包。我用的是祖传 Windows 系统,所以这里下载 Lens-Setup-3.3.1.exe
:

安装后可以看到:

点击 +
并选择要连接的集群:

我本地配置过 kubeconfig
,所以添加集群的时候能够看到配置。如果之前没配过,可以选择 Custom
手动添加。选好后点击 Add Cluster
,就可以看到集群了:

安装 Metrics
可以看到,目前集群上没有 Metrics 数据。在集群图标上右键然后点击 Settings
:

点击 Install
安装:

之后在 Cluster 界面就可以看到 Metrics 数据了:

安装 Metrics 时会新建命名空间 lens-metrics
,并通过 Prometheus
采集数据。如果之后不需要的话,可以在设置界面点击 Uninstall
卸载。
使用指南
本章将演示一些 Lens 的基本操作,包括:
- 新建 namespace
- 添加 Deployment
- 调整 Deployment 的副本数
- 进入 Pod 内部
Namespace
平时用命令行新建命名空间 test
,需要执行:
kubectl create ns test
现在只需要点击 Namespace 界面右下方的 +
,写好名字就能创建了:

Deployment
如果要创建 Deployment,一般是写好 yaml 文件,然后执行:
kubectl create -f deployment.yaml
而在 Lens 里只需点击 Create resource
,然后将 yaml 贴到文本框就好了:

以下例子中,我新建了一个 nginx
的 Deployment,并放到了命名空间 test
下:

可以看到 Deployment 已经创建成功了:

Scale
这个 Deployment 目前只有 1 个副本,我想调整下副本数该怎么操作呢?
用命令行调整至 2 个副本,需要这样写:
kubectl scale --replicas=2 deploy nginx-deployment-basic -n test
现在选择 Deployment 并点击 Scale
就可以调整副本了:

Shell
有时我们需要进到容器里去查看状况。如果用命令行的话,需要先获取到 Pod 的名字,然后再连接:
# 获取 Pod 的名字
kubectl get pod -n test
NAME READY STATUS RESTARTS AGE
nginx-deployment-basic-54f57cf6bf-ktw4f 1/1 Running 0 52m
nginx-deployment-basic-54f57cf6bf-tfstr 1/1 Running 0 30m
# 连接
kubectl exec -it nginx-deployment-basic-54f57cf6bf-ktw4f bash -n test
现在只需在 Pods 界面选择 Pod,然后点击 Shell
就可以连接了:

写在最后
总的来说,Lens 是一款简单易用的 kubernetes IDE。当然,如果你更习惯命令行操作,那也无妨,只要本地配置好 kubectl
就可以打开 Lens 的 Terminal session
进行命令行操作了,还是挺方便的。
References
[1] GitHub:https://github.com/lensapp/lens
网友评论