Linux 基础操作
一、nginx服务、nfs服务
作业:
集群搭建:
1、部署nginx反向代理三个web服务,调度算法使用加权轮询
首先安装nginx,通过yum进行源码安装
yum install epel-release -y
yum install nginx -y
关闭虚拟机克隆3台虚拟机用作提供web服务的主机,反向代理主机与其余三台web服务主机ip地址分别为:192.168.3.15、192.168.3.17、192.168.3.18、192.168.3.19
编辑反向代理主机配置文件/etc/nginx/nginx.conf
输入命令: systemctl start nginx启动所有主机nginx服务,并在网页上输入代理服务主机的uri地址http://192.168.3.15访问nginx服务,会根据加权的权重进行负载访问
集群搭建-2、所有web服务使用共享存储nfs,保证所有web都对其有读写权限,保证数据一致性
在反向代理主机上安装nfs服务软件,执行命令yum install rpcbind nfs-utils -y ,由于nfs是通过rpc进行通信的,所以web服务主机也要安装rpc服务
在nfs服务器端建立共享目录/share,修改nfs服务器端的配置文件/etc/exports,输入:/share 192.168.3.0/24(rw,sync,fsid=0)
启动rpc与nfs服务,并将其设置为开机自动启动
服务正常启动后,输入showmount -e查看nfs服务器端共享目录的状态,同时也要在客户机端输入showmount -e 192.168.3.15查看服务器端共享目录的状态
客户机端将共享目录进行挂载,并对共享目录/share增加其他人用户对其可写的权限
输入命令showmount -a 查看nfs客户端挂载状态
网页中输入nfs服务端uri地址http://192.168.3.16进行访问(由于虚机出现问题,nfs服务端ip地址变为192.168.3.16)
初次部署完成后一直报403错误,经过艳龙老师一番排错之后最终原因是因为没有关闭SELinux导致,关闭后正常。
同时也科普下有SELinux的信息,参考网址:
http://www.toxingwang.com/management/security/1011.html
开发脚本自动部署及监控-1、编写脚本自动部署反向代理、web、nfs
auto-deploy-server.sh
#!/bin/bash
# description: Deploy nginx as HTTP load balancer & NFS Server
# version: 0.0.1
# author: Elijah
# date: 2017.05.18
function auto_deploy_server(){
# First-install nginx service
echo 'Starting install epel-release,please wait...'
yum install epel-release -y
if [ $? -eq 0 ]
then
echo 'Install epel-release Successful,starting install nginx,please wait...'
elif [ $? -ne 0 ]
then
echo 'Install epel-release Fail,please check manually...'
exit
fi
yum install nginx -y
if [ $? -eq 0 ]
then
echo 'Install nginx Successful,starting install nfs service,please wait...'
elif [ $? -ne 0 ]
then
echo 'Install nginx Fail,please check manually...'
exit
fi
# Second-install nfs service
yum install rpcbind nfs-utils -y
if [ $? -eq 0 ]
then
echo 'Install nfs Successful,please wait...'
elif [ $? -ne 0 ]
then
echo 'Install nfs Fail,please check manually...'
exit
fi
# Third-setting nfs service
echo 'Install Finish,starting setting nfs service...'
msg="\ upstream my_test{\n\tserver 192.168.3.17;\n\tserver 192.168.3.18;\n\tserver 192.168.3.19;\n }"
sed -ri "/^http.*/a $msg" /etc/nginx/nginx.conf
sed -ri "/^ *location \/ \ {$/a proxy_pass http://my_test\;" /etc/nginx/nginx.conf
systemctl enable nginx
systemctl start nginx
status=`systemctl status nginx |awk 'NR==3{print $2}'`
if [ "$status" == "active" ]
then
echo 'Setting Successful! nginx service is active!'
else
echo 'Setting Failed!,please check manually...'
exit
fi
# Forth-creat share directory & starting nfs service
mkdir -p /share
chmod -R o+w /share
echo "/share 192.168.3.0/24(rw,sync,fsid=0) " > /etc/exports
systemctl enable rpcbind.service
systemctl enable nfs-server.service
systemctl start rpcbind.service
systemctl start nfs-server.service
status=`systemctl status nfs-server |awk 'NR==3{print $2}'`
if [ "$status" == "active" ]
then
echo 'Setting Successful! nfs-server service is active!'
else
echo 'Setting Failed!,please check manually...'
exit
fi
echo 'Auto deploy finished!'
}
auto_deploy_server &>> /tmp/autodeploy_monitor.log
auto-deploy-client.sh
#!/bin/bash
# description: Deploy nginx as HTTP load balancer & NFS Server
# version: 0.0.1
# author: Elijah
# date: 2017.05.18
function auto_deploy_client(){
# First-install nginx service
echo 'Starting install epel-release,please wait...'
yum install epel-release -y
if [ $? -eq 0 ]
then
echo 'Install epel-release Successful,starting install nginx,please wait...'
elif [ $? -ne 0 ]
then
echo 'Install epel-release Fail,please check manually...'
exit
fi
yum install nginx -y
if [ $? -eq 0 ]
then
echo 'Install nginx Successful,starting install nfs service,please wait...'
elif [ $? -ne 0 ]
then
echo 'Install nginx Fail,please check manually...'
exit
fi
# Second-install nfs service
yum install rpcbind nfs-utils -y
if [ $? -eq 0 ]
then
echo 'Install nfs Successful,please wait...'
elif [ $? -ne 0 ]
then
echo 'Install nfs Fail,please check manually...'
exit
fi
# Third-setting nfs client
systemctl enable nginx
systemctl enable rpcbind.service
systemctl enable nfs-server.service
systemctl start nginx
systemctl start rpcbind.service
systemctl start nfs-server.service
mount -t nfs 192.168.3.16:/share /usr/share/nginx/html/
echo 'Auto deploy finished!'
}
auto_deploy_client &>> /tmp/autodeploy_monitor.log
开发脚本自动部署及监控-2、编写监控脚本,监控集群被所有服务存活状态,内存、磁盘剩余率检测,异常则发送报警邮件
servermonitor.sh
#!/bin/bash
# description: server monitor
# version: 0.0.1
# author: Elijah
# date: 2017.05.18
#df | grep -v fs | awk "NR==$x{print $1}"
#安装bc软甲
function install_bc(){
rpm -qa |grep '^bc.*$' >> /dev/null
if [[ $? -ne 0 ]];
then
yum install bc -y
fi
}
#监控内存
function monitor_mem(){
limit_mem=30
total_mem=`free |awk 'NR==2{print $2}'`
avail_mem=`free |awk 'NR==2{print $7}'`
per_mem=`echo "scale=2;$avail_mem/$total_mem" |bc -l |cut -d. -f2`
if [ "$per_mem" -gt "$limit_mem" ]
then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig |awk 'NR==2{print $2}')
MSG:ATTENTION!!! Memory usage exceeds the limit,current value is ${per_mem}%"
echo $msg
/usr/bin/mail $msg
fi
}
#监控磁盘
function monitor_disk(){
disk='/dev/mapper/cl-root'
inode_limit=0
space_limit=10
inode_use=`df -i $disk |awk 'NR==2{print $5}'|cut -d% -f1`
space_use=`df $disk |awk 'NR==2{print $5}'|cut -d% -f1`
if [ "$inode_use" -gt "$inode_limit" ]
then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig |awk 'NR==2{print $2}')
MSG:ATTRNTION!!! Disk inode usage exceeds the limit,current value is ${inode_use}%"
echo $msg
/usr/bin/mail $msg
fi
if [ "$space_use" -gt "$space_limit" ]
then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig |awk 'NR==2{print $2}')
MSG:ATTRNTION!!! Disk space usage exceeds the limit,current value is ${space_use}%"
echo $msg
/usr/bin/mail $msg
fi
}
#监控nginx服务
function monitor_nginx(){
status_active=active
status_nginx=`systemctl status nginx |awk 'NR==3{print $2}'`
if [ "$status_nginx" != "$status_active" ]
then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig |awk 'NR==2{print $2}')
MSG:ATTRNTION!!! The nginx service is now ${status_nginx}!!"
echo $msg
/usr/bin/mail $msg
systemctl restart nginx
fi
}
#监控nfs服务
function monitor_nfs(){
ps aux | grep nfs| grep -v grep &>/dev/null
if [ $? -ne 0 ];then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(/usr/sbin/ifconfig |awk 'NR==2{print $2}')
MSG:NFS program is crash, Waiting to restart"
echo $msg
/usr/bin/my_mail $msg
systemctl restart nfs
fi
}
install_bc &>> /tmp/monitor.log
monitor_mem &>> /tmp/monitor.log
monitor_disk &>> /tmp/monitor.log
monitor_nginx &>> /tmp/monitor.log
开发脚本自动部署及监控-3、编写计划任务,定时运行监控脚本,完成监控操作
设置定时任务,每半小时执行一次脚本,写法如下:
crontab -e
/30 * * * /tmp/servermonitor.sh
查看计划任务日志:
tail -f /var/log/cron
二、网络相关配置、SSH服务、Bash命令及优先级