目录
一、获取使用restApi调用的token信息
二、client-go操作pod示例
1、获取到客户端
2、创建pod
3、获取test命令空间的所有pod
4、获取某个具体pod的详细信息
5、更新pod
6、删除pod
三、总结
官方参考地址:https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/pod-v1/
一、获取使用restApi调用的token信息
[root@k8s-master01 projects]# kubectl create token postman-sa
eyJhbGciOiJSUzI1NiIsImtpZCI6IkwzUWF1NHdyek9kZUl1NEJMWGFLV3NMenVkUmpxWTZfbzZlZktUTDZWeFkifQ.eyJhdWQiOlsiaHR0cHM6Ly9rdWJlcm5ldGVzLmRlZmF1bHQuc3ZjLmNsdXN0ZXIubG9jYWwiXSwiZXhwIjoxNzMyNjE4OTY0LCJpYXQiOjE3MzI2MTUzNjQsImlzcyI6Imh0dHBzOi8va3ViZXJuZXRlcy5kZWZhdWx0LnN2Yy5jbHVzdGVyLmxvY2FsIiwia3ViZXJuZXRlcy5pbyI6eyJuYW1lc3BhY2UiOiJkZWZhdWx0Iiwic2VydmljZWFjY291bnQiOnsibmFtZSI6InBvc3RtYW4tc2EiLCJ1aWQiOiJjZWNjOTVlOC1hMmM4LTRkZjYtYmY4Ny1lMDg1ZjU0YjI5ODcifX0sIm5iZiI6MTczMjYxNTM2NCwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50OmRlZmF1bHQ6cG9zdG1hbi1zYSJ9.qAs7SZrm90WTGHYCVT_XAZ2q_GS12jOmlM03dsr-pQINcHUCP0QoNS7rELMJZhZF2p5j7kImkHEQyEG0s0xQxrT8Gqy2-USq5YYW3HLW2VOCQYsNaR1ih3EucTgOuSVvVkqelBR3AI6PSTkV1H2q5rik-7BYr7xxyu9A05gl7Cgbg0ZtR9WfKglChpQ1cgFC7OBo0CO1F-mkVZlaXigdnIEQNhhvIO0o-mxrPNFlQ5IMI1LAtXOrKnxxja5XfmiiOh2YohTaMJPfW5CRNwDIvzbP6C8dRoK-sC_jfYS0uMn2epAdzPyXu8s_LNLUxxxAg-BZD6U3OqX_2mK4lKS0KQ
通过使用kubectl create token serviceaccountName来获取到服务账号对应的token信息,进而直接通过client-go来操作k8s集群。
二、client-go操作pod示例
1、获取到客户端
func getClient() *kubernetes.Clientset {// 假设你已经有了tokentoken := "eyJhbGciOiJSUzI1NiIsImtpZCI6IkwzUWF1NHdyek9kZUl1NEJMWGFLV3NMenVkUmpxWTZfbzZlZktUTDZWeFkifQ.eyJhdWQiOlsiaHR0cHM6Ly9rdWJlcm5ldGVzLmRlZmF1bHQuc3ZjLmNsdXN0ZXIubG9jYWwiXSwiZXhwIjoxNzMyNjE1NzMxLCJpYXQiOjE3MzI2MTIxMzEsImlzcyI6Imh0dHBzOi8va3ViZXJuZXRlcy5kZWZhdWx0LnN2Yy5jbHVzdGVyLmxvY2FsIiwia3ViZXJuZXRlcy5pbyI6eyJuYW1lc3BhY2UiOiJkZWZhdWx0Iiwic2VydmljZWFjY291bnQiOnsibmFtZSI6InBvc3RtYW4tc2EiLCJ1aWQiOiJjZWNjOTVlOC1hMmM4LTRkZjYtYmY4Ny1lMDg1ZjU0YjI5ODcifX0sIm5iZiI6MTczMjYxMjEzMSwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50OmRlZmF1bHQ6cG9zdG1hbi1zYSJ9.o_4CGeouCp8OUv7wq_TlpdraoM_zo4TXxBKy-hBWK79KdnU5GttujAe1XX49Ib2Pj1ycGChZUKHYVtsjojKD5evsWjgzaULIJLJ8809OwGZGWbvVeTUaLf9Xoo9jOTuIRR98e3MwdbgeUypstvaQbdLmlPnlG7fs8Od9jfwQBRGttlsBTDqNG0o9QleJyk_hebanh1Z_eMQvUFdYSUc9RrQlSDSNvtAF_h6vhizdQeb_ePj1MPnZOPLUs9a4rbycJ5NtuWrLsDxpxlhrly_XDVUsvU9KqazaNG6Z8lIGgY09vQnDTht_-n0X9UJdQuf_u7BIS111wMHnqC50zDnoMA"// 创建配置config := &rest.Config{Host: "https://192.168.188.101:6443",BearerToken: token,// 可能还需要设置其他字段,如TLSClientConfig等TLSClientConfig: rest.TLSClientConfig{Insecure: true, // 不要在生产环境中使用!},}// 创建客户端clientset, err := kubernetes.NewForConfig(config)if err != nil {// 处理错误log.Fatalf("Error to new client!")}return clientset
}
参数说明:
token为上面步骤获取到数据
host地址为你k8s集群中apiServer的运行地址
2、创建pod
func createPod(clientset *kubernetes.Clientset) {// 定义Podpod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "example-pod",//Namespace: "test",},Spec: v1.PodSpec{Containers: []v1.Container{{Name: "example-container",Image: "nginx:1.15-alpine",},},},}// 创建Podfmt.Println("Creating pod...")podsClient := clientset.CoreV1().Pods("test")result, err := podsClient.Create(context.TODO(), pod, metav1.CreateOptions{})if err != nil {panic(err.Error())}fmt.Printf("Namespace: %s, Name: %s, Status: %s\n", result.Namespace, result.Name, result.Status.Phase)
}
3、获取test命令空间的所有pod
func listPod(clientset *kubernetes.Clientset) {// 创建Podfmt.Println("list pod...")podsClient := clientset.CoreV1().Pods("test")result, err := podsClient.List(context.TODO(), metav1.ListOptions{})if err != nil {panic(err.Error())}for _, pod := range result.Items {fmt.Printf("Namespace: %s, Name: %s, Status: %s\n", pod.Namespace, pod.Name, pod.Status.Phase)}
}
4、获取某个具体pod的详细信息
func getPodDetail(clientset *kubernetes.Clientset) *v1.Pod {// 创建Podfmt.Println("list pod...")podsClient := clientset.CoreV1().Pods("test")pod, err := podsClient.Get(context.TODO(), "example-pod", metav1.GetOptions{})if err != nil {panic(err.Error())}fmt.Printf("Namespace: %s, Name: %s, Status: %s\n", pod.Namespace, pod.Name, pod.Status.Phase)return pod
}
5、更新pod
func updatePod(clientset *kubernetes.Clientset) {pod := getPodDetail(clientset)// Update Pod (e.g., add an annotation)pod.Annotations = map[string]string{"updated-by": "client-go",}// 创建Podfmt.Println("list pod...")podsClient := clientset.CoreV1().Pods("test")updatedPod, err := podsClient.Update(context.TODO(), pod, metav1.UpdateOptions{})if err != nil {log.Fatalf("Error updating Pod: %v", err)}fmt.Printf("Updated Pod %q with annotation.\n", updatedPod.Name)fmt.Printf("Namespace: %s, Name: %s, Status: %s\n", pod.Namespace, pod.Name, pod.Status.Phase)
}
6、删除pod
func deletePod(clientset *kubernetes.Clientset) {// Delete PoddeletePolicy := metav1.DeletePropagationForegroundpodsClient := clientset.CoreV1().Pods("test")err := podsClient.Delete(context.TODO(), "example-pod", metav1.DeleteOptions{PropagationPolicy: &deletePolicy,})if err != nil {log.Fatalf("Error deleting Pod: %v", err)}fmt.Printf("Pod %q deleted.\n", "example-pod")}
三、总结
本文详细介绍了如何通过client-go来对pod进行操作,并且具体演示了对pod的创建、获取列表、获取pod详情、更新pod、已经删除pod的操作。