写一个脚本实现 主dns的搭建
脚本名:/scripts/master-dns.sh
zone: xuepeng.com
master.xuepeng.com NS 192.168.43.9
slave.xuepeng.com NS 192.168.43.19
#!/bin/bash
. /etc/init.d/functions
package="bind"
name_conf="/etc/named.conf"
name_zone="/etc/named.rfc1912.zones"
zone="xuepeng.com"
zone_file="/var/named/${zone}.zone"
ip=192.168.43.9
slave_ip=192.168.43.19
rpm -q $package &> /dev/null
if [ $? -ne 0 ];then
yum -q install $package -y && action "package bind install seccessful" true
fi
sed -i.bak -e '/listen-on/s@^@#@' -e '/allow-query/s@^@#@' -e '/allow-query/a\\tallow-transfer { '"$slave_ip"'; };' $name_conf
sed -i.bak '$a\zone "'"$zone"'" IN {\n\ttype master;\n\tfile "'"$zone"'.zone";\n};' ${name_zone}
cat < ${zone_file}
\$TTL 1D
@ IN SOA master admin ( `date +%F|tr -dc [:digit:]` 1D 1H 1W 3H )
NS master
NS slave
master A $ip
slave A ${slave_ip}
EOF
chgrp named ${zone_file}
chmod 640 ${zone_file}
named-checkconf &> /dev/null && named-checkzone $zone ${zone_file} &> /dev/null \
&& systemctl enable --now named && action "service named started" true
写一个脚本实现 从dns的搭建
脚本名:/scripts/slave-dns.sh
zone: xuepeng.com
#!/bin/bash
. /etc/init.d/functions
package="bind"
name_conf="/etc/named.conf"
name_zone="/etc/named.rfc1912.zones"
zone="xuepeng.com"
zone_file="/var/named/${zone}.zone"
ip=192.168.43.9
rpm -q $package &> /dev/null
if [ $? -ne 0 ];then
yum -q install $package -y && action "package bind install seccessful" true
fi
sed -i.bak -e '/listen-on/s@^@#@' -e '/allow-query/s@^@#@' -e '/allow-query/a\\tallow-transfer { none; };' $name_conf
sed -i.bak '$a\zone "'"$zone"'" IN {\n\ttype slave;\n\tmasters { '"$ip"'; };\n\tfile "slaves/'"$zone"'.zone";\n};' ${name_zone}
named-checkconf &> /dev/null && systemctl enable --now named && action "service named started" true
number=$[`egrep -m 1 -o -w '[[:digit:]]+' /var/named/xuepeng.com.zone`+1]
echo "www A 192.168.43.100" >> /var/named/xuepeng.com.zone
sed -r -i '/SOA/s@\<[[:digit:]]+\>@'"$number"'@' /var/named/xuepeng.com.zone
systemctl restart named
#!/bin/bash
. /etc/init.d/functions
rpm -q httpd &> /dev/null
if [ $? -ne 0 ];then
yum -q install httpd -y && action "package httpd is installed"
fi
cat <<-EOF > /etc/httpd/conf.d/test.conf
documentroot /var/www/html
servername www.xuepeng.com
require all granted
EOF
echo "www.xuepeng.com" > /var/www/html/index.html
killall -0 httpd &> /dev/null || systemctl enable --now httpd && action "service httpd started"
在客户端上测试
ip: 192.168.43.49/24
dns: 192.168.43.19
ip规划
vmnet0 192.168.1.0/24 ens37: 192.168.1.53
vmnet8 192.168.43.0/24 ens33: 192.168.43.53
vment2 192.168.2.0/24 ens38: 192.168.2.53
写一个脚本,实现智能dns
#!/bin/bash
. /etc/init.d/functions
# definition var
package="bind"
name_conf="/etc/named.conf"
name_zone="/etc/named.rfc1912.zones"
zone="xuepeng.com"
zone_file="/var/named/${zone}.zone"
Area1="BeiJing"
Area2="ShangHai"
Area3="ShenZhen"
ip1=192.168.1.53
ip2=192.168.2.53
ip3=192.168.43.53
# install package bind
rpm -q $package &> /dev/null
if [ $? -ne 0 ];then
yum -q install $package -y && action "package bind install seccessful" true
fi
# named.conf
sed -i.bak -e '/listen-on/s@^@#@' -e '/allow-query/s@^@#@' $name_conf
sed -i '/zone/,$d' $name_conf
cat <<-EOF >> $name_conf
acl $Area1 {
192.168.1.0/24;
};
acl $Area2 {
192.168.2.0/24;
};
acl $Area3 {
192.168.43.0/24;
};
view view_$Area1 {
match-clients { $Area1; };
include "/etc/named.rfc1912.zones.$Area1";
};
view view_$Area2 {
match-clients { $Area2; };
include "/etc/named.rfc1912.zones.$Area2";
};
view view_$Area3 {
match-clients { $Area3; };
include "/etc/named.rfc1912.zones.$Area3";
};
EOF
# /etc/named.rfc1912.zones
cp -p $name_zone $name_zone.$Area1
cat <<-EOF >> ${name_zone}.$Area1
zone "." IN {
type hint;
file "named.ca";
};
zone "$zone" IN {
type master;
file "${zone}.zone.${Area1}";
};
EOF
cp -p ${name_zone}.${Area1} ${name_zone}.$Area2
sed -i 's@'"$Area1"'@'"$Area2"'@' ${name_zone}.$Area2
cp -p ${name_zone}.${Area1} ${name_zone}.$Area3
sed -i 's@'"$Area1"'@'"$Area3"'@' ${name_zone}.$Area3
# /var/named/xuepeng.com.zone
cat < ${zone_file}.$Area1
\$TTL 1D
@ IN SOA master admin ( `date +%F|tr -dc [:digit:]` 1D 1H 1W 3H )
NS master
master A $ip1
www A 192.168.1.80
EOF
cat < ${zone_file}.$Area2
\$TTL 1D
@ IN SOA master admin ( `date +%F|tr -dc [:digit:]` 1D 1H 1W 3H )
NS master
master A $ip2
www A 192.168.2.80
EOF
cat < ${zone_file}.$Area3
\$TTL 1D
@ IN SOA master admin ( `date +%F|tr -dc [:digit:]` 1D 1H 1W 3H )
NS master
master A $ip3
www A 192.168.43.80
EOF
# check
named-checkconf && \
for i in ${zone_file}.$Area1 ${zone_file}.$Area2 ${zone_file}.$Area3;do
named-checkzone $zone $i
done
# start service
if [ $? -eq 0 ];then
systemctl enable --now named
action "service named started" true
else
action "service named not started" false
fi
运行脚本
#!/bin/bash
#
. /etc/init.d/functions
id mysql &> /dev/null || useradd -r -s /sbin/nologin -d /data/mysql mysql
mkdir -p /data/mysql
chown -R mysql.mysql /data/mysql
yum install bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-devel gcc gcc-c++ cmake ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel libaio-devel -y
work_dir=`pwd`
packagename="mariadb-10.4.12.tar.gz"
cd $work_dir
if [ -f $work_dir/$packagename ];then
action "package mariadb is ok"
else
action "not install package" false
exit
fi
tar xf $packagename && action "$packagename unzip"
builddir=${packagename/".tar.gz"}
cd $builddir
cmake . -DCMAKE_INSTALL_PREFIX=/app/mysql -DMYSQL_DATADIR=/data/mysql/ \
-DSYSCONFDIR=/etc/mysql -DMYSQL_USER=mysql -DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 -DWITHOUT_MROONGA_STORAGE_ENGINE=1 \
-DWITH_DEBUG=0 -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LIBWRAP=0 \
-DENABLED_LOCAL_INFILE=1 -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
make -j `lscpu|grep -w "^CPU(s):"|awk '{print $2}'` && make install
if [ $? -eq 0 ];then
action "$builddir install successful"
fi
运行脚本之后截图
#!/bin/bash
#
. /etc/init.d/functions
buildir="/usr/local/src/mariadb-10.4.12/"
basedir="/app/mysql"
datadir="/data/mysql"
user="mysql"
cfgdir="/etc/mysql"
chown -R root.root $buildir
if [ ! -d $cfgdir ];then
mkdir $cfgdir
fi
cat <<-EOF > $cfgdir/my.cnf
[client]
port = 3306
socket = $datadir/mysql.sock
[mysqld]
port = 3306
socket = $datadir/mysql.sock
user = mysql
basedir = $basedir
datadir = $datadir
log_error= $datadir/mysql.err
pid-file = $datadir/mysql.pid
log-bin = $datadir/mysql
skip_name_resolve = 1
EOF
cd $buildir && ./scripts/mysql_install_db --basedir=$basedir --datadir=$datadir --user=$user --defaults-file=$cfgdir/my.cnf && action "init mariadb successful" true
cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
echo 'PATH=/app/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
service mysqld start