为什么我的 externalName 类型服务配置在 Kubernetes 中不起作用?

Why is my externalName type service configuration not working in Kubernetes?

我在每个命名空间中创建了两个命名空间和服务:

当我尝试从 rest-app 连接到数据库服务中的 MySQL 数据库时,出现错误:

MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts. ---> System.AggregateException: One or more errors occurred. (Name or service not known)

我在日志中打印出来,它正确地将db-service作为服务名称,并具有正确的user/pass。

这是我定义的:

数据库服务

apiVersion: v1
kind: Service
metadata:
  name: db-service
  namespace: data-layer
spec:
  selector:
    app: db-service
  ports:
   - port: 3306
  clusterIP: None

db-service-externalname

apiVersion: v1
kind: Service
metadata:
  name: db-service
  namespace: app-layer
spec:
  type: ExternalName
  externalName: db-service.data-layer.service.cluster.local
  ports:
    - port: 3306

rest-app

apiVersion: apps/v1
kind: Deployment
metadata:
  name: rest-app
  namespace: app-layer
  labels:
    app: rest-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: rest-app
  template:
    metadata:
      labels:
        app: rest-app
    spec:
      containers:
        - name: rest-app
          image: restapp:latest
          imagePullPolicy: Always
          ports:
            - containerPort: 5000
          env:
            # These are from a secret I defined, and the logs show
            # the rest app gets them correctly
            - name: MYSQL_ROOT_USERNAME
              valueFrom:
                secretKeyRef:
                  name: db-credentials
                  key: db-username
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: db-credentials
                  key: db-password
            # I hard-coded this to the externalName I created. 
            # Is that right?
            - name: MYSQL_URL
              value: db-service

问题:

ExternalName 类型的服务应该如下所示。注意 svc 而不是 service.

的用法
apiVersion: v1
kind: Service
metadata:
  name: db-service
  namespace: app-layer
spec:
  type: ExternalName
  externalName: db-service.data-layer.svc.cluster.local
  ports:
    - port: 3306