IP地址的规划
主机名 | eth0网卡 | eth1网卡 | 服务简介 |
---|---|---|---|
lb01 | 10.0.0.5/24 | 172.16.1.5/24 | 负载服务 |
lb02 | 10.0.0.6/24 | 172.16.1.6/24 | 负载服务 |
web01 | 10.0.0.7/24 | 172.16.1.7/24 | 动态 php 服务 |
web02 | 10.0.0.8/24 | 172.16.1.8/24 | 动态 php 服务 |
web03 | 10.0.0.9/24 | 172.16.1.9/24 | 动态 java 服务 |
db01 | 10.0.0.51/24 | 172.16.1.51/24 | 数据库服务(主) |
db02 | 10.0.0.52/24 | 172.16.1.52/24 | 数据库服务(从) |
nfs01 | 10.0.0.31/24 | 172.16.1.31/24 | 存储服务(用户上传的图片和视频等) |
backup | 10.0.0.41/24 | 172.16.1.41/24 | 备份服务 |
m01 | 10.0.0.61/24 | 172.16.1.61/24 | 管理服务 |
zabbix-server | 10.0.0.71/24 | 172.16.1.71/24 | 监控服务 |
elkstack | 10.0.0.81/24 | 172.16.1.81/24 | 日志收集 |
git+jenkins | 10.0.0.91/24 | 172.16.1.91/24 | 自动化部署 |
废话不多说直接开干,注意:防止复制粘贴 ;ip规划和文章中的ip有区别,每一步都细心就好;也是为了你好!!
#####################################################
[root@web01 ~]# groupadd -g 666 www
[root@web01 ~]# useradd -u666 -g666 -M -s /sbin/nologin www
[root@web01 ~]# cat /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
[root@nginx ~]# yum remove php-mysql-5.4 php php-fpm php-common -y
[root@web01]# yum localinstall -y http://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@web01 ~]# # yum -y install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb nginx
[root@web01 ~]# sed -i ‘/^user/c user www;’ /etc/nginx/nginx.conf
[root@web01 ~]# sed -i ‘/^user/c user = www’ /etc/php-fpm.d/www.conf
[root@web01 ~]# sed -i ‘/^group/c group = www’ /etc/php-fpm.d/www.conf
post_max_size = 300M
upload_max_filesize = 300M
memory_limit = 300M
[root@web01 conf.d]# cat wecenter.conf
server {
server_name zh.oldboy.com;
listen 80;
root /code/zh;
index index.php index.html;
location ~ \.php$ {
root /code/zh;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web01 conf.d]# cat wordpress.conf
server {
server_name blog.oldboy.com;
listen 80;
root /code/wordpress;
index index.php index.html;
location ~ \.php$ {
root /code/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web01 ~]# systemctl enable nginx php-fpm
[root@web01 ~]# systemctl start nginx php-fpm
5.准备对应的代码
[root@web01 ~]# mkdir /code
[root@web01 ~]# cd /code
[root@web01 code]# wget https://wordpress.org/latest.tar.gz
[root@web01 code]# unzip WeCenter_v3.2.2.zip
[root@web01 code]# tar xf latest.tar.gz
[root@web01 code]# mv WeCenter322/ zh
[root@web01 ~]# chown -R www.www /code/
#####################################################
[root@db01 ~]# rpm -ivh http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql57-community-release-el7-10.noarch.rpm
[root@db01 ~]# yum install mysql-community-server -y
[root@db01 ~]# systemctl enable mysqld
[root@db01 ~]# systemctl start mysqld
[root@db01 ~]# mysql -uroot -p$(awk '/temporary password/{print KaTeX parse error: Expected 'EOF', got '}' at position 3: NF}̲' /var/log/mysq…(awk ‘/NR==6/{print $NF}’ /var/log/mysqld.log)
mysql> ALTER USER ‘root’@‘localhost’ IDENTIFIED BY ‘Bgx123.com’;
mysql> create database wordpress;
mysql> create database zh;
mysql> create database jpress;
mysql> grant all privileges on . to ‘all’@’%’ identified by ‘Bgx123.com’;
mysql> flush privileges;
mysql> show databases;
mysql> drop database zh; 删除数据库
####################################################
#####################################################
[root@web02 ~]# groupadd -g 666 www
[root@web02 ~]# useradd -u666 -g666 -M -s /sbin/nologin www
[root@web02 ~]# scp [email protected]:/etc/yum.repos.d/* /etc/yum.repos.d/
[root@web02 ~]# yum -y install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb nginx
[root@web02 ~]# rsync -avz --delete [email protected]:/etc/nginx /etc/
[root@web02 ~]# rsync -avz --delete [email protected]:/etc/php* /etc/
[root@web01 ~]# tar czf code.tar.gz /code/
[root@web02 ~]# rsync -avz [email protected]:~/code.tar.gz ./
[root@web02 ~]# tar xf code.tar.gz -C /
[root@web02 ~]# systemctl enable nginx php-fpm
[root@web02 ~]# systemctl start nginx php-fpm
####################################################
[root@web03 ~]# yum install java -y
[root@web03 ~]# mkdir /code
[root@web03 ~]# cd /code
[root@web03 code]# wget http://mirrors.shu.edu.cn/apache/tomcat/tomcat-9/v9.0.12/bin/apache-tomcat-9.0.12.tar.gz
[root@web03 code]# tar xf apache-tomcat-9.0.12.tar.gz
[root@web03 code]# ln -s /code/apache-tomcat-9.0.12 /code/tomcat
[root@web03 ~]# cd /code/tomcat/webapps/ROOT
[root@web03 ~]# rz 上传jpress的war
[root@web03 ~]# unzip 包名
[root@web03 ~]# /code/tomcat/bin/startup.sh
[root@web03 ~]# vim /etc/rc.d/rc.local
/code/tomcat/bin/startup.sh
[root@web03 ~]# chmod +x /etc/rc.d/rc.local
####################################################
yum install nfs-utils -y
[root@nfs ~]# groupadd -g 666 www
[root@nfs ~]# useradd -g 666 -u666 -M -s /sbin/nologin www
[root@nfs ~]# cat /etc/exports
/data/blog 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
/data/zh 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
/data/jpress 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
[root@nfs ~]# mkdir /data/{blog,zh,jpress} -p
[root@nfs ~]# chown -R www.www /data
[root@nfs ~]# systemctl enable nfs-server
[root@nfs ~]# systemctl start nfs-server
####################################################
[root@web02 wp-content]# cp -rp uploads/ uploads_bak
[root@web02 wp-content]# mount -t nfs 172.16.1.31:/data/blog /code/wordpress/wp-content/uploads
[root@web02 wp-content]# cp -rp uploads_bak/* uploads/
[root@web01 ~]# mount -t nfs 172.16.1.31:/data/blog /code/wordpress/wp-content/uploads
[root@web01 ~]vim /etc/fstab
172.16.1.31:/data/blog /code/wordpress/wp-content/uploads nfs defaults 0 0
172.16.1.31:/data/zh /code/zh/uploads nfs defaults 0 0
[root@web02 ~]rsync -avz --delete [email protected]:/etc/fstat /etc/fstab
[root@web03 ~]vim /etc/fstab
172.16.1.31:/data/jpress /code/tomcat/webapps/ROOT/attachment nfs defaults 0 0
[root@web01 ~]#mount -a
####################################################
[root@lb01 ~]# scp -rp [email protected]:/etc/yum.repos.d/nginx.repo /etc/yum.repos.d/
[root@lb01 ~]# yum install nginx -y
[root@lb01 ~]# cat /etc/nginx/conf.d/blog_proxy.conf
upstream blog {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
server_name blog.oldboy.com;
listen 80;
location / {
proxy_pass http://blog;
include proxy_params;
}
}
[root@lb01 ~]# cat /etc/nginx/conf.d/zh_proxy.conf
upstream zh {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
server_name zh.oldboy.com;
listen 80;
location / {
proxy_pass http://zh;
include proxy_params;
}
}
[root@lb01 ~]# cat /etc/nginx/conf.d/jpress_proxy.conf
upstream java {
server 172.16.1.9:8080;
}
server {
listen 80;
server_name jpress.oldboy.com;
location / {
proxy_pass http://java;
include proxy_params;
}
}
[root@lb01 ~]# cat /etc/nginx/proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
[root@lb01 ~]# systemctl enable nginx
[root@lb01 ~]# systemctl start nginx
###################################################
//openssl必须是1.0.2
[root@Nginx ~]# openssl version
OpenSSL 1.0.2k-fips 26 Jan 2017
//nginx必须有ssl模块
[root@Nginx ~]# nginx -V
–with-http_ssl_module
[root@Nginx ~]# mkdir /etc/nginx/ssl_key -p
[root@Nginx ~]# cd /etc/nginx/ssl_key
[root@Nginx ssh_key]# openssl genrsa -idea -out server.key 2048
Generating RSA private key, 2048 bit long modulus
…+++
//记住配置密码, 我这里是1234
Enter pass phrase for server.key:
Verifying - Enter pass phrase for server.key:
[root@Nginx ssl_key]# openssl req -days 36500 -x509
-sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:BJ
Locality Name (eg, city) [Default City]:CP
Organization Name (eg, company) [Default Company Ltd]:edu
Organizational Unit Name (eg, section) []:BOOS
Common Name (eg, your name or your server's hostname) []:BOOS
Email Address []:[email protected]
[root@lb01 conf.d]# cat blog_proxy.conf
upstream blog {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
server_name blog.oldboy.com;
listen 80;
return 302 https://$server_name$request_uri;
}
server {
server_name blog.oldboy.com;
listen 443;
ssl on;
ssl_certificate ssl_key/server.crt;
ssl_certificate_key ssl_key/server.key;
location / {
proxy_pass http://blog;
include proxy_params;
}
}
[root@lb01 conf.d]# cat zh_proxy.conf
upstream zh {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
server_name zh.oldboy.com;
listen 80;
return 302 https://$server_name$request_uri;
}
server {
server_name zh.oldboy.com;
listen 443;
ssl on;
ssl_certificate ssl_key/server.crt;
ssl_certificate_key ssl_key/server.key;
location / {
proxy_pass http://zh;
include proxy_params;
}
}
[root@lb01 conf.d]# cat jpress_proxy.conf
upstream java {
server 172.16.1.9:8080;
}
server {
server_name jpress.oldboy.com;
listen 80;
return 302 https://$server_name$request_uri;
}
server {
server_name jpress.oldboy.com;
listen 443;
ssl on;
ssl_certificate ssl_key/server.crt;
ssl_certificate_key ssl_key/server.key;
location / {
proxy_pass http://java;
include proxy_params;
}
}
前端负载配置https 后端走web全部走http协议会 导致登录和注册 出现问题,建议在后端的php配置加入一行
fastcgi_param HTTPS on;
[root@lb02]scp -rp [email protected]:/etc/nginx /etc/
##################################################
[root@lb01 ~]# yum install keepalived -y
[root@lb02 ~]# yum install keepalived -y
[root@lb01 ~]# cat /etc/keepalived/keepalived.conf
global_defs {
router_id lb01
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 50
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3
}
}
[root@lb02 ~]# cat /etc/keepalived/keepalived.conf
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 50
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3
}
}
#lb01
[root@lb01 ~]# systemctl enable keepalived
[root@lb01 ~]# systemctl start keepalived
#lb02
[root@lb02 ~]# systemctl enable keepalived
[root@lb02 ~]# systemctl start keepalived
Keepalived配置区别 | Master配置 | Backup节配置 |
---|---|---|
route_id(唯一标识) | route_id lb01 | route_id lb02 |
state(角色状态) | state Master | state Backup |
priority(竞选优先级) | priority 150 | priority 100 |
##################################################
lb01和lb02必须完完全全一致,这样才可以实现无缝切换
[root@lb02 ~]# scp -rp [email protected]:/etc/yum.repos.d/nginx /etc/yum.repos.d/
[root@lb02 ~]# yum install nginx -y
[root@lb02 ~]# scp -rp [email protected]:/etc/nginx /etc/
[root@lb02 ~]# systemctl start nginx
[root@lb02 ~]# systemctl enable nginx
[root@backup ~]# yum install rsync -y
[root@backup ~]# cat /etc/rsyncd.conf
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#################################################
[backup]
comment = welcome to oldboyedu backup!
path = /backup
[data]
comment = welcome to oldboyedu data!
path = /data
[root@backup ~]# groupadd -g666 www
[root@backup ~]# useradd -u666 -g666 -M -s /sbin/nologin www
[root@backup ~]# chown -R www.www /{backup,data}
[root@backup ~]# echo ‘rsync_backup:123’ > /etc/rsync.passwd
[root@backup ~]# chmod 600 /etc/rsync.passwd
[root@backup ~]# systemctl enable rsyncd
[root@backup ~]# systemctl start rsyncd
#######################其他机器准备推送脚本
[root@lb01 scripts]# mkdir /server/scripts -p
[root@lb01 ~]# cat /server/scripts/client_rsync_backup.sh
#!/usr/bin/bash
#1.定义变量
Host=$(hostname)
Addr=$(ifconfig eth1|awk 'NR==2{print $2}')
Date=$(date +%F)
Dest=${Host}_${Addr}_${Date}
Path=/backup
#2.创建备份目录
[ -d $Path/$Dest ] || mkdir -p $Path/$Dest
#3.备份对应的文件
cd / && \
[ -f $Path/$Dest/system.tar.gz ] || tar czf $Path/$Dest/system.tar.gz etc/fstab etc/rsyncd.conf && \
[ -f $Path/$Dest/log.tar.gz ] || tar czf $Path/$Dest/log.tar.gz var/log/messages var/log/secure && \
#4.携带md5验证信息
[ -f $Path/$Dest/flag ] || md5sum $Path/$Dest/*.tar.gz >$Path/$Dest/flag
#4.推送本地数据至备份服务器
export RSYNC_PASSWORD=123
rsync -avz $Path/ [email protected]::backup
#5.本地保留最近7天的数据
find $Path/ -type d -mtime +7|xargs rm -rf
[root@lb01 ~]# chmod +x /server/scripts/client_rsync_backup.sh
[root@lb01 ~]# sh /server/scripts/client_rsync_backup.sh
[root@lb01 ~]# echo ‘00 00 * * * sh /server/scripts/client_rsync_backup.sh >&/dev/null’ >> /var/spool/cron/root
#######################Backup服务器上的校验脚本
[root@backup ~]# yum install mailx -y
[root@backup ~] vim /etc/mail.rc
set [email protected]
set smtp=smtps://smtp.qq.com:465
set [email protected]
set smtp-auth-password= #客户端授权码
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/
[root@backup ~]# mkdir /server/scripts -p
[root@backup ~]# vim /server/scripts/check_backup.sh
#!/usr/bin/bash
#1.定义全局的变量
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
#2.定义局部变量
Path=/backup
Date=$(date +%F)
#3.查看flag文件,并对该文件进行校验, 然后将校验的结果保存至result_时间
find $Path/*_${Date} -type f -name "flag"|xargs md5sum -c >$Path/result_${Date}
#4.将校验的结果发送邮件给管理员
mail -s "Rsync Backup $Date" 123@qq.com <$Path/result_${Date}
#5.删除超过7天的校验结果文件, 删除超过180天的备份数据文件
find $Path/ -type f -name "result*" -mtime +7|xargs rm -f
find $Path/ -type d -mtime +180|xargs rm -rf
############################################################
[root@nfs ~]# yum install inotify-tools rsync -y
wget https://raw.githubusercontent.com/wsgzao/sersync/master/sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@nfs ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@nfs ~]# mv GNU-Linux-x86/ /usr/local/sersync
[root@nfs ~]# vim /usr/local/sersync/confxml.xml
5 <fileSystem xfs="true"/>
6 <filter start="false">
7 <exclude expression="(.*)\.svn">exclude>
8 <exclude expression="(.*)\.gz">exclude>
9 <exclude expression="^info/*">exclude>
10 <exclude expression="^static/*">exclude>
11 filter>
12 <inotify>
13 <delete start="true"/>
14 <createFolder start="true"/>
15 <createFile start="true"/>
16 <closeWrite start="true"/>
17 <moveFrom start="true"/>
18 <moveTo start="true"/>
19 <attrib start="false"/>
20 <modify start="false"/>
21 inotify>
23 <sersync>
24 <localpath watch="/data">
25 <remote ip="172.16.1.41" name="data"/>
28 localpath>
29 <rsync>
30 <commonParams params="-az"/>
31 <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.pass"/>
32 <userDefinedPort start="false" port="874"/>
33 <timeout start="true" time="100"/>
34 <ssh start="false"/>
35 rsync>
36 <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/>
[root@nfs01 sersync]# echo “123” > /etc/rsync.pass
[root@nfs ~]# chmod 600 /etc/rsync.pass
[root@nfs ~]# /usr/local/sersync/sersync2 -h
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
参数-d:启用守护进程模式
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序
[root@nfs ~]# /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
[root@nfs ~]# cd /data && rsync -avz -R --delete ./ --timeout=100 [email protected]::data --password-file=/etc/rsync.pass
/usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
#####################################################################################
1.nfs和backup两台服务器应该保持一样(nfs配置。nfs共享的目录。nfs的权限)
[root@backup ~]# yum install nfs-utils -y
[root@backup ~]# rsync -avz [email protected]:/etc/exports /etc/
[root@backup ~]# groupadd -g 666 www
[root@backup ~]# useradd -u666 -g666 www
2.启动nfs
[root@backup ~]# systemctl start rpcbind
[root@backup ~]# systemctl start nfs-server
3.修改rsync的权限vim /etc/rsyncd.conf
uid = www
gid = www
4.修改授权
[root@backup ~]# chown -R www.www /data/ /backup/
5.重启rsync
[root@backup ~]# systemctl restart rsyncd
6.模拟nfs故障(挂起虚拟机)
7.web强制卸载172.16.1.31:/data
[root@web01 ~]# umount -lf /data
8.web尝试挂载172.16.1.41:/data
[root@web01 ~]# mount -t nfs 172.16.1.41:/data /data/
############################################################
[root@m01 ~]# systemctl start firewalld
[root@m01 ~]# systemctl enable firewalld
[root@m01 ~]# firewall-cmd --remove-service=ssh --permanent
[root@m01 ~]# firewall-cmd --add-rich-rule=‘rule family=ipv4 source address=10.0.0.1/32 service name=ssh accept’ --permanent
[root@m01 ~]# firewall-cmd --add-masquerade --permanent
[root@m01 ~]# firewall-cmd --reload
#########没有公网地址的内部服务器配置指向管理机的网关
[root@web01 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1 配置新增如下2条规则
GATEWAY=172.16.1.61
DNS1=223.5.5.5
[root@web01 ~]# nmcli connection down eth1 && nmcli connection up eth1
#####################################################
yum localinstall -y https://mirrors.aliyun.com/zabbix/zabbix/3.4/rhel/7/x86_64/zabbix-web-3.4.14-1.el7.noarch.rpm
yum localinstall -y https://mirrors.aliyun.com/zabbix/zabbix/3.4/rhel/7/x86_64/zabbix-web-3.4.14-1.el7.noarch.rpm
yum install zabbix-web-mysql
[root@zabbix-server ~]# rpm -ivh https://mirrors.aliyun.com/zabbix/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch.rpm
[root@zabbix-server ~]# yum install -y zabbix-server-mysql zabbix-web-mysql zabbix-agent mariadb-server
[root@zabbix-server ~]# systemctl start mariadb
[root@zabbix-server ~]# mysql -uroot
MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;
MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@localhost identified by ‘zabbix’;
[root@zabbix-server ~]# cd /usr/share/doc/zabbix-server-mysql-3.4.14/
[root@linux-node1 zabbix-server-mysql-3.4.14]# zcat create.sql.gz |mysql -uroot zabbix
[root@zabbix-server ~]# grep ^[a-Z] /etc/zabbix/zabbix_server.conf
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=zab
[root@zabbix-server ~]# systemctl start zabbix-server
[root@zabbix-server ~]# systemctl enable zabbix-server
[root@zabbix-server ~]# vim /etc/httpd/conf.d/zabbix.conf
php_value max_execution_time 300
php_value memory_limit 128M
php_value post_max_size 16M
php_value upload_max_filesize 2M
php_value max_input_time 300
php_value always_populate_raw_post_data -1
#取消注释,设置正确的时区
php_value date.timezone Asia/Shanghai
8.启动Apache服务
[root@zabbix-server ~]# systemctl enable httpd
[root@zabbix-server ~]# systemctl start httpd
9.通过浏览器访问http://IP/zabbix 进入向导页面,进行zabbix安装。
10.完成zabbix安装后,默认的账户和密码是 Admin zabbix
zabbix基础架构
zabbix-agent(数据采集)–>zabbix-server(数据分析|报警)–> 数据库(数据存储)<–zabbix web(数据展示)
Zabbix单台时代: LAMP+Zabbix
Zabbix数据拆分: LAP+MySQL(修改如下两个文件中连接数据库的配置信息)
[root@m01 ~]# ll /etc/zabbix/zabbix_server.conf
[root@m01 ~]# ll /etc/zabbix/web/zabbix.conf.php
mysql> create database zabbix character set utf8 collate utf8_bin;
mysql> grant all privileges on zabbix.* to zabbix@’%’ identified by ‘Bgx123.com’;
[root@m01 ~]# mysqldump -uroot \ --databases zabbix \ --single-transaction > date +%F%H
-zabbix.sql
[root@m01 ~]# cat 2018-08-2017-zabbix.sql |mysql -h 10.0.0.51 -uzabbix -pBgx123.com zabbix
[root@m01 ~]# vim /etc/zabbix/zabbix_server.conf
DBHost=172.16.1.51
DBName=zabbix
DBUser=zabbix
DBPassword=Bgx123.com
[root@m01 ~]# systemctl restart zabbix-server
[root@m01 ~]# vim /etc/zabbix/web/zabbix.conf.php
$DB[‘TYPE’] = ‘MYSQL’;
$DB[‘SERVER’] = ‘172.16.1.51’;
$DB[‘PORT’] = ‘0’;
$DB[‘DATABASE’] = ‘zabbix’;
$DB[‘USER’] = ‘zabbix’;
$DB[‘PASSWORD’] = ‘Bgx123.com’;
[root@m01 ~]# systemctl restart httpd
[root@m01 ~]# tail -f /var/log/zabbix/zabbix_server.log
2189:20180820:173636.941 [Z3001] connection to database ‘zabbix’ failed: [2003] Can’t connect to MySQL server on ‘172.16.1.51’ (111)
zabbix监控-agent (47.92.196.116)
[root@web01 ~]# rpm -ivh https://mirrors.aliyun.com/zabbix/zabbix/3.4/rhel/7/x86_64/zabbix-agent-3.4.12-1.el7.x86_64.rpm
[root@m01 ~]# sed -i '/^Server=/cServer=172.16.1.71' /etc/zabbix/zabbix_agentd.conf
[root@m01 ~]# sed -i '/^ServerActive=/cServerActive=172.16.1.71' /etc/zabbix/zabbix_agentd.conf
3.倒入所有的监控项和脚本
[root@m01 ~]# cd /etc/zabbix
[root@m01 ~]# rz #上传zabbix_key.tar.gz
[root@m01 zabbix]# for i in 5 7 8 9 31 41 51 52;do scp -rp scripts/ zabbix_agentd.d/ [email protected].$i:/etc/zabbix/;done
[root@web01 ~]# rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/3.4/rhel/7/x86_64/zabbix-agent-3.4.12-1.el7.x86_64.rpm
[root@web01 ~]# vim /etc/zabbix/zabbix_agentd.conf
Server=172.16.1.71
[root@web01 ~]# systemctl enable zabbix-agent
[root@web01 ~]# systemctl start zabbix-agent
[root@web01 ~]# cd /etc/zabbix/zabbix_agentd.d/
[root@web01 zabbix_agentd.d]# cat io.conf
UserParameter=tps,iostat | awk '/^sda/{print $2}'
[root@web01 zabbix_agentd.d]# systemctl restart zabbix-agent
[root@web01 zabbix_agentd.d]# zabbix_agentd -p
[root@zabbix-server ~]# yum install zabbix-get -y
[root@zabbix-server ~]# zabbix_get -s 172.16.1.7 -k tps
0.39
#####################################################
1.PHP-FPM工作模式通常与Nginx结合使用,修改php-fpm.conf
[root@Agent ~]# vim /etc/php-fpm.d/www.conf
pm.status_path = /phpfpm_status
[root@Agent ~]# systemctl restart php-fpm
2.修改nginx.conf的配置文件,增加如下location访问PHP-FPM状态信息。
location ~ /phpfpm_status {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME d o c u m e n t r o o t document_root documentrootfastcgi_script_name;
}
3.访问测试phpfpm_status
[root@Agent ~]# curl http://127.0.0.1/phpfpm_status
pool: www
process manager: dynamic
start time: 05/Jul/2016:15:30:56 +0800
start since: 409
accepted conn: 22
listen queue: 0
max listen queue: 0
listen queue len: 128
idle processes: 4
active processes: 1
total processes: 5
max active processes: 2
max children reached: 0
#PHP-FPM状态解释:
pool #fpm池名称,大多数为www
process manager #进程管理方式dynamic或者static
start time #启动日志,如果reload了fpm,时间会更新
start since #运行时间
accepted conn #当前池接受的连接数
listen queue #请求等待队列,如果这个值不为0,那么需要增加FPM的进程数量 (长时间大于0)
max listen queue #请求等待队列最高的数量
listen queue len #socket等待队列长度
idle processes #空闲进程数量0 (空闲持续多长时间0则报警)
active processes #活跃进程数量200
total processes #总进程数量200
max active processes #最大的活跃进程数量(FPM启动开始计算)
max children reached #最大数量限制的次数,如果这个数量不为0,那说明你的最大进程数量过小,可以适当调整。
slow requests # 超过5s在执行,这就算慢
1.取值
curl -s http://127.0.0.1/phpfpm_status|grep “$1”|awk ‘{print $NF}’
2.定义监控项
[root@web02 zabbix_agentd.d]# cat php.conf
UserParameter=fpm[*],curl -s http://127.0.0.1/phpfpm_status|grep ^"$1":|awk ‘{print $NF}’
3.zabbix-server获取对应的监控项
[root@zabbix-server ~]# zabbix_get -s 172.16.1.8 -k fpm[“accepted conn”]
50077
4.web界面添加模板,模板添加监控项,基于监控项创建触发器,触发则发送邮件报警。画一个图形出来。
触发器
1.当空闲的进程数为0 或 队列大于0,持续1分钟,则报警 ,练习多条件表达式
{Fpm Status:fpm[“idle processes”].avg(1m)}=0 or {Fpm Status:fpm[“listen queue”].avg(1m)}>30
5.模拟故障 ab -n10000 -c200 http://127.0.0.1/phpfpm_status
1.软件支持打印自身的状态信息(info)
2.取值
3.添加监控项
4.web界面添加模板,模板添加监控项,基于监控项创建触发器,触发则发送邮件报警。画一个图形出来。
[root@web02 zabbix_agentd.d]# yum install redis -y
[root@web02 zabbix_agentd.d]# systemctl start redis
redis-cli客户端工具,info是一个命令(打印redis当前的状态信息)
端口监听上6379
[root@web02 zabbix_agentd.d]# redis-cli info
connected_clients:1
used_memory_human:794.38K
used_memory_rss_human:5.65M
http://www.yanglixun.com/?p=176
1.使用percona工具来监控MySQL
2.需要安装percona
1.脚本{2个,1个php脚本,是用来连接mysql数据库的,2个脚本调用php取数据库的状态信息}
2.conf配置文件
3.web页面模板
7.web03安装tomcat
8.开启tomcat的远程jvm配置文件
[root@web03 ~]# vim /code/tomcat/bin/catalina.sh
CATALINA_OPTS="$CATALINA_OPTS
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=12345
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=172.16.1.9"
#jvm配置文件解释
CATALINA_OPTS="$CATALINA_OPTS
//启用远程监控JMX
-Dcom.sun.management.jmxremote
//jmx启用远程端口,Zabbix添加时必须一致
-Dcom.sun.management.jmxremote.port=12345
//不开启用户密码认证
-Dcom.sun.management.jmxremote.authenticate=false
//不启用ssl加密传输
-Dcom.sun.management.jmxremote.ssl=false
//运行tomcat主机的IP地址
-Djava.rmi.server.hostname=192.168.90.11"
9.重启tomcat
[root@web03 tomcat]# cd /code/tomcat/bin/
[root@web03 bin]# ./shutdown.sh
Using CATALINA_BASE: /code/tomcat
Using CATALINA_HOME: /code/tomcat
Using CATALINA_TMPDIR: /code/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /code/tomcat/bin/bootstrap.jar:/code/tomcat/bin/tomcat-juli.jar
[root@web03 bin]# ./startup.sh
Using CATALINA_BASE: /code/tomcat
Using CATALINA_HOME: /code/tomcat
Using CATALINA_TMPDIR: /code/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /code/tomcat/bin/bootstrap.jar:/code/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@web03 bin]# netstat -lntp
tcp6 0 0 :::12345 :::* LISTEN 8174/java
10.zabbix添加tomcat主机,并添加Zabbix自带java监控模板
[root@m01 ~]# yum install ntp -y
[root@m01 ~]# ntpq -c version
ntpq [email protected] Fri Apr 13 12:52:30 UTC 2018 (1)
[root@m01 ~]# rpm -qc ntp
/etc/ntp.conf
/etc/ntp/crypto/pw
/etc/sysconfig/ntpd
[root@m01 ~]# vim /etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict 127.0.0.1
restrict ::1
restrict 172.16.1.0 mask 255.255.255.0 nomodify notrap
#允许 172.16.1.0网段中的服务器访问本ntp服务器进行时间同步
restrict ntp.api.bz nomodify notrap noquery
restrict 210.72.145.44 nomodify notrap noquery
restrict 202.112.10.36 nomodify notrap noquery
restrict 59.124.196.83 nomodify notrap noquery
server 127.127.1.0 iburst #local clock
fudge 127.127.1.0 stratum 10
[root@web01 ~]# yum install ntp -y
[root@web01 ~]# systemctl start ntp
[root@web01 ~]# systemctl enable ntp
[root@web01 ~]# ntpdate 172.16.1.61
[root@m01 ~]# cat /etc/sysconfig/ntpd
#Drop root to id 'ntp:ntp' by default.
SYNC_HWCLOCK=yes
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"
[root@m01-61 ~]# yum install chrony
[root@m01-61 ~]# rpm -ql chrony
[root@m01-61 ~]# chronyc -v
chronyc (chrony) version 3.2 (+READLINE +IPV6 +DEBUG)
[root@m01-61 ~]# vim /etc/chrony.conf
[root@m01-61 ~]# cat /etc/chrony.conf
#Use public servers from the pool.ntp.org project.
#Please consider joining the pool (http://www.pool.ntp.org/join.html).
#使用pool.ntp.org项目中的公共服务器。以server开,理论上你想添加多少时间服务器都可以
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
#Record the rate at which the system clock gains/losses time.
#根据实际时间计算出服务器增减时间的比率,然后记录到一个文件中,在系统重启后为系统做出最佳时间补偿调整
driftfile /var/lib/chrony/drift
#Allow the system clock to be stepped in the first three updates
#if its offset is larger than 1 second.
#chronyd根据需求减慢或加速时间调整,
#在某些情况下系统时钟可能漂移过快,导致时间调整用时过长。
#该指令强制chronyd调整时期,大于某个阀值时步进调整系统时钟。
#只有在因chronyd启动时间超过指定的限制时(可使用负值来禁用限制)没有更多时钟更新时才生效。
makestep 1.0 3
#Enable kernel synchronization of the real-time clock (RTC).
#将启用一个内核模式,在该模式中,系统时间每11分钟会拷贝到实时时钟(RTC)
rtcsync
#Enable hardware timestamping on all interfaces that support it.
#通过使用hwtimestamp指令启用硬件时间戳.
#hwtimestamp *
#Increase the minimum number of selectable sources required to adjust
#the system clock.
#minsources 2
#Allow NTP client access from local network.
#指定一台主机、子网,或者网络以允许或拒绝NTP连接到扮演时钟服务器的机器
allow 172.16.1.0/24
#Serve time even if not synchronized to a time source.
#local stratum 10
#Specify file containing keys for NTP authentication.
#指定包含NTP验证密钥的文件。
#keyfile /etc/chrony.keys
#Specify directory for log files.
#指定日志文件的目录。
logdir /var/log/chrony
#Select which information is logged.
#log measurements statistics tracking
[root@m01-61 ~]# timedatectl
Time zone: Asia/Shanghai (CST, +0800)
[root@m01-61 ~]# chronyc -a makestep
[root@m01-61 ~]# systemctl start chronyd
[root@m01-61 ~]# systemctl enable chronyd
[root@web03-09 ~]# yum install chrony
[root@web03-09 ~]# vim /etc/chrony.conf
[root@web03-09 ~]# awk ‘!/^(#|$)/’ /etc/chrony.conf
server 172.16.1.61 iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
logdir /var/log/chrony
[root@web03-09 ~]# systemctl start chronyd
[root@web03-09 ~]# systemctl enable chronyd
#####################################################
[root@db02 ~]#scp -rp [email protected]:/etc/yum.repos.d/* /etc/yum.repos.d/
[root@db02 ~]# scp -rp [email protected]:/etc/pki/ /etc/
.安装mysql, 默认5.7版本, 文件过大可能会导致下载失败
[root@nginx ~]# yum install mysql-community-server -y
[root@nginx ~]# systemctl start mysqld
[root@nginx ~]# systemctl enable mysqld
============================================================================
[root@db01 ~]# cat /etc/my.cnf
[mysqld]
log-bin
server-id=160
重启数据库
[root@db01 ~]# systemctl restart mysqld
查看
[root@db01 ~]# ls /var/lib/mysql
[root@db01 ~]# mysql -uroot -pBgx123.com
mysql> grant replication slave, replication client on *.* to ‘rep’@‘172.16.1.%’ identified by ‘Rep123.com’;
mysql> flush privileges;
mysql> select * from mysql.user\G;
测试
[root@db01 ~]# mysql -h 172.16.1.51 -urep -pRep123.com
[root@db01 ~]# mysqldump -uroot -pBgx123.com \
–all-databases \
–single-transaction \
–master-data=1 \
–flush-logs > /root/db-$(date +%F)-all.sql
[root@db01 ~]# scp db-2018-08-29-all.sql [email protected]:/tmp
//1.检查是否能使用远程账户登录
[root@slave ~]# mysql -h 172.16.1.51 -urep -pRep123.com
//2.修改配置文件/etc/my.cnf, 从需开启binlog
server-id=161
[root@slave ~]# systemctl start mysqld
[root@lb02 ~]# mysql -uroot -p$(awk ‘/temporary password/{print $NF}’ /var/log/mysqld.log)
mysql> ALTER USER ‘root’@‘localhost’ IDENTIFIED BY ‘Bgx123.com’;
[root@db02 ~]# mysql -uroot -pBgx123.com < /tmp/db-2018-08-29-all.sql
[root@db02 ~]# mysql -uroot -pBgx123.com
mysql> change master to
master_host=‘172.16.1.51’,
master_user=‘rep’,
master_password=‘Rep123.com’;
mysql> start slave;
//7.查看mysql主从状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.1.51
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: db01-bin.000002
Read_Master_Log_Pos: 6671
Relay_Log_File: db02-relay-bin.000004
Relay_Log_Pos: 6882
Relay_Master_Log_File: db01-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
#####################################################
[root@web01 ~]# vim /etc/nginx/nginx.conf
proxy_cache_path /soft/cache levels=1:2 keys_zone=code_cache:10m max_size=10g inactive=60m use_temp_path=off;
代码说明:
#proxy_cache存放缓存临时文件
#levels 按照两层目录分级
#keys_zone 开辟空间名, 10m:开辟空间大小, 1m可存放8000key
#max_size 控制最大大小, 超过后Nginx会启用淘汰规则
#inactive 60分钟没有被访问缓存会被清理
#use_temp_path 临时文件, 会影响性能, 建议关闭
[root@lb01 ~] mkdir -p /soft/cache
[root@lb01 /etc/nginx/conf.d]vim blog_proxy.con
upstream blog {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
server_name blog.oldboy.com;
listen 80;
return 302 https://$server_name$request_uri;
}
server {
server_name blog.oldboy.com;
listen 443;
ssl on;
ssl_certificate ssl_key/server.crt;
ssl_certificate_key ssl_key/server.key;
location / {
proxy_pass http://blog;
include proxy_params;
proxy_cache code_cache;
proxy_cache_valid 200 304 12h;
proxy_cache_valid any 10m;
add_header Nginx-Cache "$upstream_cache_status";
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
}
}
相关参数解释:
#proxy_cache 开启缓存
#proxy_cache_valid 状态码200|304的过期为12h, 其余状态码10分钟过期
#proxy_cache_key 缓存key
#add_header 增加头信息, 观察客户端respoce是否命中
#proxy_next_upstream 出现502-504或错误, 会跳过此台服务器访问下台
#####################################################
1台web01
1台db
1台web02
1台web03
1台lb01
1台nfs,实时同步
1台redis
实现全站https 用户-> https -> 负载均衡 -->http -->web【1|2|3】
1台lb02
针对负载均衡做proxycache缓存
1台manger(做安全firewalld 做跳板、时间同步)
1台zabbix做全站的监控
1台bakcup做备份
#####################################################
扩展项
1.mysql主从、MySQL高可用
2.自动化部署集群 Ansible/Saltstack
3.日志收集系统 elkstack/日志易
4.自动化上线 git+jenkins
架构层面
因为公司的开发使用的是php环境,于是我们在架构上使用了LNMP,由于后面换了开发,我们又增加了LNMT环境,
监控层面
考虑到项目运行久了,难免会遇到各种各校的故障,为了解决快速有效的找到故障,及告警,我们引入了监控,在监控方面,我们使用的是zabbix,
日志层面
监测日志主要有网站访问日志,网站访问错误日志,外网主机安全日志,分析访问日志里是否有异常的请求及大量的攻击之类的
安全层面
在安全方面,我们一开始使用firewalld防火墙,做了一些限制以及关闭了多余的端口。在外网管理的时候,通过跳板机管理内网主机,后面考虑到市面上有好的产品比较适合咱们公司,于是引入了牛盾
云主机,配置安全组,关闭多余端口,只开放部份端口,使用云骑士避免DDOS,SYN攻击。
优化层面
排除单点故障及负载过高:我们使用两台七层负载,20台web主机,3台数据库,两台备份服务器(本地和云)
负载优化,由于咱们的物理机,配置参差不齐,我们加入了权重,使用了两台加上keepalived地址漂移 排除单点故障
数据库优化:使用一主数据库,两台从数据库,给三台数据做了mha高可用,以及使用mycat 实现读写分离
备份层面
我们使用了一台高容量的服务器来做备份存储,存放各服务器上重要的数据及配置文件,我们会在客户端添加定时任务和脚本,通过脚本定期向代理层或备份服务器推送备份数据。我们会在服务器上放上脚本和定时任务来定期清理较早的数据。让磁盘空间得以有效利用。
自动化层面
由于我们主机比较多,一但涉及到上线及有变更的情况,一台一台的操作就会显得很笨拙,效率不高,于是我们使用了ansible来批量管理整个集群,通过服务统一编组,统一管理,大大提高工作的效率。