CoreDNS实战(三)-CoreDNS+ETCD实现DNS负载均衡

1 概述

DNS负载均衡简单来说就是通过一个域名绑定多个IP地址,当客户端访问域名时,DNS服务器将轮询返回其中一个IP,实现客户端分流的作用。

K8s环境中CoreDNS作为容器服务的DNS服务器,那么就可以通过CoreDNS来实现DNS负载均衡,主要过程如下:

2 CoreDNS集成etcd插件

实验CoreDNS镜像版本:1.7.0

2.1 将etcd证书添加至CoreDNS容器中

一般情况下,部署的etcd集群服务都带有证书,CoreDNS Pod需要携带相关证书才能正确访问etcd集群服务,假如你的etcd集群未开启证书,可省略这一步。

生成证书configmap:

复制代码
# etcd证书
kubectl create cm coredns-etcd-pem --from-file /etc/kubernetes/pki/etcd/etcd.pem -n kube-system
# etcd证书key
kubectl create cm coredns-etcd-key --from-file /etc/kubernetes/pki/etcd/etcd-key.pem -n kube-system
# etcd ca证书
kubectl create cm coredns-etcd-ca -n kube-system --from-file=/etc/kubernetes/pki/etcd/etcd-ca.pem

修改coredns.yaml,将证书configmap挂载到coredns容器内部:

复制代码
# 添加证书configmap volume
      volumes:
        - name: config-volume
          configMap:
            name: coredns
            items:
              - key: Corefile
                path: Corefile
        - name: coredns-etcd-key
          configMap:
            name: coredns-etcd-key
        - name: coredns-etcd-pem
          configMap:
            name: coredns-etcd-pem
        - name: coredns-etcd-ca
          configMap:
            name: coredns-etcd-ca
# 挂载证书volume
          volumeMounts:
            - name: config-volume
              mountPath: /etc/coredns
              readOnly: true
            - name: coredns-etcd-key
              mountPath: /etc/etcd-key
              readOnly: true
            - name: coredns-etcd-pem
              mountPath: /etc/etcd-pem
              readOnly: true
            - name: coredns-etcd-ca
              mountPath: /etc/etcd-ca
              readOnly: true

2.2 修改coredns.yaml,添加coredns-etcd插件

复制代码
apiVersion: v1
kind: ConfigMap
metadata:
  name: coredns
  namespace: kube-system
data:
  Corefile: |
    .:53 {
        errors
        health {
          lameduck 5s
        }
        ready
        kubernetes cluster.local in-addr.arpa ip6.arpa {
          fallthrough in-addr.arpa ip6.arpa
        }
        #开始配置etcd插件
        etcd {
          stubzones
          path /coredns
          # etcd集群端点地址
          endpoint 192.16.58.101:2379 192.16.58.102:2379 192.16.58.103:2379
          upstream /etc/resolv.conf
          # 配置访问etcd证书,注意顺序一定要正确(无证书删除此配置即可)
          tls /etc/etcd-pem/etcd.pem /etc/etcd-key/etcd-key.pem  /etc/etcd-ca/etcd-ca.pem
          fallthrough
        }
        prometheus :9153
        forward . /etc/resolv.conf {
          max_concurrent 1000
        }
        cache 30
        loop
        reload
        loadbalance
    }

2.3 重启CoreDNS服务

复制代码
kubectl delete -f coredns.yaml
kubectl create -f corends.yaml

观察CoreDNS服务日志,如果Pod RUNNING且日志未报错,代表配置成功:

复制代码
# kubectl logs -f -n kube-system coredns-84c9869688-rfc88 
.:53
[INFO] plugin/reload: Running configuration MD5 = da836b65e7cc004a3545c13c46b1f328
CoreDNS-1.7.0
linux/amd64, go1.14.4, f59c03d

3 添加DNS A记录

A记录意为:通过域名解析IP的规则,也称之为正向解析。

#通过rest接口向etcd中添加coredns A记录

复制代码
export ETCDCTL_API=3
etcdctl put /coredns/com/test/www/ep1 '{"host":"10.10.2.123","ttl":10}'
etcdctl put /coredns/com/test/www/ep2 '{"host":"10.10.2.124","ttl":10}'
etcdctl put /coredns/com/test/www/ep3 '{"host":"10.10.2.125","ttl":10}'

其中配置了www.test.com对应了10.10.2.123~125三个IP地址

注意:

A记录添加是反向的即www.test.com要配成 /com/test/www/,后面的ep1为自定义内容,代表www.test.com对应的3个IP记录,此时访问www.test.com就可以负载均衡的访问的3个IP了。

删除A记录:

复制代码
etcdctl delete /coredns/com/test/www/ep1

4 验证A记录解析

通过dig验证是否生效

1)安装dig:

复制代码
yum install -y bind-utils

2)查看coredns服务地址:

复制代码
# kubectl get svc -n kube-system | grep dns
kube-dns         ClusterIP   10.96.0.10    <none>        53/UDP,53/TCP,9153/TCP   4h17m

3)使用dig验证配置的A记录是否生效:

复制代码
# dig @10.96.0.10 www.test.com +short
10.10.2.123
10.10.2.124
10.10.2.125

能够正常返回3个IP地址,即代表成功

此时域名 www.test.com 已配置好DNS负载均衡,K8s中的Pod访问 www.test.com域名,将负载均衡到3个IP上。

5 附录

https://coredns.io/plugins/etcd/

相关推荐
羊小蜜.19 分钟前
Mysql 01:基础查询(SELECT)全解——从单表到多字段的完整语法
数据库·mysql·查询
猿小喵33 分钟前
记录一次从库并行回放出现死锁的问题
数据库·mysql·tdsql
随风,奔跑39 分钟前
Redis
数据库·redis·缓存
IvorySQL41 分钟前
2MB 的 PostgreSQL work_mem,如何吃掉 2TB 内存?
数据库·postgresql·开源
桑榆肖物42 分钟前
有字幕,没配音?用浏览器自带语音能力,让网页视频直接“开口说话”
数据库·edge·音视频·tts
熬夜的咕噜猫1 小时前
MySQL主从复制与读写分离
网络·数据库·mysql
道清茗1 小时前
【MySQL知识点问答题】 备份技术、Invisible Indexes 和直方图的应用
数据库·mysql
芒果披萨1 小时前
sql存储过程
java·开发语言·数据库
jnrjian1 小时前
RAC 去除node的建议 dbca 和手动方法
数据库·oracle
TlYf NTLE2 小时前
redis分页查询
数据库·redis·缓存