这里分类和汇总了欣宸的全部原创(含配套源码):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