关于 tencentcloud-tke
tencentcloud-tke 是面向 Tencent Cloud TKE 集群运维的技能,可用于查看集群与节点,并通过获取的 kubeconfig 调用 kubectl 管理工作负载。它结合 Tencent Cloud SDK 的集群发现与集群内操作。
主要功能
- 01跨区域查看 TKE 集群
- 02查看节点与节点池状态
- 03列出工作负载、服务和 Pod
- 04调整 Deployment 副本数
- 05滚动重启 Deployment
- 06获取集群 kubeconfig
适用场景
- 01排查节点或 Pod 健康状况
- 02查看近期事件和应用日志
- 03按需扩缩 Deployment
- 04配置变更后滚动重启
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ acedatacloud/ skills --skill tencentcloud-tke安装后在 Claude Code 中输入/tencentcloud-tke确认已加载。
附带文件scripts/tke.py
详细介绍
腾讯云 TKE(Kubernetes)
管理 TKE 集群及其中的工作负载。
设置: 请参阅 腾讯云身份验证。集群发现和 kubeconfig 获取通过 SDK 完成;集群内的所有操作(Pod、服务、扩缩容、重启)都通过
kubectl使用我们获取的 kubeconfig 执行。
CLI(推荐)
此技能提供 scripts/tke.py — 用于封装集群发现、kubeconfig 获取以及最常见的集群内操作。
# $SKILL_DIR can point at another skill loaded this turn — anchor on our own
# script (re-run this at the top of every fresh-shell Bash block).
TKE="$SKILL_DIR/scripts/tke.py"; [ -f "$TKE" ] || TKE=$(find /tmp -maxdepth 8 -path '*/skills/*/scripts/tke.py' 2>/dev/null | head -1)
[ -f "$TKE" ] || { echo "tencentcloud-tke script not found (SKILL_DIR=$SKILL_DIR)" >&2; exit 1; }
python3 $TKE clusters # list clusters
python3 $TKE cluster cls-xxxxxxxx # one cluster's details
python3 $TKE nodes cls-xxxxxxxx
python3 $TKE pools cls-xxxxxxxx # node pools
python3 $TKE kubeconfig cls-xxxxxxxx --save ~/.kube/config-tke # write kubeconfig
python3 $TKE workloads cls-xxxxxxxx -n my-namespace
python3 $TKE pods cls-xxxxxxxx -n my-namespace
python3 $TKE events cls-xxxxxxxx -n my-namespace # recent events
python3 $TKE scale cls-xxxxxxxx -n my-namespace --name my-deploy --replicas 4
python3 $TKE restart cls-xxxxxxxx -n my-namespace --name my-deploy
集群内命令会使用 SDK 获取的 kubeconfig 调用 kubectl。沙盒中必须安装 kubectl(pip install 不会安装它)。
适用场景
- 列出各区域的 TKE 集群
- 检查节点健康状况和节点池资源使用情况
- 列出命名空间中的 Deployment / StatefulSet / DaemonSet
- 列出服务 / Pod / 最近的事件
- 扩容或缩容工作负载
- 滚动重启 Deployment(例如配置更改后)
- 获取 kubeconfig,以便临时使用
kubectl
依赖项
pip install tencentcloud-sdk-python
brew install kubectl # macOS; apt install kubectl on Debian/Ubuntu
快速入门 — 列出集群
import os
from tencentcloud.common import credential
from tencentcloud.tke.v20180525 import tke_client, models
cred = credential.EnvironmentVariableCredential().get_credential()
client = tke_client.TkeClient(cred, os.environ["TENCENTCLOUD_REGION"])
req = models.DescribeClustersRequest()
req.Limit = 100
resp = client.DescribeClusters(req)
for c in resp.Clusters:
print(c.ClusterId, c.ClusterName, c.ClusterStatus, c.ClusterVersion)
集群 ID 的格式为
cls-xxxxxxxx。生产集群通常位于ap-hongkong区域;DescribeClusters按区域查询 — 请针对所需的每个区域调用它。
工作流
获取集群详情和工作节点数量
req = models.DescribeClustersRequest()
req.ClusterIds = ["cls-xxxxxxxx"]
resp = client.DescribeClusters(req)
c = resp.Clusters[0]
print(c.ClusterName, c.ClusterStatus, c.ClusterNodeNum, c.ClusterVersion)
列出工作节点(及其 CVM 实例类型)
req = models.DescribeClusterInstancesRequest()
req.ClusterId = "cls-xxxxxxxx"
req.Limit = 100
resp = client.DescribeClusterInstances(req)
for i in resp.InstanceSet:
print(i.InstanceId, i.InstanceRole, i.InstanceState, i.NodePoolId)
获取 kubeconfig
req = models.DescribeClusterKubeconfigRequest()
req.ClusterId = "cls-xxxxxxxx"
req.IsExtranet = True # False for VPC-internal kubeconfig
resp = client.DescribeClusterKubeconfig(req)
# Save and use immediately
import os, pathlib
kubeconfig = pathlib.Path(os.path.expanduser("~/.kube/config-tke-cls-xxxxxxxx"))
kubeconfig.parent.mkdir(parents=True, exist_ok=True)
kubeconfig.write_text(resp.Kubeconfig)
print("export KUBECONFIG=" + str(kubeconfig))
许多 TKE 集群默认仅公开内部 API 端点。如果
IsExtranet=True返回空配置或不可用的配置,说明该集群未启用公网 API 访问 — 请设置IsExtranet=False,并在同一 VPC 内的主机上运行kubectl(例如 CVM、跳板机)。
运行 kubectl 命令(使用获取的 kubeconfig)
import subprocess
KUBECONFIG = os.path.expanduser("~/.kube/config-tke-cls-xxxxxxxx")
NS = "acedatacloud"
def kubectl(*args):
return subprocess.run(
["kubectl", f"--kubeconfig={KUBECONFIG}", *args],
check=True, capture_output=True, text=True,
).stdout
print(kubectl("get", "pods", "-n", NS))
print(kubectl("get", "deploy", "-n", NS))
print(kubectl("get", "svc", "-n", NS))
print(kubectl("get", "events", "-n", NS, "--sort-by=.lastTimestamp"))
查看异常 Pod 的详情
print(kubectl("describe", "pod", "<pod-name>", "-n", NS))
print(kubectl("logs", "<pod-name>", "-n", NS, "--tail=200"))
扩缩 Deployment
# To 4 replicas. Confirm with the user before running for prod workloads.
print(kubectl("scale", "deploy/platform-backend", "-n", NS, "--replicas=4"))
滚动重启 Deployment
# Forces every pod to recycle through the rolling-update strategy.
print(kubectl("rollout", "restart", "deploy/platform-backend", "-n", NS))
print(kubectl("rollout", "status", "deploy/platform-backend", "-n", NS, "--timeout=300s"))
列出节点池(TKE 中高于原始节点的概念)
req = models.DescribeClusterNodePoolsRequest()
req.ClusterId = "cls-xxxxxxxx"
resp = client.DescribeClusterNodePools(req)
for np in resp.NodePoolSet:
print(np.NodePoolId, np.Name, np.LifeState, np.DesiredNodesNum, np.AutoscalingGroupId)
故障排查流程
1. python: DescribeClusters → cluster status / version
2. python: DescribeClusterInstances → any nodes "failed" / "running"
3. kubectl get events → recent failures (image pulls, scheduling, OOM)
4. kubectl get pods → which pod is in CrashLoopBackOff / ImagePullBackOff
5. kubectl describe pod <name> → conditions, events on the pod
6. kubectl logs <name> --tail=200 → application logs
7. (optional) tencentcloud-cls skill → CLS query for the same window
重要提醒
- 在对生产工作负载执行扩缩容或重启操作前,请先与用户确认。将
replicas误写为0会导致服务中断。 - Kubeconfig 包含长期有效的 bearer token。 请将该文件视为凭证 — 使用
chmod 600,切勿提交到版本库,并在人员离职后重新生成。 - 内部与外部端点:
IsExtranet=False会生成只能从集群 VPC 内使用的 kubeconfig。在笔记本电脑上使用时,请设置IsExtranet=True,并确保集群已启用公网 API 端点(TKE 控制台 → 集群 → 基本信息 → API Server 访问)。 - 区域很重要。 位于
ap-hongkong的集群cls-xxxxxxxx,无法通过为ap-guangzhou构造的 TKE 客户端访问。
控制台链接
评论
支持 Markdown