kubectlを利用する場合
1 |
aws eks update-kubeconfig <span class="nt">--name</span> vamdemic-development-eks-cluster |
kube-systemのnamespaceでは、12のPodが起動中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
yuta:~/go-kubernentes (master *+=) $ kubectl get pod -n kube-system NAME READY STATUS RESTARTS AGE alb-ingress-controller-5b4b5fd775-8vxt7 1/1 Running 1 16d aws-node-829mc 1/1 Running 0 16d aws-node-q4d67 1/1 Running 0 16d coredns-79769ff86-h2tck 1/1 Running 0 16d coredns-79769ff86-wclkd 1/1 Running 0 16d ebs-csi-controller-587cb4db9b-6j8xz 4/4 Running 3 16d ebs-csi-controller-587cb4db9b-w5s5s 4/4 Running 2 16d ebs-csi-node-gbpx4 3/3 Running 0 16d ebs-csi-node-l8rx5 3/3 Running 0 16d efs-csi-node-v6m4m 3/3 Running 0 16d efs-csi-node-vtf5r 3/3 Running 0 16d kube-proxy-7gbmn 1/1 Running 0 16d kube-proxy-8h4tk 1/1 Running 0 16d |
GoでPod一覧を取得する場合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package main import ( "context" "fmt" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" "log" "os" "path/filepath" ) func main() { // Kubeconfigのファイルパスを指定 kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube", "config") config, err := clientcmd.BuildConfigFromFlags("", kubeconfig) if err != nil { log.Fatal(err) } // Kubeconfigを読み込む clientset, err := kubernetes.NewForConfig(config) if err != nil { log.Fatal(err) } // Pod一覧を呼び出す namespace := "kube-system" pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{}) if err != nil { log.Fatalln("failed to get pods:", err) } // print pods // pods.Items: []v1.Pod for i, pod := range pods.Items { fmt.Printf("[%d] %s\n", i, pod.GetName()) } } |
出力結果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
yuta:~/go-kubernentes (master *+=) $ go run main.go [0] alb-ingress-controller-5b4b5fd775-8vxt7 [1] aws-node-829mc [2] aws-node-q4d67 [3] coredns-79769ff86-h2tck [4] coredns-79769ff86-wclkd [5] ebs-csi-controller-587cb4db9b-6j8xz [6] ebs-csi-controller-587cb4db9b-w5s5s [7] ebs-csi-node-gbpx4 [8] ebs-csi-node-l8rx5 [9] efs-csi-node-v6m4m [10] efs-csi-node-vtf5r [11] kube-proxy-7gbmn [12] kube-proxy-8h4tk |
参考
https://pizi.netlify.app/posts/kubernetes-client-go-example/#node%E3%81%AE%E5%8F%96%E5%BE%97
https://github.com/kubernetes/client-go