Kubernetes go-client: kubectl get pods のようなポッド詳細の取得
client-go ライブラリを使用して Kubernetes クラスター内のポッド詳細を取得するにはkubectl get pods -n
Kubernetes クライアントを作成します。Kubernetes クライアントの取得の詳細については、client-go ドキュメントを参照してください。
ターゲットの名前空間を指定します。 kubectl.
の -n
ポッド情報の抽出: PodList を反復処理して、各ポッドのメタデータとステータス情報にアクセスします。詳細については、Kubernetes API ドキュメントの Pod および PodStatus 構造体の定義を参照してください。
追加の詳細を抽出します: 必要に応じて、ポッドの作成タイムスタンプを使用して、ポッドの経過時間、コンテナの再起動、準備完了ステータスなどの属性を計算します。コンテナーのステータス。
ポッド名、ステータス、準備完了ステータス、再起動、および経過時間を取得する方法を示すサンプル コード スニペットは次のとおりです。
func GetPods(client *meshkitkube.Client, namespace string) (*v1core.PodList, error) {
podInterface := client.KubeClient.CoreV1().Pods(namespace)
podList, err := podInterface.List(context.TODO(), v1.ListOptions{})
return podList, err
}
// Iterate through pods and collect required data
for _, pod := range podList.Items {
podCreationTime := pod.GetCreationTimestamp()
age := time.Since(podCreationTime.Time).Round(time.Second)
podStatus := pod.Status
containerRestarts, containerReady, totalContainers := 0, 0, len(pod.Spec.Containers)
for container := range pod.Spec.Containers {
containerRestarts = podStatus.ContainerStatuses[container].RestartCount
if podStatus.ContainerStatuses[container].Ready {
containerReady
}
}
name := pod.GetName()
ready := fmt.Sprintf("%v/%v", containerReady, totalContainers)
status := fmt.Sprintf("%v", podStatus.Phase)
restarts := fmt.Sprintf("%v", containerRestarts)
ageS := age.String()
data = append(data, []string{name, ready, status, restarts, ageS})
}
このプロセスは、kubectl get pods -n
免責事項: 提供されるすべてのリソースの一部はインターネットからのものです。お客様の著作権またはその他の権利および利益の侵害がある場合は、詳細な理由を説明し、著作権または権利および利益の証拠を提出して、電子メール [email protected] に送信してください。 できるだけ早く対応させていただきます。
Copyright© 2022 湘ICP备2022001581号-3