How to create Kubernetes service with kubectl which exposes two ports

8/10/2017

I have deployed a jenkins in Kubernetes. Now I want to create a service above the replicaset:

kubectl expose rs jenkins-xxx   --port=8080 --target-port=8080 --name=jenkins --namespace=ci

This works fine. service-ip:8080 is redirecting to pod:8080. But I want also exposing 5000 inside the same service so that the service-ip is the same? How will my kubectl command look like? I want to do this with kubectl

-- DenCowboy
kubernetes

2 Answers

1/24/2018

put this yaml declaration in a file "your-service.yaml"

apiVersion: v1
kind: Service
metadata:
  labels:
    app: your-app
  name: your-service
spec:
  type: LoadBalancer
  loadBalancerSourceRanges:
  - 10.0.0.8/32
  ports:
    - name: some-endpoint-name
      nodePort: 30100
      port: 8081
      targetPort: 8081
    - name: another-endpoint-name
      nodePort: 30101
      port: 8082
      targetPort: 8082
  selector:
    app: your-app

run this from the same folder:

kubectl create -f your-service.yaml
-- Rubber Duck
Source: StackOverflow

8/10/2017

If your Pod exposes multiple ports, you can use kubectl expose without any --port or --target-port parameters to expose them all as specified (e.g. 8080->8080 and 5000->5000).

kubectl expose rs jenkins-xxx --name=jenkins --namespace=ci

If you have more than those 2 ports specified on the Pod and only want to expose those 2, then you can't use kubectl expose and you'll have to create the Service manifest and kubectl apply it.

-- coreypobrien
Source: StackOverflow