6

我正在寻找有关如何使用 client-go API 创建/更新服务和端点的指针。似乎找不到任何有关如何执行此操作的示例或文档。

谢谢!萨蒂什

4

2 回答 2

4
   clientset.CoreV1().Services("kube-system").Create(&corev1.Service{
            ObjectMeta: metav1.ObjectMeta{
                Name:                       controllerSVCName,
                Namespace:                  "kube-system",
                Labels: map[string]string{
                    "k8s-app": "kube-controller-manager",
                },
            },
            Spec: corev1.ServiceSpec{
                Ports:                    nil,
                Selector:                 nil,
                ClusterIP:                "",

            },
   })
于 2019-09-13T11:27:04.847 回答
2

基于

对于服务:

serviceClient := clientset.AppsV1().Services(apiv1.NamespaceDefault)

service := &corev1.Service{
        ObjectMeta: metav1.ObjectMeta{
            Name:                       "myservice",
            Namespace:                  "default",
            Labels: map[string]string{
                "app": "myapp",
            },
        },
        Spec: corev1.ServiceSpec{
            Ports:                    nil,
            Selector:                 nil,
            ClusterIP:                "",

        },
})

// Create Service
fmt.Println("Creating service...")
result, err := servicesClient.Create(service)
if err != nil {
    panic(err)
}

对于部署:

deploymentsClient := clientset.AppsV1().Deployments(apiv1.NamespaceDefault)

deployment := &appsv1.Deployment{
    ObjectMeta: metav1.ObjectMeta{
        Name: "demo-deployment",
    },
    Spec: appsv1.DeploymentSpec{
        Replicas: int32Ptr(2),
        Selector: &metav1.LabelSelector{
            MatchLabels: map[string]string{
                "app": "demo",
            },
        },
        Template: apiv1.PodTemplateSpec{
            ObjectMeta: metav1.ObjectMeta{
                Labels: map[string]string{
                    "app": "demo",
                },
            },
            Spec: apiv1.PodSpec{
                Containers: []apiv1.Container{
                    {
                        Name:  "web",
                        Image: "nginx:1.12",
                        Ports: []apiv1.ContainerPort{
                            {
                                Name:          "http",
                                Protocol:      apiv1.ProtocolTCP,
                                ContainerPort: 80,
                            },
                        },
                    },
                },
            },
        },
    },
}

// Create Deployment
fmt.Println("Creating deployment...")
result, err := deploymentsClient.Create(deployment)
if err != nil {
    panic(err)
}
于 2018-12-20T21:51:45.573 回答