导语:更新deployment的各个参数配置
仅修改replicas个数为1
package main
import (
"context"
"fmt"
"log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
configPath := "./config"
config, err := clientcmd.BuildConfigFromFlags("", configPath)
if err != nil {
log.Fatal(err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err)
}
namespace := "default"
name := "nginx"
// 获取名为nginx的deployment信息
deploymentList, err := clientset.AppsV1().Deployments(namespace).Get(context.TODO(), name, metav1.GetOptions{})
fmt.Println(err, deploymentList)
var replicas int32 = 1
deploymentList.Spec.Replicas = &replicas
newdeploymentList, err := clientset.AppsV1().Deployments(namespace).Update(context.TODO(), deploymentList, metav1.UpdateOptions{})
fmt.Println(err, newdeploymentList)
}
go run editDeploy.go
package main
import (
"context"
"fmt"
"log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
configPath := "./config"
config, err := clientcmd.BuildConfigFromFlags("", configPath)
if err != nil {
log.Fatal(err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err)
}
namespace := "default"
name := "nginx"
// 获取名为nginx的deployment信息
deploymentList, err := clientset.AppsV1().Deployments(namespace).Get(context.TODO(), name, metav1.GetOptions{})
fmt.Println(err, deploymentList)
// 设置replicas的个数
var replicas int32 = 1
deploymentList.Spec.Replicas = &replicas
// 设置container 0的镜像 这里要注意只更新了一个容器的镜像
deploymentList.Spec.Template.Spec.Containers[0].Image = "nginx:1.20.2"
newdeploymentList, err := clientset.AppsV1().Deployments(namespace).Update(context.TODO(), deploymentList, metav1.UpdateOptions{})
fmt.Println(err, newdeploymentList)
}
通过scale进行replicas的扩容
package main
import (
"context"
"fmt"
"log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
configPath := "./config"
config, err := clientcmd.BuildConfigFromFlags("", configPath)
if err != nil {
log.Fatal(err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err)
}
namespace := "default"
name := "nginx"
// 获取名为nginx的deployment信息
deploymentList, err := clientset.AppsV1().Deployments(namespace).Get(context.TODO(), name, metav1.GetOptions{})
fmt.Println(err, deploymentList)
scale, err := clientset.AppsV1().Deployments(namespace).GetScale(context.TODO(), name, metav1.GetOptions{})
scale.Spec.Replicas = 2
fmt.Println(err, scale)
newslale, err := clientset.AppsV1().Deployments(namespace).UpdateScale(context.TODO(), name, scale, metav1.UpdateOptions{})
fmt.Println(err, newslale)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)