[英]Not able to access service by service name on kubernetes
我正在使用以下清单。 我有一个简单的服务器,它在/hello
上打印 pod 名称。 在这里,我正在浏览 kubernetes 文档,它提到我们也可以通过服务名称访问服务。 但这对我不起作用。 由于这是NodePort
类型的服务,我可以使用其中一个节点的 IP 访问它。 我的清单有问题吗?
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
编辑:我创建了自己的迷你 k8s 集群,并在主节点上执行这些操作。
当你说的时候,我所理解的
由于这是 NodePort 类型的服务,我可以使用其中一个节点的 IP 访问它
您正在从集群外部访问您的服务。 这就是您无法使用其名称访问它的原因。
要使用其名称访问服务,您需要在集群内部。
下面是一个示例,您使用基于 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.