kubernetes - 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 访问的页面上
现在,这里的 myhost 是什么?而且,我知道暴露的端口是 32636
但是,我无法从浏览器或 curl /wget 命令访问。
解决方案
据我所知,您无法通过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 文档。
让我知道这是否有帮助。
推荐阅读
- android - WindowInsetsCompat.isVisible(WindowInsetsCompat.Type.ime()) 为 Android 23-29 返回 true,但未打开键盘
- javascript - 无法通过 fetch javascript 向 django rest api 发出 POST 请求
- typescript - 如何保护柯里化函数中参数的顺序?
- wordpress - 根据复选框或选择创建第二个条目 - 重力形式
- amazon-web-services - 将 EC2 t2.medium 迁移到 t2.large ssh 问题
- python - UnboundLocalError:赋值前引用的局部变量()
- flutter - my-json-server.typicode.com 服务是否允许您免费创建自己的 api?
- python - 如果从 Pyinstaller 编译的应用程序调用,可执行文件找不到库
- c++ - 如何在 Eclipse 中从我的项目中包含 .cpp 和 .h 文件
- c# - 将布尔值输出到控制台