gpt4 book ai didi

go - 使用 kubernetes go 客户端扩展部署副本

转载 作者:行者123 更新时间:2023-12-01 20:02:56 24 4
gpt4 key购买 nike

我正在尝试使用 golang client 扩展部署.我发现的问题是 golang client does not have a scale method for deployments .我不知道我该怎么做。我的想法是进行部署,修改副本,然后进行更新。

package main

import (
"context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
appsv1 "k8s.io/api/apps/v1"
"flag"
"fmt"
"os"
"path/filepath"

)

func main() {
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()

config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err)
}
//get and update replicas
deploymentsClient := clientset.AppsV1().Deployments(apiv1.NamespaceDefault)
deployment, _ := deploymentsClient.Get(context.TODO(), "demo-deployment", metav1.GetOptions{})
deploymentsClient.Update(context.TODO(), deployment, metav1.UpdateOptions{})
}

(示例取自 kubernetes client go github 存储库)

但我不知道这是否是最好的方法。

最佳答案

package main

import (
"context"
"log"
"path/filepath"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)

func main() {
kubeconfig := filepath.Join(homedir.HomeDir(), ".kube", "config")
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
config, err = rest.InClusterConfig()
if err != nil {
log.Fatal(err)
}
}

client, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatal(err)
}

s, err := client.AppsV1().
Deployments("default").
GetScale(context.TODO(), "nginx", metav1.GetOptions{})
if err != nil {
log.Fatal(err)
}

sc := *s
sc.Spec.Replicas = 10

us, err := client.AppsV1().
Deployments("default").
UpdateScale(context.TODO(),
"nginx", &sc, metav1.UpdateOptions{})
if err != nil {
log.Fatal(err)
}

log.Println(*us)
}

关于go - 使用 kubernetes go 客户端扩展部署副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61653702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com