sandeep P
sandeep P

Reputation: 77

In kubernetes how to access a service using dns names?

How do i provide a service with a dns name in the deployment file so that i can access that service from some other service from the same cluster?

apiVersion: apps/v1
kind: Deployment

metadata:
  name: db
spec:
  selector:
    matchLabels:
      app: db
  replicas: 1
  template:
    metadata:
      labels:
        app: db
    spec:
      containers:
      - name: db
        image: mysql:5.7.29
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "root"
        volumeMounts:
        - mountPath: /var/log/mysql/
          name: mysql
      volumes:
      - name: mysql
        hostPath:
          path: "/home/sandeep/logs/mysql/"
---
apiVersion: v1
kind: Service
metadata:
  name: database
  labels:
    app: db
spec:
  selector:
    app: db
  type: ClusterIP
  ports:
  - name: database
    port: 3306
    targetPort: 3306

This is mysql deployment file so how can i provide this service with a dns name? In which do i need to make change?

Upvotes: 1

Views: 1347

Answers (2)

coderanger
coderanger

Reputation: 54181

The DNS is based on the service name and namespace. In most cases you just use the service name but the full name would be database.default.svc.cluster.local or similar depending on your domain config.

Upvotes: 2

Arghya Sadhu
Arghya Sadhu

Reputation: 44549

Assuming that you created the database service in default namespace you can access mysql via database.default.svc.cluster.local from any namespace and via database.svc.cluster.local from the same namespace.

Upvotes: 3

Related Questions