tencentcloud-tke について
tencentcloud-tkeは、Tencent Cloud TKEクラスターを運用するスキルです。Tencent Cloud SDKでクラスターを検出してkubeconfigを取得し、kubectlでワークロードを操作します。
主な機能
- 01複数リージョンのTKEクラスター一覧
- 02ノードとノードプールの状態確認
- 03ワークロード、サービス、Podの一覧
- 04Deploymentのレプリカ数調整
- 05Deploymentのローリング再起動
- 06クラスターkubeconfigの取得
活用シーン
- 01ノードやPodの状態調査
- 02最近のイベントとアプリログの確認
- 03Deploymentの必要に応じたスケーリング
- 04設定変更後のDeployment再起動
インストール
コマンドをコピーしてターミナルで実行します。
npx skills add https:/ / github.com/ acedatacloud/ skills --skill tencentcloud-tkeインストール後、Claude Code で/tencentcloud-tkeを入力して読み込まれたか確認します。
同梱ファイルscripts/tke.py
詳細
Tencent Cloud TKE(Kubernetes)
TKE クラスターと、その内部のワークロードを管理します。
セットアップ: tencentcloud authentication を参照してください。クラスターの検出と kubeconfig の取得は SDK 経由で行い、クラスター内部の操作(ポッド、サービス、スケーリング、再起動)はすべて、取得した kubeconfig を使って
kubectl経由で行います。
CLI(推奨)
このスキルには、クラスターの検出、kubeconfig の取得、クラスター内の一般的な操作を実行する scripts/tke.py が含まれています。
# $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 を一覧表示する
- Service/Pod/最近の Event を一覧表示する
- ワークロードをスケールアップまたはスケールダウンする
- Deployment をローリング再起動する(例:設定変更後)
- アドホックな
kubectl操作のために kubeconfig を取得する
依存関係
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 内のホスト(例:CVM、踏み台ホスト)からkubectlを実行してください。
取得した kubeconfig を使って kubectl コマンドを実行する
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 には有効期間の長いベアラートークンが含まれます。 ファイルは認証情報として扱ってください。
chmod 600を設定し、コミットしないでください。メンバーの退職後には再生成してください。 - 内部エンドポイントと外部エンドポイント:
IsExtranet=Falseでは、クラスターの VPC 内からのみ使用できる kubeconfig が生成されます。ノートパソコンから使用する場合は、IsExtranet=Trueを指定し、クラスターでパブリック API エンドポイントが有効になっていることを確認してください(TKE コンソール → クラスター → 基本情報 → API サーバーアクセス)。 - リージョンは重要です。
ap-hongkongのクラスターcls-xxxxxxxxは、ap-guangzhou用に作成された TKE クライアントからは参照できません。