0
votes

I have a cluster created in the GCP cloud having a simple k8s YAML file.

apiVersion: v1
kind: Service
metadata:
  name: lb-svc
  labels:
    app: lb-demo
spec:
  type: LoadBalancer
  ports:
  - port: 8080
  selector:
    app: np-demo

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: np-deploy
spec:
  replicas: 3
  selector:
    matchLabels:
      app: np-demo
  template:
    metadata:
      labels:
        app: np-demo
    spec:
      containers:
      - name: np-pod
        image: nigelpoulton/k8s-deep-dive:0.1
        imagePullPolicy: Always
        ports:
        - containerPort: 8080

Now; this YAML configuration has a LoadBalancer service which in return exposes an external IP address to the public. thus we can see the external IP address using:

kubectl get svc

The issue is, I can easily access the load balancer using curl within the cloud shell but couldn't reach it when trying to access it from outside (example browser).

Tried:

curl external-ip:8080

Any help?

1
have you assign that ip to your service ?Ardy Febriansyah
Please mention targetPort: 8080 in your service.ARINDAM BANERJEE

1 Answers

2
votes

Your service ip only accessible to local VPC, if you need to expose service or ingress you need reserve a static ip, read here to reserve a static ip https://cloud.google.com/compute/docs/ip-addresses/reserve-static-external-ip-address

To assign your static ip to service, you need set loadBalancerIP on your service configuration

apiVersion: v1
kind: Service
metadata:
  name: lb-svc
  labels:
    app: lb-demo
spec:
  type: LoadBalancer
  loadBalancerIP: <your reserved ip>
  ports:
  - port: 8080
  selector:
    app: np-demo

To assign your ip to ingress

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    kubernetes.io/ingress.global-static-ip-name: <name of reserved static ip>
  labels:
    app: my-app
spec:
  backend:
    serviceName: lb-svc
    servicePort: 8080

read more here