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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)