Mapping incoming port in kubernetes service to different port on docker container Mapping incoming port in kubernetes service to different port on docker container kubernetes kubernetes

Mapping incoming port in kubernetes service to different port on docker container


I found that I needed to add some arguments to my second command:

kubectl expose deployment test-app --type="LoadBalancer" --target-port=3000 --port=80

This creates a service which directs incoming http traffic (on port 80) to its pods on port 3000.

A nicer way to do this whole thing is with yaml files service.yaml and deployment.yaml and calling

kubectl create -f deployment.yamlkubectl create -f service.yaml

where the files have these contents

# deployment.yamlapiVersion: extensions/v1beta1kind: Deploymentmetadata:  name: app-deploymentspec:  replicas: 2  template:    metadata:      labels:        app: test-app    spec:      containers:        - name: user-app          image: eu.gcr.io/myproject/my_app          ports:            - containerPort: 3000

and

# service.yamlapiVersion: v1kind: Servicemetadata:  name: app-servicespec:  selector:    app: test-app  ports:  - port: 80    targetPort: 3000  type: LoadBalancer

Note that the selector of the service must match the label of the deployment.