1

我需要在 pod 模板和负载均衡器服务中声明一长串端口(以千计)。我相信必须有一种方法可以做到这一点,而无需在 yaml 中重复以下一千次。有人可以点亮一些灯吗?谢谢。

- name: containerPort10000
  containerPort: 10000

更新:恐怕问题现在更复杂了。由于我需要公开 10k 端口(用于连接设备,同时使用 TCP 和 UDP),我需要在 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

我遇到了The Service "svr" is invalid: metadata.annotations: Too long: must have at most 262144 characters错误。请帮忙。

4

1 回答 1

2

我会选择 bash 脚本,但我想看看其他方法。

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

或者你可以去 vim 宏。

于 2019-08-12T19:48:58.690 回答