无法在kubernetes client-go中创建没有复制控制器的部署

时间:2017-03-07 03:44:46

标签: go kubernetes kubernetes-go-client

问题是我无法创建部署规范而不创建复制控制器。我不想使用复制控制器,因为我的应用程序总是只使用一个pod,我想设置重启策略永远不要阻止任何终止容器试图重新启动。

apiVersion: v1
kind: Pod
metadata:
  name: two-containers
spec:
restartPolicy: Never

  volumes:
  - name: shared-data
    emptyDir: {}

  containers:

  - name: nginx-container
    image: nginx
    volumeMounts:
    - name: shared-data
      mountPath: /usr/share/nginx/html

  - name: debian-container
    image: debian
    volumeMounts:
    - name: shared-data
      mountPath: /pod-data
    command: ["/bin/sh"]
    args: ["-c", "echo Hello from the debian container > /pod-data/index.html"]

上面是目标yaml文件,我想用kubernetes client-go实现和部署,但是client-go目前只提供部署复制控制器。

// Define Deployments spec.
    deploySpec := &v1beta1.Deployment{
        TypeMeta: unversioned.TypeMeta{
            Kind:       "Deployment",
            APIVersion: "extensions/v1beta1",
    },
    ObjectMeta: v1.ObjectMeta{
        Name: "binary-search",
    },
    Spec: v1beta1.DeploymentSpec{
        Replicas: int32p(1),
        Template: v1.PodTemplateSpec{
            ObjectMeta: v1.ObjectMeta{
                Name:   appName,
                Labels: map[string]string{"app": appName},
            },
            Spec: v1.PodSpec{
                Containers: []v1.Container{
                    v1.Container{
                        Name:  "nginx-container",
                        Image: "nginx",
                        VolumeMounts: []v1.VolumeMount{
                            v1.VolumeMount{
                                MountPath: "/usr/share/nginx/html",
                                Name:      "shared-data",
                            },
                        },
                    },
                    v1.Container{
                        Name:  "debian-container",
                        Image: "debian",
                        VolumeMounts: []v1.VolumeMount{
                            v1.VolumeMount{
                                MountPath: "/pod-data",
                                Name:      "shared-data",
                            },
                        },
                        Command: []string{
                            "/bin/sh",
                        },
                        Args: []string{
                            "-c",
                            "echo Hello from the debian container > /pod-data/index1.html",
                        },
                    },
                },
                RestartPolicy: v1.RestartPolicyAlways,
                DNSPolicy:     v1.DNSClusterFirst,
                Volumes: []v1.Volume{
                    v1.Volume{
                        Name: "shared-data",
                        VolumeSource: v1.VolumeSource{
                            EmptyDir: &v1.EmptyDirVolumeSource{},
                        },
                    },
                },
            },
        },
    },
    }



// Implement deployment update-or-create semantics.
deploy := c.Extensions().Deployments(namespace)
    _, err := deploy.Update(deploySpec)

有什么建议吗?非常感谢提前!

1 个答案:

答案 0 :(得分:0)

如果您不希望重新启动该服务,则可以直接使用Pod。没有必要使用部署,因为这些只是有意义,如果您想要自动重新启动Pod并更新更新。

代码看起来像这样(未经测试):

podSpec := v1.PodSpec{
    Containers: []v1.Container{
        v1.Container{
            Name:  "nginx-container",
            Image: "nginx",
            VolumeMounts: []v1.VolumeMount{
                v1.VolumeMount{
                    MountPath: "/usr/share/nginx/html",
                    Name:      "shared-data",
                },
            },
        },
        v1.Container{
            Name:  "debian-container",
            Image: "debian",
            VolumeMounts: []v1.VolumeMount{
                v1.VolumeMount{
                    MountPath: "/pod-data",
                    Name:      "shared-data",
                },
            },
            Command: []string{
                "/bin/sh",
            },
            Args: []string{
                "-c",
                "echo Hello from the debian container > /pod-data/index1.html",
            },
        },
    },
    RestartPolicy: v1.RestartPolicyAlways,
    DNSPolicy:     v1.DNSClusterFirst,
    Volumes: []v1.Volume{
        v1.Volume{
            Name: "shared-data",
            VolumeSource: v1.VolumeSource{
                EmptyDir: &v1.EmptyDirVolumeSource{},
            },
        },
    },
}

// Implement deployment update-or-create semantics.
deploy := c.Core().PodsGetter(namespace)
_, err := deploy.Update(podSpec)