簡體   English   中英

Google容器群集作為配置

[英]Google container cluster as config

我正在嘗試使用kubernetes go-client和cloud.google.com/go/container。 我使用google cloud go容器包創建集群,然后我想使用go-client在該集群上部署。 go-client給出的群集外示例使用kube配置文件來獲取群集的憑據。 但是因為我剛剛在我的應用程序中創建了這個集群,所以我沒有那個配置文件。

如何使用“google.golang.org/genproto/googleapis/container/v1”群集設置“k8s.io/client-go/rest”配置? 有哪些必填字段? 下面的代碼是我目前擁有的(沒有顯示實際的CA證書)。

func getConfig(cluster *containerproto.Cluster) *rest.Config {
    return &rest.Config{
        Host:     "https://" + cluster.GetEndpoint(),
        TLSClientConfig: rest.TLSClientConfig{
            Insecure: false,
            CAData: []byte(`-----BEGIN CERTIFICATE-----
                ...
                -----END CERTIFICATE-----`),
        },
    }

它導致此錯誤:x509:由未知權限簽名的證書。 所以顯然缺少一些東西。 任何其他方法都非常受歡迎! 提前致謝

ClientCertificate,ClientKey和ClusterCaCertificate需要按此處所述進行解碼

func CreateK8sClientFromCluster(cluster *gkev1.Cluster) {
    decodedClientCertificate, err := base64.StdEncoding.DecodeString(cluster.MasterAuth.ClientCertificate)
    if err != nil {
        fmt.Println("decode client certificate error:", err)
        return
    }
    decodedClientKey, err := base64.StdEncoding.DecodeString(cluster.MasterAuth.ClientKey)
    if err != nil {
        fmt.Println("decode client key error:", err)
        return
    }
    decodedClusterCaCertificate, err := base64.StdEncoding.DecodeString(cluster.MasterAuth.ClusterCaCertificate)
    if err != nil {
        fmt.Println("decode cluster CA certificate error:", err)
        return
    }

    config := &rest.Config{
        Username: cluster.MasterAuth.Username,
        Password: cluster.MasterAuth.Password,
        Host:     "https://" + cluster.Endpoint,
        TLSClientConfig: rest.TLSClientConfig{
            Insecure: false,
            CertData: decodedClientCertificate,
            KeyData:  decodedClientKey,
            CAData:   decodedClusterCaCertificate,
        },
    }

    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
        fmt.Printf("failed to get k8s client set from config: %s\n", err)
        return
    }
}

我在這里回答了一個非常類似的問題: 使用client-go訪問GKE集群之外的Kubernetes GKE集群?

基本上,簡而言之,建議的方法是:

  1. 創建Google Cloud IAM服務帳戶+下載其json密鑰
  2. GOOGLE_APPLICATION_CREDENTIALS env var設置為該key.json
  3. gcloud container clusters describe查找集群的IP地址和CA證書(或者只是從gcloud get-credentials獲取.kube/config文件)
  4. 將這些值傳遞給client-go並使用env var運行程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM