一、創建clientset
//獲取kubeconfig
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
//創建config
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
//創建clientset
clientset, err := kubernetes.NewForConfig(config)
//具體的資源調用見以下例子
二、獲取資源
1、pod
//聲明pod對象
var pod *v1.Pod
//創建pod
pod, err := clientset.CoreV1().Pods(<namespace>).Create(<pod>)
//更新pod
pod, err := clientset.CoreV1().Pods(<namespace>).Update(<pod>)
//刪除pod
err := clientset.CoreV1().Pods(<namespace>).Delete(<pod.Name>, &meta_v1.DeleteOptions{})
//查詢pod
pod, err := clientset.CoreV1().Pods(<namespace>).Get(<pod.Name>, &meta_v1.GetOptions{})
//列出pod
podList, err := clientset.CoreV1().Pods(<namespace>).List(&meta_v1.ListOptions{})
//watch pod
watchInterface, err := clientset.CoreV1().Pods(<namespace>).Watch(&meta_v1.ListOptions{})
2、pods補充
fmt.Println(pods.Items[1].Name)
fmt.Println(pods.Items[1].CreationTimestamp)
fmt.Println(pods.Items[1].Labels)
fmt.Println(pods.Items[1].Namespace)
fmt.Println(pods.Items[1].Status.HostIP)
fmt.Println(pods.Items[1].Status.PodIP)
fmt.Println(pods.Items[1].Status.StartTime)
fmt.Println(pods.Items[1].Status.Phase)
fmt.Println(pods.Items[1].Status.ContainerStatuses[0].RestartCount) //重啟次數
fmt.Println(pods.Items[1].Status.ContainerStatuses[0].Image) //獲取重啟時間
//獲取NODE
fmt.Println("##################")
nodes, err := clientset.CoreV1().Nodes().List(metav1.ListOptions{})
fmt.Println(nodes.Items[0].Name)
fmt.Println(nodes.Items[0].CreationTimestamp) //加入集群時間
fmt.Println(nodes.Items[0].Status.NodeInfo)
fmt.Println(nodes.Items[0].Status.Conditions[len(nodes.Items[0].Status.Conditions)-1].Type)
fmt.Println(nodes.Items[0].Status.Allocatable.Memory().String())
3、deployment
//聲明deployment對象
var deployment *v1beta1.Deployment
//構造deployment對象
//創建deployment
deployment, err := clientset.AppsV1beta1().Deployments(<namespace>).Create(<deployment>)
//更新deployment
deployment, err := clientset.AppsV1beta1().Deployments(<namespace>).Update(<deployment>)
//刪除deployment
err := clientset.AppsV1beta1().Deployments(<namespace>).Delete(<deployment.Name>, &meta_v1.DeleteOptions{})
//查詢deployment
deployment, err := clientset.AppsV1beta1().Deployments(<namespace>).Get(<deployment.Name>, &meta_v1.GetOptions{})
//列出deployment
deploymentList, err := clientset.AppsV1beta1().Deployments(<namespace>).List(&meta_v1.ListOptions{})
//watch deployment
watchInterface, err := clientset.AppsV1beta1().Deployments(<namespace>).Watch(&meta_v1.ListOptions{})
4、service
//聲明service對象
var service *v1.Service
//構造service對象
//創建service
service, err := clientset.CoreV1().Services(<namespace>).Create(<service>)
//更新service
service, err := clientset.CoreV1().Services(<namespace>).Update(<service>)
//刪除service
err := clientset.CoreV1().Services(<namespace>).Delete(<service.Name>, &meta_v1.DeleteOptions{})
//查詢service
service, err := clientset.CoreV1().Services(<namespace>).Get(<service.Name>, &meta_v1.GetOptions{})
//列出service
serviceList, err := clientset.CoreV1().Services(<namespace>).List(&meta_v1.ListOptions{})
//watch service
watchInterface, err := clientset.CoreV1().Services(<namespace>).Watch(&meta_v1.ListOptions{})
5、ingress
var ingress *v1beta1.Ingress
//構造ingress對象
//創建ingress
ingress, err := clientset.ExtensionsV1beta1().Ingresses(<namespace>).Create(<ingress>)
//更新ingress
ingress, err := clientset.ExtensionsV1beta1().Ingresses(<namespace>).Update(<ingress>)
//刪除ingress
err := clientset.ExtensionsV1beta1().Ingresses(<namespace>).Delete(<ingress.Name>, &meta_v1.DeleteOptions{})
//查詢ingress
ingress, err := clientset.ExtensionsV1beta1().Ingresses(<namespace>).Get(<ingress.Name>, &meta_v1.GetOptions{})
//列出ingress
ingressList, err := clientset.ExtensionsV1beta1().Ingresses(<namespace>).List(&meta_v1.ListOptions{})
//watch ingress
watchInterface, err := clientset.ExtensionsV1beta1().Ingresses(<namespace>).Watch(&meta_v1.ListOptions{})
