首页 > 技术文章 > 10-50台综合架构实战

liangyuxing 2019-11-30 16:38 原文

 

1 项目的需求

2 剧本的介绍

3 rsync备份服务器

3.1 安装rsync软件

yum -y install rsyncd

3.2 编辑配置文件

uid = rsync

gid = rsync

port = 873

fake super = yes

use chroot = no

max connections = 200

timeout = 300

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsync.lock

log file = /var/log/rsyncd.log

ignore errors

read only = false

list = false

hosts allow = 172.16.1.0/24

hosts deny = 0.0.0.0/32

auth users = rsync_backup

secrets file = /etc/rsync.password

[backup]

comment = "backup dir by oldboy"

path = /backup

3.3 创建虚拟用户

[root@backup ~] # useradd -s /sbin/nologin -M rsync

3.4 创建密码文件

echo "rsync_backup:oldboy123" > /etc/rsync.password && chmod 600 /etc/rsync.password

3.5 创建备份目录

mkdir -p /backup && chown -R rsync. /backup

3.6 重启rsync服务和永久启动

systemctl restart rsyncd && systemctl enable rsyncd

 

 

4 nfs服务器的搭建

4.1 安装nfs服务

yum -y install nfs-utils

4.2 编辑配置文件

[root@nfs01 ~] # vim /etc/exports

/data 172.16.1.0/24(rw,sync)

4.3 创建存储目录,并且修改属主属组

mkdir -p /data &&chown -R nfsnobody. /data

4.4 重启服务

[root@nfs01 ~] # systemctl restart rpcbind.socket && systemctl enable rpcbind.socket

[root@nfs01 ~] # systemctl restart nfs && systemctl enable nfs

4.5 安装rsync服务

yum -y install rsync

4.6 创建密码文件

echo " oldboy123" > /etc/rsync.password && chmod 600 /etc/rsync.password

5 实时备份

5.1 安装inotify

yum -y install inotify-tools

5.2 安装sersync

5.2.1 解压sersync

[root@nfs01 ~] # cd /usr/local/

[root@nfs01 local] # unzip sersync_installdir_64bit.zip

[root@nfs01 local] # mv sersync_installdir_64bit/* ./

5.2.2 编辑配置文件

[root@nfs01 ~] # cd /usr/local/

[root@nfs01 local] # cd sersync/conf

[root@nfs01 conf] # vim confxml.xml

………………………………………………

<sersync>

<localpath watch="/data">

<remote ip="172.16.1.41" name="/backup"/>

<!--<remote ip="192.168.8.39" name="tongbu"/>-->

<!--<remote ip="192.168.8.40" name="tongbu"/>-->

</localpath>

<rsync>

<commonParams params="-az"/>

<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>

5.2.3 .启动sersync服务

[root@nfs01 bin] # chmod +x sersync && sersync -dro /usr/local/sersync/conf/confxml.xml

5.2.4 将启动添加到环境变量中

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/sersync/bin/:/usr/local/sersync/bin/

5.2.5 测试是否实时同步

5.2.5.1 ERROR: The remote path must start with a module name not a /

<sersync>

<localpath watch="/data">

<remote ip="172.16.1.41" name="backup"/>            这里是模块不是备份目录

5.2.5.2 测试成功

添加

cd /data && rsync -az -R "./11.txt" rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password

删除

cd /data && rsync -az -R --delete ./ --include="11.txt" --exclude=* rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password

6 定时备份

6.1 环境准备

服务器172.16.1.41

备份目录:/backup

服务器 172.16.1.31

本地存储目录:/backup/172.16.1.31

6.2 需要定时备份的目录

/etc/password

/etc/shadow

/etc/rc.d/rc.local

/usr/local

/var/log/messages

6.3 开始编写脚本

6.3.1 nfs

#!/bin/bash

 

DIR_INFO="/backup"

 

IP_INFO=$(hostname -i)

mkdir -p $DIR_INFO/$IP_INFO

#开始压缩备份目录

tar -zcPhf $DIR_INFO/$IP_INFO/backup_nfs01_$(date +%F_'week'%w)_data.tar.gz /etc/passwd /etc/shadow /etc/rc.d/rc.local /usr/local /var/log/messages

 

#开始创建指纹文件

find $DIR_INFO -name "*.tar.gz" -type f -mmin -20 | xargs md5sum > $DIR_INFO/$IP_INFO/finger.txt

 

#开始发送数据

rsync -azL $DIR_INFO/$IP_INFO/ rsync_backup@172.16.1.41::backup/$IP_INFO --password-file=/etc/rsync.password

 

#查找出大于7天的数据包删除

find $DIR_INFO -type f -name "*.tar.gz" -mtime +7 -delete

6.3.2 backup服务端的配置

6.3.2.1 编写脚本

[root@backup scripts] # vim backup_server.sh

#!/bin/bash

 

DIR_INFO="/backup"

 

.纹验证

find $DIR_INFO -name "*finger*.txt" -type f | xargs md5sum -c > /tmp/finger.txt

 

#给管理发送

mail -s "check finger" 460523471@qq.com < /tmp/finger.txt

6.3.2.2 编写邮件信息

set from=460523471@qq.com

set smtp=smtp.qq.com

set smtp-auth-user=460523471@qq.com

set smtp-auth-password=enoncgdkvjhlbjbd

set smtp-auth=login

7 nginx搭建(172.16.1.7-8)

7.1 官方源安装nginx

7.1.1 首先更新yum

(http://nginx.org/en/linux_packages.html)

vim /etc/yum.repos.d/nginx.repo

[nginx-stable]

name=nginx stable repo

baseurl=http://nginx.org/packages/centos/$releasever/$basearch/

gpgcheck=1

enabled=1

gpgkey=https://nginx.org/keys/nginx_signing.key

module_hotfixes=true

7.1.2 安装nginx

yum -y install nginx

7.2 修改nginx的配置文件

useradd -M -s /sbin/nologin www

 

user www;                    修改www

worker_processes 1;

 

error_log /var/log/nginx/error.log warn;

pid /var/run/nginx.pid;

 

 

events {

worker_connections 1024;

}

 

 

http {

include /etc/nginx/mime.types;

default_type application/octet-stream;

 

log_format main '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

 

access_log /var/log/nginx/access.log main;

 

sendfile on;

#tcp_nopush on;

 

keepalive_timeout 65;

 

#gzip on;

 

include /etc/nginx/conf.d/*.conf;

}

7.3 重启nginx

systemctl restart nginx && systemctl enable nginx

8 php的搭建(172.16.1.7-8)

8.1 解决yum安装软件冲突问题

yum remove php-mysql php php-fpm php-common

8.2 准备yum安装软件扩展源信息

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

8.3 利用yum安装PHP相关软件信息

yum install -y 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

8.4 修改php的配置文件

[root@web01 yum.repos.d] # vim /etc/php-fpm.d/www.conf

; Start a new pool named 'www'.

[www]

 

; Unix user/group of processes

; Note: The user is mandatory. If the group is not set, the default user's group

; will be used.

; RPM: apache Choosed to be able to access some dir as httpd

user = www                        修改属主

; RPM: Keep a group allowed to write in log dir.

group = www                        修改属组

 

vim /etc/php.ini

779 upload_max_filesize = 10M                指定最大上传文件大小限制

656 post_max_size = 10M                    请求主体最大尺寸

802 max_execution_time = 300                执行上传数据操作最大执行时间

 

8.5 重新启动php

[root@web02 yum.repos.d] # systemctl restart php-fpm.service

[root@web02 yum.repos.d] #

 

9 数据库的搭建

9.1 安装数据库

yum install mariadb-server mariadb -y

9.2 启动数据库

[root@db ~] # systemctl start mariadb.service && systemctl enable mariadb.service

Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

[root@db ~] #

9.3 172.16.1.7数据库的创建和权限密码的设置

MariaDB [(none)]> create database wordpress_7;

Query OK, 1 row affected (0.01 sec)

 

MariaDB [(none)]> grant all on dwordpress_7.* to 'wordpress_7'@'172.16.1.%' identified by 'oldboy123';

Query OK, 0 rows affected (0.00 sec)

 

 

MariaDB [(none)]> create database discuz_7;

Query OK, 1 row affected (0.01 sec)

 

MariaDB [(none)]> grant all on discuz_7.* to 'discuz_7'@'172.16.1.%' identified by 'oldboy123';

Query OK, 0 rows affected (0.00 sec)

 

MariaDB [(none)]> create database wecenter_7;

Query OK, 1 row affected (0.01 sec)

 

MariaDB [(none)]> grant all on wecenter_7.* to 'wecenter_7'@'172.16.1.%' identified by 'oldboy123';

Query OK, 0 rows affected (0.00 sec)

 

9.4 172.16.1.8数据库的创建和权限密码的设置

MariaDB [(none)]> create database wordpress_8;

Query OK, 1 row affected (0.01 sec)

 

MariaDB [(none)]> grant all on dwordpress_8.* to 'wordpress_8'@'172.16.1.%' identified by 'oldboy123';

Query OK, 0 rows affected (0.00 sec)

 

 

MariaDB [(none)]> create database discuz_8;

Query OK, 1 row affected (0.01 sec)

 

MariaDB [(none)]> grant all on discuz_8.* to 'discuz_8'@'172.16.1.%' identified by 'oldboy123';

Query OK, 0 rows affected (0.00 sec)

 

MariaDB [(none)]> create database wecenter_8;

Query OK, 1 row affected (0.01 sec)

 

MariaDB [(none)]> grant all on wecenter_8.* to 'wecenter_8'@'172.16.1.%' identified by 'oldboy123';

Query OK, 0 rows affected (0.00 sec)

 

10 动态网站的搭建(172.16.1.7(oldboy)-172.16.1.8(oldgril))

10.1 blog网站的搭建

10.1.1 编辑配置文件

[root@web01 conf.d] # vim blog.conf

server {

listen 80;

server_name blog.oldboy.com;

client_max_body_size 10m;

location / {

root /html/blog;

index index.php index.html index.htm;

}

location ~ \.php$ {

root /html/blog;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

}

10.1.2 创建站点目录

mkdir -p /html/blog && chown -R www. /html/*

10.1.3 将研发给的代码上传

10.1.4 网站搭建成功

10.2 bbs网站的搭建

10.2.1 编辑配置文件

server {

listen 80;

server_name bbs.oldboy.com;

client_max_body_size 10m;

location / {

root /html/bbs;

index index.php index.html index.htm;

}

location ~ \.php$ {

root /html/bbs;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

}

10.2.2 创建站点目录

mkdir -p /html/bbs && chown -R www. /html/*

10.2.3 将研发给的代码上传

10.2.4 网站搭建成功

 

10.3 zhihu网站的搭建

10.3.1 编辑配置文件

server {

listen 80;

server_name zhihu.oldboy.com;

client_max_body_size 10m;

location / {

root /html/zhihu;

index index.php index.html index.htm;

}

location ~ \.php$ {

root /html/zhihu;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

}

10.3.2 创建站点目录

mkdir -p /html/zhihu && chown -R www. /html/*

10.3.3 将研发给的代码上传

10.3.4 网站搭建成功

 

11 负载均衡的搭建

11.1 安装nginx(172.16.1.5-6)

yum -y install nginx

11.2 配置负载均衡

[root@lb01 conf.d] # pwd

/etc/nginx/conf.d

[root@lb01 conf.d] # cat www.conf

upstream oldboy {

server 10.0.0.7:80;

server 10.0.0.8:80;

}

server {

listen 80;

server_name localhost;

location / {

proxy_pass http://oldboy;

proxy_set_header X-Forwarded-For $remote_addr;

}

}

[root@lb01 conf.d] #

11.3 测试是否成功

成功

12 高可用的服务

12.1 安装keepalived(172.16.1.5-6)

yum -y install keepalived

12.2 编辑配置文件(主)

[root@lb01 ~] # vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

global_defs {

notification_email {

acassen@firewall.loc

}

notification_email_from Alexandre.Cassen@firewall.loc

smtp_server 192.168.200.1

smtp_connect_timeout 30

router_id lb01

}

 

vrrp_instance oldboy {

state MASTER

interface eth0

virtual_router_id 66

priority 150

advert_int 1

authentication {

auth_type PASS

auth_pass 1234

}

virtual_ipaddress {

10.0.0.3

}

}

12.3 编辑配置文件(副)

[root@lb02 ~] #

[root@lb02 ~] # vim /etc/keepalived/keepalived.conf

 

! Configuration File for keepalived

 

global_defs {

notification_email {

acassen@firewall.loc

}

notification_email_from Alexandre.Cassen@firewall.loc

smtp_server 192.168.200.1

smtp_connect_timeout 30

router_id lb02

}

 

vrrp_instance oldboy {

state BACKUP

interface eth0

virtual_router_id 66

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1234

}

virtual_ipaddress {

10.0.0.3

}

}

12.4 重启keepalived

systemctl restart keepalived.service && systemctl enable keepalived.service

12.5 高可用双主配置

12.5.1 主配置

[root@lb01 conf.d] # vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

global_defs {

notification_email {

router_id lb01

}

}

 

vrrp_instance oldboy {

state MASTER

interface eth0

virtual_router_id 66

priority 150

advert_int 1

authentication {

auth_type PASS

auth_pass 1234

}

virtual_ipaddress {

10.0.0.3

}

}

vrrp_instance girl {

state BACKUP

interface eth0

virtual_router_id 67

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1234

}

virtual_ipaddress {

10.0.0.4

}

}

12.5.2 副配置

[root@lb02 conf.d] # vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

global_defs {

notification_email {

router_id lb02

}

}

 

vrrp_instance oldboy {

state BACKUP

interface eth0

virtual_router_id 66

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1234

}

virtual_ipaddress {

10.0.0.3

}

}

vrrp_instance oldgirl {

state MASTER

interface eth0

virtual_router_id 67

priority 150

advert_int 1

authentication {

auth_type PASS

auth_pass 1234

}

virtual_ipaddress {

10.0.0.4

}

}

12.6 扩展文件配置

[root@lb02 conf.d] # vim www.conf

upstream oldboy {

ip_hash;

server 172.16.1.7:80;

server 172.16.1.8:80;

}

server {

listen 80;

server_name localhost;

location / {

proxy_pass http://oldboy;

proxy_set_header Host $host;

proxy_set_header X-Forwarded-For $remote_addr;

}

}

 

13 https的加密传输

服务器

外网

内网

web01

10.0.0.7

172.16.1.7

 

13.1 https的作用

  • 保证了数据的机密性(使用的数据对称加密算法来进行的)
  • 保证了数据的完整性(使用了指纹验证的方法来进行的)

13.2 https的原理

13.2.1 文字讲解

  • 数据机密性利用了私钥和公钥加密的对称算法,发送方和接收方都有相同的钥匙和锁,当数据发送过来,接收方只有相同的钥匙才可以打开
  • 数据完整性利用的是指纹验证信息同样指纹也会用对称加密将指纹保护起来,以防被人截取

13.2.2 图文讲解

 

13.3 https的实现(实现一个静态页面的https的转换)

13.3.1 nginx的安装

[root@web01 yum.repos.d] # yum -y install nginx

13.3.2 创建私钥和公钥(证书)

[root@web01 yum.repos.d] # openssl genrsa -idea -out /etc/nginx/server.key 2048

Generating RSA private key, 2048 bit long modulus

..................................................................................+++

....................................................+++

e is 65537 (0x10001)

Enter pass phrase for /etc/nginx/server.key:

Verifying - Enter pass phrase for /etc/nginx/server.key:

[root@web01 yum.repos.d] #

 

  • genrsa                创建私钥文件的类型
  • idea                    需要设置密码
  • out                    指定创建私钥的文件
  • 2048                创建的私钥文件密码个数

 

[root@web01 yum.repos.d] # cd /etc/nginx/

[root@web01 nginx] # openssl req -days 36500 -x509 -sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt

Generating a 2048 bit RSA private key

.....................+++

You have new mail in /var/spool/mail/root

[root@web01 yum.repos.d] #

 

  • req                        创建一个证书
  • days                        指定证书的有效期(天)
  • x509                    指定生成的证书格式类型
  • sha256                    指定生成证书的加密算法
  • nodes                    去除私钥的密码
  • keyout                    指定私钥文件
  • out                        指定生成的公钥文件

13.3.3 实现httphttps的跳转

13.3.3.1 http的实现

13.3.3.1.1 环境的准备

[root@web01 html] # vim /etc/nginx/conf.d/www.conf

1 server{

2 listen 80;

3 server_name www.oldboy.com;

4 root /html/www/;

5 autoindex on;

6 }

 

[root@web01 ~] # tree /html/www/

/html/www/

├── 10.txt

├── 1.txt

├── 2.txt

├── 3.txt

├── 4.txt

├── 5.txt

├── 6.txt

├── 7.txt

├── 8.txt

└── 9.txt

 

0 directories, 10 files

[root@web01 ~] #

13.3.3.1.2 查看是否正常

13.3.3.2 https实现

13.3.3.2.1 环境的准备

[root@web01 conf.d] # vim www.conf

server{

listen 80;

server_name www.oldboy.com;

rewrite /(.*)$ https://$host/$1;

}

server{

listen 443 ssl;

server_name www.oldboy.com;

ssl_certificate server.crt;

ssl_certificate_key server.key;

root /html/www/;

autoindex on;

}

13.3.3.2.2 查看是否正常

14 ntp服务器的搭建

服务器

外网

内网

m01

10.0.0.61

172.16.1.61

nfs01

10.0.0.31

172.16.1.31

 

14.1 什么是ntp服务器

NTP(Network Time Protocol, 网络时间协议)是由RFC 1305定义的时间同步协议,用来在分布式时间服务器和客户端之间进行时间同步,NTP基于UDP保温进行传输,使用UDP端口号为123。
使用NTP的目的是对网络内所有具有时钟的设备进行时钟同步,使网络内所有设备的时钟保持一致,从而使设备能够提供基于统一时间的多种应用。
对于运行NTP的本地系统,既可以接受来自其他时钟源的同步,又可以作为时钟源同步其他的时钟,并且可以喝其他设备互相同步。

14.2 ntp服务器的原理

14.2.1 图示原理讲解

14.2.2 文字讲解

  • 首先A服务器将带有时间戳的信息(10:00:00)发送给B服务器,B服务器接收时间是11:00:01
  • B服务器将带有时间戳(11:00:02)的信息发送给A服务器,A服务器接收到是10:00:03
  • NTP报文的延迟时间是(4-1)-(3-2)=2
  • 时间差是(4-3)+(2-1)/2=1小时

14.3 服务开始配置

14.3.1 安装ntp服务

[root@m01 ~] # yum -y install ntpdate

Loaded plugins: fastestmirror, langpacks

Loading mirror speeds from cached hostfile

14.3.2 编辑配置文件(/etc/ntp.conf)

restrict default nomodify notrap nopeer noquery ignore

 

允许内网其他机器同步时间

restrict 172.16.1.0 mask 255.255.255.0 nomodify notrap noquery

server ntp1.aliyun.com

server time1.aliyun.com

 

允许上层时间服务器修改本机时间

restrict time1.aliyun.com nomodify notrap noquery

restrict ntp1.aliyun.com nomodify notrap noquery

 

外部时间服务器不可用的时候,以本地的时间作为同步时间

server 127.127.1.0

server 127.127.1.0 stratum 10

14.3.3 重启ntp服务

[root@m01 ~] # systemctl restart ntpd

[root@m01 ~] #

14.4 测试本地同步是否成功(网络的就不测试了)

14.4.1 服务端环境搭建

[root@m01 ~] # date

Fri Nov 22 08:43:16 CST 2019

[root@m01 ~] # date -s "20191001"            修改时间为20191001

Tue Oct 1 00:00:00 CST 2019

[root@m01 ~] #

14.4.2 客户端环境搭建

[root@nfs01 ~] # ping baidu.com

ping: baidu.com: Name or service not known                    域名解析失败

[root@nfs01 ~] # ping 114.114.114.114    

connect: Network is unreachable                            外网不通畅

[root@nfs01 ~] # date

Fri Nov 22 08:45:11 CST 2019

root@nfs01 ~] # ntpdate 172.16.1.61                        内网同步服务端时间

1 Oct 00:02:51 ntpdate[2837]: step time server 172.16.1.61 offset -4524205.367614 sec

[root@nfs01 ~] # date

Tue Oct 1 00:02:56 CST 2019                            查看时间

[root@nfs01 ~] #

14.4.3 测试是否成功

[root@m01 ~] # date

Tue Oct 1 00:07:48 CST 2019                            服务端现在的时间

[root@m01 ~] #

 

 

[root@nfs01 ~] # date

Tue Oct 1 00:07:53 CST 2019                            客户端现在的时间

[root@nfs01 ~] #

 

15 yum本地仓库的搭建

服务器

外网

内网

m01

10.0.0.61

172.16.1.61

15.1 什么是yum仓库

  • yum是一个rpm包的管理工具,在rpm包已经建立成功的前提下,他能够自动查找相互依赖的rpm包来进行下载

15.2 为什么需要搭建本地yum仓库

  • yum本地仓库的搭建可以减少对外网带宽的消耗
  • yum本地仓库可以提高运维的工作效率
  • yum本地仓库搭建方便,易用
  • yum本地仓库可以解决一些外国网站上面的包下载的问题

15.3 yum仓库的原理

15.3.1 图示理解

15.3.2 文字理解

  • 客户端想要下载软件X,yum配置文件会告诉yum你去哪里找仓库
  • yum找到仓库F,提取所有关于X的rpm包以及他依赖的rpm包
  • 由于RPM包比较大,所以每次先下载到本地在安装

15.4 yum本地仓库搭建

链接: https://www.cnblogs.com/flytor/p/11415049.html

15.4.1 创建挂载目录

[root@m01 ~] # mkdir -p /mnt/cdrom

[root@m01 ~] #

[root@m01 ~] # mount /dev/cdrom /mnt/cdrom

[root@m01 ~] #

15.4.2 配置本地源

[root@m01 ~] # vim /etc/yum.repos.d/liangyuxing.repo

[liangyuxing]

name=liangyuxing

baseurl=file:///mnt/cdrom

gpgcheck=0

[root@m01 yum.repos.d] #

15.4.3 安装httpd服务进行测试

[root@m01 ~] # yum -y install httpd

Loaded plugins: fastestmirror, langpacks

Loading mirror speeds from cached hostfile

* base: mirrors.aliyun.com

* extras: mirrors.aliyun.com

* updates: mirrors.aliyun.com

15.4.4 重启httpd和永久启动

[root@m01 ~] # systemctl restart httpd && systemctl enable httpd

[root@m01 ~] #

15.4.5 浏览器输入外网地址,看下是否有这个地址

15.4.6 创建yum仓库并且重新挂载

[root@m01 ~] # mkdir -p /var/www/html/centos/7/os/x86_64

[root@m01 ~] #

[root@m01 ~] #

 

[root@m01 ~] # #mount /dev/sdb1 /var/www/html/centos/7/os/x86_64/

[root@m01 ~] #

15.4.7 查看yum仓库是否创建成功

15.4.8 域名仓库常用的命令

  • createrepo -dpo /var/www/html/centos/7/os/x86_64 /var/www/html/centos/7/os/x86_64    初始化过程
  • createrepo --update /var/www/html/centos/7/os/x86_64            每加入一个rpm包就更新下
  • yum clean all                                            清空yum仓库的缓存
  • yum install --downloadonly --downloaddir=/home/ lrzsz                yum只下载不安装

15.5 yum服务端配置

[root@m01 ~] # cat /etc/yum.repos.d/liangyuxing.repo

[liangyuxing]

name=liangyuxing

baseurl=file:///http://10.0.0.61/centos/7/os/x86_64/

gpgcheck=0

 

15.6 yum客户端的配置

[root@m01 ~] # cat /etc/yum.repos.d/liangyuxing01.repo

[liangyuxing]

name=liangyuxing

baseurl=http://10.0.0.61/centos/7/os/x86_64/

gpgcheck=0

16 vpn的搭建

16.1 什么是vpn

16.2 什么时候建立vpn

公司员工在外面出差需要

数据安全性高的公司需要

16.3 vpn的原理讲解

网址链接:https://haokan.baidu.com/v?pd=wisenatural&vid=12860811934433929218

16.3.1 vpn的图示说明

16.3.2 vpn的文字说明

  • 电脑生成一个vpn虚拟地址,用户在请求的时候,会在本身请求的地址上面加一层包装,包装的信息是电脑的虚拟地址和vpn服务器地址
  • 防火墙看见则不会进行阻挡
  • 到达vpn服务器进行解封
  • vpn服务器在将数据发送到指定的服务器上
  • 服务器接收到将数据发送到vpn,vpn在进行封装,包装成vpn服务器地址和虚拟IP地址
  • 防火墙看见返回的信息不会进行阻拦
  • 到达了电脑在进行解封就看见你要请求的地址信息了

16.4 vpn linux端的搭建

16.4.1 查看你的系统和版本是否支持vpn

[root@m01 ~] # cat /dev/ppp

cat: /dev/ppp: No such device or address            可以出现这个证明支持vpn

[root@m01 ~] #

16.4.2 设置内核转发

[root@m01 ~] # grep forw /etc/sysctl.conf            查看转发是否开启

net.ipv4.ip_forward = 1

[root@m01 ~] #

sed -i 's#net.ipv4.ip_forward = 0#net.ipv4.ip_forward = 1#g' /etc/sysctl.conf

sysctl -p                                    设置内核转发

16.4.3 安装PPTP

16.4.3.1 安装需要的PPTP

[root@m01 ~] # wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

16.4.3.2 安装pptp

[root@m01 ~] # yum -y install pptp

16.4.4 配置pptp

localip 10.0.0.61                        添加本级公网IP

remoteip 192.168.0.1-10,192.168.0.20        分配给vpn的内网网段

16.4.5 设置用户与密码

[root@m01 ~] # vim /etc/ppp/chap-secrets

# Secrets for authentication using CHAP

# client server secret IP addresses

oldboy pptpd 123456 *

  • client                        登录vpn用户
  • server                        登录的vpn类型
  • secret                        登录vpn的密码
  • IP address                    允许那些服务器的地址登录vpn

16.4.6 重启pptp,查看端口信息

[root@m01 ~] # systemctl restart pptpd

[root@m01 ~] # netstat -anptu | grep pptpd

tcp 0 0 0.0.0.0:1723 0.0.0.0:* LISTEN 4789/pptpd

[root@m01 ~] #

16.4.7 windows上面连接vpn

17 jumpserver的搭建

服务器

外网

内网

jumpserver

10.0.0.91

172.16.1.91

 

17.1 jumpserver的介绍

  • jumpserver是全球首款开源的堡垒机,使用的是GNU,GPL的开源协议.
  • jumpserver是用python和go语言进行开发的,使用的是web界面进行使用的,方便,简单
  • jumpserver现在支持ssh,telnet,vnc协议资产

17.2 搭建jumpserver的意义

  • jumpserver可以拦截非法访问和恶意攻击,对不符合法的命令进行阻断
  • jumpserver建立了完善的用户权限管理制度,可以更加的精确的指定用户所特有的服务器,最大的程度导致越权造成的问题,提高运维的安全性
  • jumpserver可以集中账号的管理,提高管理效率

17.3 支持的系统

  • 硬件:ssh协议的硬件设备
  • 软件:centos,redhat,fedora,linux,Debian,SUSE,Ubuntu,FreeBSD

17.4 jumpserver的特点

  • 完全开源,GPL授权
  • python开发,容易再次开发
  • 实现了跳板机的基本功能.认证,授权,审计
  • 支持web界面,界面美观
  • 录像回放
  • 监控管理
  • 批量上传下载

17.5 jumpserver命令行配置

17.5.1 关闭防火墙和selinux

[root@jumpserver ~] # systemctl status firewalld                关闭

firewalld.service - firewalld - dynamic firewall daemon

Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)

Active: inactive (dead)

Docs: man:firewalld(1)

[root@jumpserver ~] # getenforce

Disabled                                            关闭

[root@jumpserver ~] #

17.5.2 安装所需的jumpserver的软件信息

yum -y install git python-pip mysql-devel mariadb-server gcc automake autoconf python-devel vim sshpass lrzsz readline-devel

17.5.3 下载jumpserver

[root@jumpserver opt] # cd /opt/

[root@jumpserver opt] # git clone https://git.coding.net/jumpserver/jumpserver.git

Cloning into 'jumpserver'...

Receiving objects: 100% (11559/11559), 13.56 MiB | 218.00 KiB/s, done.

Resolving deltas: 100% (7897/7897), done.

You have new mail in /var/spool/mail/root

[root@jumpserver opt] #

17.5.4 安装python依赖的软件

17.5.4.1 更新pip

[root@jumpserver opt] # mkdir ~/.pip/

You have new mail in /var/spool/mail/root

[root@jumpserver opt] # vim ~/.pip/pip.conf

[global]

index-url = https://mirrors.aliyun.com/pypi/simple/

 

[install]

trusted-host=mirrors.aliyun.com

17.5.4.2 安装的python的依赖软件

[root@jumpserver opt] # cd /opt/jumpserver/install/

[root@jumpserver install] # pip install -r requirements.txt

17.5.4.3 重启数据库

[root@jumpserver install] # systemctl restart mariadb

[root@jumpserver install] #

17.5.5 软件进行初始化的过程

[root@jumpserver install] # python /opt/jumpserver/install/install.py

请务必先查看wiki https://github.com/ibuler/jumpserver/wiki/Quickinstall

开始关闭防火墙和selinux

sed: can't read /etc/sysconfig/i18n: No such file or directory

Redirecting to /bin/systemctl stop iptables.service

Failed to stop iptables.service: Unit iptables.service not loaded.

 

请输入您服务器的IP地址,用户浏览器可以访问 [10.0.0.81]: 10.0.0.81

是否安装新的MySQL服务器? (y/n) [y]: y

请输入SMTP地址: smtp.qq.com

请输入SMTP端口 [25]:

请输入账户: 460523471@qq.com

请输入密码: enoncgdkvjhlbjbd

 

    请登陆邮箱查收邮件, 然后确认是否继续安装

 

是否继续? (y/n) [y]: y

开始写入配置文件

请输入管理员用户名 [admin]: admin

请输入管理员密码: [5Lov@wife]: admin

请再次输入管理员密码: [5Lov@wife]: admin

Starting jumpsever service:manage.py not running

run_websocket.py not running

[SUCCESS]

安装成功,请访问web, 祝你使用愉快。

请访问 https://github.com/ibuler/jumpserver 查看文档

You have new mail in /var/spool/mail/root

[root@jumpserver install] #

17.5.1.1 出现启动失败的处理情况

[root@jumpserver install] # cd /opt/jumpserver/

You have new mail in /var/spool/mail/root

root@jumpserver jumpserver] # ./service.sh start

Starting jumpsever service: [ OK ]

[root@jumpserver jumpserver] #

 

17.6 jumpserver web界面配置

17.6.1 登录web界面(账号:admin,密码:admin)

17.6.2 新增普通用户

 

17.6.3 添加资产

17.6.4 查看资产

17.6.5 添加sudo别名

17.6.6 添加系统用户(这个系统用户必须存在)

17.6.7 推送给那个服务器进行管理

17.6.8 授权规则

17.6.9 日志审计功能

17.6.9.1 登录普通用户oldboy,并且连接成功服务器nfs01

17.6.9.2 查看日志信息

17.6.9.3 登录历史

 

 

 

 

18 iptables的搭建(内网通过外网网口上网)

服务器

外网

内网

m01

10.0.0.5

172.16.1.5

web01

10.0.0.7

172.16.1.7

 

18.1 设置的好处

  • 降低网络上外网造成的服务器造成的攻击
  • 节省外网的带宽
  • 实现网络的安全

 

18.2 iptables的作用

将网络上的不安全流量进行隔离,iptables是用表,链,规则来进行设置隔离的

18.3 iptables实现内网上网的原理

18.3.1 iptables的原理图示过程

18.3.2 iptables的原理文字说明

使用-A POSTROUTING来进行内网向外网的转换,内网地址段172.16.1.0/24的进行NAT地址转换,从eth0口出去的时候转换,转换成10.0.0.6

如果不行的我情况下,就查看是不是地址跳转失败造成的

  • 查看内网向防火墙发送请求信息到的网口eth1 -i eth1
  • 查看防火墙向外网转换的网口 -o eth0
  • 查看外网向防火墙转换的网口 -I eth0
  • 查看防火墙向内网转换的网口 -o eth1

18.4 iptables实现内网上网需要的表和链

-t                            指定需要那张表来实现

-A                            指定使用什么链

-s                            指定源IP地址

-o                            指定流出流量的网卡

-i                            指定出去的网卡流量

--doprt                        指定目标端口号

18.5 实现内网上网

18.5.1 安装iptables服务

[root@lb01 ~] # yum -y install iptables

18.5.1 修改内网网关信息

[root@web01 ~] # cat /etc/sysconfig/network-scripts/ifcfg-eth1

TYPE=Ethernet

PROXY_METHOD=none

BROWSER_ONLY=no

BOOTPROTO=none

IPADDR=172.16.1.7

PREFIX=24

DEFROUTE=yes

GATEWAY=172.16.1.5

DNS1=223.5.5.5

DNS2=223.6.6.6

18.5.2 实现内网服务器可以ping通网关

[root@lb01 ~] # iptables -A INPUT -s 172.16.1.7 -j ACCEPT

[root@lb01 ~] #

18.5.3 开启路由转发的功能

[root@lb01 ~] # cat /etc/sysctl.conf

# sysctl settings are defined through files in

# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.

net.ipv4.ip_forward=1                    开启路由转发的功能

[root@lb01 ~] #

 

[root@lb01 ~] # sysctl -p /etc/sysctl.conf

net.ipv4.ip_forward = 1                重新加载路由转发配置文件

[root@lb01 ~] #

18.5.4 配置内网通过外网上网

[root@lb01 ~] # iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -j SNAT -o eth0 --to-source 10.0.0.5

[root@lb01 ~] # iptables -A FORWARD -i eth1 -s 172.16.1.0/24 -j ACCEPT

[root@lb01 ~] # iptables -A FORWARD -o eth0 -s 172.16.1.0/24 -j ACCEPT

[root@lb01 ~] # iptables -A FORWARD -i eth0 -s 172.16.1.0/24 -j ACCEPT

[root@lb01 ~] # iptables -A FORWARD -o eth1 -s 172.16.1.0/24 -j ACCEPT

[root@lb01 ~] #

19 Zabbix的搭建

19.1 什么是zabbix

Zabbix是一个web界面的分布式的监控网络的一个软件,它分为zabbix-server和zabbix-agent

19.2 zabbix的作用

服务器性能的监控,网络设备的监控,数据库性能的监控

19.3 zabbix的原理

19.4 LAMP的搭建

19.4.1 安装zabbix需要的依赖包

[root@zabbix ~] # yum -y install wget net-snmp-devel OpenIPMI-devel httpd openssl-devel java lrzsz fping-devel libcurl-devel perl-DBI pcre-devel libxml2 libxml2-devel mysql-devel gcc php php-bcmath php-gd php-xml php-mbstring php-ldap php-mysql.x86_64 php-pear php-xmlrpc  net-tools wget vim-enhanced

19.4.2 关闭防火墙和selinux

[root@zabbix ~] # systemctl status firewalld

firewalld.service - firewalld - dynamic firewall daemon

Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)

Active: inactive (dead)

Docs: man:firewalld(1)

[root@zabbix ~] # getenforce

Disabled

[root@zabbix ~] #

19.4.3 下载yum

[root@zabbix ~] # wget -P /etc/yum.repos.d http://mirrors.aliyun.com/repo/Centos-7.repo

19.4.4 查看数据库的类型

[root@zabbix ~] # yum search mysql | tac

19.4.5 开始安装LAMP

[root@zabbix ~] # yum -y install mariadb mariadb-server php php-mysql httpd

19.4.6 启动数据库

[root@zabbix ~] # systemctl restart mariadb.service

[root@zabbix ~] # systemctl enable mariadb.service

Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

[root@zabbix ~] #

19.4.7 初始化数据库并且设置root密码

[root@zabbix ~] # mysql_secure_installation

 

 

Enter current password for root (enter for none):

OK, successfully used password, moving on...

 

Set root password? [Y/n] y

New password:

Re-enter new password:

Password updated successfully!

Reloading privilege tables..

... Success!

 

Remove anonymous users? [Y/n] y            删除匿名用户

... Success!

 

Disallow root login remotely? [Y/n] y            禁止root远程登陆

... Success!

 

Remove test database and access to it? [Y/n] y        删除测试数据库并且访问它

... Success!

 

Reload privilege tables now? [Y/n] y            重新载入特权表

19.4.8 创建zabbix用户和设置密码

[root@zabbix ~] # mysql -uroot -p123456 -e "create database zabbix default character set utf8 collate utf8_bin;"

You have new mail in /var/spool/mail/root

[root@zabbix ~] #

 

[root@zabbix ~] # mysql -uroot -p123456 -e 'grant all on zabbix.* to "zabbix"@"%" identified by "zabbix";'

[root@zabbix ~] #

19.4.9 zabbix用户链接数据库

[root@zabbix ~] # mysql -uzabbix -pzabbix

Welcome to the MariaDB monitor. Commands end with ; or \g.

Your MariaDB connection id is 14

 

MariaDB [(none)]> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| zabbix |

+--------------------+

2 rows in set (0.00 sec)

 

MariaDB [(none)]>

19.4.10 启动nginx,并且查看端口

[root@zabbix ~] # systemctl restart nginx

[root@zabbix ~] # netstat -anptu | grep nginx

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4778/nginx: master

tcp6 0 0 :::80 :::* LISTEN 4778/nginx: master

19.4.11 lnmp搭建完毕

19.5 zabbix的搭建

19.5.1 安装zabbix需要的epel

[root@zabbix ~] # rpm -ivh https://mirrors.aliyun.com/epel/7/x86_64/Packages/e/epel-release-7-12.noarch.rpm

19.5.2 安装zabbix需要的yum

[root@zabbix ~] # rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch.rpm

19.5.3 安装zabbix

[root@zabbix ~] #

yum -y install zabbix-server-mysql zabbix-web-mysql zabbix-get

19.5.4 导入zabbix数据库结构

[root@zabbix ~] # cd /usr/share/doc/zabbix-server-mysql-3.0.28/

[root@zabbix zabbix-server-mysql-3.0.28] # ll

[root@zabbix zabbix-server-mysql-3.0.28] # zcat create.sql.gz | mysql -uroot -p123456 zabbix

[root@zabbix zabbix-server-mysql-3.0.28] #

19.5.5 编辑配置文件

[root@zabbix zabbix-server-mysql-3.0.28] # vi /etc/zabbix/zabbix_server.conf

### Option: TLSKeyFile

# Full pathname of a file containing the server private key.

#

# Mandatory: no

# Default:

# TLSKeyFile=

 

DBHost=localhost

 

DBName=zabbix

 

DBUser=zabbix

 

DBPassword=Zabbix

 

[root@zabbix zabbix-server-mysql-3.0.28] # vi /etc/httpd/conf.d/zabbix.conf

<IfModule mod_php5.c>

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 max_input_vars 10000

php_value always_populate_raw_post_data -1

php_value date.timezone Asia/Shanghai

19.5.6 重启httpd和zabbix

[root@zabbix zabbix-server-mysql-3.0.28] # systemctl start zabbix-server.service && systemctl enable zabbix-server.service

Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-server.service to /usr/lib/systemd/system/zabbix-server.service.

[root@zabbix zabbix-server-mysql-3.0.28] # systemctl restart httpd &&systemctl enable httpd

Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

[root@zabbix zabbix-server-mysql-3.0.28] #

 

19.6 web界面的配置

19.6.1 数据库的连接

19.6.2 开始登陆(密码zabbix)

19.7 zabbix-agent的安装

19.7.1 centos上安装agent

[root@zabbix zabbix-server-mysql-3.0.28] # yum clean all

Loaded plugins: fastestmirror, langpacks

Repository base is listed more than once in the configuration

You have new mail in /var/spool/mail/root

[root@zabbix zabbix-server-mysql-3.0.28] # yum -y install zabbix zabbix-agent

解决办法:

在安装时如出现如上图所示错误,原因是软件这两个版本不一致,仔细观察发现这两个软件包使用了不同的仓库,把epel的关闭(enabled=0,位于/etc/yum.repos.d/epel.repo)再重装

再次安装:

[root@zabbix zabbix-server-mysql-3.0.28] # yum -y install zabbix zabbix-agent

Loaded plugins: fastestmirror, langpacks

Repository base is listed more than once in the configuration

Repository updates is listed more than once in the configuration

 

 

Installed:

zabbix-agent.x86_64 0:3.0.28-1.el7

 

Complete!

19.7.2 客户端zabbix安装的问题

  • 解决办法:更新yum源

[root@zabbix ~] # rpm -ivh https://mirrors.aliyun.com/epel/7/x86_64/Packages/e/epel-release-7-12.noarch.rpm

[root@zabbix ~] # rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch.rpm

 

19.7.2 配置zabbix-agent

Server=127.0.0.1                        ipzabbix_serverIP

ServerActive=127.0.0.1                ipzabbix_serverIP(主动检测相关的配置:agent主动向server发送数据)

Hostname=127.0.0.1                    hostname可以为字符串,但是要保证zabbix_server能够解析.web页面添加主机时需要和这个一致不然没数据

19.7.3 启动zabbix-agent

[root@zabbix zabbix] # zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf

[root@zabbix zabbix] #

 

[root@zabbix ~] # systemctl restart zabbix-agent

[root@zabbix ~] #

 

20 ELK的搭建

  1. 什么是ELK,ELK的作用是什么

  • elk是一套完整的日志收集与展示的软件,是三个产品的首字母缩写,分别是ElasticSearch、Logstash 和 Kibana。
  • 问题排查。我们常说,运维和开发这一辈子无非就是和问题在战斗,所以这个说起来很朴实的四个字,其实是沉甸甸的。很多公司其实不缺钱,就要稳定,而要稳定,就要运维和开发能够快速的定位问题,甚至防微杜渐,把问题杀死在摇篮里。日志分析技术显然问题排查的基石。基于日志做问题排查,还有一个很帅的技术,叫全链路追踪,比如阿里的eagleeye 或者Google的dapper,也算是日志分析技术里的一种。
  • 监控和预警。 日志,监控,预警是相辅相成的。基于日志的监控,预警使得运维有自己的机械战队,大大节省人力以及延长运维的寿命。
  • 关联事件。多个数据源产生的日志进行联动分析,通过某种分析算法,就能够解决生活中各个问题。比如金融里的风险欺诈等。这个可以可以应用到无数领域了,取决于你的想象力。
  • 数据分析。 这个对于数据分析师,还有算法工程师都是有所裨益的。

20.2 elk的工作原理

20.3 elk的搭建

20.3.1 elk包的下载

20.3.2 创建普通用户

[root@zabbix ~] # useradd elk && echo 123456 | passwd --stdin lyx

Changing password for user lyx.

passwd: all authentication tokens updated successfully.

[root@zabbix ~] #

20.3.3 使用普通用户开启ElasticSearch

[root@zabbix ~] # su - lyx

Last login: Tue Nov 26 16:48:54 CST 2019 on pts/1

[lyx@zabbix ~] $ cd elk/elasticsearch-7.4.2/bin/

[lyx@zabbix bin] $ ./elasticsearch -d

OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.

[lyx@zabbix bin] $

20.3.4 编辑kibana配置文件

20.3.5 启动kibana

20.4 kibanaweb界面启动

21章 nfs高可用的搭建

链接: https://www.cnblogs.com/webforward/p/6182786.html

服务器

内网

外网

nfs ()

172.16.1.31

10.0.031

mysql()

172.16.1.51

10.0.0.51

web

172.16.1.7

10.0.0.7

21.1 什么是nfs高可用

nfs高可用为了解决挂载单点故障造成的前端无法访问后端的数据而产生的

21.2 使用nfs高可用的好处

  • 利用VIP地址漂移的技术解决了存储服务器单点的故障
  • 使网络架构更加的稳定

 

21.3 高可用的原理图

21.3.1 图示讲解

21.3.2 文字讲解

  • 用户存入数据,会先访问负载均衡,负载均衡将数据分配给web端,web端通过挂载将数据放入存储服务器,存储服务器数据发生更新的时候,会实时额将数据传输到备份服务器
  • 存储服务器出现问题就会导致没有办法将数据传入到存储服务器上面
  • 这会使用高可用则可以解决这个问题

21.4 高可用的配置

21.4.1 安装软件keepalivednfs-utils

[root@nfs01 ~] # yum -y install nfs-utils keepalived

 

[root@db ~] # yum -y install nfs keepalived

21.4.2 创建存储目录和修改配置文件(主副都需要)

[root@nfs01 ~] # mkdir -p /home/shares/ &

[root@nfs01 ~] # cat /etc/exports

/home/shares 10.0.0.0/24(rw,sync,no_root_squash)

 

[root@nfs01 ~] #

21.4.3 环境的准备(主副都需要)

[root@nfs01 ~] # cd /home/shares/

[root@nfs01 shares] # ll

total 4

-rw-r--r-- 1 root root 4 Nov 19 17:08 aa.txt

-rw-r--r-- 1 root root 0 Nov 19 17:08 files1

-rw-r--r-- 1 root root 0 Nov 19 17:08 files10

-rw-r--r-- 1 root root 0 Nov 19 17:08 files2

-rw-r--r-- 1 root root 0 Nov 19 17:08 files3

-rw-r--r-- 1 root root 0 Nov 19 17:08 files4

-rw-r--r-- 1 root root 0 Nov 19 17:08 files5

-rw-r--r-- 1 root root 0 Nov 19 17:08 files6

-rw-r--r-- 1 root root 0 Nov 19 17:08 files7

-rw-r--r-- 1 root root 0 Nov 19 17:08 files8

-rw-r--r-- 1 root root 0 Nov 19 17:08 files9

[root@nfs01 shares] #

21.4.4 配置高可用文件

21.4.4.1 主配置

[root@nfs01 shares] # vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

global_defs {

router_id lb01                        集群的名称

}

 

vrrp_instance oldboy66 {                配置实例((家族信息)

state MASTER                        家族的身份

interface eth0                        指定VIP出现在哪个网卡上

virtual_router_id 66                    家族的编号

priority 100                        指定服务的优先级

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

10.0.0.10                            VIP地址的指定

}

}

21.4.4.2 副配置文件

[root@nfs01 shares] # vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

global_defs {

router_id lb02                        集群的名称

}

 

vrrp_instance oldboy66 {                配置实例((家族信息)

state BACKUP                        家族的身份

interface eth0                        指定VIP出现在哪个网卡上

virtual_router_id 66                    家族的编号

priority 50                        指定服务的优先级

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

10.0.0.10                            VIP地址的指定

}

}

21.4.5 重启nfskeepalived

[root@nfs01] # systemctl restart keepalived.service

You have new mail in /var/spool/mail/root

[root@nfs01] #

 

[root@db] # systemctl restart keepalived.service

You have new mail in /var/spool/mail/root

[root@db] #

 

[root@nfs01] # systemctl restart nfs

You have new mail in /var/spool/mail/root

[root@nfs01] #

 

[root@db] # systemctl restart nfs

You have new mail in /var/spool/mail/root

[root@db] #

21.4.6 web服务器挂载存储目录

[root@web01 ~] # mount -t nfs 10.0.0.10:/home/shares /shares

[root@web01 ~] #

 

10.0.0.10:/home/shares 99G 5.8G 93G 6% /shares

21.5 高可用的测试

21.5.1 判断nfs服务是不是开启的,利用权重值来进行调整

[root@nfs01 keepalived] # vim notify_master.sh

#!/bin/bash

num=`ps -ef|grep [n]fsd | wc -l`

if [ "$num" != 0 ];then

exit 0

else

exit 1

fi

 

vrrp_script check_web {

script "/etc/keepalived/notify_master.sh"

weight -70

interval 2

}

 

track_script {

check_web

}

21.5.2 判断主上面的vip值和挂载IP是否存在

[root@db01 keepalived] # vim 1.sh

#!/bin/bash

VIP_ADDR=`ssh 172.16.1.31 ip a s | grep -o "10.0.0.10" | wc -l`

WEB_DATA=`ssh 172.16.1.7 df -h | grep "10.0.0.31" | wc -l`

while true

do

if [ "$WEB_DATA" == 1 ] && [ "$VIP_ADDR" == 1 ]

then

ssh 172.16.1.7 umount -lf /shares &>/dev/null

ssh 172.16.1.7 mount -t nfs 10.0.0.31:/home/shares /shares & > /dev/null

else

ssh 172.16.1.7 umount -lf /shares &>/dev/null

ssh 172.16.1.7 mount -t nfs 10.0.0.10:/home/shares /shares &>/dev/null

fi

sleep 2;

done

 

22章 mysql主从同步

服务器

内网

外网

mysql()

172.16.1.51

10.0.0.51

mysql()

172.16.1.31

10.0.0.31

22.1 使用数据库主从同步(读写分离)的好处

  • 读写分离,支持更大的并发,提高用户的体验度
  • 拥有数据库的冗余机制,提高数据的安全性

22.2 数据库的主从原理

22.2.1 数据库原理图

22.2.2 数据原理图解释

  • 主服务器开启big-log日志(记录数据的增删改查)
  • 从服务器开启I/O线程响应请求想将big-log的日志内容给我
  • 从服务器会将数据放到relay-info中,生成master.info,(记录master的一些信息)
  • 从服务器会一直监控relay日志的更新,一旦有更新,将信息解析成sql内容.添加数据库中
  • 保持2个服务器的数据库内容一致

22.3 什么时候使用主从数据库

  • 并发量大的时候
  • 数据需要备份
  • 大型公司或者一些国家级的单位需要使用

22.4 开始配置主从同步

22.4.1 主服务器配置

22.4.1.1 数据库开始安装

[root@db ~] # yum install mariadb mariadb-server -y

22.4.1.2 数据库启动

[root@db ~] # systemctl start mariadb.service

22.4.1.3 安装数据库

[root@db ~] # mysql_secure_installation

 

Enter current password for root (enter for none):

OK, successfully used password, moving on...

 

Set root password? [Y/n] y

New password:

Re-enter new password:

Password updated successfully!

Reloading privilege tables..

... Success!

 

Remove anonymous users? [Y/n] y

... Success!

 

 

Disallow root login remotely? [Y/n] n

 

Remove test database and access to it? [Y/n] y

- Dropping test database...

... Success!

- Removing privileges on test database...

... Success!

 

Reload privilege tables now? [Y/n] y

... Success!

 

Thanks for using MariaDB!

22.4.1.4 登录数据库创建数据库并且添加测试数据

[root@db ~] # mysql -uroot -p123456

 

MariaDB [(none)]> create database DB1;            创建数据库DB1

Query OK, 1 row affected (0.00 sec)

 

MariaDB [(none)]> show databases;                显示数据库

+--------------------+

| Database |

+--------------------+

| information_schema |

| DB1 |

| mysql |

| performance_schema |

+--------------------+

4 rows in set (0.00 sec)

MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by '123456' with grant option;        进行数据库授权和密码的修改

Query OK, 0 rows affected (0.00 sec)

 

MariaDB [(none)]> use DB1;            使用数据库

Database changed

MariaDB [DB1]> create table test (name VARCHAR(20), sex CHAR(1), birth DATE, birthaddr VARCHAR(20));                        创建表

Query OK, 0 rows affected (0.02 sec)

 

MariaDB [DB1]> insert into test values ('abccs','f','1995-10-22','china');            插入数据

Query OK, 1 row affected (0.00 sec)

 

MariaDB [DB1]> select * from test;                    查看数据

+-------+------+------------+-----------+

| name | sex | birth | birthaddr |

+-------+------+------------+-----------+

| abccs | f | 1995-10-22 | china |

+-------+------+------------+-----------+

1 row in set (0.00 sec)

 

MariaDB [DB1]>

22.4.1.5 将数据进行备份,并且发送到从服务器上

[root@db ~] # mysqldump -h localhost -u root -p DB1 > /tmp/1.sql

Enter password:

[root@db ~] #

 

[root@db ~] # scp -r /tmp/1.sql 172.16.1.31:/tmp/

root@172.16.1.31's password:

1.sql 100% 1926 1.2MB/s 00:00

[root@db ~] #

22.4.1.6 主服务器配置文件进行配置

#设置服务器ID,1的表示为主服务器;如果服务器没有以下配置,添加,如果有就跳过。

server-id=1

###启动MySQ二进制日志系统,注意:如果原来的配置文件中已经有这一行,就不用再添加了。

log_bin=mysql-bin?

###需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行\

binlog-do-db=DB1

###不同步mysql系统数据库

binlog-ignore-db=mysql

22.4.1.7 重启服务

[root@db ~] # systemctl restart mariadb.service

[root@db ~] #

 

[root@db ~] # mysql -uroot -p123456

MariaDB [(none)]> show master status;

+-------------------+----------+--------------+------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+-------------------+----------+--------------+------------------+

| mysql-bin?.000001 | 245 | DB1 | mysql |

+-------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

 

MariaDB [(none)]>

  • 查看主服务器,出现以上类似信息;如果看不见以下结果或者为空的话,说明my.cnf配置文件配置的有问题,请检查即可。
  • 这里记住File的值:mysql-bin.000001Position的值:245,后面会用到。

 

22.4.2 从服务器配置

22.4.2.1 数据库开始安装

[root@db ~] # yum install mariadb mariadb-server -y

22.4.2.2 数据库启动

[root@db ~] # systemctl start mariadb.service

 

 

22.4.2.3 安装数据库

[root@db ~] # mysql_secure_installation

 

Enter current password for root (enter for none):

OK, successfully used password, moving on...

 

Set root password? [Y/n] y

New password:

Re-enter new password:

Password updated successfully!

Reloading privilege tables..

... Success!

 

Remove anonymous users? [Y/n] y

... Success!

 

 

Disallow root login remotely? [Y/n] n

 

Remove test database and access to it? [Y/n] y

- Dropping test database...

... Success!

- Removing privileges on test database...

... Success!

 

Reload privilege tables now? [Y/n] y

... Success!

 

Thanks for using MariaDB!

22.4.2.4 登录数据库创建数据库并且添加测试数据

[root@db ~] # mysql -uroot -p123456

 

MariaDB [(none)]> create database DB1;            创建数据库DB1

Query OK, 1 row affected (0.00 sec)

 

MariaDB [(none)]> show databases;                显示数据库

+--------------------+

| Database |

+--------------------+

| information_schema |

| DB1 |

| mysql |

| performance_schema |

+--------------------+

4 rows in set (0.00 sec)

22.4.2.5 导入数据库

[root@nfs01 ~] # mysql -h localhost -u root -p DB1 < /tmp/1.sql

Enter password:

[root@nfs01 ~] #

22.4.2.6 更新master信息

[root@nfs01 ~] # mysql -uroot -p

Enter password:

 

MariaDB [(none)]> slave stop;

Query OK, 0 rows affected, 1 warning (0.00 sec)

 

MariaDB [(none)]> use DB1;

 

Database changed

MariaDB [DB1]> change master to master_host='172.16.1.51',master_user='root',master_password='123456',master_log_file='mysql-bin?.000001',master_log_pos=245;

 

MariaDB [DB1]>

 

  • change master to master_host='主服务器',master_user='mysql用户名',master_password='mysql服务密码',master_log_file='File值',master_log_pos=Postion值;

22.4.2.7 添加配置文件(/etc/my.cnf)

 

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

#设置服务器ID,1的表示为主服务器;如果服务器没有以下配置,添加,如果有就跳过。

server-id=2

####启动MySQ二进制日志系统,注意:如果原来的配置文件中已经有这一行,就不用再添加了。

log_bin=mysql-bin?

####需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行\

binlog-do-db=DB1

####不同步mysql系统数据库

binlog-ignore-db=mysql

22.4.2.8 重启数据库

[root@nfs01 ~] # systemctl restart mariadb

[root@nfs01 ~] #

22.4.2.8 查看主从同步是否正常

[root@nfs01 ~] # mysql -uroot -p

Enter password:

Welcome to the MariaDB monitor. Commands end with ; or \g.

Your MariaDB connection id is 4

Server version: 5.5.64-MariaDB MariaDB Server

 

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

MariaDB [(none)]> show master status;                     查看状态

+-------------------+----------+--------------+------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+-------------------+----------+--------------+------------------+

| mysql-bin?.000001 | 245 | DB1 | mysql |

+-------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

 

MariaDB [(none)]> slave start;                            启动slave

Query OK, 0 rows affected, 1 warning (0.00 sec)

 

MariaDB [(none)]> show master status\G;                    查看主从同步是否正常

*************************** 1. row ***************************

File: mysql-bin?.000001

Position: 245

Binlog_Do_DB: DB1

Binlog_Ignore_DB: mysql

1 row in set (0.00 sec)

 

ERROR: No query specified

 

MariaDB [(none)]> show slave status\G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 172.16.1.51

Master_User: root

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin?.000001

Read_Master_Log_Pos: 245

Relay_Log_File: mariadb-relay-bin.000005

Relay_Log_Pos: 530

Relay_Master_Log_File: mysql-bin?.000001

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_DB:

Replicate_Ignore_DB:

Replicate_Do_Table:

Replicate_Ignore_Table:

Replicate_Wild_Do_Table:

Replicate_Wild_Ignore_Table:

Last_Errno: 0

Last_Error:

Skip_Counter: 0

Exec_Master_Log_Pos: 245

Relay_Log_Space: 826

Until_Condition: None

Until_Log_File:

Until_Log_Pos: 0

Master_SSL_Allowed: No

Master_SSL_CA_File:

Master_SSL_CA_Path:

Master_SSL_Cert:

Master_SSL_Cipher:

Master_SSL_Key:

Seconds_Behind_Master: 0

Master_SSL_Verify_Server_Cert: No

Last_IO_Errno: 0

Last_IO_Error:

Last_SQL_Errno: 0

Last_SQL_Error:

Replicate_Ignore_Server_Ids:

Master_Server_Id: 1

1 row in set (0.00 sec)

 

ERROR: No query specified

 

MariaDB [(none)]>

 

推荐阅读