【问题标题】:configmap port forward doesn't work in kubernetes multicontainer podconfigmap 端口转发在 kubernetes 多容器 pod 中不起作用
【发布时间】:2020-02-13 00:44:23
【问题描述】:

下面是包含多个容器的 pod 的 configMap 文件。 端口号 80 暴露给外部世界,然后它将重定向到在 pod 中运行的另一个容器的端口 5000。

apiVersion: v1
kind: ConfigMap
metadata:
  name: mc3-nginx-conf
data:
  nginx.conf: |-
    user  nginx;
    worker_processes  1;

    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;

    events {
        worker_connections  1024;
    }

    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;

        sendfile        on;
        keepalive_timeout  65;

        upstream webapp {
            server 127.0.0.1:5000;
        }

        server {
            listen 80;

            location / {
                proxy_pass         http://webapp;
                proxy_redirect     off;
            }
        }
    }


$kubectl apply -f confimap.yaml

pod 配置:

apiVersion: v1
kind: Pod
metadata:
  name: mc3
  labels:
    app: mc3
spec:
  containers:
  - name: webapp
    image: training/webapp
  - name: nginx
    image: nginx:alpine
    ports:
    - containerPort: 80
    volumeMounts:
    - name: nginx-proxy-config
      mountPath: /etc/nginx/nginx.conf
      subPath: nginx.conf
  volumes:
  - name: nginx-proxy-config
    configMap:
      name: mc3-nginx-conf

步骤 3. 使用 NodePort 服务公开 Pod:

$ kubectl expose pod mc3 --type=NodePort --port=80
service "mc3" exposed

步骤 4. 识别节点上转发到 Pod 的端口:

$ kubectl describe service mc3

Name:                     mc3
Namespace:                default
Labels:                   app=mc3
Annotations:              <none>
Selector:                 app=mc3
Type:                     NodePort
IP:                       100.68.152.108
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  32636/TCP
Endpoints:                100.96.2.3:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

但我无法执行 curl

$ curl 100.96.2.3:80

$ curl http://100.96.2.3:80

$ curl http://100.96.2.3:32636

所以,我想知道为什么这个重定向不起作用。

来源:https://www.mirantis.co.jp/blog/multi-container-pods-and-container-communication-in-kubernetes/

它写在我们可以使用url访问的页面上

http://myhost:

现在,这里的 myhost 是什么? 而且,我知道暴露的端口是 32636

但是,我无法从浏览器或 curl /wget 命令访问。

【问题讨论】:

  • 您在什么平台上进行部署,您的节点端口是否暴露在互联网上?您是否使用堡垒从提供程序内部卷曲?
  • 我在 google cloud shell 上执行。 Nodeport 不暴露在互联网上,而是暴露在 pod 之外的本地环境中。您可以参考还显示图表的链接。单pod多容器架构,容器端口转发的地方。所以从谷歌云外壳本身我正在执行 curl 命令和 wget 命令。
  • 好的,所以第三种形式是好的...现在你有没有ipaliasing?
  • 我实际上尝试过你配置映射、pod 和服务,它们对我来说工作正常,你可能使用了错误的 IP 地址,你应该卷曲到的地址是你的集群之一的 IP 地址节点
  • 你应该添加到你的问题,outout:Kubectl get all,也许你的 pod 还没有运行。

标签: kubernetes


【解决方案1】:

据我所知,您无法通过 NodePort 连接您的应用程序。

在您发布的 cmets 中: I am executing on google cloud shell,所以我假设你在 GKE 上运行。

您还在 cmets 中发帖:

XXXXX@cloudshell:~ (pubsub-quickstart-XXXXX)$ curl -v 10.59.242.245:31357 * Rebuilt URL to: 10.59.242.245:31357 * Trying 10.59.242.245... * TCP_NODELAY set * connect to 10.59.242.245 port 31357 failed: Connection timed out * Failed to connect to 10.59.242.245 port 31357: Connection timed out * Closing connection 0 curl: (7)`

所以我看到您正在尝试从 cloudshell 获取集群节点的私有 IP 地址 curl 这将不起作用。

无法通过来自cloudshell 的私有地址连接到节点 因为这些实例位于不同的网络中(彼此分离)。

要从外部网络连接到您的应用程序,您需要使用运行 kubectl get no -owide 的节点中的 EXTERNAL-IP's

第二件事(非常重要)是创建防火墙规则以允许进入该端口的流量,例如使用 gcloud cli:

gcloud compute firewall-rules create test-node-port --allow tcp:[NODE_PORT]

有关在 GKE 上公开应用程序的更多信息,请参阅GKE documentation here

如果有帮助,请告诉我。

【讨论】:

  • 谢谢@HelloWorld。高超!!添加防火墙规则后得到结果。
  • 乐于助人。如果此答案或任何其他答案解决了您的问题,请将其标记为已接受。
猜你喜欢
  • 2021-08-29
  • 1970-01-01
  • 2019-01-28
  • 1970-01-01
  • 2020-02-08
  • 2019-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多