首页 > 解决方案 > configmap 端口转发在 kubernetes 多容器 pod 中不起作用

问题描述

下面是包含多个容器的 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

吊舱配置:

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 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 命令访问。

标签: kubernetes

解决方案


据我所知,您无法通过NodePort.

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

你还在评论中发帖:

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)`

因此,我看到您正在尝试从中获取curl集群节点的私有 IP 地址,cloudshell 但这是行不通的。

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

要从外部网络连接到您的应用程序,您需要使用EXTERNAL-IP可以找到正在运行的节点kubectl get no -owide

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

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

有关在 GKE 上公开应用程序的更多信息,请参阅此处的 GKE 文档

让我知道这是否有帮助。


推荐阅读