我在每个 namespace 中创建了两个 namespace 和服务:
当我尝试从
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
问题: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/