• 利用CoreDNS实现DNS负载均衡


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

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

    一、CoreDNS集成etcd插件

    实验CoreDNS镜像版本:1.7.0

    1.将etcd证书添加至CoreDNS容器中

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

    生成证书configmap:

    1. # etcd证书
    2. kubectl create cm coredns-etcd-pem --from-file /etc/kubernetes/pki/etcd/etcd.pem -n kube-system
    3. # etcd证书key
    4. kubectl create cm coredns-etcd-key --from-file /etc/kubernetes/pki/etcd/etcd-key.pem -n kube-system
    5. # etcd ca证书
    6. kubectl create cm coredns-etcd-ca -n kube-system --from-file=/etc/kubernetes/pki/etcd/etcd-ca.pem

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

    1. # 添加证书configmap volume
    2. volumes:
    3. - name: config-volume
    4. configMap:
    5. name: coredns
    6. items:
    7. - key: Corefile
    8. path: Corefile
    9. - name: coredns-etcd-key
    10. configMap:
    11. name: coredns-etcd-key
    12. - name: coredns-etcd-pem
    13. configMap:
    14. name: coredns-etcd-pem
    15. - name: coredns-etcd-ca
    16. configMap:
    17. name: coredns-etcd-ca
    18. # 挂载证书volume
    19. volumeMounts:
    20. - name: config-volume
    21. mountPath: /etc/coredns
    22. readOnly: true
    23. - name: coredns-etcd-key
    24. mountPath: /etc/etcd-key
    25. readOnly: true
    26. - name: coredns-etcd-pem
    27. mountPath: /etc/etcd-pem
    28. readOnly: true
    29. - name: coredns-etcd-ca
    30. mountPath: /etc/etcd-ca
    31. readOnly: true

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

    1. apiVersion: v1
    2. kind: ConfigMap
    3. metadata:
    4. name: coredns
    5. namespace: kube-system
    6. data:
    7. Corefile: |
    8. .:53 {
    9. errors
    10. health {
    11. lameduck 5s
    12. }
    13. ready
    14. kubernetes cluster.local in-addr.arpa ip6.arpa {
    15. fallthrough in-addr.arpa ip6.arpa
    16. }
    17. #开始配置etcd插件
    18. etcd {
    19. stubzones
    20. path /coredns
    21. # etcd集群端点地址
    22. endpoint 192.16.58.101:2379 192.16.58.102:2379 192.16.58.103:2379
    23. upstream /etc/resolv.conf
    24. # 配置访问etcd证书,注意顺序一定要正确(无证书删除此配置即可)
    25. tls /etc/etcd-pem/etcd.pem /etc/etcd-key/etcd-key.pem /etc/etcd-ca/etcd-ca.pem
    26. fallthrough
    27. }
    28. prometheus :9153
    29. forward . /etc/resolv.conf {
    30. max_concurrent 1000
    31. }
    32. cache 30
    33. loop
    34. reload
    35. loadbalance
    36. }

    重启CoreDNS服务

    1. kubectl delete -f coredns.yaml
    2. kubectl create -f corends.yaml

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

    1. # kubectl logs -f -n kube-system coredns-84c9869688-rfc88
    2. .:53
    3. [INFO] plugin/reload: Running configuration MD5 = da836b65e7cc004a3545c13c46b1f328
    4. CoreDNS-1.7.0
    5. linux/amd64, go1.14.4, f59c03d

    二、添加DNS A记录

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

    1. #通过rest接口向etcd中添加coredns A记录
    2. export ETCDCTL_API=3
    3. etcdctl put /coredns/com/test/www/ep1 '{"host":"192.16.58.114","ttl":10}'
    4. etcdctl put /coredns/com/test/www/ep2 '{"host":"192.16.58.115","ttl":10}'
    5. etcdctl put /coredns/com/test/www/ep3 '{"host":"192.16.58.116","ttl":10}'

    其中配置了www.test.com对应了192.16.58.114 ~ 116三个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

    三、验证A记录解析

    通过dig验证是否生效

    安装dig: 

    yum install -y bind-utils

    查看coredns服务地址:

    1. # kubectl get svc -n kube-system | grep dns
    2. kube-dns ClusterIP 10.96.0.10 53/UDP,53/TCP,9153/TCP 4h17m

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

    1. # dig @10.96.0.10 www.test.com +short
    2. 192.16.58.115
    3. 192.16.58.116
    4. 192.16.58.114

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

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

    四、参考资源

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

  • 相关阅读:
    2024电脑录屏软件排行第一Camtasia喀秋莎
    基于SSM的化妆品购物商城/美妆销售系统
    2022/12/4 胡言乱语
    STM32——一线协议之DS18B20温度采样
    唐岩重新出山任挚文CEO:仍难挡陌陌颓势 营收持续下滑
    低代码在物品领用领域数字化转型的案例分析
    GreenPlum列存解密
    c++ 类内可以定义引用数据成员吗?
    面试心经
    JavaScript Date对象中的常用方法有哪些?
  • 原文地址:https://blog.csdn.net/u013189824/article/details/126248874