亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法加載集群內配置,必須定義 KUBERNETES_SERVICE_HOST

無法加載集群內配置,必須定義 KUBERNETES_SERVICE_HOST

Go
精慕HU 2022-07-11 15:44:37
我正在開發一個動態的 kubernetes 通知器來監視我的 kubernetes 集群中的事件和所有 kubernetes 組件的發現。但是,當我嘗試KUBECONFIG通過該InClusterConfig方法訪問時,出現以下錯誤:// go run main.goFATA[0000] could not get config                          error="unable to load in-cluster configuration, KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT must be defined"exit status 1我在 github 上的 kubernetes repo 以及 stackoverflow 上發現了與此相關的各種類似問題,但找不到任何解決方案或解決方法。[ kubernetes 問題, kubernetes 問題, stackoverflow 類似問題, stackoverflow 類似問題]以下是 go 代碼和 go.mod 文件去代碼:go.mod 文件:module discovery-testgo 1.15require (    github.com/googleapis/gnostic v0.5.3 // indirect    github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect    github.com/imdario/mergo v0.3.11 // indirect    github.com/peterbourgon/diskv v2.0.1+incompatible // indirect    github.com/sirupsen/logrus v1.7.0    golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 // indirect    golang.org/x/net v0.0.0-20201029055024-942e2f445f3c // indirect    golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43 // indirect    golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect    k8s.io/apimachinery v0.17.0    k8s.io/client-go v0.17.0    k8s.io/klog v1.0.0 // indirect    k8s.io/utils v0.0.0-20201027101359-01387209bb0d // indirect)
查看完整描述

1 回答

?
尚方寶劍之說

TA貢獻1788條經驗 獲得超4個贊

他的評論幫助我弄清楚我正試圖從集群外部獲取集群配置,該配置在我無法訪問集群的本地計算機 (127.0.0.1) 上引導程序。


此外,我試圖弄清楚如何從集群外部訪問集群,發現InClusterConfig用于在集群內部運行的用例,當在集群外部運行時,可以使用如下內容:


//go run main.go

package main


import (

    "os"

    "os/signal"

  //"context"

    "flag"

    //"fmt"

    "path/filepath"

    //"time"


    "github.com/sirupsen/logrus"

    v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

    "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

    "k8s.io/apimachinery/pkg/runtime/schema"

    "k8s.io/client-go/dynamic"

    "k8s.io/client-go/dynamic/dynamicinformer"

    //"k8s.io/client-go/rest"

    "k8s.io/client-go/tools/cache"

    "k8s.io/client-go/tools/clientcmd"

  "k8s.io/client-go/util/homedir"

)


func main() {

  //kubeconfig := os.Getenv("KUBECONFIG")

  

  var kubeconfig *string

    if home := homedir.HomeDir(); home != "" {

        kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")

    } else {

        kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")

    }

    flag.Parse()

  

    cfg, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)

    if err != nil {

        logrus.WithError(err).Fatal("could not get config")

    }


    // Grab a dynamic interface that we can create informers from

    dc, err := dynamic.NewForConfig(cfg)

    if err != nil {

        logrus.WithError(err).Fatal("could not generate dynamic client for config")

    }


    // Create a factory object that we can say "hey, I need to watch this resource"

    // and it will give us back an informer for it

    f := dynamicinformer.NewFilteredDynamicSharedInformerFactory(dc, 0, v1.NamespaceAll, nil)


    // Retrieve a "GroupVersionResource" type that we need when generating our informer from our dynamic factory

    gvr, _ := schema.ParseResourceArg("deployments.v1.apps")


    // Finally, create our informer for deployments!

    i := f.ForResource(*gvr)


    stopCh := make(chan struct{})

    go startWatching(stopCh, i.Informer())


    sigCh := make(chan os.Signal, 0)

    signal.Notify(sigCh, os.Kill, os.Interrupt)


    <-sigCh

    close(stopCh)

}


func startWatching(stopCh <-chan struct{}, s cache.SharedIndexInformer) {

    handlers := cache.ResourceEventHandlerFuncs{

        AddFunc: func(obj interface{}) {

            u := obj.(*unstructured.Unstructured)


            logrus.WithFields(logrus.Fields{

                "name":      u.GetName(),

                "namespace": u.GetNamespace(),

                "labels":    u.GetLabels(),

            }).Info("received add event!")

        },

        UpdateFunc: func(oldObj, obj interface{}) {

            logrus.Info("received update event!")

        },

        DeleteFunc: func(obj interface{}) {

            logrus.Info("received update event!")

        },

    }


    s.AddEventHandler(handlers)

    s.Run(stopCh)

}


查看完整回答
反對 回復 2022-07-11
  • 1 回答
  • 0 關注
  • 1052 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號