内容包括:
- Redis安裝腳本:
- installRedis.sh、Redis.conf、Sentinel.conf、VIP綁定腳本
- Nginx安裝腳本:
- installNginx.sh、makeNginx.sh、nginx.conf
- RabbitMQ及叢集的安裝腳本:
- installRabbitMQ.sh 、 installRabbitMQMirrorCluster.sh
- HAProxy安裝腳本:
- installHAProxy.sh、haproxy.cfg
- Keepalived安裝腳本:
- installKeepalived.sh 、keepalived.conf、 ha_check.sh
- Mysql安裝腳本
- centos7的repo樣本、重新整理yum源的語句:
安裝Redis
cd /tools
scp [email protected]:/tools/redis-5.0.5.tar.gz /tools
yum install -y gcc
tar -zxvf redis-5.0.5.tar.gz -C /usr/local/
cd /usr/local/redis-5.0.5/
make install prefix=/usr/local/bin
mkdir /etc/redis
cp /usr/local/redis-5.0.5/redis.conf /etc/redis/
chmod 777 /etc/redis/redis.conf
sed -i 's/daemonize no/daemonize yes/g' /etc/redis/redis.conf
sed -i 's/bind 127.0.0.1/bind 0.0.0.0/g' /etc/redis/redis.conf
sed -i 's/# requirepass foobared/requirepass ljfirst/g' /etc/redis/redis.conf
cp /usr/local/redis-5.0.5/utils/redis_init_script /etc/init.d/redis
chmod +x /etc/init.d/redis
sed -i 's/REDISPORT=6379/REDISPORT=redis/g' /etc/init.d/redis
chkconfig --add redis
systemctl enable redis
systemctl status redis.service
systemctl start redis.service
protected-mode yes
bind 0.0.0.0
port 6379
daemonize yes
pidfile "/var/run/redis_6379.pid"
logfile "/etc/redis/redis.log"
dir "/etc/redis"
dbfilename "dump.rdb"
replica-read-only yes
masterauth "ljfirst"
requirepass "ljfirst"
replicaof 10.xxx.xxx.214 6379 # 從節點才配置這個
port 26379
daemonize yes
protected-mode no
pidfile "/var/run/redis-sentinel.pid"
logfile "/etc/redis/sentinel.log"
dir "/tmp"
sentinel myid 9632dc4e6abd37cfbc1cf347bcf38f3aa81a2450
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster01 10.xxx.xxx.214 6379 2
sentinel down-after-milliseconds mymaster01 5000
sentinel failover-timeout mymaster01 10000
sentinel client-reconfig-script mymaster01 /etc/redis/transip.sh # 啟用VIP的語句
sentinel auth-pass mymaster01 Sxx3d3xxx4
#!/bin/bash
MASTER_IP=$6
LOCAL_IP='10.xxx.xxx.212'
VIP='10.xxx.xxx.202'
NETMASK='24'
# 此處的網卡用 ifconfig 檢視
INTER
if [ ${MASTER_IP}=${LOCAL_IP} ]; then
/sbin/ip addr add ${VIP}/${NETMASK} dev ${INTERFACE}
/sbin/arping -q -c 3 -A ${VIP} -I ${INTERFACE}
exit 0
else
/sbin/ip addr del ${VIP}/${NETMASK} dev ${INTERFACE}
exit 0
fi
exit 1
安裝NGINX
cd /tools
scp [email protected]:/tools/\{nginx.sh,nginx-1.19.0.tar.gz} /tools
tar -zxvf /tools/nginx-1.19.0.tar.gz -C /usr/local/
yum install -y pcre pcre-devel zlib zlib-devel openssl openssl-devel
cd /usr/local/nginx-1.19.0
cp /tools/nginx.sh ./
source nginx.sh
make && make install
mkdir -p /var/temp/nginx/client
cd /usr/local/nginx/sbin
./nginx
ps aux |grep nginx
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-file-aio \
--with-http_realip_module
- 如果需要安裝支援tcp的nginx,那麼需要在最後加上一句–with-stream,并進行重新編譯(make)
- nginx.conf
#user nobody;
user root;
worker_processes 1;
error_log /usr/local/nginx/logs/error.log;
pid /usr/local/nginx/logs/nginx.pid;
events {
worker_connections 1024;
}
# 此處是 TCP 的分流寫法,與HTTP的好像不能同時存在
stream{
upstream cloudadapt{
server 10.145.33.221:81 weight=1;
server 10.145.33.222:81 weight=1;
}
server {
listen 81;
proxy_timeout 500s;
#server_name localhost;
proxy_pass cloudadapt;
}
}
# 此處是 HTTP 的分流寫法
http {
include mime.types;
default_type application/octet-stream;
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
# 此處是Nginx本服務對外暴露的位址
server {
listen 88;
server_name localhost;
location / {
root /usr/local/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# 此處是用于負載均衡的寫法
upstream loverlj{
server 10.xx.xx.15:8104 ;
#此處為主備模式
server 10.xx.xx.16:8104 backup;
}
server {
listen 1104;
server_name localhost;
location / {
proxy_pass http://loverlj;
root /usr/local/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
安裝RabbitMQ
cd /tools
scp [email protected]:/tools/\{rabbitmq-server-3.7.17-1.el7.noarch.rpm,socat-1.7.3.2-2.el7.x86_64.rpm,erlang-22.0.7-1.el7.x86_64.rpm} /tools
rpm -ivh erlang-22.0.7-1.el7.x86_64.rpm
yum -y install socat
rpm -ivh rabbitmq-server-3.7.17-1.el7.noarch.rpm
service rabbitmq-server start
chkconfig rabbitmq-server on
cd /usr/lib/rabbitmq/bin
rabbitmqctl add_user sa [email protected]
rabbitmqctl set_user_tags sa administrator
rabbitmqctl set_permissions -p / sa ".*" ".*" ".*"
rabbitmq-plugins enable rabbitmq_management
- installRabbitMQMirrorCluster.sh
echo "please handle"
read
ipmaster=10.12
ip1=10.12
ip2=10.12
scp /var/lib/rabbitmq/.erlang.cookie [email protected]$ip1:/root/
scp /var/lib/rabbitmq/.erlang.cookie [email protected]$ip2:/root/
rabbitmq-server --detached
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster --ram [email protected]
rabbitmqctl cluster_status
echo $ipmaster kf06mq01 >> /etc/hosts
echo $ip1 kf06mq2 >> /etc/hosts
echo $ip2 kf06mq03 >> /etc/hosts
rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
安裝HAProxy
scp [email protected]:/tools/\{haproxy.cfg,haproxy-2.1.0.tar.gz,ha_check.py} /tools
tar -zxvf haproxy-2.1.0.tar.gz -C /usr/local/
cd /usr/local/haproxy-2.1.0
make TARGET=linux31
make install PREFIX=/usr/local/haproxy
cp /tools/haproxy.cfg /usr/local/haproxy/conf
cp /usr/local/haproxy/sbin/haproxy /etc/init.d/
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg
####################HAProxy配置中分成五部分内容,當然這些元件不是必選的,可以根據需要選擇部分作為配置。
#global :參數是程序級的,通常和作業系統(OS)相關。這些參數一般隻設定一次,如果配置無誤,就不需要再次配置進行修改
#defaults:配置預設參數的,這些參數可以被利用配置到frontend,backend,listen元件
#frontend:接收請求的前端虛拟節點,Frontend可以根據規則直接指定具體使用後端的 backend(可動态選擇)。
#backend :後端服務叢集的配置,是真實的伺服器,一個Backend對應一個或者多個實體伺服器。
#listen :Frontend和Backend的組合體。
global
log 127.0.0.1 local0
maxconn 20000
chroot /usr/local/haproxy-2.1.0
uid 200
gid 200
daemon
pidfile /usr/local/haproxy-2.1.0/haproxy.pid
ulimit-n 40020
nbproc 1
defaults
log global
mode tcp
option tcplog
option dontlognull
retries 3
timeout connect 5000
timeout client 50000
timeout server 50000
# errorfile 400 /etc/haproxy/errors/400.http
# errorfile 403 /etc/haproxy/errors/403.http
# errorfile 408 /etc/haproxy/errors/408.http
# errorfile 500 /etc/haproxy/errors/500.http
# errorfile 502 /etc/haproxy/errors/502.http
# errorfile 503 /etc/haproxy/errors/503.http
# errorfile 504 /etc/haproxy/errors/504.http
listen middlesoft
bind *:5673
balance roundrobin
mode tcp
option tcplog
option tcpka
#bind-process 2
timeout client 15s
timeout connect 3s
timeout server 15s
server HARabbitmq1 10.129.0.162:5672 check inter 5000 rise 2 fall 3
server HARabbitmq2 10.129.0.163:5672 check inter 5000 rise 2 fall 3
server HARabbitmq3 10.129.0.164:5672 check inter 5000 rise 2 fall 3
# weight - 調節伺服器的負重
# check - 允許對該伺服器進行健康檢查
# inter - 設定連續的兩次健康檢查之間的時間,機關為毫秒(ms),預設值 2000(ms)
# rise - 指定多少次連續成功的健康檢查後,可認定該伺服器處于可操作狀态,預設值 2
# fall - 指定多少次不成功的健康檢查後,認為伺服器為當掉狀态,預設值 3
# maxconn - 指定可被發送到該伺服器的最大并發連接配接數
listen rabbitmqbrower
bind *:15673
balance roundrobin
server HARabbitmq1 10.129.0.162:15672 check inter 5000 rise 2 fall 3
server HARabbitmq2 10.129.0.163:15672 check inter 5000 rise 2 fall 3
server HARabbitmq3 10.129.0.164:15672 check inter 5000 rise 2 fall 3
# 配置haproxy web監控,檢視統計資訊
listen monitoring
bind 0.0.0.0:8100
mode http
option httplog
stats enable
#設定haproxy監控位址為http://localhost:8100/stats
stats uri /stats
stats refresh 5s
安裝 Keepalived
scp [email protected]:/tools/\{keepalived.conf,keepalived-2.0.20.tar.gz} /tools
tar -zxvf keepalived-2.0.20.tar.gz -C /usr/local/
cd /usr/local/keepalived-2.0.20
yum -y install openssl-devel
./configure --prefix=/usr/local/keepalived
make && make install
mkdir /etc/keepalived
cp /tools/keepalived.conf /etc/keepalived/
chmod 664 /etc/keepalived/keepalived.conf
cp /usr/local/keepalived-2.0.20/keepalived/etc/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin
chmod +x /etc/init.d/keepalived
chkconfig --add keepalived
systemctl enable keepalived.service
systemctl start keepalived.service
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id HARabbitmq3
#vrrp_mcast_group4 224.26.1.1
}
#此處需要保證“chk_haproxy”和“{”之間有空格,實在不行就換行,可以解決一些奇怪的問題
vrrp_script chk_haproxy {
script "/tools/ha_check.sh"
interval 1 #定時1秒執行一次檢查
weight -2
timeout 30
}
vrrp_instance haproxy {
state BACKUP
interface eth0
virtual_router_id 108
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
track_script {
chk_haproxy
}
virtual_ipaddress {
10.129.0.180
}
}
- ha_check.sh
- 用腳本的時候,source 一下,確定腳本是正确可以執行的,另外${ps -C haproxy --no-header |wc -l} 如果執行不了,試着把{}換成 : ``
- 需要多測試幾次這個腳本,特别是sleep語句的位置,是在if内外?
- 給ha_check.sh腳本加777權限:chmod 777 ha_check.sh
- 千萬注意Linux寫法:
- vim ha的腳本以後,用:set ff看一下格式
- #!/bin/bash需要頂格寫,第二行要空出來
#!/bin/bash
if [ ${ps -C haproxy --no-header |wc -l} -eq 0 ]; then
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg
sleep 3
# echo aaaa
fi
if [ ${ps -C haproxy --no-header |wc -l} -eq 0 ]; then
systemctl stop keepalived
# echo bbbb
fi
Mysql安裝腳本
在這裡插入代碼片
在這裡插入代碼片
centos7的repo樣本、重新整理yum源的語句
[base7_7]
name=CentOS-7Server - Base
baseurl=http://10.129.9.217/repo/20200701/centos7_7/base7_7
enabled=1
gpgcheck=0
[updates7_7]
name=CentOS-7Server - Updates
baseurl=http://10.129.9.217/repo/20200701/centos7_7/updates7_7
enabled=1
gpgcheck=0
[extras7_7]
name=CentOS-7Server - Extras
baseurl=http://10.129.9.217/repo/20200701/centos7_7/extras7_7
enabled=1
gpgcheck=0
scp [email protected]:/tools/\{centos7.repo,installHAProxy.sh,installkeepalived.sh,installNginx.sh,installRabbitMQMirrorCluster.sh,installRabbitMQ.sh,installRedis.sh} /etc/yum.repos.d/
chmod 777 /etc/yum.repos.d/centos7.repo
yum repolist all
yum clean all
yum makecache
yum install -y gcc-c++ tcl