jcgh582
jcgh582

Reputation: 909

Set static external IP for my load balancer on GKE

I am trying to set up a static external IP for my load balancer on GKE but having no luck. Here is my Kubernetes service config file:

kind: Service
apiVersion: v1
metadata:
  name: myAppService
spec:
  selector:
    app: myApp
  ports:
  - protocol: TCP
    port: 3001
    targetPort: 3001
  type: LoadBalancer
  loadBalancerIP: *********

This doesn't work. I expect to see my external IP as ********* but it just says pending:

➜  git:(master) kubectl get services
NAME         CLUSTER-IP    EXTERNAL-IP   PORT(S)          AGE
kubernetes   *********    <none>        443/TCP          5m
myAppService   *********   <pending>     3001:30126/TCP   5m

More details:

➜  git:(master) kubectl describe services
Name:           kubernetes
Namespace:      default
Labels:         component=apiserver
            provider=kubernetes
Annotations:        <none>
Selector:       <none>
Type:           ClusterIP
IP:         *********
Port:           https   443/TCP
Endpoints:      *********
Session Affinity:   ClientIP
Events:         <none>


Name:           myAppService
Namespace:      default
Labels:         <none>
Annotations:        <none>
Selector:       app=myApp
Type:           LoadBalancer
IP:         *********
Port:           <unset> 3001/TCP
NodePort:       <unset> 30126/TCP
Endpoints:
Session Affinity:   None
Events:
  FirstSeen LastSeen    Count   From            SubObjectPath   Type        Reason              Message
  --------- --------    -----   ----            -------------   --------    ------              -------
  5m        20s     7   service-controller          Normal      CreatingLoadBalancer        Creating load balancer
  5m        19s     7   service-controller          Warning     CreatingLoadBalancerFailed  Error creating load balancer (will retry): Failed to create load balancer for service default/myAppService: Cannot EnsureLoadBalancer() with no hosts

Any ideas?

Upvotes: 20

Views: 19802

Answers (3)

Oladapo Ajala
Oladapo Ajala

Reputation: 91

To use a static IP address in your kubernetes service, you need to first reserve the IP address in GCP.

Next ensure your GKE cluster has the HttpLoadBalancing addon is enabled in your cluster.

You can now create a kubernetes service of type LoadBalancer with a loadBalancer IP address equal to the reserved IP.

To learn more check out the GCP documentation. You can also see other methods of exposing your applications in the documentation.

Upvotes: 0

chriz
chriz

Reputation: 1896

This got me stuck as well, I hope someone finds this helpful.

In addition to what Dirk said, if you happen to reserve a global static IP address as oppose to a regional one; you need to use Ingres as describe here in documentation: Configuring Domain Names with Static IP Addresses specifically step 2b.

So basically you reserve the static ip gcloud compute addresses create helloweb-ip --global

and add an Ingres:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: helloweb
# this is where you you add your reserved ip
  annotations:
    kubernetes.io/ingress.global-static-ip-name: helloweb-ip 
  labels:
    app: hello
spec:
  backend:
    serviceName: helloweb-backend
    servicePort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: helloweb-backend
  labels:
    app: hello
spec:
  type: NodePort
  selector:
    app: hello
    tier: web
  ports:
  - port: 8080
    targetPort: 8080

The doc also describe how to assign a static ip if you choose type "LoadBalancer" under step 2a.

Upvotes: 13

Dirk Jablonski
Dirk Jablonski

Reputation: 362

I've encountered the same problem, but after reading the docs carefully, it turned out that I was just reserving the static IP incorrectly. A service of type LoadBalancer creates a network load balancer, which is regional. Therefore, also the static IP address you reserve needs to be regional also (in the regoin of your cluster). When I changed to this solution, everything worked fine for me...

Upvotes: 12

Related Questions