Expose publicly Kubernetes service using deployment and LoadBalancer service

12/5/2018

When creating a simple hello-world deployment via a declarative yaml manifest, and a LoadBalancer typed service to expose it, everything is created successfully however when hitting the external IP of the service it is unreachable.

Here is the yaml file I'm using:

apiVersion: v1
kind: Service
metadata:
  name: my-test
  labels:
    app: my-test
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 8888
    protocol: TCP
    name: http
  # - port: 443
  #   targetPort: 8888
  #   protocol: TCP
  #   name: https
  selector:
    app: my-test
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-test
  labels:
    app: my-test
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-test
  template:
    metadata:
      labels:
        app: my-test
    spec:
      containers:
      - name: my-test
        image: gcr.io/google-samples/hello- app:2.0
        ports:
        - containerPort: 8888

I've figured out that If I create the deployment using a run command I can create the service either via yaml or kubectl expose command. I am new to K8s so not sure what is wrong with the deployment declaration.

Using the following commands it works fine

kubectl run hello-server --image gcr.io/google-samples/hello-app:1.0 --port 8080

Then I ran the following command to expose the deployment:

kubectl expose deployment hello-server --type LoadBalancer --port 80 --target-port 8080

Any ideas?

-- Joe Z
google-kubernetes-engine
kubernetes

0 Answers