client-go创建service

client-go创建service,第1张

package main

import (
	"context"
	"fmt"

	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

	service := &apiv1.Service{
		ObjectMeta: metav1.ObjectMeta{
			Name: "nginx-svc",
			Labels: map[string]string{
				"app": "nginx",
			},
		},
		Spec: apiv1.ServiceSpec{
			Selector: map[string]string{
				"app": "nginx",
			},

			Ports: []apiv1.ServicePort{
				{
					Name:     "http",
					Port:     80,
					Protocol: apiv1.ProtocolTCP,
				},
			},
		},
	}

	service, err := clientset.CoreV1().Services(namespace).Create(context.TODO(), service, metav1.CreateOptions{})
	fmt.Println(err, service)
}
[root@node66 ~]# kubectl get svc -n devops  | grep nginx
nginx-svc      ClusterIP   10.108.5.7               80/TCP                          23s

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存