Day 16 - Rancher 指令工具的操作

本文将於赛後同步刊登於笔者部落格

有兴趣学习更多 Kubernetes/DevOps/Linux 相关的资源的读者,欢迎前往阅读

更多相关科技的技术分享,欢迎追踪 矽谷牛的耕田笔记

对於 Kubernetes 与 Linux Network 有兴趣的可以参阅笔者的线上课程

前言

之前曾经探讨过如何透过 Terraform 来管理 Rancher,为了完成这个步骤必须要於 Rancher UI 中去取得相关的 Access Token/Secret Key。实际上该 Access Token 除了给 Terraform 去使用外,也可以让 Rancher 自行开发维护的 CLI 工具来使用。
本篇文章就来介绍一下 Rancher CLI 可以怎麽用,里面有什麽好用值得注意的功能

CLI

基本上 Rancher CLI 的功用跟网页没差多少,最主要的目的是让使用者可以透过指令列的方式去操作 Rancher 而非透过网页操作,这个概念跟 Terraform 是完全一致的。
因此实务上我会推荐都使用 Terraform 工具来管理 Rancher 而非使用 CLI 这个工具,那这样还有必要学习 CLI 的用法吗?

答案是肯定的,因为学得愈广,当问题出现时脑中就会有更多的候选工具供你选择去思考该如何解决面前的问题。
Rancher CLI 有一个好用的功能我认为是 Terraform 比不上的,这点稍後会来探讨。

首先如同先前操作一样,到 Rancher UI 去取得相关的 Access Token/Secret Key,不过这一次因爲 CLI 会透过 HTTP 进行授权存取,所以会用到的是下方的 Bearer Token,其实就是把 Access Key 跟 Secret Key 给合并而已。


取得这些资讯之後就来去 Rancher CLI 官网下载相对应的 CLI 版本,这边要注意的是 Rancher CLI 的版本不会完全跟 Rancher 对齐。
Rancher 本身的 Release Note 都会描述当前版本对应的 CLI 与 RKE 的版本。
对应到 Rancher v2.5.9 的 CLI 版本是 v.2.4.11

安装完毕後可以执行看看确认版本是否符合

╰─$ rancher --version
rancher version v2.4.11

╰─$ rancher --help
Rancher CLI, managing containers one UTF-8 character at a time

Usage: rancher [OPTIONS] COMMAND [arg...]

Version: v2.4.11

Options:
  --debug                   Debug logging
  --config value, -c value  Path to rancher config (default: "/Users/hwchiu/.rancher") [$RANCHER_CONFIG_DIR]
  --help, -h                show help
  --version, -v             print the version

Commands:
  apps, [app]                                       Operations with apps. Uses helm. Flags prepended with "helm" can also be accurately described by helm documentation.
  catalog                                           Operations with catalogs
  clusters, [cluster]                               Operations on clusters
  context                                           Operations for the context
  globaldns                                         Operations on global DNS providers and entries
  inspect                                           View details of resources
  kubectl                                           Run kubectl commands
  login, [l]                                        Login to a Rancher server
  multiclusterapps, [multiclusterapp mcapps mcapp]  Operations with multi-cluster apps
  namespaces, [namespace]                           Operations on namespaces
  nodes, [node]                                     Operations on nodes
  projects, [project]                               Operations on projects
  ps                                                Show workloads in a project
  server                                            Operations for the server
  settings, [setting]                               Show settings for the current server
  ssh                                               SSH into a node
  up                                                apply compose config
  wait                                              Wait for resources cluster, app, project, multiClusterApp
  token                                             Authenticate and generate new kubeconfig token
  help, [h]                                         Shows a list of commands or help for one command

Run 'rancher COMMAND --help' for more information on a command.

从上述的 Help 可以看到该 CLI 有满多子指令可以使用的,包含了 clusters, context, nodes, projects, ssh 等各种功能。

为了使用这些功能,必须要使用 login 来获得与目标 Rancher 沟通的能力,这时候前述获得的 Bearer Token 就派上用场了

╰─$ rancher login --name test -t token-8s72l:b425shbg49l7rs9mwlqzk89z6tr472qj94wx6vrm9pwh5r6mklsxf6 https://rancher.hwchiu.com/v3                                                                                                130 ↵
NUMBER    CLUSTER NAME   PROJECT ID        PROJECT NAME    PROJECT DESCRIPTION
1         rke-it         c-9z2kx:p-5gdg9   System          System project created for the cluster
2         rke-it         c-9z2kx:p-lxsz6   Default         Default project created for the cluster
3         rke-qa         c-p4fmz:p-fccdb   System          System project created for the cluster
4         rke-qa         c-p4fmz:p-r8wvz   Default         Default project created for the cluster
5         ithome-dev     c-z8j6q:p-p6xrd   myApplication
6         ithome-dev     c-z8j6q:p-q46q5   System          System project created for the cluster
7         ithome-dev     c-z8j6q:p-vblmb   Default         Default project created for the cluster
8         local          local:p-6knqb     System          System project created for the cluster
9         local          local:p-hgjqp     Default         Default project created for the cluster
Select a Project:5
INFO[0121] Saving config to /Users/hwchiu/.rancher/cli2.json

登入完毕後,系统会要求你选择一个 Project 做为预设操作的 Project,选择完毕後就可以透过 CLI 进行操作了。

CLI 基本上可以完成 UI 所能达到的功能,譬如可以使用 cluster 子指令来观察 Cluster 的状态,知道目前有哪些 Cluster,上面的名称与资源又分别有多少。

╰─$ rancher clusters
CURRENT   ID        STATE     NAME         PROVIDER                    NODES     CPU         RAM             PODS
          c-9z2kx   active    rke-it       Azure Container Service     3         1.25/5.70   1.61/13.38 GB   18/330
          c-p4fmz   active    rke-qa       Rancher Kubernetes Engine   2         0.42/4      0.24/7.49 GB    14/220
*         c-z8j6q   active    ithome-dev   Rancher Kubernetes Engine   5         5.97/10     3.37/38.39 GB   110/550
          local     active    local        Imported                    3         0.53/6      0.31/11.24 GB   22/330

如果采用的是旧版本的 catalog 的安装方式的话,也可以透过 apps 子指令观察安装的所有资源,当然也可以透过 Rancher CLI 来安装 application, 所以也会有团队尝试使用 Rancher CLI 搭配 CI/CD 流程来安装 Rancher 服务,不过实务上会推荐使用 Terraform, 因为更有结构同时使用更为容易。

╰─$ rancher apps
ID                            NAME                  STATE     CATALOG               TEMPLATE               VERSION
p-p6xrd:dashboard-terraform   dashboard-terraform   active    dashboard-terraform   kubernetes-dashboard   4.5.0

那到底有什麽功能是值得使用 Rancher CLI 的? 我认为有两个,分别是

  1. Node SSH
  2. Kubernetes KUBECONFIG

前述安装的 Kubernetes 丛集有一个是采用动态节点的方式,Rancher 透过 Azure 创造这些节点的时候都会准备一把连接用的 SSH Key,这把 Key 是可以透过 UI 的方式下载,不过使用上我认为不太方便。而 Rancher CLI 就有实作这功能,可以让使用者很方便的透过 CLI 进入到节点中。

指令的使用非常简单,透过 rancher ssh 搭配节点名称即可使用。
如果节点本身有两个 IP 时,透过 -e 可以选择使用 external 的 IP 地址来使用,否则预设会使用 internal 的 IP 地址。

╰─$ rancher ssh -e node1
The authenticity of host '40.112.223.2 (40.112.223.2)' can't be established.
ECDSA key fingerprint is SHA256:dqMCUUC4iZk/gZealQ+Ck3VhG/KaLaCVdkuLYwZfgsE.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '40.112.223.2' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 18.04.5 LTS (GNU/Linux 5.4.0-1055-azure x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Wed Aug 25 22:00:22 UTC 2021

  System load:  0.39               Users logged in:        0
  Usage of /:   33.9% of 28.90GB   IP address for eth0:    192.168.0.5
  Memory usage: 44%                IP address for docker0: 172.17.0.1
  Swap usage:   0%                 IP address for cni0:    10.42.2.1
  Processes:    244

 * Super-optimized for small spaces - read how we shrank the memory
   footprint of MicroK8s to make it the smallest full K8s around.

   https://ubuntu.com/blog/microk8s-memory-optimisation

13 updates can be applied immediately.
To see these additional updates run: apt list --upgradable


*** System restart required ***
Last login: Wed Aug 11 07:28:47 2021 from 52.250.127.84
docker-user@node1:~$

因此如果今天有需求想要进入到这些节点进行除错时,透过 CLI 可以大大的简化整个过程。

另外一个好用的功能就是 kubeconfig 的存取,试想今天一个系统管理员想要透过指令的方式去管理数十个由 Rancher 维护的 Kubernetes 丛集,最简单的做法透过网页的方式将每个丛集的 Kubeconfig 一个又一个的抓下来并且自行处理 kubeconfig 的格式。

透过 CLI 的方式可以让上述的行为更加简单甚至自动化。

╰─$ rancher cluster kf
Return the kube config used to access the cluster

Usage:
  rancher clusters kubeconfig [CLUSTERID CLUSTERNAME]

透过 rancher clusters kf 的指令加上 cluster 名称就可以取得该丛集的 KUBECONFIG 内容,譬如

╰─$ rancher cluster kf rke-it
apiVersion: v1
kind: Config
clusters:
- name: "rke-it"
  cluster:
    server: "https://rancher.hwchiu.com/k8s/clusters/c-9z2kx"

users:
- name: "rke-it"
  user:
    token: "kubeconfig-user-qr5lq:v7htf5kcz2s5nv7b5fzjz68ntlxf2978d5rrgxbrjhz2zv7vjhq9h7"


contexts:
- name: "rke-it"
  context:
    user: "rke-it"
    cluster: "rke-it"

current-context: "rke-it"

同时搭配 rancher cluster ls 的指令,我们就可以撰写一个 for 回圈来依序取得这些内容,并且将这些内容抓下来处理,譬如

╰─$
for c in $(rancher clusters ls --format  '{{.Cluster.Name}}');
do
        rancher cluster kf $c    ;
done

上述功能如果与 kubectl 的 plugin, kconfig 整合就可以更顺利的将多个 KUBECONFIG 整合成一个档案,并且将此功能撰写成一个 shell function, 这样就可以随时随地的去更新当前环境的 Kubeconfig.
譬如

function update_k8s_config {
    mv ~/.kube/configs ~/.kube/configs-`date +%Y-%m-%d-%H%M%S`
    mkdir ~/.kube/configs


    for c in $(rancher clusters ls --format  '{{.Cluster.Name}}'); do
        rancher cluster kf $c > ~/.kube/configs/$c
    done

    kubectl konfig merge ~/.kube/configs/* > ~/.kube/config
}

剩下 CLI 的功能就留给大家自己去尝试看看罗。


<<:  Day03 - Gem-strip_attributes 介绍与应用

>>:  DAY04 - 建置程序开发环境

系统分析师养成之路-当责Accountability

因私人因素欧吉桑有一段时间没发文了,不知道有没有人期待我的新文章呢? 今天,我想跟大家分享的主题是【...

资料库连接练习

我们继续熟悉资料库连接的操作吧! 这次是沿用漫画爬虫的程序码,但我目前只要两笔资料:漫画编号、漫画名...

【Day 24】 实作 - 创建 AWS Kinesis Data Firehose for WAF

今天我们要来实作 - 『如何启用 WAF 日志以及汇入 BI 进行分析』, Data Analyti...

失败的升级维护...

以Android的环境特性来看,这种事情迟早要碰到... 「核心功能的元件要整个换掉。」 这种事情理...

用 Python 畅玩 Line bot - 23:Flask(一)

如果想要将从 line 上蒐集到的资料或数据呈现在自己的网页上,我们可以使用 flask 建立好网页...