简体   繁体   中英

How to expose service outside k8s cluster?

I have run a Hello World application using the below command.

kubectl run hello-world --replicas=2 --labels="run=load-balancer-example" --image=gcr.io/google-samples/node-hello:1.0 --port=8080

Created a service as below

kubectl expose deployment hello-world --type=NodePort --name=example-service

The pods are running

NAME                          READY   STATUS    RESTARTS   AGE
hello-world-68ff65cf7-dn22t   1/1     Running   0          2m20s
hello-world-68ff65cf7-llvjt   1/1     Running   0          2m20s

Service:

NAME              TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
example-service   NodePort    10.XX.XX.XX     <none>        8080:32023/TCP   66s

Here, I am able to test it through curl inside the cluster.

curl http://10.XX.XX.XX:8080
Hello Kubernetes!

How can I access this service outside my cluster? (example, through laptop browser)

you shoud try

http://IP_OF_KUBERNETES:32023

IP_OF_KUBERNETES can be your master IP your worker IP when you expose a port in kubernetes .It expose that port in all of your server in cluster.Imagine you have two worker node with IP1 and IP2 and one pode is running in IP1 and in worker2 there is no pods but you can access your pod by

http://IP1:32023

http://IP2:32023

You should be able to access it outside the cluster using NodePort assingned(32023). Please paste following http://<IP>:<Port> in your browser and you will able to access your app:

http://<MASTER/WORKER_IP>:32023

You can access your service using MasterIP or WorkerIP. If you are planning to use it in production or in a more reliable way you should create a service with type LoadBalancer . And use load balancers IP to access it.

If you are using any cloud env, make sure the firewall rules allow incoming traffic.

This will take care of redirecting request to which ever node the pod is running on. Else you will have to manually hit masterIP or workerIP depending on where the pod is running. If the pod gets moved to different node, you will have to change the ip you are hitting

Service Load Balancer

There are answers already provided, but I felt like this topic needed some consolidation.

This seems to be fairly easy. NodePort actually exposes your application as the name says on the port of each node. So all you have to do is just find the IP address of the Node on which the pod is. You can do it by running: kubectl get pods -o wide so you can find the IP or name of the node on which the pod is, then just follow what previous answers state: so http://<MASTER/WORKER_IP>:PORT

There is more methods: You can deploy Ingress Controller and configure Ingress so the application will be reachable through the internet.

You can also use kubectl proxy to expose ClusterIP service outside of the cluster. Like in this example with Dashboard .

Another way is to use LoadBalancer type, which requires underlying cloud infrastructure.

If you are using minikube you can try to run minikube service list to check your exposed services and their IP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM