自定义 DNS 配置

自定义一些解析,custom-dns-configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: custom-dns-config
  namespace: default
data:
  Corefile: |
    .:53 {
        errors
        health
        # 自定义解析规则
        rewrite name regex (.*)\.t\.yiqisoft\.cn t.yiqisoft.cn

        hosts {
          192.168.1.10 example.com      # 自定义域名解析
          192.168.1.11 test.example.com # 更多自定义解析
        }
        log                   # 可选:记录查询日志
        cache 30              # 可选:缓存响应
        reload
    }

部署自定义 DNS 服务

使用 K3S 内置的 CoreDNS 进行部署,custom-dns-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: custom-dns
  namespace: default
spec:
  replicas: 1  # 可根据需要增加副本
  selector:
    matchLabels:
      app: custom-dns
  template:
    metadata:
      labels:
        app: custom-dns
    spec:
      containers:
      - name: coredns
        image: rancher/mirrored-coredns-coredns:1.10.1
        args: [ "-conf", "/etc/coredns/Corefile" ]
        volumeMounts:
        - name: config-volume
          mountPath: "/etc/coredns"
          readOnly: true
        ports:
        - containerPort: 53
          name: dns
          protocol: UDP
        - containerPort: 53
          name: dns-tcp
          protocol: TCP
      volumes:
      - name: config-volume
        configMap:
          name: custom-dns-config
          items:
          - key: Corefile
            path: Corefile

暴露 DNS 服务给集群外部使用

使用 LoadBalancer 暴露给外部, custom-dns-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: custom-dns
  namespace: default
spec:
  selector:
    app: custom-dns
  ports:
  - name: dns
    port: 53
    targetPort: 53
    protocol: UDP
  - name: dns-tcp
    port: 53
    targetPort: 53
    protocol: TCP
  type: LoadBalancer

测试

假设集群两台服务器:192.168.123.135/136

nslookup
> server 192.168.123.135
Default server: 192.168.123.135
Address: 192.168.123.135#53
> example.com
Server:     192.168.123.135
Address:    192.168.123.135#53

Name:   example.com
Address: 192.168.1.10
> 
> server 192.168.123.136
Default server: 192.168.123.136
Address: 192.168.123.136#53
> test.example.com
Server:     192.168.123.136
Address:    192.168.123.136#53

Name:   test.example.com
Address: 192.168.1.11
> 

标签: CoreDNS, 自定义DNS

添加新评论