简体   繁体   English

如何使用 kubectl 识别 kubernetes 集群提供者

[英]How to identify kubernetes cluster provider using kubectl

I work on a k8s daemonset whose deployment requires that certain values are changed depending on the k8s cluster provider type (gke, eks, aks, minikube, k3s, kind, self-managed k8s installed using kubeadm on a vm, ... etc).我在一个 k8s 守护程序集上工作,其部署要求根据 k8s 集群提供程序类型更改某些值(gke、eks、aks、minikube、k3s、种类、使用 kubeadm 在 vm 上安装的自我管理 k8s 等) .

What is the right way to identify what is the k8s cluster provider type given that kubectl/kubeconfig is already configured?鉴于kubectl/kubeconfig已配置,识别 k8s 集群提供程序类型的正确方法是什么?

One option is to use kubectl config current-context view :一种选择是使用kubectl config current-context view

CURRENT_CONTEXT_NAME="$(kubectl config current-context view)"
PLATFORM="self-managed"

autoDetectEnvironment(){
    if [[ -z "$CURRENT_CONTEXT_NAME" ]]; then
        echo "no configuration has been provided"
        return
    fi  

    echo "Autodetecting environment"
    if [[ $CURRENT_CONTEXT_NAME =~ ^minikube.* ]]; then
        PLATFORM="minikube"
    elif [[ $CURRENT_CONTEXT_NAME =~ ^gke_.* ]]; then
        PLATFORM="gke"
    elif [[ $CURRENT_CONTEXT_NAME =~ ^kind-.* ]]; then
        PLATFORM="kind"
    elif [[ $CURRENT_CONTEXT_NAME =~ ^k3d-.* ]]; then
        PLATFORM="k3d"
        elif [[ $CURRENT_CONTEXT_NAME =~ ^kubernetes-.* ]]; then
                PLATFORM="self-managed"
        else
                echo "No k8s cluster configured or unknown env!"
                exit 2
        fi  
}

However, this seems hacky and am sure it will not work under all cases.但是,这似乎很老套,并且确信它不会在所有情况下都有效。 For eg, for EKS I could not figure out what regex to use.例如,对于 EKS,我无法弄清楚要使用什么正则表达式。

yes grepping gke is working for me $ kubectl get nodes -o wide | awk '{print $1}' | grep -e "gke"是的 grepping gke 正在为我工作$ kubectl get nodes -o wide | awk '{print $1}' | grep -e "gke" $ kubectl get nodes -o wide | awk '{print $1}' | grep -e "gke" $ kubectl get nodes -o wide | awk '{print $1}' | grep -e "gke" however there should be a fancier way to get the Cloud provider, I also tried kubectl get nodes -o wide that in output of OS-IMAGE column says the provider $ kubectl get nodes -o wide | awk '{print $1}' | grep -e "gke"但是应该有一种更好的方式来获取云提供商,我还尝试kubectl get nodes -o wide that in output of OS-IMAGE column 说提供商

~$ kubectl get nodes -o wide | awk '{print $8,$9,$10,$11}'
OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME 
Container-Optimized OS from Google
Container-Optimized OS from Google
Container-Optimized OS from Google

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM