dongnan1899 2017-03-07 03:44
浏览 42
已采纳

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

The issue is I cannot create a deployment spec without creating replication controller along with it.I would not like to use replication controller because my app always use only one pod and I would like to set restart policy to never to prevent any terminated container tries to restart.

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"]

Above is the target yaml file, which I would like to implement and deploy with kubernetes client-go, however client-go currently only provides deployment with 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)

Any suggestion? Many thanks in advance!

  • 写回答

1条回答 默认 最新

  • dougan1884 2017-03-07 15:15
    关注

    If you don't want the service to be restarted, then you can just use the Pod directly. There is no need to use a Deployment, since these only make sense, if you want to have automatic Pod restarts and roll-outs of updates.

    The code would look somehow like this (not tested):

    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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题