tkctl 使用指南

    安装 tkctl 时,可以直接下载预编译的可执行文件,也可以自行从源码进行编译。

    下载解压后,将 tkctl 可执行文件加入到可执行文件路径 (PATH) 中即完成安装。

    源码编译

    要求:Go 版本 1.11 及以上

    你可以配置 tkctl 的自动补全以简化使用。

    为 BASH 配置自动补全(需要预先安装 )的方法如下。

    在当前 shell 中设置自动补全:

    1. source <(tkctl completion bash)

    永久设置自动补全:

    1. echo "if hash tkctl 2>/dev/null; then source <(tkctl completion bash); fi" >> ~/.bashrc

    为 ZSH 配置自动补全的方法如下。

    在当前 shell 中设置自动补全:

    1. source <(tkctl completion zsh)

    永久设置自动补全:

    1. echo "if hash tkctl 2>/dev/null; then source <(tkctl completion zsh); fi" >> ~/.zshrc

    tkctl 复用了 kubeconfig 文件(默认位置是 ~/.kube/config)来连接 Kubernetes 集群。你可以通过下面的命令来验证 kubeconfig 是否设置正确:

    1. tkctl version

    假如上面的命令正确输出服务端的 TiDB Operator 版本,则 kubeconfig 配置正确。

    tkctl version

    该命令用于展示本地 tkctl 和集群中 tidb-operator 的版本:

    1. tkctl version
    1. Client Version: v1.0.0-beta.1-p2-93-g6598b4d3e75705-dirty
    2. TiDB Controller Manager Version: pingcap/tidb-operator:latest
    3. TiDB Scheduler Version: pingcap/tidb-operator:latest

    该命令用于列出所有已安装的 TiDB 集群:

    示例如下:

    1. tkctl list -A
    1. NAMESPACE NAME PD TIKV TIDB AGE
    2. foo demo-cluster 3/3 3/3 2/2 11m
    3. bar demo-cluster 3/3 3/3 1/2 11m

    tkctl use

    该命令用于指定当前 tkctl 操作的 TiDB 集群,在使用该命令设置当前操作的 TiDB 集群后,所有针对集群的操作命令会自动选定该集群,从而可以略去 --tidbcluster 参数。

    示例如下:

      tkctl info

      该命令用于展示 TiDB 集群的信息。

      示例如下:

      1. tkctl info
      1. Name: demo-cluster
      2. Namespace: foo
      3. CreationTimestamp: 2019-04-17 17:33:41 +0800 CST
      4. Overview:
      5. Phase Ready Desired CPU Memory Storage Version
      6. ----- ----- ------- --- ------ ------- -------
      7. PD: Normal 3 3 200m 1Gi 1Gi pingcap/pd:v3.0.0-rc.1
      8. TiKV: Normal 3 3 1000m 2Gi 10Gi pingcap/tikv:v3.0.0-rc.1
      9. TiDB Upgrade 1 2 500m 1Gi pingcap/tidb:v3.0.0-rc.1
      10. Endpoints(NodePort):
      11. - 172.16.4.158:31441
      12. - 172.16.4.155:31441

      该命令用于获取 TiDB 集群中组件的详细信息。

      可选的组件 (component) 有: pd、、tidbvolumeall(用于同时查询所有组件)。

      示例如下:

      1. tkctl get tikv
      1. NAME READY STATUS MEMORY CPU RESTARTS AGE NODE
      2. demo-cluster-tikv-0 2/2 Running 2098Mi/4196Mi 2/2 0 3m19s 172.16.4.155
      3. demo-cluster-tikv-1 2/2 Running 2098Mi/4196Mi 2/2 0 4m8s 172.16.4.160
      4. demo-cluster-tikv-2 2/2 Running 2098Mi/4196Mi 2/2 0 4m45s 172.16.4.157
      1. tkctl get volume
      1. VOLUME CLAIM STATUS CAPACITY NODE LOCAL
      2. local-pv-d5dad2cf tikv-demo-cluster-tikv-0 Bound 1476Gi 172.16.4.155 /mnt/disks/local-pv56
      3. local-pv-5ade8580 tikv-demo-cluster-tikv-1 Bound 1476Gi 172.16.4.160 /mnt/disks/local-pv33
      4. local-pv-ed2ffe50 tikv-demo-cluster-tikv-2 Bound 1476Gi 172.16.4.157 /mnt/disks/local-pv13
      5. local-pv-74ee0364 pd-demo-cluster-pd-0 Bound 1476Gi 172.16.4.155 /mnt/disks/local-pv46
      6. local-pv-842034e6 pd-demo-cluster-pd-1 Bound 1476Gi 172.16.4.158 /mnt/disks/local-pv74
      7. local-pv-e54c122a pd-demo-cluster-pd-2 Bound 1476Gi 172.16.4.156 /mnt/disks/local-pv72

      tkctl debug [pod_name]

      该命令用于诊断 TiDB 集群中的 Pod。

      实际使用时,该命令会在目标 Pod 的宿主机上以指定镜像启动一个 debug 容器,该容器会与目标 Pod 中的容器共享 namespace,因此可以无缝使用 debug 容器中的各种工具对目标容器进行诊断。

      1. tkctl debug demo-cluster-tikv-0
      1. ps -ef

      由于 debug 容器和目标容器拥有不同的根文件系统,在 tidb-debug 容器中使用 GDB 和 perf 等工具时可能会碰到一些问题,下面将补充说明如何解决这些问题。

      GDB

      使用 GDB 调试目标容器中的进程时,需要将 program 参数设置为目标容器中的可执行文件。假如是在 tidb-debug 以外的其它 debug 容器中进行调试,或者调试的目标进行 pid 不为 1,则需要使用 set sysroot 命令调整动态链接库的加载位置。操作如下:

      1. tkctl debug demo-cluster-tikv-0
      1. gdb /proc/${pid:-1}/root/tikv-server 1

      tidb-debug 中预配置的 .gdbinit 会将 sysroot 设置为 /proc/1/root/,因此在 tidb-debug 中,假如目标容器的 pid 为 1,则下面的命令可以省略。

      开始调试:

      1. (gdb) thread apply all bt
      1. (gdb) info threads

      Perf 以及火焰图

      使用 命令和 run-flamegraph.sh 脚本时,需要将目标容器的可执行文件拷贝到 Debug 容器中:

      1. tkctl debug demo-cluster-tikv-0
        1. ./run_flamegraph.sh 1

        该脚本会自动将生成的火焰图(SVG 格式)上传至 transfer.sh,通过访问脚本输出的链接即可下载火焰图。

        tkctl ctop

        命令的完整形式:tkctl ctop [pod_name | node/node_name ]

        该命令用于查看集群中 Pod 或 Node 的实时监控信息,和 kubectl top 相比,tkctl ctop 还会展示网络和磁盘的使用信息。

        示例如下:

        1. tkctl ctop demo-cluster-tikv-0
        1. tkctl ctop node/172.16.4.155

        该命令用于展示各个子命令的帮助信息。

        示例如下:

        1. tkctl help debug

        tkctl options

        该命令用于展示 tkctl 的所有的全局参数。

        示例如下:

        1. tkctl options
        1. The following options can be passed to any command:
        2. --alsologtostderr=false: log to standard error as well as files
        3. --as='': Username to impersonate for the operation
        4. --as-group=[]: Group to impersonate for the operation, this flag can be repeated to specify multiple groups.
        5. --cache-dir='/Users/alei/.kube/http-cache': Default HTTP cache directory
        6. --certificate-authority='': Path to a cert file for the certificate authority
        7. --client-certificate='': Path to a client certificate file for TLS
        8. --client-key='': Path to a client key file for TLS
        9. --cluster='': The name of the kubeconfig cluster to use
        10. --context='': The name of the kubeconfig context to use
        11. --insecure-skip-tls-verify=false: If true, the server's certificate will not be checked for validity. This will
        12. make your HTTPS connections insecure
        13. --kubeconfig='': Path to the kubeconfig file to use for CLI requests.
        14. --log_backtrace_at=:0: when logging hits line file:N, emit a stack trace
        15. --log_dir='': If non-empty, write log files in this directory
        16. --logtostderr=true: log to standard error instead of files
        17. -n, --namespace='': If present, the namespace scope for this CLI request
        18. --request-timeout='0': The length of time to wait before giving up on a single server request. Non-zero values
        19. should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests.
        20. -s, --server='': The address and port of the Kubernetes API server
        21. --stderrthreshold=2: logs at or above this threshold go to stderr
        22. -t, --tidbcluster='': Tidb cluster name
        23. --token='': Bearer token for authentication to the API server
        24. --user='': The name of the kubeconfig user to use
        25. -v, --v=0: log level for V logs
        26. --vmodule=: comma-separated list of pattern=N settings for file-filtered logging
        • :指定目标 Kubernetes 集群