k8s 二次开发
K8s 的认证管理
项目基于 K8s/k3s 的二次开发过程中,往往需要频繁的进行 Apiserver 的接口调用,如何生成并调用接口的认证信息将会是很关键的一步呦!
Token 管理
Token实际就是在计算机身份验证中的令牌(临时)的意思。
当用户向后端发起数据请求的时候,后端需要对用户进行身份验证,但是我们又不想每次都输入用户名和密码,这是就需要一个标识来证明自己的身份,这个标识就是token。
# 创建一个名叫 vpc-admin 命名空间在kube-system 下的服务账户 kubectl create serviceaccount vpc-admin -n kube-system # 针对账号 vpc-admin 进行赋权 kubectl create clusterrolebinding vpc-admin --clusterrole=cluster-admin --serviceaccount=kube-system:vpc-admin # vpc-admin 绑定为集群账户(显示出名字为vpc-admin-*的第一个匹配账户的详细信息) kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/vpc-admin/{print $1}') # 集群中的账户查看 kubectl -n kube-system get sa | grep vpc-admin
- Kubeconfig 管理
# 获取 token 的加密文件(文件转成字节进行获取)
cat ~/.kube/config | base64
client-go 的接入
package main import ( "context" "fmt" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "sigs.k8s.io/controller-runtime/pkg/log" ) type Config struct { Host string Token string Port int } func main() { clientSet,_:=NewKubernetesClient(&Config{ Host: "192.168.139.134", Port: 6443, Token: "eyJhbGciOiJSUzI1NiIsImtpZCI6Ikg3THcwRzVHTkdQNDREUi1fRjlQOE45NjBUd29pZDQydEs4Tmd1SFJpNnMifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkYXNoYm9hcmQtYWRtaW4tdG9rZW4tbWpudmsiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGFzaGJvYXJkLWFkbWluIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiNjdiNGZjM2ItZGEyZS00OTdhLWJmOTEtZGFmYWU3NmU5ZTU3Iiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmRhc2hib2FyZC1hZG1pbiJ9.kW_8iz1GpZPWL2hqDd2Jhkc-rLEX5QPKYrDCmEATyhl_834rmxRg9PJBmRhPY6T7IL58JP9ffUXlF-m65A3H8nOi47dVoAOy9jAPul8C0jS2uZrXYB4zrz_XwXfoonK4lEJtiT86ULd3M3lrUXvEI5kR8ywn3fRBTz5hVRbs0lrgfmFRY_87zELZuBFjSi-pAZTNr_lrAUtBT3Q3h3JyDXHdUJzqoWM-WcszNAZD2wJDV06PpSkNxMOCl6l0BNvUmaY3uLODb5-2yiywasfI9Ue6vKIYEmisNTk48mvbaoIEO34Gg7N1DnvFsO7raoiV_NZ_1KCJDYnxw0jC88Cr0w", }) namespaceList, err := clientSet.CoreV1().Namespaces().List(context.TODO(),metav1.ListOptions{}) if err!=nil{ log.Log.Info(err.Error()) } //namespaces:=GetAllNamespace(clientset) var namespaces []string fmt.Println("******************") for _,nsList := range namespaceList.Items { fmt.Println(nsList.Name) namespaces=append(namespaces, nsList.Name) } fmt.Println("******************") } func NewKubernetesClient(c *Config) (*kubernetes.Clientset, error) { kubeConf := &rest.Config{ Host: fmt.Sprintf("%s:%d", c.Host, c.Port), BearerToken: c.Token, TLSClientConfig: rest.TLSClientConfig{ Insecure: true, }, } return kubernetes.NewForConfig(kubeConf) } // GetAllNamespace get all namespace in cluster. func GetAllNamespace(clientset *kubernetes.Clientset) ([]string){ var namespaces []string namespaceList, err := clientset.CoreV1().Namespaces().List(context.TODO(),metav1.ListOptions{}) if err != nil { log.Log.Info("***************err*****************") log.Log.Info(err.Error()) }else{ //fmt.Printf(namespaces[0]) for _,nsList := range namespaceList.Items { namespaces=append(namespaces, nsList.Name) } } return namespaces }