I created two namespaces and services in each namespace:
When I try to connect to the MySQL database in DB Service from the rest-app
, I get the error:
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)
I printed out in logs, and it correctly has db-service
as the service name, and has the right user/pass.
Here's what I defined:
db-service
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
Questions:
db-service
which is the name of the externalName service?The ExternalName type service should be as below. Notice usage of svc
instead of 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