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

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

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.

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: two-containers
  5. spec:
  6. restartPolicy: Never
  7. volumes:
  8. - name: shared-data
  9. emptyDir: {}
  10. containers:
  11. - name: nginx-container
  12. image: nginx
  13. volumeMounts:
  14. - name: shared-data
  15. mountPath: /usr/share/nginx/html
  16. - name: debian-container
  17. image: debian
  18. volumeMounts:
  19. - name: shared-data
  20. mountPath: /pod-data
  21. command: ["/bin/sh"]
  22. 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.

  1. // Define Deployments spec.
  2. deploySpec := &v1beta1.Deployment{
  3. TypeMeta: unversioned.TypeMeta{
  4. Kind: "Deployment",
  5. APIVersion: "extensions/v1beta1",
  6. },
  7. ObjectMeta: v1.ObjectMeta{
  8. Name: "binary-search",
  9. },
  10. Spec: v1beta1.DeploymentSpec{
  11. Replicas: int32p(1),
  12. Template: v1.PodTemplateSpec{
  13. ObjectMeta: v1.ObjectMeta{
  14. Name: appName,
  15. Labels: map[string]string{"app": appName},
  16. },
  17. Spec: v1.PodSpec{
  18. Containers: []v1.Container{
  19. v1.Container{
  20. Name: "nginx-container",
  21. Image: "nginx",
  22. VolumeMounts: []v1.VolumeMount{
  23. v1.VolumeMount{
  24. MountPath: "/usr/share/nginx/html",
  25. Name: "shared-data",
  26. },
  27. },
  28. },
  29. v1.Container{
  30. Name: "debian-container",
  31. Image: "debian",
  32. VolumeMounts: []v1.VolumeMount{
  33. v1.VolumeMount{
  34. MountPath: "/pod-data",
  35. Name: "shared-data",
  36. },
  37. },
  38. Command: []string{
  39. "/bin/sh",
  40. },
  41. Args: []string{
  42. "-c",
  43. "echo Hello from the debian container > /pod-data/index1.html",
  44. },
  45. },
  46. },
  47. RestartPolicy: v1.RestartPolicyAlways,
  48. DNSPolicy: v1.DNSClusterFirst,
  49. Volumes: []v1.Volume{
  50. v1.Volume{
  51. Name: "shared-data",
  52. VolumeSource: v1.VolumeSource{
  53. EmptyDir: &v1.EmptyDirVolumeSource{},
  54. },
  55. },
  56. },
  57. },
  58. },
  59. },
  60. }
  61. // Implement deployment update-or-create semantics.
  62. deploy := c.Extensions().Deployments(namespace)
  63. _, err := deploy.Update(deploySpec)

Any suggestion? Many thanks in advance!

答案1

得分: 0

如果您不想重新启动服务,可以直接使用Pod。无需使用Deployment,因为只有在您想要自动重新启动Pod和更新发布时才有意义。

代码应该如下所示(未经测试):

  1. podSpec := v1.PodSpec{
  2. Containers: []v1.Container{
  3. v1.Container{
  4. Name: "nginx-container",
  5. Image: "nginx",
  6. VolumeMounts: []v1.VolumeMount{
  7. v1.VolumeMount{
  8. MountPath: "/usr/share/nginx/html",
  9. Name: "shared-data",
  10. },
  11. },
  12. },
  13. v1.Container{
  14. Name: "debian-container",
  15. Image: "debian",
  16. VolumeMounts: []v1.VolumeMount{
  17. v1.VolumeMount{
  18. MountPath: "/pod-data",
  19. Name: "shared-data",
  20. },
  21. },
  22. Command: []string{
  23. "/bin/sh",
  24. },
  25. Args: []string{
  26. "-c",
  27. "echo Hello from the debian container > /pod-data/index1.html",
  28. },
  29. },
  30. },
  31. RestartPolicy: v1.RestartPolicyAlways,
  32. DNSPolicy: v1.DNSClusterFirst,
  33. Volumes: []v1.Volume{
  34. v1.Volume{
  35. Name: "shared-data",
  36. VolumeSource: v1.VolumeSource{
  37. EmptyDir: &v1.EmptyDirVolumeSource{},
  38. },
  39. },
  40. },
  41. }
  42. // 实现部署的更新或创建语义。
  43. deploy := c.Core().PodsGetter(namespace)
  44. _, 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):

  1. podSpec := v1.PodSpec{
  2. Containers: []v1.Container{
  3. v1.Container{
  4. Name: "nginx-container",
  5. Image: "nginx",
  6. VolumeMounts: []v1.VolumeMount{
  7. v1.VolumeMount{
  8. MountPath: "/usr/share/nginx/html",
  9. Name: "shared-data",
  10. },
  11. },
  12. },
  13. v1.Container{
  14. Name: "debian-container",
  15. Image: "debian",
  16. VolumeMounts: []v1.VolumeMount{
  17. v1.VolumeMount{
  18. MountPath: "/pod-data",
  19. Name: "shared-data",
  20. },
  21. },
  22. Command: []string{
  23. "/bin/sh",
  24. },
  25. Args: []string{
  26. "-c",
  27. "echo Hello from the debian container > /pod-data/index1.html",
  28. },
  29. },
  30. },
  31. RestartPolicy: v1.RestartPolicyAlways,
  32. DNSPolicy: v1.DNSClusterFirst,
  33. Volumes: []v1.Volume{
  34. v1.Volume{
  35. Name: "shared-data",
  36. VolumeSource: v1.VolumeSource{
  37. EmptyDir: &v1.EmptyDirVolumeSource{},
  38. },
  39. },
  40. },
  41. }
  42. // Implement deployment update-or-create semantics.
  43. deploy := c.Core().PodsGetter(namespace)
  44. _, 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:

确定