在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Github地址:https://github.com/kubernetes/client-go
访问kubernetes集群有几下几种方式:
使用client-go获取Deployment列表: package main import ( "path/filepath" "k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/kubernetes" "k8s.io/apimachinery/pkg/apis/meta/v1" "fmt" ) func main() { path, err := filepath.Abs("./") if err != nil { panic(err) } configFilePath := path + "/K8s/API/config" // use the config file config, err := clientcmd.BuildConfigFromFlags("", configFilePath) if err != nil { panic(err.Error()) } // NewForConfig creates a new ClientSet for the given config. clientSet, err := kubernetes.NewForConfig(config) if err != nil { panic(err.Error()) } // get all deployment in default namespace deploymentList, err := clientSet.AppsV1().Deployments("default").List(v1.ListOptions{}) if err != nil { panic(err.Error()) } // print list deployment name and replicas for _, v := range deploymentList.Items { fmt.Printf(" * %s (%d replicas)\n", v.Name, *v.Spec.Replicas) } }
|
请发表评论