lamp(3)redis服务

文章目录

    • 1.redis主从复制环境部署
        • 1)system启动
        • 2)redis常用指令:
        • 3)redis自动故障迁移
    • 2.Redis 集群
      • 1.redis cluster 16384号哈希槽
      • 2.redis持久化
      • 3.mysql和redis同步机制
        • 1)安装 lib_mysqludf_json
        • 2)安装 gearman-mysql-udf
    • 3.varnish
      • 1.根据不同的域名,分发到不同的后端主机
      • 2.负载均衡
      • 3.健康检查
      • 4.nginx代理配置
      • 5.为发往后端主机的请求添加X-Forward-For首部:


1.redis主从复制环境部署

适用数据多,持久化,slave端只读

[root@zhenji Downloads]# scp redis-6.2.1.tar.gz server1:
[root@zhenji Downloads]# scp redis-6.2.1.tar.gz server2:
[root@zhenji Downloads]# scp redis-6.2.1.tar.gz server3:
##server1
[root@server1 ~]# tar zxf redis-6.2.1.tar.gz 
[root@server1 ~]# yum install gcc -y##先安装gcc,否则报错的话需要清理环境,重新解压
[root@server1 ~]# cd redis-6.2.1/
[root@server1 redis-6.2.1]# make 
[root@server1 redis-6.2.1]# make install
[root@server1 redis-6.2.1]# mkdir /etc/redis
[root@server1 redis-6.2.1]# cp redis.conf /etc/redis/6379.conf
[root@server1 redis-6.2.1]# cd utils/
[root@server1 utils]# ls
[root@server1 utils]# cp redis_init_script /etc/init.d/redis_6379
[root@server1 utils]# vim /etc/redis/6379.conf
##修改三个地方
bind 0.0.0.0##监听所有端口
dir /var/lib/redis
daemonize yes##打入后台
[root@server1 utils]# mkdir /var/lib/redis
[root@server1 utils]# /etc/init.d/redis_6379 start
[root@server1 utils]# scp /etc/redis/6379.conf server2:/etc/redis/6379.conf
[root@server1 utils]# scp /etc/redis/6379.conf server3:/etc/redis/6379.conf
[root@server1 utils]# scp /etc/init.d/redis_6379 server2:/etc/init.d/redis_6379
[root@server1 utils]# scp /etc/init.d/redis_6379 server3:/etc/init.d/redis_6379

##server2

[root@server2 ~]# tar zxf redis-6.2.1.tar.gz 
[root@server2 ~]# yum install gcc -y
[root@server2 ~]# cd redis-6.2.1/
[root@server2 redis-6.2.1]# make 
[root@server2 redis-6.2.1]# make install
[root@server2 redis-6.2.1]# mkdir /etc/redis

[root@server2 utils]# vim /etc/redis/6379.conf
##添加
# replicaof  
replicaof 172.25.3.1 6379
[root@server2 redis-6.2.1]# mkdir /var/lib/redis
[root@server2 redis-6.2.1]# /etc/init.d/redis_6379 start
[root@server1 utils]# redis-cli
127.0.0.1:6379> set name westos
OK
127.0.0.1:6379> get name
"westos"
[root@server2 redis-6.2.1]# redis-cli
127.0.0.1:6379> get name
"westos"
127.0.0.1:6379> info
# Replication
role:slave

1)system启动

[Service]
#ExecStart=/usr/local/bin/redis-server --supervised systemd --daemonize no
## Alternatively, have redis-server load a configuration file:
#ExecStart=/usr/local/bin/redis-server /path/to/your/redis.conf
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli -p 6379 shutdown
ExecRload=/bin/kill/-s HUP $MAINPID
LimitNOFILE=10032
NoNewPrivileges=yes
#OOMScoreAdjust=-900
PrivateTmp=yes
Type=notify
#TimeoutStartSec=infinity
#TimeoutStopSec=infinity
#UMask=0077
#User=redis
#Group=redis
#WorkingDirectory=/var/lib/redis

2)redis常用指令:

config get * 查看配置
select 1 选择数据库
flushdb 清空当前数据库
flushall 清空所有数据库
move key 1 移动key
del key 删除
rename oldkey newkey 改名
expire key 10 设置过期时间
persist key 设置持久化
keys user* 查询
exists key 判断是否存在
[root@server1 utils]# redis-cli
127.0.0.1:6379> config get *

127.0.0.1:6379> config get bind
1) "bind"
2) "0.0.0.0"
127.0.0.1:6379> config get save
1) "save"
2) "3600 1 300 100 60 10000"
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get name
(nil)
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> 
127.0.0.1:6379> get name
"westos"
127.0.0.1:6379> FLUSHDB
OK
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379> FLUSHALL
OK
127.0.0.1:6379> set name westos
OK
127.0.0.1:6379> KEYS name
1) "name"
127.0.0.1:6379> config get min-slaves-to-write
1) "min-slaves-to-write"
2) "0"
127.0.0.1:6379> config get min-slaves-max-lag
1) "min-slaves-max-lag"
2) "10"

3)redis自动故障迁移

##负载均衡

[root@server1 utils]# vim /etc/redis/6379.conf 
min-slaves-to-write 1
[root@server1 utils]# /etc/init.d/redis_6379 stop
[root@server1 utils]# /etc/init.d/redis_6379 start

[root@server2 utils]# vim /etc/redis/6379.conf 
min-slaves-to-write 1
[root@server2 utils]# /etc/init.d/redis_6379 stop
[root@server2 utils]# /etc/init.d/redis_6379 start

[root@server3 utils]# vim /etc/redis/6379.conf 
min-slaves-to-write 1
[root@server3 utils]# /etc/init.d/redis_6379 stop
[root@server3 utils]# /etc/init.d/redis_6379 start



[root@server1 ~]# cd redis-6.2.1/
[root@server1 redis-6.2.1]# cp sentinel.conf /etc/redis/
[root@server1 redis-6.2.1]# cd /etc/redis/
[root@server1 redis]# vim sentinel.conf 
##改动两个
sentinel monitor mymaster 172.25.0.1 6379 2
sentinel down-after-milliseconds mymaster 10000
[root@server1 redis]# scp sentinel.conf server2:/etc/redis/
[root@server1 redis]# scp sentinel.conf server3:/etc/redis/

[root@server1 redis-6.2.1]# redis-sentinel /etc/redis/sentinel.conf
[root@server2 redis-6.2.1]# redis-sentinel /etc/redis/sentinel.conf
[root@server3 redis-6.2.1]# redis-sentinel /etc/redis/sentinel.conf

##重开一个后端连接server1
[root@server1 ~]# redis-cli
127.0.0.1:6379> SHUTDOWN
##当一个主服务器不能正常工作时, Sentinel 会开始一 次自动故障迁移操作,它会将失效主服务器的其中一个从服务器升级为新的主服务器, 并让失效主服务器的其他从服务器改为复制新的主服务器。 

2.Redis 集群

官网:http://www.redis.cn/topics/cluster-tutorial.html

server1,2,3为master,server4客户端

[root@server2 ~]# vim /etc/redis/6379.conf 
#replicaof 172.25.3.1 6379
#min-slaves-to-write 1
[root@server2 ~]# /etc/init.d/redis_6379 stop
[root@server2 ~]# /etc/init.d/redis_6379 start
[root@server2 ~]# redis-cli
127.0.0.1:6379> info
# Replication
role:master

[root@server3 ~]# vim /etc/redis/6379.conf 
#replicaof 172.25.3.1 6379
#min-slaves-to-write 1
[root@server3 ~]# /etc/init.d/redis_6379 stop
[root@server3 ~]# /etc/init.d/redis_6379 start
[root@server3 ~]# redis-cli
127.0.0.1:6379> info
# Replication
role:master

1.redis cluster 16384号哈希槽

[root@server1 bin]# scp /usr/local/bin/redis-cli server4:/usr/local/bin/
[root@server1 ~]# cd redis-6.2.1/utils/create-cluster/
[root@server1 create-cluster]# ls
create-cluster  README
[root@server1 create-cluster]# vim create-cluster 
[root@server1 create-cluster]# ./create-cluster start 
[root@server1 create-cluster]# ./create-cluster create
redis-cli --cluster check 127.0.0.1:30001

[root@server1 create-cluster]# cd /etc/redis/
[root@server1 redis]# ls
6379.conf  sentinel.conf
[root@server1 redis]# vim 6379.conf 
#min-slaves-to-write 1##注释
[root@server1 redis]# /etc/init.d/redis_6379 stop
/var/run/redis_6379.pid does not exist, process is not running
[root@server1 redis]# /etc/init.d/redis_6379 start
##server4上连集群:
redis-cli -c -p 30001#从某一端口进去都可以连接任意个后端
redis-cli --cluster check 127.0.0.1:30001
./create-cluster start 
vim create-cluster 
##8个
./create-cluster  start 
redis-cli --cluster help
redis-cli --cluster add-node 127.0.0.1:30007 127.0.0.1:30001 ##添加节点,默认是master
redis-cli --cluster check 127.0.0.1:30001
redis-cli --cluster add-node 127.0.0.1:30008 127.0.0.1:30001 --cluster-slave --cluster-master-id c0105d4a4b8bdd23afe91be8712f974515f1678d
redis-cli --cluster check 127.0.0.1:30001
redis-cli --cluster reshard  127.0.0.1:30002 --cluster-from 127.0.0.1:30001 --cluster-to 127.0.0.1:30007 --cluster-slots 1000 --cluster-yes##需要用交互式
redis-cli --cluster reshard 127.0.0.1:30001##交互式
redis-cli --cluster check 127.0.0.1:30001

2.redis持久化

官网:http://www.redis.cn/topics/cluster-tutorial.html

redis持久化原理:读的请求都给redis(速度快),若redis没有就村到mysql,再备份到redis中,下次直接读redis;update给mysql,canal伪装成数据库的slave端去读mysql的数据(mysql的内部的主从复制),canal再把数据给redis。

lamp(3)redis服务_第1张图片
server2做;server3做redis;server4做mysql数据库;

[root@server2 ~]# ls##下载rhel7目录下的rpm包
rhel7
[root@server2 ~]# cd rhel7/
[root@server2 rhel7]# ls
gearmand-1.1.12-18.el7.x86_64.rpm          php-mysql-5.4.16-46.el7.x86_64.rpm
libevent-devel-2.0.21-4.el7.x86_64.rpm     php-pdo-5.4.16-46.el7.x86_64.rpm
libgearman-1.1.12-18.el7.x86_64.rpm        php-pecl-gearman-1.1.2-1.el7.x86_64.rpm
libgearman-devel-1.1.12-18.el7.x86_64.rpm  php-pecl-igbinary-1.2.1-1.el7.x86_64.rpm
libzip-0.10.1-8.el7.x86_64.rpm             php-pecl-redis-2.2.8-1.el7.x86_64.rpm
openssl-1.0.2k-16.el7.x86_64.rpm           php-process-5.4.16-46.el7.x86_64.rpm
openssl-libs-1.0.2k-16.el7.x86_64.rpm      php-xml-5.4.16-46.el7.x86_64.rpm
php-cli-5.4.16-46.el7.x86_64.rpm           test.php
php-common-5.4.16-46.el7.x86_64.rpm        test.sql
php-fpm-5.4.16-46.el7.x86_64.rpm           worker.php
[root@server2 rhel7]# yum install php-* libgearman-* libevent-* -y
[root@server2 rhel7]# systemctl start php-fpm
[root@server2 rhel7]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      6883/php-fpm: maste 


[root@server1 ~]# yum install rsync -y
[root@server2 rhel7]# yum install rsync -y
[root@server1 redis]# cd /usr/local/
[root@server1 local]# ls
bin  games    lib    libexec  nginx      php   share
etc  include  lib64  mysql    openresty  sbin  src
[root@server1 local]# rsync -a nginx server2:/usr/local/##保留软连接copy
[root@server2 ~]# vim .bash_profile
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin/:/usr/local/nginx/sbin
[root@server2 ~]# source .bash_profile
[root@server2 local]# cd nginx/conf
[root@server2 conf]# cp nginx.conf.default nginx.conf
[root@server2 conf]# vim nginx.conf
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
          #  fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;
        }
[root@server2 conf]# nginx -t
[root@server2 conf]# nginx

http://172.25.3.2/index.php

lamp(3)redis服务_第2张图片

[root@server2 conf]# php -m | grep mysql
mysql
mysqli
pdo_mysql
[root@server2 conf]# php -m | grep redis
redis

[root@server4 ~]# systemctl start  mariadb
[root@server4 ~]# yum install -y mariadb-server

[root@server3 init.d]# systemctl start redis-server
[root@server3 init.d]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      6261/redis-server 0 
[root@server2 rhel7]# cp test.php /usr/local/nginx/html/
[root@server2 rhel7]# cd /usr/local/nginx/html/
[root@server2 html]# ls
50x.html  index.html  memcache.php  report.html  test.php
download  index.php   phpadmin      test.html
[root@server2 html]# vim test.php 
connect('172.25.3.3',6379) or die ("could net connect redis server");
  #      $query = "select * from test limit 9";
        $query = "select * from test";
        for ($key = 1; $key < 10; $key++)
        {
                if (!$redis->get($key))
                {
                        $connect = mysql_connect('172.25.3.4','redis','westos');
                        mysql_select_db(test);
                        $result = mysql_query($query);
                        //如果没有找到$key,就将该查询sql的结果缓存到redis
                        while ($row = mysql_fetch_assoc($result))
                        {
                                $redis->set($row['id'],$row['name']);
                        }
                        $myserver = 'mysql';
                        break;
                }
                else
                {
                        $myserver = "redis";
                        $data[$key] = $redis->get($key);
                }
        }
 
        echo $myserver;
        echo "
"; for ($key = 1; $key < 10; $key++) { echo "number is $key"; echo "
"; echo "name is $data[$key]"; echo "
"; } ?> [root@server4 ~]# mysql MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec) MariaDB [(none)]> grant all on test.* to redis@'%' identified by 'westos'; [root@server2 rhel7]# scp test.sql server4: [root@server4 ~]# ls MHA-7 mysql-router-community-8.0.21-1.el7.x86_64.rpm test.sql [root@server4 ~]# vim test.sql [root@server4 ~]# cat test.sql use test; CREATE TABLE `test` (`id` int(7) NOT NULL AUTO_INCREMENT, `name` char(8) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `test` VALUES (1,'test1'),(2,'test2'),(3,'test3'),(4,'test4'),(5,'test5'),(6,'test6'),(7,'test7'),(8,'test8'),(9,'test9'); #DELIMITER $$ #CREATE TRIGGER datatoredis AFTER UPDATE ON test FOR EACH ROW BEGIN # SET @RECV=gman_do_background('syncToRedis', json_object(NEW.id as `id`, NEW.name as `name`)); # END$$ #DELIMITER ; [root@server4 ~]# mysql < test.sql [root@server4 ~]# mysql MariaDB [(none)]> use test MariaDB [test]> show tables;

lamp(3)redis服务_第3张图片

lamp(3)redis服务_第4张图片

网页访问http://172.25.3.2/test.php

lamp(3)redis服务_第5张图片

[root@server3 init.d]# redis-cli
127.0.0.1:6379> get 1
"test1"
127.0.0.1:6379> get 2
"test2"
server4的mysql更新数据
MariaDB [test]> update test set name='westos' where id=1;

刷新网页访问http://172.25.3.2/test.php,id1的test不变,server3上redis-cli操作del 1,把key删除再刷新才同步会更新

127.0.0.1:6379> del 1
(integer) 1
127.0.0.1:6379> get 1
'westos'

更新的mysql,mysql还没有同步到redis,只有把key删除才同步,下面做mysql和redis同步机制

3.mysql和redis同步机制

1)安装 lib_mysqludf_json

lib_mysqludf_json UDF 库函数将关系数据映射为 JSON 格式。通常,数据库中的数据映
射为 JSON 格式,是通过程序来转换的。
https://github.com/mysqludf/lib_mysqludf_json

[root@server4 ~]# ls
lib_mysqludf_json-master.zip  mysql-router-community-8.0.21-1.el7.x86_64.rpm
MHA-7                         test.sql
[root@server4 ~]# yum install unzip -y
[root@server4 ~]# unzip lib_mysqludf_json-master.zip
[root@server4 lib_mysqludf_json-master]# yum install gcc mariadb-devel -y
[root@server4 lib_mysqludf_json-master]# gcc $(mysql_config --cflags) -shared -fPIC -o lib_mysqludf_json.so lib_mysqludf_json.c
[root@server4 lib_mysqludf_json-master]# ll lib_mysqludf_json.so
-rwxr-xr-x 1 root root 17528 Apr 17 15:05 lib_mysqludf_json.so
[root@server4 lib_mysqludf_json-master]# cp lib_mysqludf_json.so /usr/lib64/mysql/plugin/
[root@server4 lib_mysqludf_json-master]# mysql
MariaDB [(none)]> show global variables like 'plugin_dir';##查看 mysql 的模块目录: /usr/lib64/mysql/plugin/

lamp(3)redis服务_第6张图片

##注册 UDF 函数

MariaDB [(none)]> CREATE FUNCTION json_object RETURNS STRING SONAME 'lib_mysqludf_json.so';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> select * from mysql.func;查看函数
+-------------+-----+----------------------+----------+
| name        | ret | dl                   | type     |
+-------------+-----+----------------------+----------+
| json_object |   0 | lib_mysqludf_json.so | function |
+-------------+-----+----------------------+----------+

2)安装 gearman-mysql-udf

这个插件是用来管理调用 Gearman 的分布式的队列。
https://launchpad.net/gearman-mysql-udf

lftp 172.25.254.250:/pub/docs/redis/rhel7> mget libgearman-*  gearman-mysql-udf-0.6.tar.gz libevent-devel-2.0.21-4.el7.x86_64.rpm 

[root@server4 ~]# ls
gearman-mysql-udf-0.6                    lib_mysqludf_json-master
gearman-mysql-udf-0.6.tar.gz             lib_mysqludf_json-master.zip
libevent-devel-1.4.13-4.el6.x86_64.rpm   MHA-7
libgearman-1.1.8-2.el6.x86_64.rpm        mysql-router-community-8.0.21-1.el7.x86_64.rpm
libgearman-devel-1.1.8-2.el6.x86_64.rpm  test.sql

[root@server4 ~]# tar zxf gearman-mysql-udf-0.6.tar.gz 
[root@server4 ~]# yum install -y libgearman-* libevent-devel-2.0.21-4.el7.x86_64.rpm 
[root@server4 gearman-mysql-udf-0.6]# ./configure --libdir=/usr/lib64/mysql/plugin/
[root@server4 gearman-mysql-udf-0.6]# make
[root@server4 gearman-mysql-udf-0.6]# make install

###注册 UDF 函数
[root@server4 ~]# mysql
MariaDB [(none)]> CREATE FUNCTION gman_do_background RETURNS STRING SONAME 'libgearman_mysql_udf.so';
MariaDB [(none)]> CREATE FUNCTION gman_servers_set RETURNS STRING SONAME 'libgearman_mysql_udf.so';

MariaDB [(none)]> select * from mysql.func;##查看函数

lamp(3)redis服务_第7张图片

###指定 gearman 的服务信息
MariaDB [(none)]> SELECT gman_servers_set('172.25.3.2:4730');
+-------------------------------------+
| gman_servers_set('172.25.3.2:4730') |
+-------------------------------------+
| 172.25.3.2:4730                     |
+-------------------------------------+

[root@server2 rhel7]# yum install gearmand-1.1.12-18.el7.x86_64.rpm -y
[root@server2 rhel7]# systemctl start gearmand.service 
[root@server2 rhel7]# netstat -antlp|grep :4730
tcp        0      0 0.0.0.0:4730            0.0.0.0:*               LISTEN      11980/gearmand      
tcp6       0      0 :::4730                 :::*                    LISTEN      11980/gearmand 

##work是实际的数据同步工具,接收 gearman的数据,还连接redis,同步redis
[root@server2 rhel7]# vim worker.php 
[root@server2 rhel7]# cat worker.php 
addServer();
$worker->addFunction('syncToRedis', 'syncToRedis');
 
$redis = new Redis();
$redis->connect('172.25.3.3', 6379);##改ip
 
while($worker->work());
function syncToRedis($job)
{
        global $redis;
        $workString = $job->workload();
        $work = json_decode($workString);
        if(!isset($work->id)){
                return false;
        }
        $redis->set($work->id, $work->name);
}
?>

[root@server2 rhel7]# nohup php worker.php &##通过php运行到后端

[root@server4 ~]# vim test.sql 
[root@server4 ~]# cat test.sql 
use test;
#CREATE TABLE `test` (`id` int(7) NOT NULL AUTO_INCREMENT, `name` char(8) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
#INSERT INTO `test` VALUES (1,'test1'),(2,'test2'),(3,'test3'),(4,'test4'),(5,'test5'),(6,'test6'),(7,'test7'),(8,'test8'),(9,'test9');

DELIMITER $$
CREATE TRIGGER datatoredis AFTER UPDATE ON test FOR EACH ROW BEGIN
    SET @RECV=gman_do_background('syncToRedis', json_object(NEW.id as `id`, NEW.name as `name`)); 
  END$$
DELIMITER ;

[root@server4 ~]# mysql < test.sql 
[root@server4 ~]# mysql
MariaDB [(none)]> use test
MariaDB [test]> SHOW TRIGGERS FROM test;

lamp(3)redis服务_第8张图片

MariaDB [test]> update test set name='hello' where id=1;
###此时修改后,去刷新网页访问http://172.25.3.2/test.php,id1的会同步更新
[root@server4 ~]# systemctl stop mariadb.service 

lamp(3)redis服务_第9张图片

分布式的分发框架:客户端、服务端、消费端
client端::mysql数据库的更新,触发trigger触发器。触发器调用udf func,然后发往gearmand:4730,由server端周转
server端::gearmand:4730
worker::从php-gearmand读取到数据,再通过php-redis连接redis,然后同步redis的数据

lamp(3)redis服务_第10张图片

3.varnish

server4做varnish的cdn,server2做nginx

[root@server4 ~]# lftp 172.25.254.250
lftp 172.25.254.250:/pub/docs/varnish> get varnish-4.0.5-1.el7.x86_64.rpm varnish-libs-4.0.5-1.el7.x86_64.rpm jemalloc-devel-3.6.0-1.el7.x86_64.rpm jemalloc-3.6.0-1.el7.x86_64.rpm
[root@server4 ~]# yum install jemalloc-* varnish-* -y
[root@server4 ~]# systemctl status varnish
● varnish.service - Varnish Cache, a high-performance HTTP accelerator
   Loaded: loaded (/usr/lib/systemd/system/varnish.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
[root@server4 ~]# vim /usr/lib/systemd/system/varnish.service#看环境文件在etc/varnish/varnish.params
[root@server4 ~]# cd /etc/varnish/
[root@server4 varnish]# ll
total 12
-rw-r--r-- 1 root root 1220 Aug  3  2017 default.vcl
-rw------- 1 root root   37 Apr 17 15:48 secret
-rw-r--r-- 1 root root 1093 Aug  3  2017 varnish.params

[root@server4 varnish]# vim varnish.params 
VARNISH_LISTEN_PORT=80##修改成80端口

[root@server4 varnish]# vim default.vcl 
##改动
backend default {
    .host = "172.25.3.2";
    .port = "80";
}

[root@server4 varnish]# systemctl start varnish
[root@server4 varnish]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      8157/varnishd       

[root@zhenji ~]# curl 172.25.3.4 -I
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Sat, 17 Apr 2021 08:06:28 GMT
Content-Type: text/html
Content-Length: 625
Last-Modified: Sun, 04 Apr 2021 06:01:35 GMT
ETag: "6069563f-271"
X-Varnish: 2
Age: 0
Via: 1.1 varnish-v4
Connection: keep-alive

[root@zhenji ~]# curl 172.25.3.4 -I
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Sat, 17 Apr 2021 08:06:28 GMT
Content-Type: text/html
Content-Length: 625
Last-Modified: Sun, 04 Apr 2021 06:01:35 GMT
ETag: "6069563f-271"
X-Varnish: 32770 3
Age: 17
Via: 1.1 varnish-v4
Connection: keep-alive

lamp(3)redis服务_第11张图片

[root@server4 varnish]# vim default.vcl 
##最后添加为响应添加X-Cache首部,显示缓存是否命中:

        if (obj.hits > 0) {
        set resp.http.X-Cache = "HIT from westos cache";
        }
        else {
        set resp.http.X-Cache = "MISS from westos cache";
        }
        return (deliver);
[root@server4 varnish]# systemctl reload varnish

lamp(3)redis服务_第12张图片

[root@zhenji ~]# curl 172.25.3.4 -I
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Sat, 17 Apr 2021 08:11:10 GMT
Content-Type: text/html
Content-Length: 625
Last-Modified: Sun, 04 Apr 2021 06:01:35 GMT
ETag: "6069563f-271"
X-Varnish: 5
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS from westos cache###MISS
Connection: keep-alive

[root@zhenji ~]# curl 172.25.3.4 -I
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Sat, 17 Apr 2021 08:11:10 GMT
Content-Type: text/html
Content-Length: 625
Last-Modified: Sun, 04 Apr 2021 06:01:35 GMT
ETag: "6069563f-271"
X-Varnish: 32772 6
Age: 3
Via: 1.1 varnish-v4
X-Cache: HIT from westos cache###显示缓存是否命中
Connection: keep-alive

lamp(3)redis服务_第13张图片

1.根据不同的域名,分发到不同的后端主机

[root@server3 local]# yum install rsync -y
[root@server1 local]# rsync -a nginx server3:/usr/local/
[root@server3 ~]# vim .bash_profile
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin/:/usr/local/nginx/sbin
[root@server3 ~]# source .bash_profile


[root@server2 ~]# cd /usr/local/nginx/conf/
[root@server2 conf]# vim nginx.conf
##最后大括号前添加
server {
        listen 80;
        server_name     www.westos.org;
        location / {
                root /www;
                index index.html;
        }
}
[root@server2 conf]# mkdir /www
[root@server2 conf]# echo www.westos.org > /www/index.html
[root@server2 conf]# nginx -s reload
[root@server3 ~]# nginx
[root@server3 ~]# vim /usr/local/nginx/conf/nginx.conf
##最后大括号前添加
server {
        listen 80;
        server_name     bbs.westos.org;
        location / {
                root /bbs;
                index index.html;
        }
}
[root@server3 ~]# mkdir /bbs
[root@server3 ~]# echo bbs.westos.org > /bbs/index.html
[root@server3 ~]# nginx -s reload

[root@server4 varnish]# vim default.vcl 
##添加
backend default {
    .host = "172.25.3.2";
    .port = "80";
}

backend web2 {
    .host = "172.25.3.3";
    .port = "80";
}


sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
                set req.http.host = "www.westos.org";
                set req.backend_hint = default;
                #return(pass);
        }
        elseif (req.http.host ~ "^bbs.westos.org") {
                set req.backend_hint = web2;
                #return(pass);
        }
        else {
                return(synth(404,"Not in cache"));
        }

[root@server4 varnish]# systemctl reload varnish

lamp(3)redis服务_第14张图片

[root@zhenji ~]# vim /etc/hosts
172.25.3.4 server1  mfsmaster controller www.westos.org www.linux.org bbs.linux.org bbs.westos.org
[root@zhenji ~]# curl www.westos.org
www.westos.org
[root@zhenji ~]# curl bbs.westos.org
bbs.westos.org

lamp(3)redis服务_第15张图片

2.负载均衡

[root@server3 ~]# vim /usr/local/nginx/conf/nginx.conf
##最后大括号前添加
server {
        listen 80;
        server_name     www.westos.org;
        location / {
                root /www;
                index index.html;
        }
}

[root@server4 varnish]# vim default.vcl 
##添加
import directors;

sub vcl_init {
        new web_cluster = directors.round_robin();
        web_cluster.add_backend(default);
        web_cluster.add_backend(web2);
}

sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
                set req.http.host = "www.westos.org";
                set req.backend_hint = web_cluster.backend();
		        return(pass);


[root@server4 varnish]# systemctl reload varnish
[root@server3 ~]# vim /www/index.html
[root@server3 ~]# cat /www/index.html
www.westos.org --server3

##负载均衡
[root@zhenji ~]# curl www.westos.org
www.westos.org
[root@zhenji ~]# curl www.westos.org
www.westos.org --server3

3.健康检查

[root@server4 varnish]# vim default.vcl 

probe backend_healthcheck {
    .url = "/index.html";
    .window = 3;
    .threshold = 2;
    .interval = 3s;
}
backend default {
    .host = "172.25.3.2";
    .port = "80";
    .probe = backend_healthcheck;
}

backend web2 {
    .host = "172.25.3.3";
    .port = "80";
    .probe = backend_healthcheck;
}
[root@server4 varnish]# systemctl reload varnish
[root@server3 ~]# nginx -s stop

lamp(3)redis服务_第16张图片

[root@zhenji ~]# curl www.westos.org
www.westos.org
[root@zhenji ~]# curl www.westos.org
www.westos.org
[root@server3 ~]# nginx
[root@zhenji ~]# curl www.westos.org
www.westos.org
[root@zhenji ~]# curl www.westos.org
www.westos.org --server3

lamp(3)redis服务_第17张图片

4.nginx代理配置

[root@server2 conf]# vim nginx.conf
##第一个本地
http {
        upstream westos {
        server 172.25.3.3:80;
        }
#最后更改

server {
        listen 80;
        server_name     www.westos.org;
        location / {
                #root /www;
                #index index.html;
                proxy_pass http://westos;
        }
}

[root@server2 conf]# nginx -s reload

[root@server4 ~]# cd /etc/varnish/
[root@server4 varnish]# ls
default.vcl  secret  varnish.params
[root@server4 varnish]# vim default.vcl
#probe backend_healthcheck {
#    .url = "/index.html";
#    .window = 3;
#    .threshold = 2;
#    .interval = 60s;
#}
backend default {
    .host = "172.25.3.2";
    .port = "80";
#    .probe = backend_healthcheck;
}

#backend web2 {
#    .host = "172.25.3.3";
#    .port = "80";
#    .probe = backend_healthcheck;
#}

#import directors;
#
#sub vcl_init {
#        new web_cluster = directors.round_robin();
#        web_cluster.add_backend(default);
#        web_cluster.add_backend(web2);
#}

sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
                set req.http.host = "www.westos.org";
                set req.backend_hint = default;
                return(pass);
        }
#        elseif (req.http.host ~ "^bbs.westos.org") {
#                set req.backend_hint = web2;
#                #return(pass);
#        }
[root@server4 varnish]# systemctl reload varnish

[root@server3 html]# vim index.html 
[root@server3 html]# cat index.html 
www.westos.org -server3
[root@server3 html]# pwd
/usr/local/nginx/html

[root@zhenji ~]# curl www.westos.org
www.westos.org -server3

5.为发往后端主机的请求添加X-Forward-For首部:

[root@server2 ~]# cd /usr/local/nginx/conf/
[root@server2 conf]# vim nginx.conf
server {
        listen 80;
        server_name     www.westos.org;
        location / {
                #root /www;
                #index index.html;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://westos;
        }
}
[root@server2 conf]# nginx -s reload

##server3上缺少real_ip_header,所以要重新编译,不要maker install
[root@server3 html]# nginx -s stop
[root@server3 ~]# ls
nginx-1.18.0.tar.gz
[root@server3 ~]# yum install gcc prce-devel openssl-devel -y
[root@server3 ~]# tar zxf nginx-1.18.0.tar.gz 
[root@server3 ~]# cd nginx-1.18.0/
[root@server3 nginx-1.18.0]# vim auto/cc/gcc 
# debug
#CFLAGS="$CFLAGS -g"
[root@server3 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module
[root@server3 nginx-1.18.0]# make
[root@server3 nginx-1.18.0]# cd objs/
[root@server3 objs]# ls
autoconf.err  nginx    ngx_auto_config.h   ngx_modules.c  src
Makefile      nginx.8  ngx_auto_headers.h  ngx_modules.o
[root@server3 objs]# cp nginx /usr/local/nginx/sbin/

[root@server3 ~]# cd /usr/local/nginx/conf/
[root@server3 conf]# vim nginx.conf
##第一个本地80下添加real_ip_header
    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        real_ip_header    X-Forwarded-For;
        real_ip_recursive on;
        set_real_ip_from 172.25.0.0/24;

[root@server3 conf]# nginx -t
[root@server3 conf]# nginx
##此时访问www.westos.org
[root@zhenji ~]# curl www.westos.org
www.westos.org -server3
[root@server3 nginx]# tail -f logs/access.log ###是真实的ip172.25.3.2
172.25.3.2 - - [18/Apr/2021:09:03:09 +0800] "GET / HTTP/1.0" 200 24 "-" "curl/7.61.1"

lamp(3)redis服务_第18张图片

lamp(3)redis服务_第19张图片

你可能感兴趣的:(lamp(3)redis服务)