为什么 nginx 入口 minikube 插件 hostPort 工作但 hostPort 不适用于任何其他插件?

Why does nginx ingress minikube plugin hostPort work but hostPort does not work for any other plugin?

在 minikube 中有一个 nginx 入口插件,它使用 80 和 443 的主机端口。显然来自外部的流量可以很好地到达这些端口。但是,如果您使用带有 hostPort: 9999 的容器创建 pod,例如 telnet $(minikube ip) 9999 会得到以下结果:

Trying 192.168.99.165...
telnet: connect to address 192.168.99.165: Connection refused
telnet: Unable to connect to remote host

nginx ingress controller 是不是在做一些特殊的魔法?如果是这样,我还可以使用什么魔法?

请不要回答有关使用 NodePort 的问题。

没有什么特别的魔法。我怀疑您正在收到该 telnet 响应,因为在设置了 hostPort: 9999 的容器中,端口 9999 上没有任何内容正在侦听。

运行 minikube ssh 并查看 netstat -nlt,您会在那里看到您的端口 9999。尝试 运行 监听开放主机端口的真实服务,它应该可以工作,例如

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    run: redis
  name: redis
spec:
  replicas: 1
  selector:
    matchLabels:
      run: redis
  template:
    metadata:
      labels:
        run: redis
    spec:
      containers:
      - image: redis
        imagePullPolicy: Always
        name: redis
        ports:
        - containerPort: 6379
          hostPort: 6379
          protocol: TCP

来自我的终端:

> telnet $(minikube ip) 6379
Trying 192.168.99.189...
Connected to 192.168.99.189.
Escape character is '^]'.

如果 有东西在端口 9999 上侦听,则 Kubernetes 在主机和容器之间设置代理的方式可能存在问题。您可以查找 docker-proxy 进程来检查:

$ ps aux | grep docker-proxy
root      3579  0.0  0.0   3668  1768 ?        Sl   14:43   0:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 10000 -container-ip 172.17.0.2 -container-port 10000
root     19862  0.0  0.0   9240   476 pts/1    S+   16:21   0:00 grep docker-proxy
root     23466  0.0  0.0   3668  1768 ?        Sl   15:20   0:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 18080 -container-ip 172.17.0.9 -container-port 18080
root     23480  0.0  0.0   3668  1768 ?        Sl   15:20   0:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 443 -container-ip 172.17.0.9 -container-port 443
root     23494  0.0  0.0   3668  1676 ?        Sl   15:20   0:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 80 -container-ip 172.17.0.9 -container-port 80
root     25840  0.0  0.0   3668  1768 ?        Sl   15:24   0:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 9999 -container-ip 172.17.0.10 -container-port 9999
$