cannot expose port using kubernetes service

10/23/2018

My objective: To expose a pod's(running angular image) port so that I can access it from the host machine's browser.

service.yml:

apiVersion: v1
kind: Service
metadata:  
  name: my-frontend-service
spec:
  selector:
    app: MyApp
  ports:
  - protocol: TCP
    port: 8000
    targetPort: 4200

Pod's yml:

apiVersion: v1
kind: Pod
metadata:
  name: angular.frontend
  labels:
    app: MyApp
spec:
  containers:
  - name: angular-frontend-demo
    image: angular-frontend-image
    ports:
    - name: nodejs-port
      containerPort: 4200

Weird thing is that doing kubectl port-forward pod/angular.frontend 8000:4200 works. However, my objective is to write that in service.yml

-- noobie
dockerfile
kubernetes

2 Answers

10/23/2018

Use Nodeport:

apiVersion: v1
kind: Service
metadata:  
  name: my-frontend-service
spec:
  selector:
    app: MyApp
  type: NodePort
  ports:
  - protocol: TCP
    port: 8000
    targetPort: 4200
    nodePort: 30001

then you can access the service on nodeport 30001 on any node of the cluster.

For example the machine name is node01 , you can then do curl http://node01:30001

-- Ijaz Ahmad Khan
Source: StackOverflow

10/23/2018

The service you've defined here is of type ClusterIP (since you haven't set a type in the spec). This means the service is only available and reachable within the cluster. You can use Ingress to make it accessible from outside the cluster, see for example this post showing how to do that for Minikube.

-- Michael Hausenblas
Source: StackOverflow