k8s主节点无法通过虚拟IP访问Pod或服务

时间:2020-02-05 15:31:27

标签: kubernetes

主节点无法通过虚拟ip访问服务或Pod,网络插件绒布工作正常。

[root@www ~]# clear
[root@www ~]# kubectl get pod --all-namespaces -o wide
NAMESPACE     NAME                          READY   STATUS    RESTARTS   AGE     IP              NODE               NOMINATED NODE   READINESS GATES
default       java-demo-c7765d5cd-kfglv     1/1     Running   1          3h48m   10.244.1.13     www.server03.com   <none>           <none>
default       java-demo-c7765d5cd-pcdjk     1/1     Running   1          3h48m   10.244.0.12     www.server02.com   <none>           <none>
kube-system   coredns-68d7b6f657-mn7fx      1/1     Running   1          6d17h   10.244.1.14     www.server03.com   <none>           <none>
kube-system   kube-flannel-ds-amd64-f8hd2   1/1     Running   3          6d19h   192.168.254.5   www.server02.com   <none>           <none>
kube-system   kube-flannel-ds-amd64-h9xsq   1/1     Running   2          6d19h   192.168.254.6   www.server03.com   <none>           <none>
[root@www ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
java-demo    NodePort    10.0.0.153   <none>        80:30018/TCP   3h18m
kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP        6d23h
[root@www ~]# curl 10.0.0.153
curl: (7) Failed connect to 10.0.0.153:80; 拒绝连接
[root@www ~]# curl 10.244.1.14:8080
curl: (7) Failed connect to 10.244.1.14:8080; 拒绝连接
[root@www ~]# ping 10.0.0.153 
PING 10.0.0.153 (10.0.0.153) 56(84) bytes of data.
--- 10.0.0.153 ping statistics ---
119 packets transmitted, 0 received, 100% packet loss, time 118011ms

节点可以访问服务虚拟ip,在节点上执行指令如下:

[root@www ~]# clear
[root@www ~]# ping 10.0.0.153
PING 10.0.0.153 (10.0.0.153) 56(84) bytes of data.
64 bytes from 10.0.0.153: icmp_seq=1 ttl=64 time=0.124 ms
64 bytes from 10.0.0.153: icmp_seq=2 ttl=64 time=0.040 ms
64 bytes from 10.0.0.153: icmp_seq=3 ttl=64 time=0.038 ms
64 bytes from 10.0.0.153: icmp_seq=4 ttl=64 time=0.072 ms
64 bytes from 10.0.0.153: icmp_seq=5 ttl=64 time=0.039 ms
^C
--- 10.0.0.153 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4000ms
rtt min/avg/max/mdev = 0.038/0.062/0.124/0.034 ms
[root@www ~]# 

java-demo.yaml


apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: java-demo
  name: java-demo
spec:
  replicas: 2
  selector:
    matchLabels:
      app: java-demo
  template:
    metadata:
      labels:
        app: java-demo
    spec:
      containers:
      - image: java-demo:v1
        name: java-demo

service.yaml


apiVersion: v1
kind: Service
metadata:
  labels:
    app: java-demo
  name: java-demo
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080
    nodePort: 30018
  selector:
    app: java-demo
  type: NodePort

此服务可以在暴露后正常在节点上访问,并且pod容器只是主节点上不可访问的虚拟IP。 请帮助我,谢谢!

1 个答案:

答案 0 :(得分:0)

您使用NodePort类型公开Deployment,将无法通过虚拟IP访问您的服务。

在Kubernetes文档中,我们可以阅读:

对于应用程序的某些部分(例如前端),您可能希望将服务公开到群集外部的外部IP地址上。

Kubernetes ServiceTypes允许您指定所需的服务类型。默认值为ClusterIP

Type值及其行为为:

  • ClusterIP:在群集内部IP上公开服务。选择此值将使服务仅可从群集内访问。这是默认的ServiceType
  • NodePort:通过静态端口(NodePort)在每个节点的IP上公开服务。 ClusterIP服务路由到的NodePort服务是自动创建的。您可以通过请求NodePort从群集外部与<NodeIP>:<NodePort>服务联系。
  • LoadBalancer:使用云提供商的负载平衡器在外部公开服务。自动创建外部负载均衡器路由到的NodePortClusterIP服务。
  • ExternalName:通过返回带有其值的externalName记录,将服务映射到foo.bar.example.com字段的内容(例如CNAME)。没有设置任何代理。

您的Deployemnt也丢失了containerPort。这是文档如何Create a Deployment的链接。

也请考虑阅读Connecting Applications with Services,因为它提供了不同类型的示例。