为什么我的 externalName 类型服务配置在 Kubernetes 中不起作用?
Why is my externalName type service configuration not working in Kubernetes?
我在每个命名空间中创建了两个命名空间和服务:
- 命名空间:应用层
- rest-app
- db-service-externalname
- 命名空间:数据层
- 数据库服务
当我尝试从 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 是否正确?
- 我需要用不同的方式定义它吗?
- 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
我在每个命名空间中创建了两个命名空间和服务:
- 命名空间:应用层
- rest-app
- db-service-externalname
- 命名空间:数据层
- 数据库服务
当我尝试从 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 是否正确?
- 我需要用不同的方式定义它吗?
- 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