我现在尝试在Kubernetes集群上使用shell (/bin/bash)运行一个简单的容器。

我认为有一种方法可以通过使用伪tty和detach选项(Docker run命令上的-td选项)来保持容器在Docker容器上运行。

例如,

$ sudo docker run -td ubuntu:latest

Kubernetes中有这样的选项吗?

我尝试使用kubectl run-container命令运行一个容器,如下所示:

kubectl run-container test_container ubuntu:latest --replicas=1

但是容器会退出几秒钟(就像使用没有上面提到的选项的docker run命令启动一样)。ReplicationController会重复启动。

有没有办法让容器在Kubernetes上运行,比如docker run命令中的-td选项?


当前回答

最简单的命令,因为它可以为k8s pod manifest永远运行容器:

apiVersion: v1
kind: Pod
metadata:
  name: ubuntu
spec:
  containers:
  - name: ubuntu
    image: ubuntu:latest
    # Just sleep forever
    command: [ "sleep" ]
    args: [ "infinity" ]

其他回答

添加这个:在模板->在规格->在集装箱->在港口和之后的集装箱港口线

    command: ["/bin/sh", "-ec", "while :; do echo '.'; sleep 6 ; done"]

容器意味着运行到完成。您需要为容器提供一个永远不会完成的任务。像这样的东西应该工作:

apiVersion: v1
kind: Pod
metadata:
  name: ubuntu
spec:
  containers:
  - name: ubuntu
    image: ubuntu:latest
    # Just spin & wait forever
    command: [ "/bin/bash", "-c", "--" ]
    args: [ "while true; do sleep 30; done;" ]

在我的例子中,带有initContainer的pod初始化失败。运行docker ps -a,然后docker logs exits -container-id-here给我一个日志消息,kubectl logs podname没有显示。谜团解开了:-)

我做了一个hack,把它放在后台:

[root@localhost ~]# kubectl run hello -it --image ubuntu -- bash &
[2] 128461

执行舱上,你好

[root@localhost ~]# kubectl exec -it hello -- whoami
root
[root@localhost ~]# kubectl exec -it hello -- hostname
hello

获得外壳

[root@localhost ~]# kubectl exec -it hello -- bash
root@hello:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

在Dockerfile中使用这个命令来保持容器在K8s集群中运行:

CMD tail -f /dev/null