运维随笔

笔记


  • 首页

  • 关于

  • 标签

  • 分类

  • 归档

  • 搜索

openvpn+squid搭建通过http代理访问openvpn服务器

发表于 2018-08-30 | 更新于: 2022-06-05 | 分类于 Linux
字数统计: 1k | 阅读时长 ≈ 6

1 环境:

OS : Centos 7.2 x86_64

2 Squid部署

1
[root@bogon ~]# yum install squid

2.1 编辑配置文件

1
[root@bogon ~]# vim /etc/squid/squid.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#
# Recommended minimum configuration:
#

# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed
acl all src all                      #添加允许所有IP地址访问
acl localnet src 10.0.0.0/8 # RFC1918 possible internal network
acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl localnet src fc00::/7       # RFC 4193 local private network range
acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machines

acl SSL_ports port 443
acl Safe_ports port 80 # http
acl Safe_ports port 21 # ftp
acl Safe_ports port 443 # https
acl Safe_ports port 70 # gopher
acl Safe_ports port 210 # wais
acl Safe_ports port 1025-65535 # unregistered ports
acl Safe_ports port 280 # http-mgmt
acl Safe_ports port 488 # gss-http
acl Safe_ports port 591 # filemaker
acl Safe_ports port 777 # multiling http
acl CONNECT method CONNECT

#
# Recommended minimum Access Permission configuration:
#
# Deny requests to certain unsafe ports
http_access allow !Safe_ports         #
# Deny CONNECT to other than secure SSL ports
http_access allow CONNECT !SSL_ports       #设置允许所有SSL通过(不设置的话无法连接到http服务器)
# Only allow cachemgr access from localhost
http_access allow localhost manager
http_access allow manager

access_log /var/log/squid/access.log combined
cache_log /var/log/squid/cache.log
# We strongly recommend the following be uncommented to protect innocent
# web applications running on the proxy server who think the only
# one who can access services on "localhost" is a local user
#http_access deny to_localhost

#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#

# Example rule allowing access from your local networks.
# Adapt localnet in the ACL section to list your (internal) IP networks
# from where browsing should be allowed

# And finally deny all other access to this proxy
http_access allow all                #修改允许所有人使用该代理.因为这里是代理加速web服务器
http_reply_access allow all

# Squid normally listens to port 3128
#http_port 3128
http_port 4346                    #修改默认监听端口,即代理端口。

# Uncomment and adjust the following to add a disk cache directory.
#cache_dir ufs /var/spool/squid 100 16 256

# Leave coredumps in the first cache dir
coredump_dir /var/spool/squid

#
# Add any of your own refresh_pattern entries above these.
#
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern -i (/cgi-bin/|\?) 0 0% 0
refresh_pattern . 0 20% 4320

2.2 启动squid及设置开机启动

1
2
3
[root@bogon ~]# systemctl start squid.service

[root@bogon ~]# systemctl enable squid.service

2.3 openvpn客户端配置proxy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
##############################################
# Sample client-side OpenVPN 2.0 config file #
# for connecting to multi-client server.     #
#                                            #
# This configuration can be used by multiple #
# clients, however each client should have   #
# its own cert and key files.                #
#                                            #
# On Windows, you might want to rename this  #
# file so it has a .ovpn extension           #
##############################################

# Specify that we are a client and that we
# will be pulling certain config file directives
# from the server.
client

# Use the same setting as you are using on
# the server.
# On most systems, the VPN will not function
# unless you partially or fully disable
# the firewall for the TUN/TAP interface.
;dev tap
dev tun

# Windows needs the TAP-Win32 adapter name
# from the Network Connections panel
# if you have more than one.  On XP SP2,
# you may need to disable the firewall
# for the TAP adapter.
;dev-node MyTap

# Are we connecting to a TCP or
# UDP server?  Use the same setting as
# on the server.
proto tcp
;proto udp

# The hostname/IP and port of the server.
# You can have multiple remote entries
# to load balance between the servers.
;remote 172.20.20.25 1194
remote openvpn服务器地址或域名 1194
;remote my-server-2 1194

;http-proxy vpnproxy.dealeasy.com 4346
http-proxy squid服务器地址 4346            #添加http代理


# Choose a random host from the remote
# list for load-balancing.  Otherwise
# try hosts in the order specified.
;remote-random

# Keep trying indefinitely to resolve the
# host name of the OpenVPN server.  Very useful
# on machines which are not permanently connected
# to the internet such as laptops.
resolv-retry infinite

# Most clients don't need to bind to
# a specific local port number.
nobind

# Downgrade privileges after initialization (non-Windows only)
;user nobody
;group nobody

# Try to preserve some state across restarts.
persist-key
persist-tun

# If you are connecting through an
# HTTP proxy to reach the actual OpenVPN
# server, put the proxy server/IP and
# port number here.  See the man page
# if your proxy server requires
# authentication.
;http-proxy-retry # retry on connection failures
;http-proxy [proxy server] [proxy port #]

# Wireless networks often produce a lot
# of duplicate packets.  Set this flag
# to silence duplicate packet warnings.
;mute-replay-warnings

# SSL/TLS parms.
# See the server config file for more
# description.  It's best to use
# a separate .crt/.key file pair
# for each client.  A single ca
# file can be used for all clients.
ca ca.crt
#cert client.crt
#key client.key

auth-user-pass

# Verify server certificate by checking that the
# certicate has the correct key usage set.
# This is an important precaution to protect against
# a potential attack discussed here:
#  http://openvpn.net/howto.html#mitm
#
# To use this feature, you will need to generate
# your server certificates with the keyUsage set to
#   digitalSignature, keyEncipherment
# and the extendedKeyUsage to
#   serverAuth
# EasyRSA can do this for you.
remote-cert-tls server

# If a tls-auth key is used on the server
# then every client must also have the key.
;tls-auth ta.key 1

# Select a cryptographic cipher.
# If the cipher option is used on the server
# then you must also specify it here.
;cipher x

# Enable compression on the VPN link.
# Don't enable this unless it is also
# enabled in the server config file.
comp-lzo

# Set log file verbosity.
verb 3

# Silence repeating messages
;mute 20

现在可以通过http代理连接VPN服务器。

windows重置网卡解决无法上网问题

发表于 2018-08-30 | 更新于: 2022-03-31 | 分类于 Windows
字数统计: 118 | 阅读时长 ≈ 1

1 故障表现:

阅读全文 »

centos7安装redis

发表于 2018-08-30 | 更新于: 2022-03-31 | 分类于 Linux
字数统计: 213 | 阅读时长 ≈ 1

1 下载redis

下载redis-3.2.9.tar.gz

1
2
3
4
5
wget http://download.redis.io/releases/redis-3.2.9.tar.gz

tar -zxvf redis-3.2.9.tar.gz

mv redis-3.2.9 redis

2 安装redis

1
2
3
[root@bogon home]# cd redis/

[root@bogon redis]# make && make install

图1

2.1 初始化redis

1
2
3
[root@bogon redis]# cd utils/

[root@bogon utils]# ./install_server.sh

图2

通过上图,我们可以看出redis初始化后redis配置文件为/etc/redis/6379.conf,日志文件为/var/log/redis_6379.log,数据文件dump.rdb存放到/var/lib/redis/6379目录下,启动脚本为/etc/init.d/redis_6379。

现在我们要使用systemd,所以在 /etc/systems/system 下创建一个单位文件名字为redis_6379.service。

1
vim /etc/systemd/system/redis_6379.service

填写以下内容:

1
2
3
4
5
6
7
8
[Unit]
Description=Redis on port 6379
[Service]
Type=forking
ExecStart=/etc/init.d/redis_6379 start
ExecStop=/etc/init.d/redis_6379 stop
[Install]
WantedBy=multi-user.target

2.2 查看redis版本

1
redis-cli --version

图3

3 参考资料:

http://www.cnblogs.com/sandea/p/5782192.html

Redis+Keepalived双机热备

发表于 2018-08-30 | 更新于: 2022-06-05 | 分类于 Linux
字数统计: 1.9k | 阅读时长 ≈ 8

1 环境

主机 IP OS

主机 IP OS
master-server 172.20.22.145 centos7.2
slave-server 172.20.22.53 centos7.2

2 参考资料

https://my.oschina.net/guol/blog/182491

http://blog.csdn.net/qguanri/article/details/51120178

http://blog.csdn.net/zgf19930504/article/details/52024724

3 整体思路

在keepalived+redis的使用过程中有四种情况:

  1. 一种是keepalived挂了,同时redis也挂了,这样的话直接VIP飘走之后,是不需要进行redis数据同步的,因为redis挂了,你也无法去master上同步,不过会损失已经写在master上却还没同步到slave上面的这部分数据。

  2. 另一种是keepalived挂了,redis没挂,这时候VIP飘走后,redis的master/slave还是老的对应关系,如果不变化的话会把数据写入redis slave中,从而不会同步到master上去,这就要借助监控脚本反转redis的master/slave关系。这时候就要预留一点时间进行数据同步,然后反转master/slave。

  3. 还有一种是keepalived没挂,redis挂了,这时候根据监控脚本会检测到redis挂了,并且降低keepalived master的优先级,同样会导致VIP飘走,情况和第二种一样,也是需要进行数据同步,然后反转当前redis的master/slave关系的。

  4. 随后一种是keepalived没挂,redis也没挂,大吉大利啊,什么都不用操作。

本文的实验环境四种情况都适合,第一种是不需要同步数据的,脚本会默认去同步数据,但是其实是不会成功的。脚本主要是用来处理第二和第三种情况的。

4 安装redis

参考本站:centos7安装redis

4.1 修改主从redis配置文件

1
vim /etc/redis/6379.conf

4.1.1 主redis:

1
2
#修改bind绑定IP,只有绑定的IP才能访问redis,0.0.0.0标识所有地址都能访问。
bind 0.0.0.0

4.1.2 从redis:

1
2
3
4
#修改bind绑定IP,只有绑定的IP才能访问redis,0.0.0.0标识所有地址都能访问。
bind 0.0.0.0
#添加slaveof IP Port,表示作为该IP服务器的备机
slaveof 172.20.22.145 6379

4.2 启动并测试

1
service redis_6379 start

4.2.1 主redis

1
2
3
4
5
6
7
8
9
10
11
[root@bogon ~]# redis-cli -p 6379                       #登录redis

127.0.0.1:6379> set hao 333                                  #添加hao值为333 

OK

127.0.0.1:6379> get hao                                        #查看hao的值

"333"

127.0.0.1:6379>

4.2.2 从redis

1
2
3
4
5
6
7
[root@bogon scripts]# redis-cli -p 6379

127.0.0.1:6379> get hao                               #查看hao的值,获取到值说明同步成功。

"333"

127.0.0.1:6379>

5 安装keepalived

1
yum install keepalived

5.1 修改配置文件

1
vim /etc/keepalived/keepalived.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#MASTER配置文件:

! Configuration File for keepalived
vrrp_script chk_redis {
        script "/etc/keepalived/scripts/chk_redis.sh"     #监控脚本
        interval 2                                        #监控间隔时间 秒
        timout 2                                          #响应超时:超过多长时间未响应认为是失败
        fall 3                                            #检测失败几次,认为是redis服务器挂了
        weight -60                       #自我确定服务器挂了之后,优先级加多少, 也就是宕机之后 priority 加多少weight,这里是减60
}
vrrp_instance VI_1 {
    state MASTER                         #MASTER表示主,BACKUP表示为从
    interface eth0                       #eth0表示监听的网卡
    virtual_router_id 51
    priority 100                         #优先级,从服务器推选主服务器就是根据这个来比较的,从服务器必须小于主服务器优先级。
    advert_int 1
    authentication {
        auth_type PASS                   #认证凭证,可自定义
        auth_pass 1111
    }
    track_script {
          chk_redis                      #运行chk_redis模块
    }
    virtual_ipaddress {
        172.20.22.199                                      #VIP地址(虚拟IP)
    }
    unicast_src_ip 172.20.22.145               #keepalived 内部通信,本机ip 地址(没加也没影响)
    unicast_peer {
        172.20.22.53                      
    }
    notify_master /etc/keepalived/scripts/redis-master.sh           #keepalived 状态切换master时执行的脚本
    notify_backup /etc/keepalived/scripts/redis-backup.sh           #keepalived 状态切换为backup时执行的脚本
    notify_fault /etc/keepalived/scripts/redis-fault.sh             #keepalived 状态为fault时执行的脚本
    notify_stop /etc/keepalived/scripts/redis-stop.sh               #keepalived 服务停止时执行脚本
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# BACKUP配置文件

backup配置文件至修改了2处,其它都一样。

! Configuration File for keepalived
vrrp_script chk_redis {
        script "/etc/keepalived/scripts/chk_redis.sh"   ###监控脚本
        interval 2                                        ###监控时间
        timout 2          #响应超市:超过多长时间未响应认为是失败
        fall 3            #检测失败几次,认为是redis服务器挂了
        weight -60           #自我确定服务器挂了之后,优先级加多少, 也就是宕机之后 priority + weight
}
vrrp_instance VI_1 {
    state BACKUP
    interface enp2s0

virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
          chk_redis
    }
    virtual_ipaddress {
        172.20.22.199
    }
    unicast_src_ip 172.20.22.53
    unicast_peer {
        172.20.22.145
    }
    notify_master /etc/keepalived/scripts/redis-master.sh
    notify_backup /etc/keepalived/scripts/redis-backup.sh
    notify_fault /etc/keepalived/scripts/redis-fault.sh
    notify_stop /etc/keepalived/scripts/redis-stop.sh
}

5.2 创建脚本

创建scripts目录 /etc/keepalived,在scripts目录内创建redis-master.sh,redis-backup.sh,redis-fault.sh,redis-stop.sh,chk_redis.sh

5.2.1 chk_redis.sh脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash  
#检测redis是否正常运行,根据检测结果返回不同的值
     
#日志文件位置  
logFile=/usr/etc/redis/keepalived/logs/redis-keepalived.log  
  
#ping 本机redis服务  
pingRS=`/usr/local/bin/redis-cli -a 123456 PING`  
  
#如果ping 的结果为PONG,那么返回0 ,否则返回1;PONG代表PING通redis,当返回值为1时,执行降低优先级操作。
if [ $pingRS == "PONG" ]; then  
   echo "[`date`] ping is ok !" >>$logFile  
   exit 0  
else  
   echo "[`date`] ping is error !" >>$logFile  
   exit 1  
fi

5.2.2 redis-master.sh

(master主机脚本与slave主机基本雷同,只需修改远程ip地址)

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
rediscli="redis-cli"
logfile="/var/log/keepalived-redis-state.log"
date >> %logfile
echo "[master]" >> $logfile
echo " 运行作为远程server备机并同步数据命令" >> $logfile
$rediscli SLAVEOF 172.20.22.53 6379 >> $logfile                  #远程redis IP地址,解释为以该IP为主,做数据同步。
sleep 10      #等待10秒,再运行下面命令。
echo "运行升级为主服务器命令" >> $logfile
$rediscli SLAVEOF NO ONE >> $logfile
echo "切换master完成" >> $logfile

5.2.3 redis-backup.sh

(master主机脚本与slave主机基本雷同,只需修改远程ip地址)

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
rediscli="redis-cli"
logfile="/var/log/keepalived-redis-state.log"
date >> $logfile
echo "[backup]" >> $logfile
echo "等待13秒同步数据后运行下面命令" >> $logfile
sleep 13
echo "以远方IP为主机,同步数据" >> $logfile
$rediscli slaveof 172.20.22.53 6379 >> $logfile
echo "切换backup完成" >> $logfile

keepalived进入backup/stop/fault时的检测脚本,由于内容都一致,所以只写出redis_backup.sh

主从主机脚本内容一致,只需修改IP地址为对方IP

1
$rediscli slaveof 172.20.22.53 6379      #修改为对方IP

6 测试

7 注意事项:

  1. VRRP脚本(vrrp_script)和VRRP实例(vrrp_instance)属于同一个级别

  2. notify_master 、notify_backup、notify_fault、notify_stop参数

notify_stop keepalived停止运行前运行notify_stop指定的脚本
notify_master keepalived切换到master时执行的脚本
notify_backup keepalived切换到backup时执行的脚本
notify_fault keepalived出现故障时执行的脚本

  1. 启动顺序,先启动redis,后启动keepalived

8 说明

此方法可以实现完全的自动化主从切换,但同步数据的时间(即脚本中的sleep时间)生产环境中无法完全掌控,实际使用中建议手动切回,或在主服务器上keepalived.conf中添加不抢占主机参数nopreempt(此参数要求配置文件中keepalived状态都为backup,根据优先级选择master)

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51i
    nopreempt                #redis或keepalived恢复后不抢占master,依然作为backup运行。
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
            chk_redis
    }
    virtual_ipaddress {
        172.20.22.199
    }
    notify_backup /etc/keepalived/scripts/redis_backup.sh
    notify_master /etc/keepalived/scripts/redis_master.sh
    notify_fault  /etc/keepalived/scripts/redis_fault.sh
}

centos7通过smtp发送邮件(解决阿里云ECS不能通过25端口发送邮件)

发表于 2018-08-30 | 更新于: 2022-06-05 | 分类于 Linux
字数统计: 262 | 阅读时长 ≈ 1

1 参考资料

https://bbs.aliyun.com/read/316576.html

http://blog.csdn.net/qq_25551295/article/details/51803942

2 安装mailx

1
yum install mailx

2.1 修改配置文件

1
vim /etc/mail.rc

追加如下内容

1
2
3
4
5
6
set smtp="smtps://smtp.mxhichina.com:465"
set smtp-auth=login
set smtp-auth-user="sales@vfutai.xxx"
set smtp-auth-password="Ni-De-Mi-Ma"
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb

2.2 发送测试

例子1:

1
echo message3 | mail -v -r "sales@vfutai.xxx" -s "This is the subject" dongshan3@foxmail.xxx

message3 正文

`dongshan3@foxmail.xxx` 收件人地址,发送多人加逗号添加邮件地址

-r “sales@vfutai.xxx” 发件人地址

-s "This is the subject" 邮件标题

-a /etc/*.txt 附件

列子2:

1
2
3
4
5
6
7
8
9
#!/usr/bin/bash
#发送邮件到kxhuanzi@163.com
sj=`date +%Y%m%d`
echo "备份mysql" >> /opt/mysqldata/$sj.log
mysqldump -uroot -ppassword bitnami_wordpress > /opt/mysqldata/$sj.sql >> /opt/mysqldata/$sj.log
echo "发送mysql备份电子邮件到k*****@163.com" >> /opt/mysqldate/$sj.log
echo MYSQL备份,备份文件名字:$sj.sql,备份日期:`date`.| mail -v -r "32****@qq.com" -a /opt/mysqldata/$sj.sql -s "wordpress备份/$sj" k****@163.com,xh****@gmail.com
echo "发送完成" >> /opt/mysqldata/$sj.log
~
1…222324…32
OperationMAN

OperationMAN

日常运维文档整理。

157 日志
9 分类
107 标签
E-Mail GitHub 码云 Linux命令大全 鸟哥的私房菜 RUNOOB.com 陈沙克日志 Bitnami Font Awesome
友情链接
  • Next优化
  • Next深度优化
© 2023 OperationMAN | 豫ICP备 17032296 号