[英]Not able to access service by service name on kubernetes
I am using below manifest.我正在使用以下清单。 I am having a simple server which prints pod name on
/hello
.我有一个简单的服务器,它在
/hello
上打印 pod 名称。 Here, I was going through kubernetes documentation and it mentioned that we can access service via service name as well.在这里,我正在浏览 kubernetes 文档,它提到我们也可以通过服务名称访问服务。 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.由于这是
NodePort
类型的服务,我可以使用其中一个节点的 IP 访问它。 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
Edit: I created my own mini k8s cluster and I am doing these operations on the master node.编辑:我创建了自己的迷你 k8s 集群,并在主节点上执行这些操作。
From what I understand when you say当你说的时候,我所理解的
As this is a service of type NodePort, I am able to access it using IP of one of the nodes
由于这是 NodePort 类型的服务,我可以使用其中一个节点的 IP 访问它
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.要使用其名称访问服务,您需要在集群内部。
Below is an example where you use a pod based on centos in order to connect to your service using its name :下面是一个示例,您使用基于 centos 的 pod 以使用其名称连接到您的服务:
# 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
您需要在集群内部,这意味着您可以从另一个 pod 访问它。
kubectl run --generator=run-pod/v1 test-nslookup --image=busybox:1.28 --rm -it -- nslookup servit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.