CoreDNS 部署手册 (Helm 版本)

1. 概述

本文档介绍如何使用 Helm 在 Kubernetes 集群中部署 CoreDNS 作为公司内网的 DNS 解析服务。

2. 前提条件

  • Kubernetes 集群 (v1.16+)
  • Helm (v3.0+)
  • 集群节点可访问公司内网
  • 足够的 Kubernetes 操作权限

3. 部署步骤

3.1 添加 Helm 仓库

helm repo add coredns https://coredns.github.io/helm
helm repo update

3.2 创建配置值文件

创建 coredns-values.yaml 文件,内容如下:

# coredns-values.yaml

# 基本配置
image:
  repository: coredns/coredns
  # Overrides the image tag whose default is the chart appVersion.
  tag: "1.12.0"
  pullPolicy: IfNotPresent

replicaCount: 1

resources:
  limits:
    cpu: 500m
    memory: 512Mi
  requests:
    cpu: 100m
    memory: 128Mi

serviceType: "NodePort"

# isClusterService specifies whether chart should be deployed as cluster-service or normal k8s app.
isClusterService: false    

servers:
- zones:
  - zone: .
  port: 53
  # -- expose the service on a different port
  # servicePort: 5353
  # If serviceType is nodePort you can specify nodePort here
  nodePort: 53
  # hostPort: 53
  plugins:
  - name: errors
  # Serves a /health endpoint on :8080, required for livenessProbe
  - name: health
    configBlock: |-
      lameduck 10s
  # Serves a /ready endpoint on :8181, required for readinessProbe
  - name: ready
  # Serves a /metrics endpoint on :9153, required for serviceMonitor
  - name: hosts
    parameters: /etc/coredns-custom/hosts  # 自定义host文件路径
    configBlock: |-
      fallthrough
  - name: prometheus
    parameters: 0.0.0.0:9153
  - name: forward
    parameters: . 8.8.8.8 8.8.4.4
  - name: cache
    parameters: 30
  - name: loop
  - name: reload
  - name: loadbalance

extraVolumes:
  - name: custom-hosts
    configMap:
      name: coredns-custom-hosts  # 引用的 ConfigMap 名称
      items:
        - key: custom.hosts
          path: hosts  #  在挂载目录中创建的文件名
# - name: some-volume-name
#   emptyDir: {}
# optional array of mount points for extraVolumes
extraVolumeMounts:
  - name: custom-hosts
    mountPath: /etc/coredns-custom/  # 容器内的挂载路径
# - name: some-volume-name
#   mountPath: /etc/wherever

3.3 创建自定义域名解析规则的 ConfigMap

创建 coredns-custom-hosts.yaml 文件,内容如下:

# coredns-custom-hosts.yaml

apiVersion: v1
kind: ConfigMap
metadata:
name: coredns-custom-hosts
namespace: external-coredns
data:
custom.hosts: |
# 自定义域名解析规则
# 格式:IP 地址 域名
192.168.1.100 example.com
192.168.1.101 test.example.com
# 添加更多自定义解析规则

3.4 应用 ConfigMap

执行以下命令创建 ConfigMap:

#创建命名空间 external-coredns
kubectl create namespace external-coredns

#应用configmap
kubectl apply -f coredns-custom-hosts.yaml

3.5 部署 CoreDNS

执行以下命令部署 CoreDNS:

# 使用 Helm 部署
helm install external-coredns coredns/coredns \
--namespace external-coredns \
--create-namespace \
--values coredns-values.yaml \
--version 1.42.1

3.6 验证部署

执行以下命令验证 CoreDNS 部署状态:

1. 检查 Pod 状态

# 检查 Pod 状态
kubectl get pods -n external-coredns -o wide

# 检查服务状态
kubectl get svc -n external-coredns

#验证域名解析是否成功(举例)
dig @10.110.0.193 ids.demo.com

2.验证 ConfigMap

# 查看 ConfigMap 内容
kubectl get configmap coredns-custom-hosts -n external-coredns -o yaml

4.注意事项

  • k8s默认service端口是30000-32767,因使用NodePort绑定53端口,所以需要修改端口范围(如1-32767)

  • 确保 Kubernetes 集群版本和 Helm 版本符合要求。

  • 如果需要修改自定义域名解析规则,可以直接编辑 ConfigMap,CoreDNS 会自动重新加载配置。

  • 如果遇到问题,可以查看 CoreDNS Pod 的日志以获取更多信息:

kubectl logs <coredns-pod-name> -n external-coredns

希望这份手册能帮助你顺利部署 CoreDNS!

posted @ 2025-05-15 14:52  怀恋小时候  阅读(62)  评论(0)    收藏  举报