How can I access the Kubernetes service through Cl

2020-06-04 05:03发布

I am trying to create Kubernetes cluster using three VMs(Master – 10.x.x.4, Node1 – 10.x.x.150, Node2 – 10.x.x.160).

I was able to create the guestbook application successfully following this link: http://kubernetes.io/v1.0/examples/guestbook/. Only one change I made to frontend-service.yaml: to use NodePort. I can access the frontend service using nodes IP and port number(10.x.x.150:30724 or 10.x.x.160:30724). So everything is working as expected but I am not able to access the frontend service using ClusterIP address(in my case 10.x.x.79).

My understanding of NodePort is that the service can be accessed through cluster IP and also on a port on each node of the cluster. How can I access the service through ClusterIP so that I don’t have to access the each node? Am I missing something here?

service and pod details

$sudo kubectl describe service frontend

Name:                   frontend
Namespace:              default
Labels:                 name=frontend
Selector:               name=frontend
Type:                   NodePort
IP:                     10.x.x.79
Port:                   <unnamed>       80/TCP
NodePort:               <unnamed>       30724/TCP
Endpoints:              172.x.x.13:80,172.x.x.14:80,172.x.x.11:80
Session Affinity:       None

No events.

$sudo kubectl describe pod frontend-2b5us

Name:                           frontend-2b5us
Namespace:                      default
Image(s):                       gcr.io/google_samples/gb-frontend:v3
Node:                           10.x.x.150/10.x.x.150
Labels:                         name=frontend
Status:                         Running
Reason:
Message:
IP:                             172.x.x.11
Replication Controllers:        frontend (3/3 replicas created)
Containers:
  php-redis:
    Image:              gcr.io/google_samples/gb-frontend:v3
    State:              Running
      Started:          Fri, 30 Oct 2015 04:00:40 -0500
    Ready:              True
    Restart Count:      0

I tried to search but would not find any solution for my exact problem but I did find similar problem that looks like for GCE.

Why can't I access my Kubernetes service via its IP?

2条回答
聊天终结者
2楼-- · 2020-06-04 05:34

From where are you trying to access clusterIP? The clusterIP (by default) only works from within the cluster. It is a virtual IP, not routed.

查看更多
来,给爷笑一个
3楼-- · 2020-06-04 05:40

You do not have ClusterIP service. You do have a NodePort service. To access it, you connect to the NodePort on any of your nodes in the cluster, as you've already discovered. You do get load-balancing here. Even though you connect to a cluster node, the pod you get does not necessarily run on that particular node.

Read the relevant section in the documentation at https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services---service-types to learn about additional service types. You probably do not want NodePort on GCP.

Talking about ClusterIP. To access a ClusterIP service for debugging purposes, you can run kubectl port-forward. You will not actually access the service, but you will directly connect to one of the pods.

For example

kubectl port-forward frontend-2b5us 80 8080

Now connect to localhost:8080

More sophisticated command, which discovers the port on its own, given namespace -n weave and a selector. Taken from https://www.weave.works/docs/scope/latest/installing/

kubectl port-forward -n weave \
    "$(kubectl get -n weave pod \
         --selector=weave-scope-component=app \
         -o jsonpath='{.items..metadata.name}')" \
    4040
查看更多
登录 后发表回答