前言

现需要为公司搭建私有DNS,私有服务器都使用私有DNS的地址,便于访问内部自定义的域名。采用CoreDNS + ETCD方案部署,coredns和etcd都以三实例运行,etcd为集群模式,使用nginx做coredns的udp负载均衡,避免单机性能问题。另使用prometheus监控coredns和etcd。

本文中的etcd、coredns、prometheus都以二进制方式运行,也可以用docker容器。

环境信息

IP 系统版本 应用 备注
192.168.0.10 CentOS 7.9 x86_64 Nginx 1.21 udp负载均衡
192.168.0.11 CentOS 7.9 x86_64 coredns v1.10.0, etcd v3.5.4
192.168.0.12 CentOS 7.9 x86_64 coredns v1.10.0, etcd v3.5.4
192.168.0.13 CentOS 7.9 x86_64 coredns v1.10.0, etcd v3.5.4
192.168.0.14 CentOS 7.9 x86_64 prometheus

大致架构如下图

flowchart LR
client --> nginx["nginx-192.168.0.10:53"]
subgraph coredns+etcd集群
nginx["nginx-192.168.0.10:53"] --> dns11["coredns-192.168.0.11:53"]
nginx --> dns12["coredns-192.168.0.12:53"]
nginx --> dns13["coredns-192.168.0.13:53"]
dns11 --> etcd11["etcd-192.168.0.11:2379"]
dns11 --> etcd12["etcd-192.168.0.12:2379"]
dns11 --> etcd13["etcd-192.168.0.13:2379"]
dns12 --> etcd11
dns12 --> etcd12
dns12 --> etcd13
dns13 --> etcd11
dns13 --> etcd12
dns13 --> etcd13
end

步骤

1. 部署etcd

  1. 官方下载etcd的二进制压缩包,将解压后目录内的二进制文件放到/usr/local/bin
  2. 找个空目录,执行启动脚本。注意修改每个etcd服务器的IP。脚本里面用python自动获取本机ip,然后根据ip启动对应的etcd。(PS:其实脚本里面的启动函数写重复了,只需要写一个然后传参就行了。)
#!/bin/bash

set -u
script_dir=$(cd $(dirname $0) && pwd) # 注意修改IP
etcd1IP='192.168.0.11'
etcd2IP='192.168.0.12'
etcd3IP='192.168.0.13'
etcdClusterToken='etcd-cluster-1' function getLocalIP(){
# 通过python获取本机IP, 如果新的linux发行版默认没有python2, 注意更改为python3
cat > /tmp/getLocalIP.py <<EOF
#!/usr/bin/env python
import socket s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 53))
print(s.getsockname()[0])
EOF
IP=$(python /tmp/getLocalIP.py)
} function start() {
nohup etcd --name $1 \
--listen-client-urls http://$2:2379 \
--advertise-client-urls http://$2:2379 \
--listen-peer-urls http://$2:2380 \
--initial-advertise-peer-urls http://$2:2380 \
--initial-cluster-token ${etcdClusterToken} \
--initial-cluster="etcd1=http://${etcd1IP}:2380,etcd2=http://${etcd2IP}:2380,etcd3=http://${etcd3IP}:2380" \
--initial-cluster-state 'new' \
--enable-pprof \
--logger 'zap' \
--log-outputs=stderr \
--data-dir="${script_dir}/data" > ${script_dir}/logs/app.log 2>&1 &
} function main() {
mkdir -p ./{data,logs}
getLocalIP
case $IP in
$etcd1IP)
start etcd1 ${IP}
;;
$etcd2IP)
start etcd2 ${IP}
;;
$etcd3IP)
start etcd3 ${IP}
;;
*)
echo "Unknown Node ip"
esac
} main
  1. 测试集群是否正常
# 如果正常则全部显示successfully
etcdctl --endpoints http://192.168.0.11:2379,http://192.168.0.12:2379,http://192.168.0.13:2379 endpoint health

2. 部署coredns

  1. 从官方github仓库下载二进制包,个人一般会把coredns的二进制文件放到coredns/bin目录下
  2. 编写配置文件coredns.conf/Corefile。注意修改本机IP和etcd实例的host
.:53 {
# 绑定本机IP
bind 192.168.0.11
etcd {
path /coredns
endpoint http://192.168.0.11:2379 http://192.168.0.12:2379 http://192.168.0.13:2379
fallthrough
}
# 最后所有的都转发到系统配置的上游dns服务器去解析
forward . /home/apps/coredns/conf/forwards
# 缓存时间ttl
cache 1800
# 自动加载配置文件的间隔时间
reload 6s
# 输出日志
#log
# 输出错误
errors
# 监控
prometheus 192.168.0.11:19097
}

其中forwards内容如下:

nameserver 223.6.6.6
nameserver 223.5.5.5
  1. 通过脚本启动coredns
#!/bin/bash
# description: 启动CoreDNS set -u scriptDir=$(cd $(dirname $0) && pwd)
baseDir=$(cd ${scriptDir}/.. && pwd)
pidFile=${baseDir}/logs/app.pid function prepare(){
# 检查当前用户是否为root
if [[ $(whoami) != "root" ]]; then
echo "please use root privilege"
exit 1
fi # 检查是否存在配置文件, 无则报错退出
if [[ ! -f ${baseDir}/conf/Corefile ]]; then
echo "${baseDir}/conf/Corefile not found"
exit 1
fi # 检测是否存在日志目录, 无则创建
if [[ ! -d ${baseDir}/logs ]]; then
mkdir -p ${baseDir}/logs
fi # 检查进程是否已存在, 存在则退出
ps -ef | grep -v grep | grep ${scriptDir}/coredns > /dev/null
if [[ $? -eq 0 ]]; then
echo "coredns is running"
exit 1
fi
} function startApp(){
nohup ${scriptDir}/coredns --conf ${baseDir}/conf/Corefile \
-pidfile ${pidFile} > ${baseDir}/logs/start.log 2>&1 &
} function check(){
# 检查是否正常启动
for i in $(seq 2); do
echo "checking coredns whether is running or not ..."
sleep 1
done
ps -ef | grep -v grep | grep ${scriptDir}/coredns > /dev/null
if [[ $? -eq 0 ]]; then
echo "coredns is running"
fi
} function main(){
prepare
startApp
check
} main

3. nginx配置udp负载均衡

PS:强调一点,nginx早就支持tcp和udp的四层网络代理转发了,别再听信一些老掉牙的教程说nginx只支持http的七层网络代理转发。

nginx在stream域的配置udp的网络转发,以下为示例

stream {
upstream coredns {
server 192.168.0.11:53;
server 192.168.0.12:53;
server 192.168.0.13:53;
} server {
listen 53 udp;
proxy_pass coredns;
}
}

4. 测试dns是否可用

  1. 通过etcdctl添加一条dns解析记录。这里将zhangsan.com解析到192.168.0.10
etcdctl --endpoints http://192.168.0.11:2379,http://192.168.0.12:2379,http://192.168.0.13:2379 put /coredns/com/zhangsan/x1 '{"host":"192.168.0.10", "ttl": 60}'
  1. 使用nslookup工具测试。如果提示没有命令,centos 可安装bind-utils,ubuntu可安装dns-utils
nslookup zhangsan.com 192.168.0.10
  1. 如果上条命令正常返回解析结果,说明coredns集群搭建完成,后面新增服务器只需要将dns配置为nginx的地址即可。

5. 配置prometheus监控

prometheus的安装和使用略过,以下仅为etcd和coredns的监控配置示例,使用的是基于文件的服务发现。

  - job_name: "etcd"
file_sd_configs:
- files: ['/home/apps/prometheus/sd_configs/etcd/*.yaml']
refresh_interval: 10s
- job_name: "coredns"
file_sd_configs:
- files: ['/home/apps/prometheus/sd_configs/coredns/*.yaml']
refresh_interval: 10s

sd_configs/etcd/nodes.yaml内容如下:

- targets: ['192.168.0.11:2379']
labels:
instance: 192.168.0.11 - targets: ['192.168.0.12:2379']
labels:
instance: 192.168.0.12 - targets: ['192.168.0.13:2379']
labels:
instance: 192.168.0.13

sd_configs/coredns/nodes.yaml内容如下:

- targets: ['192.168.0.11:19097']
labels:
instance: 192.168.0.11 - targets: ['192.168.0.12:19097']
labels:
instance: 192.168.0.12 - targets: ['192.168.0.13:19097']
labels:
instance: 192.168.0.13

之后去grafana官网找个合适的dashboard进行import即可。

部署基于etcd的coredns集群的更多相关文章

  1. Centos 7.9 基于二进制文件部署kubernetes v1.25.5集群

    简述 Kubernetes(简称为:k8s)是Google在2014年6月开源的一个容器集群管理系统,使用Go语言开发,用于管理云平台中多个主机上的容器化的应用,Kubernetes的目标是让部署容器 ...

  2. 基于Hadoop2.7.3集群数据仓库Hive1.2.2的部署及使用

    基于Hadoop2.7.3集群数据仓库Hive1.2.2的部署及使用 HBase是一种分布式.面向列的NoSQL数据库,基于HDFS存储,以表的形式存储数据,表由行和列组成,列划分到列族中.HBase ...

  3. 企业运维实践-还不会部署高可用的kubernetes集群?使用kubeadm方式安装高可用k8s集群v1.23.7

    关注「WeiyiGeek」公众号 设为「特别关注」每天带你玩转网络安全运维.应用开发.物联网IOT学习! 希望各位看友[关注.点赞.评论.收藏.投币],助力每一个梦想. 文章目录: 0x00 前言简述 ...

  4. 基于zookeeper的Swarm集群搭建

    简介 Swarm:docker原生的集群管理工具,将一组docker主机作为一个虚拟的docker主机来管理. 对客户端而言,Swarm集群就像是另一台普通的docker主机. Swarm集群中的每台 ...

  5. 使用 Kubeadm 安装部署 Kubernetes 1.12.1 集群

    手工搭建 Kubernetes 集群是一件很繁琐的事情,为了简化这些操作,就产生了很多安装配置工具,如 Kubeadm ,Kubespray,RKE 等组件,我最终选择了官方的 Kubeadm 主要是 ...

  6. 庐山真面目之十二微服务架构基于Docker搭建Consul集群、Ocelot网关集群和IdentityServer版本实现

    庐山真面目之十二微服务架构基于Docker搭建Consul集群.Ocelot网关集群和IdentityServer版本实现 一.简介      在第七篇文章<庐山真面目之七微服务架构Consul ...

  7. Tomcat:基于Apache+Tomcat的集群搭建

    根据Tomcat的官方文档说明可以知道,使用Tomcat配置集群需要与其它Web Server配合使用才可以完成,典型的有Apache和IIS. 这里就使用Apache+Tomcat方式来完成基于To ...

  8. 基于Kubernetes的WAF集群介绍

    Kubernetes是Google开源的容器集群管理系统.它构建Docker技术之上,为容器化的应用提供资源调度.部署运行.服务发现.扩容缩容等整一套功能,可看作是基于容器技术的PaaS平台. 本文旨 ...

  9. 基于prometheus监控k8s集群

    本文建立在你已经会安装prometheus服务的基础之上,如果你还不会安装,请参考:prometheus多维度监控容器 如果你还没有安装库k8s集群,情参考: 从零开始搭建基于calico的kuben ...

  10. [原创]自动化部署K8S(v1.10.11)集群

          标准运维实现自动化部署K8S集群主要分两步,第一步是部署gse-agent,拱第二步执行部署. 第一步:部署gse-agent.如下: 第二步:部署k8s集群.主要通过作业平台分为5小步执 ...

随机推荐

  1. 理解 React 中的 useEffect、useMemo 与 useCallback

    useEffect 先理解 useEffect 有助于学习 useMemo 和 useCallback.因为 useMemo 和 useCallback 的实现实际上都是基于 useEffect 的. ...

  2. vue3 + vite 多项目多模块打包

    vue3 + vite 多项目多模块打包 本示例基于vite-plugin-html插件,实现多个独立项目共存,共享组件和依赖,运行.打包互不干扰. npm create vite@latest 兼容 ...

  3. 我做了一个 VSCode 插件版的 ChatGPT

    大家好,我是风筝 其实很早之前就想学学 VSCode 插件开发了,但是又不知道做什么,加上我这半吊子前端水平,迟迟没有动手. 最近 ChatGPT 火的一塌糊涂,我也一直在用,真的非常好用,有些问题之 ...

  4. 2022-09-22:以下go语言代码输出什么?A:5、B:不能编译;C:运行时死锁。 package main import ( “fmt“ “time“ ) func main

    2022-09-22:以下go语言代码输出什么?A:5.B:不能编译:C:运行时死锁. package main import ( "fmt" "time" ) ...

  5. 2022-07-21:给定一个字符串str,和一个正数k, 你可以随意的划分str成多个子串, 目的是找到在某一种划分方案中,有尽可能多的回文子串,长度>=k,并且没有重合。 返回有几个回文子串。 来

    2022-07-21:给定一个字符串str,和一个正数k, 你可以随意的划分str成多个子串, 目的是找到在某一种划分方案中,有尽可能多的回文子串,长度>=k,并且没有重合. 返回有几个回文子串 ...

  6. 2021-09-03:直线上最多的点数。给你一个数组 points ,其中 points[i] = [xi, yi] 表示 X-Y 平面上的一个点。求最多有多少个点在同一条直线上。力扣149。

    2021-09-03:直线上最多的点数.给你一个数组 points ,其中 points[i] = [xi, yi] 表示 X-Y 平面上的一个点.求最多有多少个点在同一条直线上.力扣149. 福大大 ...

  7. SqliLabs 第六关 报错注入!!!

    点开网址,首先看到一个页面,首先尝试闭合字符 id=1 报错 id=1' 报错 id=1"成功 然后开始爆字段?id=1" order by 3 --+ 发现有三个字段然后查询显示 ...

  8. flutter填坑之旅(环境搭建篇--mac系统)

    上次配置过Mac到flutter环境,但是由于最近系统更新了,什么都没了又得从新配置,发现自己竟然好多都忘记了,看来还是得把它记下来才行 在Mac上安装并运行Flutter 最低要求: 操作系统:ma ...

  9. Python基础 - 输入和输出

    输出   Python提供了 print() 内置函数完成输出 1 print("你好") 2 3 4 # 你好 n = "你好" print(n) # 你好 ...

  10. SpringIOC和SpringAOP

    作为一个Spring使用者条件: 拥有深入的Spring框架知识和开发经验,能够熟练地运用Spring框架来构建复杂的应用程序. 了解Spring框架的核心概念和设计思想,如控制反转(IoC).依赖注 ...