0

问题是我不能在不创建复制控制器的情况下创建部署规范。我不想使用复制控制器,因为我的应用程序总是只使用一个 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目前只提供replication controller的部署。

// 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)

有什么建议吗?提前谢谢了!

4

1 回答 1

0

如果不想重启服务,直接使用 Pod 即可。没有必要使用 Deployment,因为这些只有在您想要自动重启 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)
于 2017-03-07T15:15:42.657 回答