gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 20:53:49 24 4
gpt4 key购买 nike

我在每个 namespace 中创建了两个 namespace 和服务:

  • 命名空间:应用程序层
  • rest-app
  • 数据库服务外部名称

  • 命名空间:数据层
  • 数据库服务


  • 当我尝试从 rest-app连接到DB Service中的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作为服务名称,并且具有正确的用户/密码。
    这是我定义的:
    数据库服务
    apiVersion: v1
    kind: Service
    metadata:
    name: db-service
    namespace: data-layer
    spec:
    selector:
    app: db-service
    ports:
    - port: 3306
    clusterIP: None
    数据库服务外部名称
    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
    问题:
  • 我是否正确在应用层 namespace 中创建了externalName?
  • 我需要以不同的方式定义它吗?
  • docker容器中的C#应用​​程序能否不引用db-service(这是externalName服务的名称)?
  • 最佳答案

    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

    关于mysql - 为什么我的externalName类型服务配置在Kubernetes中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64013958/

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