1
votes

I have a simple app which I am following from a basic tutorial :

https://medium.com/@bhargavshah2011/hello-world-on-kubernetes-cluster-6bec6f4b1bfd

  1. I create a cluster hello-world2-cluster

enter image description here

  1. I "connect" to the cluster using :

    gcloud container clusters get-credentials hello-world2-cluster --zone us-central1-c --project strange-vortex-286312

  2. I perform a git clone of the "hellow world" project, which is basically some kind of snake game :

    $ git clone https://github.com/skynet86/hello-world-k8s.git

    $ cd hello-world-k8s/

Next I run "create" on the YAML:

kubectl create -f hello-world.yaml

Deplyments and services are created as expected :

enter image description here

Fine! ALl good!

But.... now what????

What do I do now?

I want to access this app from the outside world. Where is the URL where I can access the application?

How can I get my friend Bob in Timbuktu to call some kind of URL (no need for DNS) to access my application?

I know the answer has something to do with LoadBalancer nodes and Ingress, but there seems to be no proper documentation out there on how to do this for a simple hello world app.

4
Well if are your service as NodePort and using the image you sent like reference, you application should be accessible in machine-ip:30081. NodePort service opens a port that is accessible from outside of the cluster. - Daniel Marques
I don't know how GKE works exactly but it should depend on your cluster configuration. As you can see a service called "hello-world" has been created and it has an internal IP. It means it is accessible only from INSIDE the cluster. By default you have to create a service of type "LoadBalancer" so it will have an external IP. But as you are on GKE, maybe your cluster already has an external IP and you could try to access it by "cluster-ip:30081" but I highly doubt it will work. I'm used to Azure Kubernetes Service and it comes with a default "global" LoadBalancer. - Flo
so what would my URL be? machine-ip:30081, is it google.cloud.hello-world.com ? I am so thoroughly confused. I just need to link to my app from the outside world. - Oliver Watkins

4 Answers

3
votes

GKE hello-world with Ingress Official Example: https://cloud.google.com/kubernetes-engine/docs/how-to/load-balance-ingress

The ingress controller is built-in in GKE, you just need a kind: Ingress object that points to your Service. Addressing your question:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: hello-ingress
  annotations:
    kubernetes.io/ingress.class: "gce"
spec:
  rules:
  - http:
      paths:
      - path: /*
        backend:
          serviceName: hello-world
          servicePort: 80

Once deployed, query kubectl get ingress hello-ingress -oyaml to find out the resulting load-balancer's IP in the status: field.

1
votes

If you don't want to set up a DNS and/or an ingress, you can simply do it with what you already have using the node external ip.

It seems that you already have the deployement and the NodePort service is mapped to port 30081.

So, below are the remaining steps to make it work with what you have :

  1. Get your node IP :
kubectl get nodes -o wide
  1. Now that you have your node external IP, you need to allow tcp traffic to this port
gcloud compute firewall-rules create allow-tcp-30081 --allow tcp:30081
  1. That's it ! You can just tell you're friend Bob that the service should be accessible at node-ip:30081

You can have a look at this page to see the other possibilities depending on your service type.

Also, when you use a NodePort the port is mapped to all the nodes of your cluster, so you can use any IP. No need to look for the node where your replicas is deployed.

1
votes

If you don't want to use Ingress or anything. You can expose your deployment as LoadBalancer then using external IP your friend bob can access application. So to use

kubectl expose deployment hello-world-deployment --name=hello-world-deployment --type=LoadBalancer --port 80 --target-port 80

This will take around 1 minute to assign an external IP address to the service.

to get external IP use

kubectl get services

Get the External IP address in-front of your service and open your browser and enter the IP address that you copied.

Done !! This is the easiest and simplest way to get start with !!

1
votes

You can expose your Kubernetes services to the internet with an Ingress.

GKE allows you to use different kinds of Ingress (nginx based, and so on) but, by default, it will expose your service through the Cloud Load Balancing service.

As indicated by @MaxLobur, you can configure this default Ingress applying a configuration file similar to this:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: hello-world-ingress
spec:
  backend:
    serviceName: hello-world
    servicePort: 80

Or, equivalently, if you want to prepare your Ingress for various backend services and configure it based on rules:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: hello-world-ingress
spec:
  rules:
  - http:
      paths:
      - path: /*
          backend:
            serviceName: hello-world
            servicePort: 80

This Ingress will be assigned an external IP address.

You can find which with the following command:

kubectl get ingress hello-world-ingress

This command will output something similar to the following:

NAME                  HOSTS     ADDRESS         PORTS     AGE
hello-world-ingress   *         203.0.113.12    80        2m

But, if you give this IP address to your friend Bob in Timbuktu, he will call you back soon telling you that he cannot access the application...

Now seriously: this external IP address assigned by GCP is temporary, an ephemeral one subject to change.

If you need a deterministic, fixed IP address for your application, you need to configure a static IP for your Ingress.

This will also allow you to configure, if necessary, a DNS record and SSL certificates for your application.

In GCP, the first step in this process is reserving a static IP address. You can configure it in the GCP console, or by issuing the following gcloud command:

gcloud compute addresses create hello-world-static-ip --global

This IP should be later assigned to your Ingress by modifying its configuration file:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: hello-world-ingress
  annotations:
    kubernetes.io/ingress.global-static-ip-name: "hello-world-static-ip"
spec:
  backend:
    serviceName: hello-world
    servicePort: 80

This static IP approach only adds value because as long as it is associated with a Google Cloud network resource, you will not be charged for it.

All this process is documented in this GCP documentation page.