我有一个非常简单的设置。我在电脑上使用Docker Desktop Kubernetes功能运行Kubernete。
- 有两个吊舱从山上运行:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
labels:
app: my-nginx
spec:
replicas: 2
selector:
matchLabels:
app: my-nginx
template:
metadata:
labels:
app: my-nginx
spec:
containers:
- name: my-nginx
image: nginx:alpine
有另一个pod从命令运行:
kubectl run nginx-standalone --image nginx:alpine
有一项来自yaml的服务:
apiVersion: v1
kind: Service
metadata:
name: nginx-nodeport
spec:
type: NodePort
selector:
app: my-nginx
ports:
- port: 80
targetPort: 80
nodePort: 31000
基本上,该服务是";连接的";由于标签选择器,仅适用于来自yaml部署的pod。
我在做什么:
- I";ssh";进入nginx单机版
- 我安装了curl(nginx单机版内部(
- 我尝试了以下操作(在nginx单机版中(:
curl nginx-nodeport
-运行良好,我得到了正确的响应curl nginx-nodeport:31000
-不工作,我得到curl: (7) Failed to connect to nginx-nodeport port 31000: Connection refused
我不明白为什么第二个命令没有返回成功的HTTP响应。我知道31000
端口可以工作,因为我可以在主机上执行curl nginx-nodeport:31000
。为什么它不能在nginx独立吊舱上工作?
这是预期行为,因为nodePort
31000
正在节点网络接口上侦听,而在pod的网络接口中不存在。如果您想通过kubernetes服务从另一个pod访问一个pod,请使用clusterIP
类型的服务,而不是NodePort
类型的服务。NodePort
类型的服务应该用于向kubernetes集群之外的消费者公开kubernetespod。