无法在kubernetes上按服务名称访问服务 [英] Not able to access service by service name on kubernetes

查看:127
本文介绍了无法在kubernetes上按服务名称访问服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下清单.我有一台简单的服务器,该服务器在/hello 上打印窗格名称.在这里,我正在阅读kubernetes文档,它提到我们也可以通过服务名称访问服务.但这对我不起作用.由于这是 NodePort 类型的服务,因此我可以使用其中一个节点的IP来访问它.我的清单有问题吗?

I am using below manifest. I am having a simple server which prints pod name on /hello. Here, I was going through kubernetes documentation and it mentioned that we can access service via service name as well. But that is not working for me. As this is a service of type NodePort, I am able to access it using IP of one of the nodes. Is there something wrong with my manifest?

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myhttpserver
  labels:
    day: zero
    name: httppod
spec:
  replicas: 1
  selector:
    matchLabels:
      name: httppod
      day: zero
  template:
    metadata:
      labels:
        day: zero
        name: httppod
    spec:
      containers:
        - name: myappcont
          image: agoyalib/trial:tryit
          imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
  name: servit
  labels:
    day: zeroserv
spec:
  type: NodePort
  selector:
    day: zero
    name: httppod
  ports:
    - name: mine
      port: 8080
      targetPort: 8090

我创建了自己的mini k8s集群,并且正在主节点上执行这些操作.

I created my own mini k8s cluster and I am doing these operations on the master node.

推荐答案

根据我的理解,

由于这是NodePort类型的服务,因此我可以使用其中一个节点的IP来访问它

As this is a service of type NodePort, I am able to access it using IP of one of the nodes

您正在从群集外部访问服务.这就是为什么您无法使用其名称来访问它.

You're accessing your service from outside your cluster. That's why you can't access it using its name.

要使用服务名称访问服务,您需要位于集群内部.

To access a service using its name, you need to be inside the cluster.

下面是一个示例,其中您使用基于centos的pod来使用其名称连接到服务:

Below is an example where you use a pod based on centos in order to connect to your service using its name :

# Here we're just creating a pod based on centos
$ kubectl run centos --image=centos:7 --generator=run-pod/v1 --command sleep infinity

# Now let's connect to that pod 
$ kubectl exec centos -ti bash
[root@centos /]# curl servit:8080/hello

这篇关于无法在kubernetes上按服务名称访问服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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