這裡分類和彙總了欣宸的全部原創(含配套原始碼):https://github.com/zq2599/blog_demos
curl -LO "https://dl.k8s.io/release/v1.22.8/bin/darwin/arm64/kubectl" \
&& chmod +x ./kubectl \
&& sudo mv ./kubectl /usr/local/bin/kubectl \
&& sudo chown root: /usr/local/bin/kubectl
➜ ~ kubectl version --client
Client Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.8", GitCommit:"7061dbbf75f9f82e8ab21f9be7e8ffcaae8e0d44", GitTreeState:"clean", BuildDate:"2022-03-16T14:10:06Z", GoVersion:"go1.16.15", Compiler:"gc", Platform:"darwin/arm64"}
現在雖然kubectl安裝成功,但還不能遠端連線kubernetes,咱們需要把組態檔搞到手
登入kubernetes所在伺服器,在~/.kube/目錄下有個config檔案,將其下載下來,放在本機的~/.kube/目錄下(沒有目錄就新建)
另外還要注意:如果您的kubernetes環境部署在阿里雲,那麼伺服器可能有多個IP,此時x509證書上如果沒有外網IP,那麼在本地是無法用kubectl連線這個kubernetes的,需要把外網IP新增到證書上去,具體方法就不在本篇展開了,可以參考這位大佬的文章:https://blog.csdn.net/lwlfox/article/details/122718568
現在,在本機執行kubectl命令就能存取遠端kubernetes環境了
➜ ~ kubectl get pod -A
NAMESPACE NAME READY STATUS RESTARTS AGE
calico-apiserver calico-apiserver-bf576f79d-ljtst 1/1 Running 0 105m
calico-apiserver calico-apiserver-bf576f79d-tmmxm 1/1 Running 0 105m
calico-system calico-kube-controllers-78687bb75f-86nm6 1/1 Running 0 106m
calico-system calico-node-njxv4 1/1 Running 0 106m
calico-system calico-typha-59df5f67b9-hmngq 1/1 Running 0 106m
calico-system csi-node-driver-5l6nk 2/2 Running 0 106m
kube-system coredns-78fcd69978-gvzh8 1/1 Running 0 106m
kube-system coredns-78fcd69978-xfftz 1/1 Running 0 106m
kube-system etcd-izwz9h7q9tnbtp2qnzu8prz 1/1 Running 0 107m
kube-system kube-apiserver-izwz9h7q9tnbtp2qnzu8prz 1/1 Running 0 107m
kube-system kube-controller-manager-izwz9h7q9tnbtp2qnzu8prz 1/1 Running 0 107m
kube-system kube-proxy-cqsgp 1/1 Running 0 106m
kube-system kube-scheduler-izwz9h7q9tnbtp2qnzu8prz 1/1 Running 0 107m
tigera-operator tigera-operator-6f669b6c4f-7tssg 1/1 Running 0 106m
go get k8s.io/[email protected]
go get k8s.io/client-go/[email protected]
go get k8s.io/client-go/tools/[email protected]
go get k8s.io/client-go/[email protected]
package main
import (
"context"
"flag"
"fmt"
"path/filepath"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)
func main() {
var kubeconfig *string
// 試圖取到當前賬號的家目錄
if home := homedir.HomeDir(); home != "" {
// 如果能取到,就把家目錄下的.kube/config作為預設組態檔
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
// 如果取不到,就沒有預設組態檔,必須通過kubeconfig引數來指定
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
// 載入組態檔
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
// 用clientset類來執行後續的查詢操作
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
namespace := "kube-system"
// 查詢pod列表
pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
nums := len(pods.Items)
fmt.Printf("There are %d pods in the cluster\n", nums)
// 如果沒有pod就返回了
if nums < 1 {
return
}
// 遍歷列表中的每個pod
for index, pod := range pods.Items {
fmt.Printf("%v. pod name : %v\n", index, pod.Name)
// 用pod name精確搜尋單個pod
podObj, err := clientset.CoreV1().Pods(namespace).Get(context.TODO(), pod.Name, metav1.GetOptions{})
if errors.IsNotFound(err) {
fmt.Printf("Pod %s in namespace %s not found\n", pod.Name, namespace)
} else if statusError, isStatus := err.(*errors.StatusError); isStatus {
fmt.Printf("Error getting pod %s in namespace %s: %v\n",
pod.Name, namespace, statusError.ErrStatus.Message)
} else if err != nil {
panic(err.Error())
} else {
fmt.Printf("Found pod %s in namespace %s\n", podObj.Name, namespace)
}
}
}
There are 7 pods in the cluster
0. pod name : coredns-78fcd69978-gvzh8
Found pod coredns-78fcd69978-gvzh8 in namespace kube-system
1. pod name : coredns-78fcd69978-xfftz
Found pod coredns-78fcd69978-xfftz in namespace kube-system
2. pod name : etcd-izwz9h7q9tnbtp2qnzu8prz
Found pod etcd-izwz9h7q9tnbtp2qnzu8prz in namespace kube-system
3. pod name : kube-apiserver-izwz9h7q9tnbtp2qnzu8prz
Found pod kube-apiserver-izwz9h7q9tnbtp2qnzu8prz in namespace kube-system
4. pod name : kube-controller-manager-izwz9h7q9tnbtp2qnzu8prz
Found pod kube-controller-manager-izwz9h7q9tnbtp2qnzu8prz in namespace kube-system
5. pod name : kube-proxy-cqsgp
Found pod kube-proxy-cqsgp in namespace kube-system
6. pod name : kube-scheduler-izwz9h7q9tnbtp2qnzu8prz
Found pod kube-scheduler-izwz9h7q9tnbtp2qnzu8prz in namespace kube-system