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

1.5k views Asked by At

I created two namespaces and services in each namespace:

  • Namespace: app-layer
    • rest-app
    • db-service-externalname
  • Namespace: data-layer
    • db-service

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:

  • Am I correct to make the externalName in the app-layer namespace?
  • Do I need to define it differently?
  • Can the C# app in the docker container not refer to db-service which is the name of the externalName service?
1

There are 1 answers

0
Arghya Sadhu On BEST ANSWER

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