client-go更新镜像
package mainimport ("context""fmt"metav1 "k8s.io/apimachinery/pkg/apis/meta/v1""k8s.io/client-go/kubernetes""k8s.io/client-go/tools/clientcmd"//"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured")func
·
package main
import (
"context"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
//"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
func main() {
configPath := "C:\\Users\\admin\\Desktop\\api-six\\k8s-config-admin"
config, _ := clientcmd.BuildConfigFromFlags("", configPath)
clientset, _ := kubernetes.NewForConfig(config)
namespace := "devops"
name := "nginx"
//定义副本数量
var replicas int32 = 1
//获取deployment
deployment, err := clientset.AppsV1().Deployments(namespace).Get(context.TODO(), name, metav1.GetOptions{})
//修改副本数
deployment.Spec.Replicas = &replicas
//更改镜像
deployment.Spec.Template.Spec.Containers[0].Image = "httpd:alpine"
deployment, err = clientset.AppsV1().Deployments(namespace).Update(context.TODO(), deployment, metav1.UpdateOptions{})
fmt.Println(err, deployment)
}
更多推荐
已为社区贡献82条内容
所有评论(0)