对不起,如果这是一个幼稚的问题。如果我的理解有误,请指正。
使用此命令创建 POD:
kubectl run nginx --image=nginx --port=8888
我对这个命令的理解,nginx(应用程序)容器将在 8888 端口暴露/可用
kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 10m 10.244.1.2 node01 <none> <none>
curl -v 10.244.1.2:8888 ===> 我想知道为什么会失败?
* Trying 10.244.1.2:8888...
* TCP_NODELAY set
* connect to 10.244.1.2 port 8888 failed: Connection refused
* Failed to connect to 10.244.1.2 port 8888: Connection refused
* Closing connection 0
curl: (7) Failed to connect to 10.244.1.2 port 8888: Connection refused
curl -v 10.244.1.2 ===> 令我惊讶的是这返回了 200 成功响应
* Trying 10.244.1.2:80...
* TCP_NODELAY set
* Connected to 10.244.1.2 (10.244.1.2) port 80 (#0)
> GET / HTTP/1.1
> Host: 10.244.1.2
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
如果应用程序仍然引用默认的 80 端口,我想知道容器端口 8888 的意义? 好的,所以它可以用来将 POD 暴露给外界。
让我们看看,我继续为 POD 创建服务:
kubectl expose pod nginx --port=80 --target-port=8888
kubectl 获取 svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx ClusterIP 10.96.214.161 <none> 80/TCP 13m
$ curl -v 10.96.214.161 ==> 这里默认端口 (80) 不起作用
* Trying 10.96.214.161:80...
* TCP_NODELAY set
* connect to 10.96.214.161 port 80 failed: Connection refused
* Failed to connect to 10.96.214.161 port 80: Connection refused
* Closing connection 0
curl: (7) Failed to connect to 10.96.214.161 port 80: Connection refused
$ curl -v 10.96.214.161:8888 ==> 目标端口也不起作用
* Trying 10.96.214.161:8888...
* TCP_NODELAY set
....waiting forever
我需要使用哪个端口才能使其工作?我错过了什么吗?
答案 0 :(得分:1)
默认情况下,nginx
服务器侦听端口 80。您可以在他们的 docker 映像 ref 中看到它。
对于 kubectl run nginx --image=nginx --port=8888
,您在这里所做的是公开了另一个端口和 80。但服务器仍在侦听 80
端口。
因此,尝试使用目标端口 80
。因此,当您尝试使用端口 80
以外的其他端口时,它不起作用。尝试将 --target-port=8888
设置为 --target-port=80
。
或者,如果您想更改服务器端口,您需要使用 configmap 和 pod 将自定义配置传递给服务器。