client-go 连接 kubernetes

https://github.com/kubernetes/client-go.git

部署go 环境
wget https://studygolang.com/dl/golang/go1.10.3.linux-amd64.tar.gz
tar xf go1.10.3.linux-amd64.tar.gz -C /usr/local/

vim /etc/profile
# Go
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
export GOPATH=/root/gopath

source /etc/profile

创建对应目录
mkdir -p /root/gopath
mkdir -p /root/gopath/src
mkdir -p /root/gopath/pkg
mkdir -p /root/gopath/bin

go version
go version go1.10.3 linux/amd64
client-go 安装
client-go 是调用kubernetes集群资源对象API的客户端,通过client-go实现对kubernetes集群中资源对象(包括deployment、service、ingress、pod、namespace、node等)的增删改查等操作
go get -v k8s.io/client-go/
go get -v k8s.io/apimachinery/
链接kubernetes集群
参考
https://github.com/kubernetes/client-go/blob/master/examples/out-of-cluster-client-configuration/main.go
使用 client-go 在 k8s 集群外操作资源,首先需要通过获取 kubeconfig 配置文件,来建立连接
	var kubeconfig *string
	if home := homeDir(); home != ""{
		kubeconfig = flag.String("kubeconfig", filepath.Join(home, "go", "src", "go-k8s", "config"), "(optional) absolute path to the kubeconfig file")
	}else {
		kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
	}
	flag.Parse()

在 kubeconfig 中使用当前上下文环境,config 获取支持 url 和 path 方式,通过 BuildConfigFromFlags() 函数获取 restclient.Config 对象,用来下边根据该 config 对象创建 client 集合
	config,err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
	if err != nil {
		fmt.Println(err.Error())
	}
根据获取的 config 来创建一个 clientset 对象。通过调用 NewForConfig 函数创建 clientset 对象。
NewForConfig 函数具体实现就是初始化 clientset 中的每个 client,基本涵盖了 k8s 内各种类型

https://github.com/kubernetes/client-go/blob/master/kubernetes/clientset.go

	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		fmt.Println(err.Error())
	}
	fmt.Println("Connect kubernetes Successful!!!")

通过实现 clientset 的 CoreV1Interface 接口列表中的 PodsGetter 接口方法 Pods(namespace string) 返回 PodInterface
PodInterface 接口拥有操作 Pod 资源的方法,例如 Create、Update、Get、List 等方法

注意:Pods() 方法中 namespace 不指定则获取 Cluster 所有 Pod 列表

pods, err := clientset.CoreV1().Pods("").List(metav1.ListOptions{})
Pod 信息又包含了三大类型:metav1.ObjectMeta、Spec、Status,每个类型又包含了不同的属性值
pod.ObjectMeta.Name
pod.Status.PodIP
pod.Spec.NodeName
链接k8s 代码示例
package main

import (

	"os"
	"fmt"
	"flag"
	"path/filepath"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/tools/clientcmd"
)


func homeDir() string {
	if h := os.Getenv("HOME"); h != "" {
		return h
	}
	return os.Getenv("USERPROFILE")
}

func main() {
	var kubeconfig *string
	if home := homeDir(); home != ""{
		// 配置 k8s 集群 kubeconfig 配置文件,默认位置 $HOME/.kube/config
		kubeconfig = flag.String("kubeconfig", filepath.Join(home, "go", "src", "go-k8s", "config"), "(optional) absolute path to the kubeconfig file")
	}else {
		kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
	}
	flag.Parse()

	config,err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
	if err != nil {
		fmt.Println(err.Error())
	}

	_, err = kubernetes.NewForConfig(config)
	if err != nil {
		fmt.Println(err.Error())
	}
	fmt.Println("Connect kubernetes Successful!!!")
}

执行结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/u011327801/article/details/91974153