client-go创建pods

client-go创建pods,第1张

package main

import (
	"context"
	"fmt"

	appsV1 "k8s.io/api/apps/v1"
	apiv1 "k8s.io/api/core/v1"
	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"
	var replicas int32 = 2
	deployment := &appsV1.Deployment{
		ObjectMeta: metav1.ObjectMeta{
			Name: "nginx",
			Labels: map[string]string{
				"app": "nginx",
			},
		},

		Spec: appsV1.DeploymentSpec{
			Replicas: &replicas,
			//Replicas: int32Ptr(3),
			Selector: &metav1.LabelSelector{
				MatchLabels: map[string]string{
					"app": "nginx",
				},
			},

			Template: apiv1.PodTemplateSpec{

				ObjectMeta: metav1.ObjectMeta{
					Labels: map[string]string{
						"app": "nginx",
					},
				},

				Spec: apiv1.PodSpec{
					Containers: []apiv1.Container{
						{
							Name:  "nginx",
							Image: "nginx:latest",
							Ports: []apiv1.ContainerPort{
								{
									Name:          "http",
									Protocol:      apiv1.ProtocolTCP,
									ContainerPort: 80,
								},
							},
						},
					},
				},
			},
		},
	}

	deployment, err := clientset.AppsV1().Deployments(namespace).Create(context.TODO(), deployment, metav1.CreateOptions{})
	if err != nil {
		panic(err)
	}

	fmt.Println(err, deployment)

}

[root@node66 ~]# kubectl get pods -n devops  | grep nginx
nginx-86b8cc866b-dkxkr          1/1     Running   0               90s
nginx-86b8cc866b-tp74k          1/1     Running   0               90s

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/995051.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存