How to declare a long list of ports (in thousands)? (over 262144 characters)

8/12/2019

I need to declare a long list of ports (in thousands) in both the pod template and the loadbalancer service. I believe there must be a way to do it without repeating the following a thousand times in the yaml. Can someone please shed some lights? Thanks.

- name: containerPort10000
  containerPort: 10000

Update: I'm afraid the problem is more complicated now. As I need to expose 10k ports (for connecting devices, using both TCP and UDP), I need to specify the followings in the yaml:

  - targetPort: 10000
    port: 10000
    nodePort: 10000
    name: t10000
    protocol: TCP
  - targetPort: 10000
    port: 10000
    nodePort: 10000
    name: u10000
    protocol: UDP
...
  - targetPort: 20000
    port: 20000
    nodePort: 20000
    name: t20000
    protocol: TCP
  - targetPort: 20000
    port: 20000
    nodePort: 20000
    name: u20000
    protocol: UDP

and I've hit The Service "svr" is invalid: metadata.annotations: Too long: must have at most 262144 characters error. Please help.

-- skwokie
kubernetes

1 Answer

8/12/2019

I would go for bash script, but I would like to see other approaches.

bash svc.sh | kubectl apply -f -

service/multiport created
multiport    LoadBalancer   10.100.63.192    <pending>     1000:32545/TCP,1001:32324/TCP,1002:32559/TCP,1003:31523/TCP,1004:31027/TCP,1005:31671/TCP,1006:31532/TCP,1007:30568/TCP,1008:30105/TCP,1009:32649/TCP   3s

$ cat svc.sh

#!/bin/bash
#set -x
BODY=""
for p in `echo {1000..1009}`;do
  BODY=$BODY$(echo -e "\n  - port: $p\n    protocol: TCP\n    targetPort: $p\n    name: tcp$p\n")
done
cat << TEMPLATE
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    run: ubuntu
  name: multiport
spec:
  ports:
  ${BODY}
  selector:
    run: ubuntu
  type: LoadBalancer
status:
  loadBalancer: {}
TEMPLATE

or you can go for vim macro.

-- FL3SH
Source: StackOverflow