Squid Cache(简称为Squid)是HTTP代理服务器软件。Squid用途广泛,可以作为缓存服务器,可以过滤流量帮助网络安全,也可以作为代理服务器链中的一环,向上级代理转发数据或直接连接互联网。Squid程序在Unix一类系统运行。由于它是开源软件,有网站修改Squid的源代码,编译为原生Windows版[2];用户也可在Windows里安装Cygwin,然后在Cygwin里编译Squid。
Squid历史悠久,功能完善。除了HTTP外,对FTP与HTTPS的支持也相当好,在3.0测试版中也支持了IPv6。但是Squid的上级代理不能使用SOCKS协议。

1、环境说明

本文中涉及两台服务器,这两台服务器均有内网ip地址,分别为

A: 192.168.0.200
B: 192.168.0.100
C: 192.168.0.101

其中A具有公网访问能力,BC不具备公网访问能力。

因此,可以让B具有公网访问能力或让BC实现某些功能例如yum安装软件能正常使用,可行的方案有两种:

  • 方案1

A服务器开启数据包内核转发功能,接收BC的数据包,BC将自己的网关配置成A的地址,这样BC的上网能力全部通过A来代理转发实现,这种做法简单好用,但是不适用于云厂商提供的虚拟主机例如阿里云ECS,因为云主机的ip和网卡配置一般是不支持自己定义的

  • 方案2

A服务器运行一个squid服务,BC的某些操作通过squid来代理转发实现

本文通过在A服务器上搭建squid代理,并在BC服务器上实现了wgetyumdocker使用A的代理,以及把ABC这三台服务器作为k8s集群的节点利用kubeadm搭建一个k8s集群需要配置的地方

2、搭建squid服务

2.1、安装服务

A服务器能上网,先把A服务器的yum源配置好,这里不做赘述,然后安装squid服务

# rpm -qa | grep squid
# yum install -y squid

默认安装的squid服务只需要配置一点,默认是拒绝所有服务器连接,只需要修改成允许所有服务器连接即可

# vim /etc/squid/squid.conf
http_access allow all

启动服务并设置成开机启动

# systemctl start squid.service
# systemctl enable squid.service

2.2、配置受信

这样配置之后,squid代理服务器就可以使用了,默认的端口是3128,但是为了安全,只让受信的服务器连接,通常还需要对squid配置账号验证授权使用,通过httpd-tools生成密码文件,安装

# yum install httpd-tools -y

生成密码文件

# mkdir /etc/squid3/
# 生成密码文件,指定文件路径,其中squid是用户名
# htpasswd -cd /etc/squid3/passwords squid
#提示输入密码,不能超过8个字符,输入密码123456

测试密码文件

# /usr/lib64/squid/basic_ncsa_auth /etc/squid3/passwords                
squid 123456
OK 
# 测试完成,crtl + c 打断

配置squid使用验证,修改配置文件

# vim /etc/squid/squid.conf
# And finally deny all other access to this proxy
auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid3/passwords #账户密码文件
auth_param basic realm proxy
auth_param basic children 50 #最多 50 个账户同时运行
auth_param basic realm CoolTube Proxy Server #密码框描述
auth_param basic credentialsttl 2 hours #认证持续时间
acl authenticated proxy_auth REQUIRED #对 authenticated 进行外部认证
http_access allow authenticated #允许 authenticated 中的成员访问
http_access deny all #拒绝所有其他访问
visible_hostname squid.CoolTube #代理机名字

# 这里是端口号,可以按需修改
# http_port 3128 这样写会同时监听 ipv6 和 ipv4 的端口,推荐适应下面的配置方法。
http_port 0.0.0.0:3128

初始化服务并重新启动

# squid -z
# systemctl restart squid.service

如果开启了防火墙,还要在防火墙中允许端口3128的策略,以CentOS7为例

# 把 3128 端口加入防火墙过滤掉
# firewall-cmd --permanent --zone=public --add-port=3128/tcp
# 重启防火墙
# firewall-cmd --reload

如果想新增用户,只需要按照上面的步骤再次生成一次密码文件就可以

3、配置代理

3.1、配置全局代理

修改环境变量文件并使其生效

vim /etc/profile
#在最后加入(有认证的情况)
export http_proxy="http://squid:123456@192.168.0.200:3128"
export https_proxy="http://squid:123456@192.168.0.200:3128"
如果没有开启认证
http_proxy=http://192.168.0.200:3128
https_proxy=http://192.168.0.200:3128
# source /etc/profile

3.2、配置wget代理

新增wget的环境变量文件

# vim ~/.wgetrc
http_proxy=http://192.168.0.200:3128
https_proxy=http://192.168.0.200:3128
use_proxy = on
wait = 30
# 验证,如果返回值为0表示wget可用
# wget --spider -T 5 -q -t 2 www.baidu.com | echo $?
0

3.3、配置yum代理

编辑yum的配置文件,新增下面的内容

# vim /etc/yum.conf
# 有认证的情况
# Proxy
proxy=http://squid:123456@192.168.0.200:3128
# 没有认证
# Proxy
proxy=http://192.168.0.200:3128

3.4、配置docker代理

docker配置代理是为了保证docker能从外网pull镜像,在安装了docker之后,在docker配置文件下新增配置并重启docker

# mkdir -p /etc/systemd/system/docker.service.d
# vim /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://squid:123456@192.168.0.200:3128" 
# systemctl daemon-reload
# systemctl restart docker

验证,通过docker pull命令pull一个公网的镜像

docker pull busybox:latest

3.5、问题排查

如果在配置上述代理后进行验证的时候,出现没有返回,卡顿或超时的情况,可以通过查看A服务器即squid代理服务器的日志进行检查,在日志中一般都可以查看到较为明显的错误
cache.log

tail -f /var/log/squid/cache.log 
2020/02/12 22:56:12 kid1| Store logging disabled
2020/02/12 22:56:12 kid1| DNS Socket created at [::], FD 9
2020/02/12 22:56:12 kid1| DNS Socket created at 0.0.0.0, FD 10
2020/02/12 22:56:12 kid1| Adding nameserver 114.114.114.114 from /etc/resolv.conf
2020/02/12 22:56:12 kid1| Adding nameserver 114.114.115.115 from /etc/resolv.conf
2020/02/12 22:56:12 kid1| helperOpenServers: Starting 0/50 'basic_ncsa_auth' processes
2020/02/12 22:56:12 kid1| helperOpenServers: No 'basic_ncsa_auth' processes needed.
2020/02/12 22:56:12 kid1| HTCP Disabled.
2020/02/12 22:56:12 kid1| Finished loading MIME types and icons.
2020/02/12 22:56:12 kid1| Accepting HTTP Socket connections at local=0.0.0.0:3128 remote=[::] FD 11 flags=9

access.log

# tail -f /var/log/squid/access.log 
1581519614.049     47 192.168.0.100 TCP_TUNNEL/200 5383 CONNECT registry.cn-beijing.aliyuncs.com:443 - HIER_DIRECT/47.95.181.38 -
1581542724.596     39 192.168.0.100 TCP_MISS/503 397 HEAD http://url/ - HIER_NONE/- text/html
1581542754.860    256 192.168.0.100 TCP_TUNNEL/200 4460 CONNECT www.baidu.com:443 - HIER_DIRECT/180.101.49.41 -
1581542818.197    107 192.168.0.100 TCP_MISS/200 262 HEAD http://www.baidu.com/ - HIER_DIRECT/180.101.49.41 text/html
1581542820.465     53 192.168.0.100 TCP_MISS/200 262 HEAD http://www.baidu.com/ - HIER_DIRECT/180.101.49.41 text/html
1581542821.292     53 192.168.0.100 TCP_MISS/200 262 HEAD http://www.baidu.com/ - HIER_DIRECT/180.101.49.41 text/html
1581542821.864     54 192.168.0.100 TCP_MISS/200 262 HEAD http://www.baidu.com/ - HIER_DIRECT/180.101.49.41 text/html
1581542822.323     53 192.168.0.100 TCP_MISS/200 262 HEAD http://www.baidu.com/ - HIER_DIRECT/180.101.49.41 text/html
1581542822.764     53 192.168.0.100 TCP_MISS/200 262 HEAD http://www.baidu.com/ - HIER_DIRECT/180.101.49.41 text/html
1581542823.243     53 192.168.0.100 TCP_MISS/200 262 HEAD http://www.baidu.com/ - HIER_DIRECT/180.101.49.41 text/html

4、搭建k8s时配置代理

在使用kubeadm或二进制方式搭建k8s集群时,集群内部podservice会单独占用不同的网络,service网络是一个虚拟的网段,这个时候还需要声明流量不让其走http/https代理,这种声明方式也适用于对那些没有域名解析通过绑定hosts文件来访问的域名。

如果不声明,在使用kubeadm部署k8s集群,执行kubeadm init初始化集群的时候会看到如下的提示并执行超时失败

[WARNING HTTPProxyCIDR]: connection to "10.1.0.0/12" uses proxy "http://192.168.0.200:3128". This may lead to malfunctional cluster setup. Make sure that Pod and Services IP ranges specified correctly as exceptions in proxy configuration
[WARNING HTTPProxyCIDR]: connection to "10.244.0.0/16" uses proxy "http://192.168.0.200:3128". This may lead to malfunctional cluster setup. Make sure that Pod and Services IP ranges specified correctly as exceptions in proxy configuration

在不断重试的时候可以查看代理服务器的squid日志,发现日志中一直不断有来自kubeadm所在的机器向本机的6443端口发送的请求,却被拒绝了

1581507643.783      0 192.168.0.100 TCP_DENIED/403 3927 CONNECT 192.168.0.8:6443 - HIER_NONE/- text/html
1581507644.283      0 192.168.0.100 TCP_DENIED/403 3927 CONNECT 192.168.0.8:6443 - HIER_NONE/- text/html
1581507644.783      0 192.168.0.100 TCP_DENIED/403 3927 CONNECT 192.168.0.8:6443 - HIER_NONE/- text/html
1581507645.283      0 192.168.0.100 TCP_DENIED/403 3927 CONNECT 192.168.0.8:6443 - HIER_NONE/- text/html
1581507645.783      0 192.168.0.100 TCP_DENIED/403 3927 CONNECT 192.168.0.8:6443 - HIER_NONE/- text/html
1581507646.283      0 192.168.0.100 TCP_DENIED/403 3927 CONNECT 192.168.0.8:6443 - HIER_NONE/- text/html
1581507646.783      0 192.168.0.100 TCP_DENIED/403 3927 CONNECT 192.168.0.8:6443 - HIER_NONE/- text/html
1581507647.283      0 192.168.0.100 TCP_DENIED/403 3927 CONNECT 192.168.0.8:6443 - HIER_NONE/- text/html

通过踩坑,正确可行的配置流程是:

  • 修改docker代理配置
# vim /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://squid:123456@192.168.0.200:3128" "NO_PROXY=localhost,127.0.0.1,192.168.0.200,192.168.0.100,192.168.0.101,10.244.0.0/16"
# systemctl daemon-reload
# systemctl restart docker
  • 修改全局代理配置
# vim /etc/profile
export http_proxy="http://squid:123456@192.168.0.200:3128"
export https_proxy="http://squid:123456@192.168.0.200:3128"
export no_proxy=localhost,127.0.0.1,192.168.0.200,192.168.0.100,192.168.0.101,10.244.0.0/16
# source /etc/profile
  • 命令行声明
export http_proxy="http://squid:123456@192.168.0.200:3128"
export https_proxy="http://squid:123456@192.168.0.200:3128"
export no_proxy=localhost,127.0.0.1,192.168.0.200,192.168.0.100,192.168.0.101,10.244.0.0/16

然后再次执行kubeadm init初始化集群就成功了

5、总结

本文记录了在特定的网络环境下如何通过squid配置代理,实现内网机器常用功能上网的方法,当然,这里提到的yumwgetdockerk8s集群安装常用的操作之外还有一些也需要通过上网代理,这里不一一提及。

好啦,疫情没结束,只能在家上班熬夜失眠,愿平安!🙏🙏🙏