简体   繁体   中英

How to expose k8 pods to the public internet?

I'm currently learning docker and kubernetes. One of the issues that I'm having trouble with is exposing my nginx pod to the public internet. I would like to visit my serverIP from my web browser and see the nginx page as if nginx was installed on the server.

pod-nginx.yml from kubernetes website

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.7.9
    ports:
    - containerPort: 80

I can port forward from the pod and then access the default nginx page via curl. sudo kubectl port-forward nginx 80:80

curl http://localhost returns the nginx page, while curl http://<serverIP> returns failed to connect <serverIP> port:80 Connection refused

Do I need to port forward between my pubic network interface to my cluster network interface by modifying iptables and firewall rules? I feel like im missing something really obvious here.

I have tried using the nodeport property and have read the documentation on ingress and loadbalancers, but my cloud provider doesn't have those back end functionalities, so those commands just end up pending indefinitely.

There are different ways to expose your services:

  • Using NodePort : This will open a port in the host where you can access your service. For example something like 192.168.100.99:37843, being 192.168.100.99 one of the HOST system where the cluster in installed in.

  • Using LoadBalancer : If your cluster is in a cloud like Google, then you can use the underlying infrastructure to generate an external IP for your service. I insist on the fact that the underlying cloud must support it.

  • Using Ingress rules : A proper alternative to LoadBalancers is the use of a reverse proxy. Kubernetes allows you to have this reverse proxy listening in port 80 and 443 and, using Ingress rules, to forward traffic to your different services.

Looking at your case, I think that the Ingress Rules option would suit your needs. If your cluster does not have an Ingress controller installed, you can install this one based on nginx.

To expose pods or deployments you must do the following.

  1. Use the nodeport flag to assign the same port across all nodes to the application. Kubernetes will create a ServiceIP where your application will be exposed.

kubectl expose <deployment> --nodeport=<common-port> --port=<container-port>

After creating the exposing service, you can get your ServiceIP with kubectl get services

  1. Use nginx or another load balancer to reverse proxy into your nodes. I configured nginx to proxy_pass to my application by creating a defaults file in /etc/nginx/sites-enabled

    server { listen 80;

    location / { proxy_pass http://ServiceIP:ApplicationPort ; } }

This method allows for unique routing and even round robin load balancing.

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