Liuzhiqianblog 2019-11-04
containerd 安装的环境在内网,无法拉取外网 dockerhub 的镜像,为了实现 拉取外网镜像,需要 containerd 服务配置正向代理,使 containerd 可以通过代理访问 外网。
代理服务器可以选用 nginx 和 squid。squid 代理支持 https 代理但性能不如 nginx 代理好,nginx 默认不支持 https 正向代理,但可以通过安装 ngx_http_proxy_connect_module 模块支持。本次代理选用nginx搭建。
下载 ngx_http_proxy_connect_module 模块
# cd /root # git clone [email protected]:chobits/ngx_http_proxy_connect_module.git
下载 nginx 源码并编译
# wget http://nginx.org/download/nginx-1.9.2.tar.gz # tar -xzvf nginx-1.9.2.tar.gz # cd nginx-1.9.2/ # patch -p1 < /path/to/ngx_http_proxy_connect_module/patch/proxy_connect.patch # ./configure --add-module=/path/to/ngx_http_proxy_connect_module # make && make install
server {
listen 3128;
# dns resolver used by forward proxying
resolver 8.8.8.8;
# forward proxy for CONNECT request
proxy_connect;
proxy_connect_allow 443 563;
# forward proxy for non-CONNECT request
location / {
proxy_pass http://$host;
proxy_set_header Host $host;
}
}配置成功后启动 nginx 服务
# /usr/local/nginx/sbin/nginx
代理服务器搭建成功后即可配置 containerd 服务使用该代理拉取镜像。配置如下:
# mkdir /etc/systemd/system/containerd.service.d # cat > /etc/systemd/system/containerd.service.d/http_proxy.conf << EOF > [Service] > Environment="HTTP_PROXY=http://<proxy_ip>:<proxy_port>/" > EOF # # # 配置 no_proxy 指定不走代理的域名或ip # cat > /etc/systemd/system/containerd.service.d/no_proxy.conf << EOF > [Service] > Environment="NO_PROXY=http://<apiserver_ip>:<apiserver_port>/" > EOF
重启 containerd 服务
# systemctl daemon-reload # systemctl restart containerd
重启后测试拉取外网镜像
# crictl --debug pull nginx
DEBU[0000] PullImageRequest: &PullImageRequest{Image:&ImageSpec{Image:nginx,},Auth:nil,SandboxConfig:nil,}
DEBU[0004] PullImageResponse: &PullImageResponse{ImageRef:sha256:5a3221f0137beb960c34b9cf4455424b6210160fd618c5e79401a07d6e5a2ced,}
Image is up to date for sha256:5a3221f0137beb960c34b9cf4455424b6210160fd618c5e79401a07d6e5a2ced
#到此,containerd 代理配置完成