首页 > 其他分享 >使用CoreDNS自建dns

使用CoreDNS自建dns

时间:2022-10-12 00:56:55浏览次数:74  
标签:fi grep 自建 baseDir scriptDir dns coredns CoreDNS echo

前言

公司有些内网服务需要使用域名访问,安装bind比较麻烦,故使用coredns实现域名服务。

IP 说明
192.168.0.41 安装dns,作为dns服务器
192.168.0.20 测试服务器

安装

有docker和预编译二进制文件等更多方式,这里采用预编译二进制文件进行安装。
下载地址:https://github.com/coredns/coredns/releases

运行

  1. 编写配置文件 Corefile
.:53 {
	# 绑定本机IP
    bind 192.168.0.41
    # 因为需要用域名的服务比较少,这里就直接使用hosts方式。
    # 如果需要配置大量域名,建议使用文件方式
    hosts {
        192.168.0.41 web.local.com
        ttl 60
        reload 1m
        fallthrough
    }
    # 最后所有的都转发到系统配置的上游dns服务器去解析
    forward . /etc/resolv.conf
    # 缓存时间ttl
    cache 120
    # 自动加载配置文件的间隔时间
    reload 6s
    # 输出日志
    log
    # 输出错误
    errors
}
  1. 运行。这里写了脚本来启动

目录结构如下:

├── bin
│   ├── coredns
│   └── start.sh
├── conf
│   └── Corefile
└── logs
    └── start.log

启动脚本示例(注意,因为默认监听53端口,所以需要使用root用户启动)

#!/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
  1. 启动
./start.sh

测试

  1. 修改测试服务器的/etc/resolv.conf,示例:
nameserver 192.168.0.41
  1. ping测试
ping -c4 web.local.com
ping -c4 www.baidu.com
  1. 如果上一步都正常响应的话,则说明成功。

参考

补充

停止脚本

#!/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
	
	# 检查进程是否已存在, 存在则退出
	ps -ef | grep -v grep | grep ${scriptDir}/coredns > /dev/null
	if [[ $? -ne 0 ]]; then
		echo "coredns is not running"
		exit 1
	fi
}

function stopApp(){
	if [[ -f ${pidFile} ]]; then
		kill $(cat ${pidFile})
		rm -f ${pidFile}
	else
		local pid=$(ps -ef | grep -v grep | grep ${scriptDir}/coredns | awk '{print $2}')
		kill ${pid}
	fi
}

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 [[ $? -ne 0 ]]; then
		echo "coredns is stopped"
	fi
}

function main(){
	prepare
	stopApp
	check
}

main

标签:fi,grep,自建,baseDir,scriptDir,dns,coredns,CoreDNS,echo
From: https://www.cnblogs.com/XY-Heruo/p/16783123.html

相关文章

  • Centos7 ping 未知的名称或服务 DNS 配置问题
    Centos7ping未知的名称或服务DNS配置问题Centos7的DNS配置有以下几种方法:方法一:先确定登录的身份是否是root用户,如果不是,最好贴换为root用户输入vi /etc/sys......
  • 3、dns服务搭建
    3.1、dns服务简介:1、DNS(DomainNameSystem)域名系统。目前提供网络服务的应用使用唯一的32位的IP地址来标识,但是由于数字比较复杂、难以记忆,因此产生了域名系统(DNS),通过......
  • @网络通信及dns解析
    网络通信文章目录​​网络通信​​​​一.网络通信的实现​​​​二.通信实现的过程​​​​三.DNS域名解析​​​​1.dns的出现​​​​2.dns的作用​​​​3.dns的查询​......
  • 网络安全(一)主动进攻之DNS基础和ettercap实现DNS流量劫持
    alittlemc,个人原创,个人理解和观点。若有错误、不理解请与我联系,谢谢!介绍了DNS的解析过程。DNS劫持的思路和实践。DNS域名以为live.bilibili.com为例子,从后到前依次......
  • 更换dns,加快网页加载速度
    阿里DNS223.5.5.5223.6.6.6腾讯DNS119.29.29.29182.254.116.116百度DNS180.76.76.76自己输入DNS地址。  输入完成,点击确定即可。可以用ping+ip的方式,确定自己所在......
  • Ubuntu部署DNS服务器
    Bind服务1.安装服务aptinstall-ybind92.配置定义域文件vim/etc/bind/named.conf.default-zones'''zone"con.org"{typemaster;file"/etc/bind/d......
  • DNS之bind服务
    1.DNS之bind部署配置1.1安装bind安装命令dnfinstall-ybindbind-libsbind-utilsbind-chroot安装包解释bind服务器bind-libs相关库bind-util......
  • 自建git服务
    自建Git服务由于工作或者自学的需求,我们有时候需要在内网创建一个Git服务方便保存代码和部署应用。有一个轻量级易部署的开源项目gitea,不仅功能适用还支持跨平台。GitHub......
  • 自建API接口管理平台的产品脑图和解决方案
    API接口管理平台如果需要自建API接口管理平台,首先要定位和明确需要给谁(开发者是谁)、以什么方式(免费/付费)、提供什么接口(内部接口,数据接口还是上游供应商的API接口)。站在产品......
  • 【非插件实现】wordpress自建网站页脚底部添加网站总访问次数/今日访客数/当前访问者
    1/**2*统计全站总访问量/今日总访问量/当前是第几个访客3*@return[type][description]4*/5functionwb_site_count_user(){6$addnum=1;//初始化......