[golang]-k8s集群外使用

该博客介绍如何使用Go语言的client-go库连接并测试Kubernetes环境。首先,将kubeconfig文件拷贝到指定目录。然后,通过clientcmd构建配置并创建客户端。最后,列出集群中的Pods、PersistentVolumes、PersistentVolumeClaims和Namespaces。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

导语:使用client-go 调用测试环境

将测试环境的kubelet.kubeconfig 或者/root/.kube/config 拷贝到电脑的$HOME/.kube/下

go.mod

module demo

go 1.13

require (
	github.com/evanphx/json-patch v4.9.0+incompatible // indirect
	github.com/fsnotify/fsnotify v1.4.9 // indirect
	github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7 // indirect
	github.com/golang/protobuf v1.4.2 // indirect
	github.com/googleapis/gnostic v0.4.0 // indirect
	github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
	github.com/imdario/mergo v0.3.11 // indirect
	github.com/json-iterator/go v1.1.10 // indirect
	github.com/pkg/errors v0.9.1 // indirect
	golang.org/x/net v0.0.0-20200707034311-ab3426394381 // indirect
	golang.org/x/sys v0.0.0-20200622214017-ed371f2e16b4 // indirect
	golang.org/x/text v0.3.3 // indirect
	golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect
	google.golang.org/protobuf v1.24.0 // indirect
	k8s.io/apimachinery v0.17.0
	k8s.io/client-go v0.17.0
	k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac // indirect
	k8s.io/klog/v2 v2.2.0 // indirect
	k8s.io/utils v0.0.0-20201110183641-67b214c5f920 // indirect
	sigs.k8s.io/structured-merge-diff/v4 v4.0.1 // indirect
)

main.go

package main

import (
	"flag"
	//    "context"
	"fmt"
	"path/filepath"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/tools/clientcmd"
	"k8s.io/client-go/util/homedir"
)

func main() {
	var kubeconfig *string
	if home := homedir.HomeDir(); home != "" {
		kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
	} else {
		kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
	}
	flag.Parse()

	// use the current context in kubeconfig
	config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
	if err != nil {
		panic(err.Error())
	}
	// creates the clientset
	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		panic(err.Error())
	}

	pods, err1 := clientset.CoreV1().Pods("").List(metav1.ListOptions{})
	if err1 != nil {
		panic(err1.Error())
	}
	pvs, err2 := clientset.CoreV1().PersistentVolumes().List(metav1.ListOptions{})
	if err2 != nil {
		panic(err2.Error())
	}
	pvcs, err3 := clientset.CoreV1().PersistentVolumeClaims("").List(metav1.ListOptions{})
	if err3 != nil {
		panic(err3.Error())
	}
	namespaces, err4 := clientset.CoreV1().Namespaces().List(metav1.ListOptions{})
	if err4 != nil {
		panic(err4.Error())
	}
	fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
	fmt.Printf("There are %d pvs in the cluster\n", len(pvs.Items))
	fmt.Printf("There are %d pvcs in the cluster\n", len(pvcs.Items))
	fmt.Printf("There are %d namespaces in the cluster\n", len(namespaces.Items))

	fmt.Println("---------pods----------")
	for _, pod := range pods.Items {
		fmt.Printf("Name: %s, Status: %s, CreateTime: %s,NameSpace: %s\n", pod.ObjectMeta.Name, pod.Status.Phase, pod.ObjectMeta.CreationTimestamp, pod.Namespace)
	}
	fmt.Println("---------pvs----------")
	for _, pv := range pvs.Items {
		fmt.Printf("Name: %s, Status: %s, CreateTime: %s\n", pv.ObjectMeta.Name, pv.Status.Phase, pv.ObjectMeta.CreationTimestamp)
	}
	fmt.Println("---------pvcs----------")
	for _, pvc := range pvcs.Items {
		fmt.Printf("Name: %s, Status: %s, CreateTime: %s\n", pvc.ObjectMeta.Name, pvc.Status.Phase, pvc.ObjectMeta.CreationTimestamp)
	}
	fmt.Println("---------namespaces----------")
	for _, namespace := range namespaces.Items {
		fmt.Printf("Name: %s, Status: %s, CreateTime: %s\n", namespace.ObjectMeta.Name, namespace.Status.Phase, namespace.ObjectMeta.CreationTimestamp)
	}
}

运行测试

go run main.go

参考

https://blog.csdn.net/xixihahalelehehe/article/details/110358813

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爷来辣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值