tencentcloud-tke
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTencent Cloud TKE (Kubernetes)
腾讯云TKE(Kubernetes)
Manage TKE clusters and the workloads inside them.
Setup: See tencentcloud authentication. Cluster discovery and kubeconfig retrieval go through the SDK; everything inside the cluster (pods, services, scale, restart) goes throughagainst the kubeconfig we fetch.kubectl
管理TKE集群及其内部的工作负载。
配置步骤: 参考tencentcloud 认证。集群发现和kubeconfig获取通过SDK完成;集群内的所有操作(pod、service、扩容、重启)均通过基于我们获取的kubeconfig执行。kubectl
CLI (preferred)
CLI(推荐方式)
The skill ships — wraps cluster discovery, kubeconfig retrieval, and the most common in-cluster operations.
scripts/tke.pybash
TKE=$SKILL_DIR/scripts/tke.py
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-deployIn-cluster commands shell out to against an SDK-fetched kubeconfig. must be installed in the sandbox ( doesn't ship it).
kubectlkubectlpip install该技能提供了脚本——封装了集群发现、kubeconfig获取以及最常用的集群内操作。
scripts/tke.pybash
TKE=$SKILL_DIR/scripts/tke.py
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执行。沙箱环境中必须安装(不会安装它)。
kubectlkubectlpip installWhen to Use
使用场景
- List TKE clusters across regions
- Check node health and node-pool resource usage
- List Deployments / StatefulSets / DaemonSets in a namespace
- List Services / Pods / recent Events
- Scale a workload up or down
- Rolling restart a Deployment (e.g. after a config change)
- Fetch kubeconfig for ad-hoc work
kubectl
- 跨地域列出TKE集群
- 检查节点健康状态和节点池资源使用情况
- 列出命名空间中的Deployment / StatefulSets / DaemonSets
- 列出Services / Pods / 近期Events
- 对工作负载进行扩容或缩容
- 对Deployment执行滚动重启(例如配置变更后)
- 获取kubeconfig以进行临时操作
kubectl
Dependencies
依赖项
bash
pip install tencentcloud-sdk-python
brew install kubectl # macOS; apt install kubectl on Debian/Ubuntubash
pip install tencentcloud-sdk-python
brew install kubectl # macOS; apt install kubectl on Debian/UbuntuQuick start — list clusters
快速入门——列出集群
python
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)Cluster IDs look like. Thecls-xxxxxxxxregion typically holds the production clusters;ap-hongkongis region-scoped — call it per region you care about.DescribeClusters
python
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
Workflows
工作流程
Get cluster details + worker node count
获取集群详情及工作节点数量
python
req = models.DescribeClustersRequest()
req.ClusterIds = ["cls-xxxxxxxx"]
resp = client.DescribeClusters(req)
c = resp.Clusters[0]
print(c.ClusterName, c.ClusterStatus, c.ClusterNodeNum, c.ClusterVersion)python
req = models.DescribeClustersRequest()
req.ClusterIds = ["cls-xxxxxxxx"]
resp = client.DescribeClusters(req)
c = resp.Clusters[0]
print(c.ClusterName, c.ClusterStatus, c.ClusterNodeNum, c.ClusterVersion)List worker nodes (and their CVM instance types)
列出工作节点(及其CVM实例类型)
python
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)python
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)Fetch kubeconfig
获取kubeconfig
python
req = models.DescribeClusterKubeconfigRequest()
req.ClusterId = "cls-xxxxxxxx"
req.IsExtranet = True # False for VPC-internal kubeconfig
resp = client.DescribeClusterKubeconfig(req)python
req = models.DescribeClusterKubeconfigRequest()
req.ClusterId = "cls-xxxxxxxx"
req.IsExtranet = True # False for VPC-internal kubeconfig
resp = client.DescribeClusterKubeconfig(req)Save and use immediately
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))
> Many TKE clusters expose only the **internal** API endpoint by default. If `IsExtranet=True` returns an empty / unusable config, the cluster's public API access isn't enabled — set `IsExtranet=False` and run `kubectl` from a host inside the same VPC (e.g. CVM, jump host).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内的主机(如CVM、跳板机)运行`kubectl`。Run kubectl commands (with the fetched kubeconfig)
执行kubectl命令(使用获取的kubeconfig)
python
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"))python
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"))Describe a misbehaving pod
排查异常Pod
python
print(kubectl("describe", "pod", "<pod-name>", "-n", NS))
print(kubectl("logs", "<pod-name>", "-n", NS, "--tail=200"))python
print(kubectl("describe", "pod", "<pod-name>", "-n", NS))
print(kubectl("logs", "<pod-name>", "-n", NS, "--tail=200"))Scale a Deployment
扩容Deployment
python
undefinedpython
undefinedTo 4 replicas. Confirm with the user before running for prod workloads.
To 4 replicas. Confirm with the user before running for prod workloads.
print(kubectl("scale", "deploy/platform-backend", "-n", NS, "--replicas=4"))
undefinedprint(kubectl("scale", "deploy/platform-backend", "-n", NS, "--replicas=4"))
undefinedRolling restart a Deployment
滚动重启Deployment
python
undefinedpython
undefinedForces every pod to recycle through the rolling-update strategy.
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"))
undefinedprint(kubectl("rollout", "restart", "deploy/platform-backend", "-n", NS))
print(kubectl("rollout", "status", "deploy/platform-backend", "-n", NS, "--timeout=300s"))
undefinedList node pools (TKE concept above raw nodes)
列出节点池(TKE在原生节点之上的概念)
python
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)python
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)Troubleshooting flow
故障排查流程
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 window1. 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 windowImportant reminders
重要提醒
- Confirm scale / restart actions with the user before running for production workloads. A typo takes the service down.
replicas=0 - Kubeconfigs contain a long-lived bearer token. Treat the file like a credential — , never commit, regenerate after offboarding people.
chmod 600 - Internal vs external endpoint: gives a kubeconfig usable only from inside the cluster VPC. From a laptop, use
IsExtranet=Falseand ensure the cluster has a public API endpoint enabled (TKE console → Cluster → Basic Info → API Server access).IsExtranet=True - Region matters. Cluster in
cls-xxxxxxxxis invisible from a TKE client constructed forap-hongkong.ap-guangzhou
- 在对生产环境工作负载执行扩容/重启操作前,务必与用户确认。如果误写会导致服务下线。
replicas=0 - Kubeconfig包含长期有效的Bearer令牌。请将该文件视为凭据——设置权限,切勿提交到代码仓库,人员离职后需重新生成。
chmod 600 - 内部与外部端点:提供的kubeconfig仅能在集群VPC内部使用。在笔记本电脑上使用时,请设置
IsExtranet=False并确保集群已启用公网API端点(TKE控制台 → 集群 → 基本信息 → API Server访问)。IsExtranet=True - 地域至关重要。地域的集群
ap-hongkong无法在针对cls-xxxxxxxx地域构建的TKE客户端中被发现。ap-guangzhou