为什么 pulumi kubernetes 提供商要更改服务和部署名称?

why pulumi kubernetes provider is changing the service and deployment name?

我试图从

转换下面的工作 kubernetes 清单
##namespace
---
apiVersion: v1
kind: Namespace
metadata:
  name: poc

##postgress
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: db
  name: db
  namespace: poc
spec:
  replicas: 1
  selector:
    matchLabels:
      app: db
  template:
    metadata:
      labels:
        app: db
    spec:
      containers:
      - image: postgres
        name: postgres
        env:
        - name: POSTGRES_USER
          value: postgres
        - name: POSTGRES_PASSWORD
          value: postgres
        ports:
        - containerPort: 5432
          name: postgres
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: db
  name: db
  namespace: poc
spec:
  type: ClusterIP
  ports:
  - name: "db-service"
    port: 5432
    targetPort: 5432
  selector:
    app: db

##adminer
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: ui
  name: ui
  namespace: poc
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ui
  template:
    metadata:
      labels:
        app: ui
    spec:
      containers:
      - image: adminer
        name: adminer
        ports:
        - containerPort: 8080
          name: ui
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: ui
  name: ui
  namespace: poc
spec:
  type: NodePort
  ports:
  - name: "ui-service"
    port: 8080
    targetPort: 8080
  selector:
    app: ui

import * as k8s from "@pulumi/kubernetes";
import * as kx from "@pulumi/kubernetesx";

//db
const dbLabels = { app: "db" };
const dbDeployment = new k8s.apps.v1.Deployment("db", {
    spec: {
        selector: { matchLabels: dbLabels },
        replicas: 1,
        template: {
            metadata: { labels: dbLabels },
            spec: { 
                containers: [
                    { 
                        name: "postgres", 
                        image: "postgres",
                        env: [{ name: "POSTGRES_USER", value: "postgres"},{ name: "POSTGRES_PASSWORD", value: "postgres"}],
                        ports: [{containerPort: 5432}]
                    }
                ] 
            }
        }
    }
});

const dbService = new k8s.core.v1.Service("db", {
    metadata: { labels: dbDeployment.spec.template.metadata.labels },
    spec: {
        selector: dbLabels,
        type: "ClusterIP",
        ports: [{ port: 5432, targetPort: 5432, protocol: "TCP" }],
    }
});

//adminer
const uiLabels = { app: "ui" };
const uiDeployment = new k8s.apps.v1.Deployment("ui", {
    spec: {
        selector: { matchLabels: uiLabels },
        replicas: 1,
        template: {
            metadata: { labels: uiLabels },
            spec: { 
                containers: [
                    { 
                        name: "adminer", 
                        image: "adminer",
                        ports: [{containerPort: 8080}],
                    }
                ] 
            }
        }
    }
});

const uiService = new k8s.core.v1.Service("ui", {
    metadata: { labels: uiDeployment.spec.template.metadata.labels },
    spec: {
        selector: uiLabels,
        type: "NodePort",
        ports: [{ port: 8080, targetPort: 8080, protocol: "TCP" }]
    }
});

有了这个 pulumi up -y 就成功了,没有错误,但是应用程序还没有完全启动并且 运行。因为 adminer 图像试图将 Postgres 数据库主机名用作 db,但看起来 pulumi 正在更改服务名称,如下所示:

我的问题是,如何让它可行? pulumi有没有严格命名的方法?

注意 - 我知道我们可以轻松地将主机名作为环境变量传递给 adminer 图像,但我想知道是否有什么可以让我们不更改名称。

Pulumi 会自动将随机字符串添加到您的资源中,以帮助替换资源。您可以在 FAQ

中找到更多相关信息

如果您想禁用它,您可以使用 metadata 覆盖它,如下所示:

import * as k8s from "@pulumi/kubernetes";
import * as kx from "@pulumi/kubernetesx";

//db
const dbLabels = { app: "db" };
const dbDeployment = new k8s.apps.v1.Deployment("db", {
    spec: {
        selector: { matchLabels: dbLabels },
        replicas: 1,
        template: {
            metadata: { labels: dbLabels },
            spec: { 
                containers: [
                    { 
                        name: "postgres", 
                        image: "postgres",
                        env: [{ name: "POSTGRES_USER", value: "postgres"},{ name: "POSTGRES_PASSWORD", value: "postgres"}],
                        ports: [{containerPort: 5432}]
                    }
                ] 
            }
        }
    }
});
    
const dbService = new k8s.core.v1.Service("db", {
    metadata: { 
        name: "db", // explicitly set a name on the service
        labels: dbDeployment.spec.template.metadata.labels 
    },
    spec: {
        selector: dbLabels,
        type: "ClusterIP",
        ports: [{ port: 5432, targetPort: 5432, protocol: "TCP" }],
    }
});

话虽如此,像这样对名称进行硬编码并不总是最佳做法,如果可能的话,您应该引用资源的输出并将它们传递给新资源。