noobie
noobie

Reputation: 781

cannot expose port using kubernetes service

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

Upvotes: 2

Views: 1095

Answers (2)

Ijaz Ahmad
Ijaz Ahmad

Reputation: 12110

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

Upvotes: 2

Michael Hausenblas
Michael Hausenblas

Reputation: 14011

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.

Upvotes: 1

Related Questions