【问题标题】:Simple networking in a kubernetes podKubernetes pod 中的简单网络
【发布时间】:2016-09-02 09:41:51
【问题描述】:

我想在 pod 中的 2 个容器和主机之间进行通信,以更好地了解 kubernetes ketworking:

    #DockerfileA
    FROM debian:sid
    RUN /bin/bash -c "apt-get update && apt-get install -y netcat"
    CMD /bin/bash echo hello world | nc 127.0.0.1 40000

    #DockerfileB
    FROM debian:sid
    RUN /bin/bash -c "apt-get update && apt-get install -y netcat"
    EXPOSE 40000
    CMD /bin/bash nc -l 40000 | nc 127.0.0.1 50000

    #my_pod.yml
    apiVersion: v1
    kind: Pod
    metadata:
        name: netcat
        labels:
            app: netcat
    spec:
      containers:
      - name: my_container_a
        image: my_image_a:1
      - name: my_container_b
        image: my_image_b:1
        ports:
        - containerPort: 40000

    #my_service.yml        
    apiVersion: v1
    kind: Service
    metadata:
      name: netcat-service
    spec:
      ports:
      - port: 50000
        targetPort: 50000
        protocol: TCP
      selector:
        app: netcat

在实践中,我希望 containerA 通过 netcat "hello world" 发送到 containerB,然后 containerB 将其转发到主机。

但是当我创建 pod 时,我收到了以下警告(kubectl describe pod netcat):

警告 FailedSync 同步 pod 时出错,正在跳过:[failed to 带有 CrashLoopBackOff 的“my_container_a”的“StartContainer”:“Back-off 10s 重启失败的容器=my_container_a pod=netcat_default(f141c598-1439-11e6-83fc-009c028bec97)",未能 带有 CrashLoopBackOff 的“my_container_b”的“StartContainer”:“Back-off 10s 重启失败的容器=my_container_b pod=netcat_default(f141c598-1439-11e6-83fc-009c028bec97)"

文件有错误吗?

是否需要 netcat 服务来使用 netcat 侦听主机上的“hello world”消息,还是不需要?

我应该在主机的哪个地址:端口上使用 netcat/curl?

谢谢!

【问题讨论】:

    标签: kubernetes dockerfile


    【解决方案1】:

    以下 docker 文件可以在我的机器上运行:

    #Dockerfile A
    FROM debian:sid
    RUN /bin/bash -c "apt-get update && apt-get install -y netcat"
    CMD sleep 10 && echo hello world | nc 127.0.0.1 40000
    
    #Dockerfile B
    FROM debian:sid
    RUN /bin/bash -c "apt-get update && apt-get install -y netcat"
    CMD nc -l -p 40000 | nc <your_host_ip> 50000
    

    我认为您不需要服务。我刚刚在主机上打开了一个终端并运行了“nc -l 50000”。你的 pod.yaml 是正确的。

    关于“Warning FailedSync Error syncing pod”错误信息,看起来很正常。每当容器终止时,我都会看到此消息。在您的情况下,两个容器都因命令失败而终止。

    如果您还不知道,可以使用“kubectl exec netcat -c mycontainera -i -t -- bash -il”进入容器并尝试您的命令。

    【讨论】:

    • 它有效,谢谢!我忘记了 bash 语法中的“-c”,我正在查阅我主机的 netcat OpenBSD 手册,而容器有不同的版本。
    猜你喜欢
    • 2020-02-18
    • 1970-01-01
    • 2021-11-21
    • 2019-04-06
    • 2020-06-16
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 2018-03-06
    相关资源
    最近更新 更多