Search code examples
google-cloud-platformkubectl

Kubectl create service type Loadbalancer (on GCP but add flag Global?)


I've create loadbalancer for my microservices, with this template:, all is good and works but wanted to somehow add the global flag (when you create lb through gcp console you have option to add it) to meet expectations of the app functionality, does anyone know what other flag might I need to add ?

apiVersion: v1
kind: Service
metadata:
  name: my-app-jmprlb
  annotations:
    cloud.google.com/load-balancer-type: "Internal"
  labels:
    app: my-app
    env: dev
spec:
  type: LoadBalancer
  selector:
    app: my-app
    env: dev
  ports:
  - port: 80
    targetPort: 8080
    protocol: TCP
  loadBalancerIP: 10.10.10.10
  externalTrafficPolicy: Local

EDIT: I found some nice annotations from google docs, seem to do the trick,https://cloud.google.com/kubernetes-engine/docs/how-to/internal-load-balance-ingress

# web-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: hostname
  namespace: default
  annotations:
    cloud.google.com/neg: '{"ingress": true}'
spec:
  ports:
  - name: host1
    port: 80
    protocol: TCP
    targetPort: 9376
  selector:
    app: hostname
  type: NodePort

and

# internal-ingress.yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ilb-demo-ingress
  namespace: default
  annotations:
    kubernetes.io/ingress.class: "gce-internal"
spec:
  backend:
    serviceName: hostname
    servicePort: 80

Solution

  • If you want to make it a global LoadBalancer which accessible from the outside your cluster with public IP you can use:

    apiVersion: v1
    kind: Service
    metadata:
      name: my-app-jmprlb
      labels:
        app: my-app
        env: dev
    spec:
      type: LoadBalancer
      selector:
        app: my-app
        env: dev
      ports:
      - port: 80
        targetPort: 8080
        protocol: TCP
    

    Note that the annotation of cloud.google.com/load-balancer-type: "Internal" means that your service is only accessible withing subnets that were peer with the subnet where your cluster resided.