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

huangapple go评论82阅读模式
英文:

unable to create a deployment without replication controller in kubernetes client-go

问题

问题是我无法创建一个没有创建副本控制器的部署规范。我不想使用副本控制器,因为我的应用程序始终只使用一个 Pod,并且我想将重启策略设置为 never,以防止任何终止的容器尝试重新启动。

上面是我想要实现和部署的目标 YAML 文件,我想使用 Kubernetes 的 client-go 进行部署,但是 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

得分: 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{},
            },
        },
    },
}

// 实现部署的更新或创建语义。
deploy := c.Core().PodsGetter(namespace)
_, err := deploy.Update(podSpec)
英文:

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)

huangapple
  • 本文由 发表于 2017年3月7日 11:44:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/42639801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定