为什么pulumi kubernetes提供程序正在更改服务和部署名称?

时间:2020-10-21 18:52:22

标签: kubernetes pulumi

我尝试将以下工作的kubernetes清单转换为

https://{IShost}:{port}/services/IdentityApplicationManagementService?wsdl

##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映像正试图将Postgres数据库主机名用作adminer,但是pulumi似乎在更改服务名称,如下所示:

enter image description here

我的问题是,如何使其可行? 普鲁米语中是否有一种严格的命名方式?

注意:我知道我们可以轻松地将主机名作为env变量传递给db图像,但是我想知道是否有任何东西可以使我们不更改名称。

1 个答案:

答案 0 :(得分:1)

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" }],
    }
});

话虽这么说,对这样的名称进行硬编码并不总是最佳实践,如果可能的话,您应该引用资源中的输出并将其传递给新资源。