gpt4 book ai didi

kubernetes - 如何获得对我的服务分配给我的部署的NodePort的更多控制?

转载 作者:行者123 更新时间:2023-12-02 12:03:29 27 4
gpt4 key购买 nike

我有5个副本的部署。全部都有ssh和telnet。它们不应负载均衡。我希望每个人都从5个可预测的列表中进行选择。

这是我的部署

apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
selector:
matchLabels:
app: myapp
replicas: 5
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:1.0
ports:
- name: ssh
protocol: TCP
containerPort: 22
- name: telnet
protocol: TCP
containerPort: 23

这是我的服务,具有无效的nodePort值,仅供说明。
apiVersion: v1
kind: Service
metadata:
name: myapp
labels:
app: myapp
spec:
type: NodePort
ports:
- name: ssh
port: 22
nodePort: [30022, 30122, 30222, 30322, 30422, 30522]
- name: telnet
port: 23
nodePort: [30023, 30123, 30223, 30323, 30423, 30523]

我希望能够完成两件事:
  • 每个pod副本实例仅从[30022、30122、30222、30322、30422、30522]获得ssh端口,并从[30023、30123、30223、30323、30423、30523]获得telnet端口。
  • 获得ssh端口为30022的pod副本实例也获得telnet端口30023。获得ssh端口为30122的pod副本实例获得telnet端口30123,依此类推。

  • 谢谢!

    最佳答案

    您可以使用StatefulSet代替Deployment:

    Like a Deployment, a StatefulSet manages Pods that are based on an identical container spec. Unlike a Deployment, a StatefulSet maintains a sticky identity for each of their Pods. These pods are created from the same spec, but are not interchangeable: each has a persistent identifier that it maintains across any rescheduling.



    StatefulSets的特别有用的功能是,您将获得可预测地为每个 pods 生成的唯一标签:

    When the StatefulSet controller creates a Pod, it adds a label, statefulset.kubernetes.io/pod-name, that is set to the name of the Pod. This label allows you to attach a Service to a specific Pod in the StatefulSet. [source]



    然后,您将创建 五个不同的服务,每个 pods 一个,格式如下:
    apiVersion: v1
    kind: Service
    metadata:
    name: myapp-${n}
    labels: { ... } # whatever you want
    spec:
    type: NodePort
    selector:
    statefulset.kubernetes.io/pod-name: myapp-${n} # assuming you keep the name
    # "myapp" and just switch kind
    # from Deployment to StatefulSet
    ports:
    - name: ssh
    port: 22
    nodePort: 30${n}22
    - name: telnet
    port: 23
    nodePort: 30${n}23

    通过 ${n}0替换为 4

    关于kubernetes - 如何获得对我的服务分配给我的部署的NodePort的更多控制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57516110/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com