环境准备

(我没环境,看个热闹)

通过kind创建多节点的k8s集群: 3个master节点 + 3个worker节点
tip:kind简介
安不上!!!

  1. # 先安装kind
  2. go get sigs.k8s.io/kind@v0.5.1
  3. # 创建集群
  4. kind create cluster --config=init_cluster.yaml

init_cluster.yaml

  1. # a cluster with 3 control-plane nodes and 3 workers
  2. kind: Cluster
  3. apiVersion: kind.x-k8s.io/v1alpha4
  4. nodes:
  5. - role: control-plane
  6. - role: control-plane
  7. - role: control-plane
  8. - role: worker
  9. - role: worker
  10. - role: worker

RestClient使用例子

这段代码的作用:从namespace为kube-system中获取所有的pod并输出到屏幕
main.go

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. corev1 "k8s.io/api/core/v1"
  6. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  7. "k8s.io/client-go/kubernetes/scheme"
  8. "k8s.io/client-go/rest"
  9. "k8s.io/client-go/tools/clientcmd"
  10. )
  11. func main() {
  12. fmt.Println("Prepare config object.")
  13. // 加载k8s配置文件,生成Config对象
  14. config, err := clientcmd.BuildConfigFromFlags("", "/root/.kube/config")
  15. if err != nil {
  16. panic(err)
  17. }
  18. config.APIPath = "api"
  19. config.GroupVersion = &corev1.SchemeGroupVersion
  20. config.NegotiatedSerializer = scheme.Codecs
  21. fmt.Println("Init RESTClient.")
  22. // 定义RestClient,用于与k8s API server进行交互
  23. restClient, err := rest.RESTClientFor(config)
  24. if err != nil {
  25. panic(err)
  26. }
  27. fmt.Println("Get Pods in cluster.")
  28. // 获取pod列表。这里只会从namespace为"kube-system"中获取指定的资源(pods)
  29. result := &corev1.PodList{}
  30. if err := restClient.
  31. Get().
  32. Namespace("kube-system").
  33. Resource("pods").
  34. VersionedParams(&metav1.ListOptions{Limit: 500}, scheme.ParameterCodec).
  35. Do(context.TODO()).
  36. Into(result); err != nil {
  37. panic(err)
  38. }
  39. fmt.Println("Print all listed pods.")
  40. // 打印所有获取到的pods资源,输出到标准输出
  41. for _, d := range result.Items {
  42. fmt.Printf("NAMESPACE: %v NAME: %v \t STATUS: %v \n", d.Namespace, d.Name, d.Status.Phase)
  43. }
  44. }

go.mod

  1. module main
  2. go 1.15
  3. require (
  4. github.com/go-logr/logr v0.2.1 // indirect
  5. github.com/google/gofuzz v1.2.0 // indirect
  6. github.com/imdario/mergo v0.3.11 // indirect
  7. golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0 // indirect
  8. golang.org/x/net v0.0.0-20201010224723-4f7140c49acb // indirect
  9. golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43 // indirect
  10. golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634 // indirect
  11. golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect
  12. k8s.io/api v0.19.2
  13. k8s.io/apimachinery v0.19.2
  14. //k8s.io/client-go v11.0.0+incompatible
  15. // k8s.io/client-go v11.0.0+incompatible
  16. k8s.io/klog v1.0.0 // indirect
  17. k8s.io/klog/v2 v2.3.0 // indirect
  18. k8s.io/utils v0.0.0-20201005171033-6301aaf42dc7 // indirect
  19. )
  20. require k8s.io/client-go v0.19.2

输出结果

  1. [root@xxx-wsl ~/client-go-example] go run main.go
  2. Prepare config object.
  3. Init RESTClient.
  4. Get Pods in cluster.
  5. Print all listed pods.
  6. NAMESPACE: kube-system NAME: coredns-f9fd979d6-rhzfd STATUS: Running
  7. NAMESPACE: kube-system NAME: coredns-f9fd979d6-whrj2 STATUS: Running
  8. NAMESPACE: kube-system NAME: etcd-kind-control-plane STATUS: Running
  9. NAMESPACE: kube-system NAME: etcd-kind-control-plane2 STATUS: Running
  10. NAMESPACE: kube-system NAME: etcd-kind-control-plane3 STATUS: Running
  11. NAMESPACE: kube-system NAME: kindnet-bpsfl STATUS: Running
  12. NAMESPACE: kube-system NAME: kindnet-ks6zv STATUS: Running
  13. NAMESPACE: kube-system NAME: kindnet-pm6zl STATUS: Running
  14. NAMESPACE: kube-system NAME: kindnet-qfhqt STATUS: Running
  15. NAMESPACE: kube-system NAME: kindnet-s7qqn STATUS: Running
  16. NAMESPACE: kube-system NAME: kindnet-trk5l STATUS: Running
  17. NAMESPACE: kube-system NAME: kube-apiserver-kind-control-plane STATUS: Running
  18. NAMESPACE: kube-system NAME: kube-apiserver-kind-control-plane2 STATUS: Running
  19. NAMESPACE: kube-system NAME: kube-apiserver-kind-control-plane3 STATUS: Running
  20. NAMESPACE: kube-system NAME: kube-controller-manager-kind-control-plane STATUS: Running
  21. NAMESPACE: kube-system NAME: kube-controller-manager-kind-control-plane2 STATUS: Running
  22. NAMESPACE: kube-system NAME: kube-controller-manager-kind-control-plane3 STATUS: Running
  23. NAMESPACE: kube-system NAME: kube-proxy-7gz67 STATUS: Running
  24. NAMESPACE: kube-system NAME: kube-proxy-bvbkk STATUS: Running
  25. NAMESPACE: kube-system NAME: kube-proxy-clf72 STATUS: Running
  26. NAMESPACE: kube-system NAME: kube-proxy-d8zpb STATUS: Running
  27. NAMESPACE: kube-system NAME: kube-proxy-dsmsj STATUS: Running
  28. NAMESPACE: kube-system NAME: kube-proxy-fplkk STATUS: Running
  29. NAMESPACE: kube-system NAME: kube-scheduler-kind-control-plane STATUS: Running
  30. NAMESPACE: kube-system NAME: kube-scheduler-kind-control-plane2 STATUS: Running
  31. NAMESPACE: kube-system NAME: kube-scheduler-kind-control-plane3 STATUS: Running