kubernetes集群中部署CoreDNS服务
前言
从k8s 1.11版本开始,k8s集群的dns服务由CoreDNS提供。之前已经使用二进制文件部署了一个三master三node的k8s集群,现在需要在集群内部部署DNS服务。
- 环境信息
步骤
1. 修改node的kubelet启动参数
修改node上的kubelet启动参数,添加以下两个参数,添加完成后重启kubelet。
--cluster-dns=169.169.0.100
,这是集群内DNS的服务地址--cluster-domain=cluster.local
,为在dns服务中设置的域名
2. 部署CoreDNS服务
- 创建ConfigMap。主要设置CoreDNS的主配置文件Corefile的内容,其中可以定义各种域名的解析方式和使用的插件。
apiVersion: v1kind: ConfigMapmetadata: name: coredns namespace: kube-system labels: addonmanager.kubernetes.io/mode: EnsureExistsdata: Corefile: | cluster.local { errors health { lameduck 5s } ready kubernetes cluster.local 169.169.0.0/16 { fallthrough in-addr.arpa ip6.arpa } prometheus :9153 forward . /etc/resolv.conf cache 30 loop reload loadbalance } . { cache 30 loadbalance forward . /etc/resolv.conf }
- 创建deployment。
apiVersion: apps/v1kind: Deploymentmetadata: name: coredns namespace: kube-system labels: k8s-app: kube-dns kubernetes.io/name: \"CoreDNS\"spec: replicas: 1 strategy: type: RollingUpdate rollingUpdate: maxUnavailable: 1 selector: matchLabels: k8s-app: kube-dns template: metadata: labels: k8s-app: kube-dns spec: priorityClassName: system-cluster-critical tolerations: - key: \"CriticalAddonsOnly\" operator: \"Exists\" nodeSelector: kubernetes.io/os: linux affinity: podAntiAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 100 podAffinityTerm: labelSelector: matchExpressions: - key: k8s-app operator: In values: [\"kube-dns\"] topologyKey: kubernetes.io/hostname containers: - name: coredns image: coredns/coredns:1.10.1 imagePullPolicy: IfNotPresent resources: limits: memory: 170Mi requests: cpu: 100m memory: 70Mi 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 - containerPort: 9153 name: metrics protocol: TCP securityContext: allowPrivilegeEscalation: false capabilities: add: - NET_BIND_SERVICE drop: - all readOnlyRootFilesystem: true livenessProbe: httpGet: path: /health port: 8080 scheme: HTTP initialDelaySeconds: 60 timeoutSeconds: 5 successThreshold: 1 failureThreshold: 5 readinessProbe: httpGet: path: /ready port: 8181 scheme: HTTP dnsPolicy: Default volumes: - name: config-volume configMap: name: coredns items: - key: Corefile path: Corefile
- 创建service
apiVersion: v1kind: Servicemetadata: name: kube-dns namespace: kube-system annotations: prometheus.io/port: \"9153\" prometheus.io/scrape: \"true\" labels: k8s-app: kube-dns kubernetes.io/cluster-service: \"true\" kubernetes.io/name: \"CoreDNS\"spec: selector: k8s-app: kube-dns clusterIP: 169.169.0.100 ports: - name: dns port: 53 protocol: UDP - name: dns-tcp port: 53 protocol: TCP - name: metrics port: 9153 protocol: TCP
3. 验证
使用带有nslookup工具的pod来验证dns服务是否正常(《kubernetes权威指南》中用的busybox,但自己测试不行,所以改成了用ubuntu镜像,pod起来后在容器中安装nslookup。):
# pod-ubuntu.yamlapiVersion: v1kind: Podmetadata: name: ubuntu namespace: defaultspec: containers: - name: ubuntu image: ubuntu:22.04 command: - sleep - \"3600\"
- 先创建pod
kubectl create -f pod-ubuntu.yaml
- 容器启动后,执行命令测试
apt updateapt install -y dnsutilsnslookup svc-nginx
Corefile配置说明
CoreDNS的主要功能是通过插件系统实现的。常用插件如下: