无法使用 NodePort 访问我本地 kubernetes 集群中的服务 [英] Can't access service in my local kubernetes cluster using NodePort

查看:120
本文介绍了无法使用 NodePort 访问我本地 kubernetes 集群中的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个清单如下

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 修复之后,我还需要更改选择器以匹配 pod 中的标签,这是我的一个错字!

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆