无法使用NodePort访问本地kubernetes集群中的服务 [英] Can't access service in my local kubernetes cluster using NodePort
问题描述
我有以下清单
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: my-redis
spec:
selector:
matchLabels:
app: my-redis
replicas: 1
template:
metadata:
labels:
app: my-redis
spec:
containers:
- name: my-redis
image: redis
ports:
- name: redisport1
containerPort: 6379
hostPort: 6379
---
apiVersion: v1
kind: Service
metadata:
name: redis-service
labels:
app: my-redis
spec:
type: NodePort
selector:
name: my-redis
ports:
- name: redisport1
port: 6379
targetPort: 6379
nodePort: 30036
protocol: TCP
这是一个重现我的问题的示例.我的目的是创建一个简单的集群,其中包含一个带有Redis容器的Pod,并且应该将其公开给我的本地主机.尽管如此,获取服务仍会为我提供以下输出:
This is a sample that reproduces my problem. My intention here is to create a simple cluster that has a pod with a redis container in it, and it should be exposed to my localhost. Still, get services gives me the following output:
redis-service NodePort 10.107.233.66 <none> 6379:30036/TCP 10s
如果我用LoadBalancer交换NodePort,我会得到一个外部IP,但是端口仍然不起作用.
If I swap NodePort with LoadBalancer, I get an external-ip but still port doesn't work.
能帮我确定为什么我无法将6379端口映射到我的本地主机吗?
Can you help me identify why I'm failing to map the 6379 port to my localhost, please?
谢谢
推荐答案
对于任何对此问题感兴趣的人,我都发现了问题.在Ijaz修复之后,我还需要更改选择器以匹配吊舱中的标签,这是我的错字!
For anybody who's interested in the question, I found the problem. After Ijaz's fix, I also needed to change the selector to match the label in the pod, it was a typo on my end!
pod具有"app = my-redis"标签,但服务选择器具有"name = my-redis".匹配它们可以解决访问问题.
pod has "app=my-redis" tag, but Service selector had "name=my-redis". Matching them fixed the access problem.
这篇关于无法使用NodePort访问本地kubernetes集群中的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!