部署未创建

时间:2017-11-08 16:41:12

标签: kubernetes kubernetes-go-client

我正在尝试从client-go创建部署,但它不会创建并抛出错误

the server could not find the requested resource

我的client-go版本: 4.0.0

我的Kubernetes版本是:

客户端版本:

version.Info{Major:"1", Minor:"6", GitVersion:"v1.6.0", GitCommit:"fff5156092b56e6bd60fff75aad4dc9de6b6ef37", GitTreeState:"clean", BuildDate:"2017-03-28T16:36:33Z", GoVersion:"go1.7.5", Compiler:"gc", Platform:"linux/amd64"}

服务器版本:

version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.3", GitCommit:"029c3a408176b55c30846f0faedf56aae5992e9b", GitTreeState:"clean", BuildDate:"2017-02-15T06:34:56Z", GoVersion:"go1.7.4", Compiler:"gc", Platform:"linux/amd64"}

我的示例代码是

package main

import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
intstr "k8s.io/apimachinery/pkg/util/intstr"
kube "k8s.io/client-go/kubernetes"
v1 "k8s.io/client-go/pkg/api/v1"
appsv1beta1 "k8s.io/client-go/pkg/apis/apps/v1beta1"
rest "k8s.io/client-go/rest"
"net"
)

var (
KubeMasterIP  = "x.x.x.x"
Port          = "xxxx"
UserName      = "xxxxx"
Password      = "xxxxxxxxxx"
TLSValue      = true
Protocol      = "https"
NameSpaceName = "test-namespace"
)

func main() {

fmt.Println("***************************")
buildAndDeployApp()
fmt.Println("***************************")
}

func buildAndDeployApp() {

tlsClientConfig := rest.TLSClientConfig{}
tlsClientConfig.Insecure = TLSValue
fmt.Println("HostPath: ", net.JoinHostPort(KubeMasterIP, Port))
restConfig := &rest.Config{
    Host:            net.JoinHostPort(KubeMasterIP, Port),
    Username:        UserName,
    Password:        Password,
    TLSClientConfig: tlsClientConfig,
}

cSet, err := kube.NewForConfig(restConfig)
if err != nil {
    fmt.Println("Error in Kube clientSet : ", err.Error())
}

deploy, err := cSet.AppsV1beta1().Deployments(NameSpaceName).Create(BuildDeployment())
fmt.Println("Deploy Output: ", deploy)
fmt.Println("Error: ", err)
}

func int32Ptr(i int32) *int32 { return &i }

func BuildDeployment() *appsv1beta1.Deployment {

return &appsv1beta1.Deployment{
    ObjectMeta: metav1.ObjectMeta{
        Name: "test-deploy",
    },
    Spec: appsv1beta1.DeploymentSpec{
        Replicas: int32Ptr(1),
        Strategy: appsv1beta1.DeploymentStrategy{
            Type: "RollingUpdate",
            RollingUpdate: &appsv1beta1.RollingUpdateDeployment{
                MaxSurge: &intstr.IntOrString{
                    IntVal: 1,
                },
                MaxUnavailable: &intstr.IntOrString{
                    IntVal: 1,
                },
            },
        },
        MinReadySeconds:      int32(5),
        RevisionHistoryLimit: int32Ptr(5),
        Template: v1.PodTemplateSpec{
            ObjectMeta: metav1.ObjectMeta{
                Labels: map[string]string{
                    "app": "demo",
                },
            },
            Spec: v1.PodSpec{
                Containers: []v1.Container{
                    {
                        Name:  "web",
                        Image: "nginx:1.13",
                        Ports: []v1.ContainerPort{
                            {
                                Name:          "http",
                                Protocol:      v1.ProtocolTCP,
                                ContainerPort: 80,
                            },
                        },
                        ImagePullPolicy: "Always",
                    },
                },
                RestartPolicy: "Always",
            },
        },
    },
}
}

1 个答案:

答案 0 :(得分:1)

apps / v1beta1在1.5中不起作用,从1.6支持。我使用extensions / v1beta1来访问1.5中的部署。