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)

}


Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐