pat
pat

Reputation: 41

Change the IP address of my LoadBalancer on GKE

I want to change the IP address of my LoadBalancer ingress-nginx-controller in Google Cloud. I have now assigned the IP address via LoadBalancer. See the screenshot. Unfortunately it is not adopted in GKE. Why? Is that a bug? GKE lb IP address change

Upvotes: 3

Views: 3734

Answers (2)

Charlie
Charlie

Reputation: 23798

The following method works well:

Prerequisites:

  • Ingress Controller Installed
  • Ingress Load Balancer Service created
  • External Load Balancer created
  • Ingress Resource created
  • Static IP reserved

Imagine that the static IP needs to be assigned as the External Load Balancer IP.

  1. Save locally and Delete the Ingress Load Balancer Service. This will delete the External Load Balancer too.

  2. Save locally and delete the Ingress Load Balancer.

  3. Recreate the Ingress Load Balancer Service with the following line. xxx.xxx.xxx.xxx = your reserved static ip

     spec:
       loadBalancerIP: xxx.xxx.xxx.xxx  
    
  4. Recreate the ingress resource

Upvotes: 0

PjoterS
PjoterS

Reputation: 14092

I have verified this on my GKE test cluster.

When you Reserving a static external IP address it isn't assigned to any of your VMs. Depends on how you created cluster/reserved ip (standard or premium) you can get error like below:

Error syncing load balancer: failed to ensure load balancer: failed to create forwarding rule for load balancer (a574130f333b143a2a62281ef47c8dbb(default/nginx-ingress-controller)): googleapi: Error 400: PREMIUM network tier (the project's default network tier) is not supported: The network tier of specified IP address is STANDARD, that of Forwarding Rule must be the same., badRequest

In this scenario I've used cluster based in us-central-1c and reserved IP as Network Service Tier: Premium, Type: Regional and used region where my cluster is based - us-central-1. My ExternalIP: 34.66.79.1X8

NOTE Reserved IP must be in the same reagion as your cluster

Option 1: - Use Helm chart

Deploy Nginx

helm install nginx-ingress stable/nginx-ingress  --set controller.service.loadBalancerIP=34.66.79.1X8,rbac.create=true

Service output:

$ kubectl get svc
NAME                            TYPE           CLUSTER-IP    EXTERNAL-IP   PORT(S)                      AGE
kubernetes                      ClusterIP      10.8.0.1      <none>        443/TCP                      5h49m
nginx-ingress-controller        LoadBalancer   10.8.5.158    <pending>     80:31898/TCP,443:30554/TCP   27s
nginx-ingress-default-backend   ClusterIP      10.8.13.209   <none>        80/TCP                       27s

Service describe output:

$ kubectl describe svc nginx-ingress-controller
...
Events:
  Type    Reason                Age   From                Message
  ----    ------                ----  ----                -------
  Normal  EnsuringLoadBalancer  32s   service-controller  Ensuring load balancer
  Normal  EnsuredLoadBalancer   5s    service-controller  Ensured load balancer

Final output:

$ kubectl get svc
NAME                            TYPE           CLUSTER-IP    EXTERNAL-IP    PORT(S)                      AGE
kubernetes                      ClusterIP      10.8.0.1      <none>         443/TCP                      5h49m
nginx-ingress-controller        LoadBalancer   10.8.5.158    34.66.79.1X8   80:31898/TCP,443:30554/TCP   35s
nginx-ingress-default-backend   ClusterIP      10.8.13.209   <none>         80/TCP                       35s

Option 2 - Editing Nginx YAMLs before deploying Nginx

As per docs: Initialize your user as a cluster-admin with the following command:

kubectl create clusterrolebinding cluster-admin-binding \
  --clusterrole cluster-admin \
  --user $(gcloud config get-value account)

Download YAML

$ wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.35.0/deploy/static/provider/cloud/deploy.yaml

Edit LoadBalancer service and add loadBalancerIP: <your-reserved-ip> like below:

# Source: ingress-nginx/templates/controller-service.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    helm.sh/chart: ingress-nginx-2.13.0
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/version: 0.35.0
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/component: controller
  name: ingress-nginx-controller
  namespace: ingress-nginx
spec:
  type: LoadBalancer
  loadBalancerIP: 34.66.79.1x8             #This line
  externalTrafficPolicy: Local
  ports:

Deploy it kubectl apply -f deploy.yaml. Service output below:

$ kubectl get svc -A
NAMESPACE       NAME                                 TYPE           CLUSTER-IP   EXTERNAL-IP   PORT(S)                      AGE
default         kubernetes                           ClusterIP      10.8.0.1     <none>        443/TCP                      6h6m
ingress-nginx   ingress-nginx-controller             LoadBalancer   10.8.5.165   <pending>     80:31226/TCP,443:31161/TCP   17s
ingress-nginx   ingress-nginx-controller-admission   ClusterIP      10.8.9.216   <none>        443/TCP                      18s
                   6h6m
...

Describe output:

$ kubectl describe svc ingress-nginx-controller -n ingress-nginx
Events:
  Type    Reason                Age   From                Message
  ----    ------                ----  ----                -------
  Normal  EnsuringLoadBalancer  40s   service-controller  Ensuring load balancer
  Normal  EnsuredLoadBalancer   2s    service-controller  Ensured load balancer

Service with reserved IP:

$ kubectl get svc -n ingress-nginx
NAME                                 TYPE           CLUSTER-IP   EXTERNAL-IP    PORT(S)                      AGE
ingress-nginx-controller             LoadBalancer   10.8.5.165   34.66.79.1X8   80:31226/TCP,443:31161/TCP   2m22s
ingress-nginx-controller-admission   ClusterIP      10.8.9.216   <none>         443/TCP                      2m23s

In Addition

Also please keep in mind that you should add annotations: kubernetes.io/ingress.class: nginx in your ingress resource when you want force GKE to use Nginx Ingress features, like rewrite.

Upvotes: 3

Related Questions