undefined

RUM_APP 集群
地址:https://console.cloud.tencent.com/tke2/cluster/sub/list/basic/info?rid=1&clusterId=cls-ed09d1ks

开启内网访问,获取到访问地址: https://cls-ed09d1ks.ccs.tencent-cloud.com
根据说明配置 kubeconfig 权限

使用 kubectl 连接 k8s 集群

  1. 下载最新的 kubectl 客户端。

  2. 配置 Kubeconfig:
    若当前访问客户端尚未配置任何集群的访问凭证,即 ~/.kube/config 内容为空,可直接复制上方 kubeconfig 访问凭证内容并粘贴入 ~/.kube/config 中。
    若当前访问客户端已配置了其他集群的访问凭证,你可下载上方 kubeconfig 至指定位置,并执行以下指令以追加本集群的 kubeconfig 至环境变量。
    export KUBECONFIG=$KUBECONFIG:$HOME/Downloads/cls-ed09d1ks-config
    其中,$HOME/Downloads/cls-ed09d1ks-config 为本集群的 kubeconfig 的文件路径,请替换为下载至本地后的实际路径。有关多集群 Kubeconfig 配置及管理请参考:配置对多集群的访问

  3. 访问 Kubernetes 集群:
    完成 kubeconfig 配置后,执行以下指令查看并切换 context 以访问本集群:
    kubectl config --kubeconfig=$HOME/Downloads/cls-ed09d1ks-config get-contexts
    kubectl config --kubeconfig=$HOME/Downloads/cls-ed09d1ks-config use-context cls-ed09d1ks-100015397980-context-default
    而后可执行 kubectl get node 测试是否可正常访问集群。如果无法连接请查看是否已经开启公网访问或内网访问入口,并确保访问客户端在指定的网络环境内。

命令补全

使用 oh-my-zsh ,增加对 kubectl 命令自动补全
修改 ~/.zshrc 文件,增加如下两行

1
2
plugins=(kubectl)
source <(lubectl completion zsh)

设置终端启动后切换

1
2
3
if [ -t 1 ]; then
exec zsh
fi

操作 k8s 命令

一、kubectl 上下文和配置

1
kubectl config view # 显示合并后的 kubeconfig 配置

二、显示和查找资源

  1. get 相关的基础命令

    1
    2
    3
    4
    5
    6
    kubectl get services # 列出所有 namespace 中所有的 service
    kubectl get pods --all-namespaces # 列出所有 namespace 中的所有 pod
    kubectl get pods -n umbrella # 列出 namespace=umbrella 中的 pod
    kubectl get pods -o wide # 列出所有 pod 并显示详细信息
    kubectl get deployment web -n umbrella # 列出指定 deployment
    kubectl get pods --include-uninitialized # 列出该 namespace 中所有 pod 包括未初始化的
  2. 使用详细输出来描述命令

    1
    2
    3
    4
    5
    6
    7
    8
    kubectl describe nodes vm-rum1-centos  # 查看某个 node 的详细信息
    kubectl describe pods web-78866d68c6-b74fs -n umbrella # 查看某个 pod 的详细信息

    # 根据重启次数排序列出 pod
    kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'

    # 获取所有节点的 ExternalIP (有问题)
    kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'
  3. 更新资源

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    $ kubectl rolling-update frontend-v1 -f frontend-v2.json           # 滚动更新 pod frontend-v1
    $ kubectl rolling-update frontend-v1 frontend-v2 --image=image:v2 # 更新资源名称并更新镜像
    $ kubectl rolling-update frontend --image=image:v2 # 更新 frontend pod 中的镜像
    $ kubectl rolling-update frontend-v1 frontend-v2 --rollback # 退出已存在的进行中的滚动更新
    $ cat pod.json | kubectl replace -f - # 基于 stdin 输入的 JSON 替换 pod

    # 强制替换,删除后重新创建资源。会导致服务中断。
    $ kubectl replace --force -f ./pod.json

    # 为 nginx RC 创建服务,启用本地 80 端口连接到容器上的 8000 端口
    $ kubectl expose rc nginx --port=80 --target-port=8000

    # 更新单容器 pod 的镜像版本(tag)到 v4
    $ kubectl get pod mypod -o yaml | sed 's/\(image: myimage\):.*$/\1:v4/' | kubectl replace -f -

    $ kubectl label pods my-pod new-label=awesome # 添加标签
    $ kubectl annotate pods my-pod icon-url=http://goo.gl/XXBTWq # 添加注解
    $ kubectl autoscale deployment foo --min=2 --max=10 # 自动扩展 deployment “foo”
  4. 与运行中的 pod 进行交互

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    kubectl logs -n umbrella web-78866d68c6-b74fs  # dump 输出 pod 的日志(stdout)
    kubectl logs -n umbrella web-78866d68c6-b74fs -c web # dump 输出 pod 中容器的日志(stdout,pod 中有多个容器的情况下使用)
    kubectl logs -f web-78866d68c6-b74fs -n umbrella # 流式输出 pod 的日志(stdout)
    kubectl logs -f my-pod -c my-container # 流式输出 pod 中容器的日志(stdout,pod 中有多个容器的情况下使用)
    kubectl run -i --tty busybox --image=busybox -- sh # 交互式 shell 的方式运行 pod
    kubectl attach my-pod -i # 连接到运行中的容器 (有问题)
    kubectl port-forward my-pod 5000:6000 # 转发 pod 中的 6000 端口到本地的 5000 端口
    kubectl exec my-pod -- ls / # 在已存在的容器中执行命令(只有一个容器的情况下)
    kubectl exec my-pod -c my-container -- ls / # 在已存在的容器中执行命令(pod 中有多个容器的情况下)
    kubectl top pod POD_NAME --containers # 显示指定 pod 和容器的指标度量

kubectl logs -f web-6b647749-dvc88 -n umbrella
kubectl edit deployment -n umbrella web