kubernetes Deployment PodName 设置

kubernetes Deployment PodName setting

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-deployment
  labels:
    app: test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: test
  template:
    metadata:
      name: test
      labels:
        app: test
    spec:
      containers:
        - name: server
          image: test_ml_server:2.3
          ports:
            - containerPort: 8080
          volumeMounts:
            - name: hostpath-vol-testserver
              mountPath: /app/test/api
#          env:
#            - name: POD_NAME
#              valueFrom:
#                fieldRef:
#                  fieldPath: template.metadata.name
        - name: testdb
          image: test_db:1.4
          ports:
            - name: testdb
              containerPort: 1433
          volumeMounts:
            - name: hostpath-vol-testdb
              mountPath: /var/opt/mssql/data
#          env:
#            - name: POD_NAME
#              valueFrom:
#                fieldRef:
#                  fieldPath: template.metadata.name
      volumes:
        - name: hostpath-vol-testserver
          hostPath:
            path: /usr/testhostpath/testserver
        - name: hostpath-vol-testdb
          hostPath:
            path: /usr/testhostpath/testdb

我想设置pod的名字因为它内部是根据pod的名字进行通信的 但是当创建一个 pod 时,它不能使用,因为变量名被附加到最后。 如何设置广告连播名称?

最好使用 statefulset 而不是 deployment。 Statefulset 的 pod 名称将类似于 <statefulsetName-0><statefulsetName-1>... 并且您将需要一个 clusterIP 服务。你可以用它来绑定你的 pods。有关详细信息,请参阅文档。 Ref

apiVersion: v1
kind: Service
metadata:
  name: test-svc
  labels:
    app: test
spec:
  ports:
  - port: 8080
    name: web
  clusterIP: None
  selector:
    app: test
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: test-StatefulSet
  labels:
    app: test
spec:
  replicas: 1
  serviceName: test-svc
  selector:
    matchLabels:
      app: test
  template:
    metadata:
      name: test
      labels:
        app: test
    spec:
      containers:
        - name: server
          image: test_ml_server:2.3
          ports:
            - containerPort: 8080
          volumeMounts:
            - name: hostpath-vol-testserver
              mountPath: /app/test/api
        - name: testdb
          image: test_db:1.4
          ports:
            - name: testdb
              containerPort: 1433
          volumeMounts:
            - name: hostpath-vol-testdb
              mountPath: /var/opt/mssql/data
      volumes:
        - name: hostpath-vol-testserver
          hostPath:
            path: /usr/testhostpath/testserver
        - name: hostpath-vol-testdb
          hostPath:
            path: /usr/testhostpath/testdb

在这里,pod 名称将像这样test-StatefulSet-0

如果您使用的是 kind: Deployment,在这种情况下理想情况下是不可能的,您可以使用 kind: Statefulset

您可以使用 Kubernetes 服务 进行通信,而不是 POD 到 POD 通信。

仍然,statefulset 按顺序管理 pod 名称

statefulsetname - 0
statefulsetname - 1
statefulsetname - 2

你不能。

Deployment 的 pods 的 属性 没有与之关联的身份。

如果您希望 pods 具有状态,您可以查看 Statefulset 而不是 Deployment。

来自docs

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.

因此,如果您有一个名为 myappStatefulset 对象和两个副本,则 pods 将被命名为 myapp-0myapp-1