解决docker的tls(ssl)证书过期问题
解决docker的tlsssl证书过期问题问题现象:可能的原因分析:[root@localhost ~]# docker image pull xxx.com.cn/centos7
Using default tag: latest
Error response from daemon: Get https://xxx.com.cn/v1/_ping: x509: certificate has expired or is not yet valid
linux使用查看date查看当前时间,与证书的有效时间作比对,得出具体的原因,可能为以下二种之一:
1.本机的时间不对;
2.Registry的证书确实已过期;
解决方法:1.本机的时间不对;
修改本机时间即可
2.Registry的证书确实已过期;
对 Registry 创建 SSL 安全例外,放弃对 Registry 服务器证书合法性校验,但是具有安全风险.
insecure registries 使能后, Docker 将以以下步骤尝试https连接:
首先尝试使用HTTPS.
如果 HTTPS 连接可达但是证书不可用, 忽略证书错误;
如果 HTTPS 连接不可用, 使用 HTTP.
centos 在 /etc/docker/ 目录下创建daemon的配置文件 daemon.json ,将你的目标 Registry 所处的 IP 地址段或者具体的服务域名端口号写入 json 文件,
举个例子,笔者的服务器所在网段为10.0.0.0/8.那么内容如下:
{ "insecure-registries" : ["10.0.0.0/8"] }
也可以使用域名加端口号,示例如下:
{ "insecure-registries" : ["myregistrydomain.com:5000"] }
windows 则修改文件 C:\ProgramData\docker\config\daemon.json ,格式与linux操作的一样.
重启docker服务.
查看是否生效,注意 Insecure Registries 字段.
[root@localhost ~]# docker info docker info : Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 2 Server Version: 17.05.0-ce Storage Driver: overlay Backing Filesystem: xfs Supports d_type: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host macvlan null overlay Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: 9048e5e50717ea4497b757314bad98ea3763c145 runc version: 9c2d8d184e5da67c95d601382adf14862e4f2228 init version: 949e6fa Security Options: seccomp Profile: default Kernel Version: 3.10.0-693.el7.x86_64 Operating System: CentOS Linux 7 (Core) OSType: linux Architecture: x86_64 CPUs: 24 Total Memory: 62.74GiB Name: localhost.localdomain ID: 755F:OEFV:VP3S:BMGQ:VUFW:WGT5:YQHO:EW6T:AAVE:NHS2:TPV3:SBTJ Docker Root Dir: /var/lib/docker Debug Mode (client): false Debug Mode (server): false Registry: https://index.docker.io/v1/ Experimental: false Insecure Registries: 10.0.0.0/8 127.0.0.0/8 Live Restore Enabled: false
以火狐浏览器为例
补充:Docker私有仓库更换过期的自签证书
更换Docker registry证书拉取镜像时报错如下:
k8s@master:~/shiyu$ docker pull reg.netlab.com/tensorflow-cpu Using default tag: latest Error response from daemon: Get https://reg.netlab.com/v2/: x509: certificate has expired or is not yet valid
root@master:~# openssl x509 -in /etc/docker/certs.d/reg.netlab.com/reg.netlab.com.crt -noout -dates notBefore=Apr 1 13:21:22 2019 GMT notAfter=Mar 31 13:21:22 2020 GMT
显然,该自签证书在2020年3月31号已过期。
重新自签新证书创建~/certs文件夹存放key和密钥
mkdir -p ~/certs
生成key
cd ~/certs openssl genrsa -out reg.netlab.com.key 2048
生密钥文件
openssl req -newkey rsa:4096 -nodes -sha256 -keyout reg.netlab.com.key -x509 -days 365 -out reg.netlab.com.crt
填写相关信息
Country Name (2 letter code) [XX]:CN # 你的国家名称 State or Province Name (full name) []:guangdong # 省份 Locality Name (eg, city) [Default City]:guagnzhou # 所在城市 Organization Name (eg, company) [Default Company Ltd]:sysu # 组织名称 Organizational Unit Name (eg, section) []:netlab # 组织单元名称 Common Name (eg, your name or your server's hostname) []:reg.netlab.com # 域名 Email Address []:urmsone@163.com
至此,证书自签完成。
将该证书添加到docker根证书中,重启docker注:由于是自签名证书,默认是不受Docker信任的,故而需要将证书添加到Docker的根证书中,Docker在CentOS 7/ubuntu 18中,证书存放路径是/etc/docker/certs.d/域名:
添加证书到docker根证书中
mkdir -p /etc/docker/certs.d/reg.netlab.com cp ~/certs/reg.netlab.com.crt /etc/docker/certs.d/reg.netlab.com/
重启Docker
systemctl restart docker
替换Docker registry容器中的过期证书
查看registry容器ID
k8s@master:~$ docker ps |grep registry 3eb5eda4b75e registry.docker-cn.com/library/registry:2 "/entrypoint.sh /etc…" 13 months ago Up 44 minutes 0.0.0.0:443->5000/tcp registry b84ea71a572f f32a97de94e1 "/entrypoint.sh /etc…" 13 months ago Up About an hour 0.0.0.0:5000->5000/tcp registry_mirror
根据ID查看rigstry的挂载路径
k8s@master:~$ docker inspect 3eb5eda4b75e ... "Binds": [ "/root/certs:/certs", "/home/registry:/var/lib/registry" ] ...
将刚刚新生成的证书cp到/root/certs:/certs目录下
root@master:~/certs# ll 总用量 16 drwxr-xr-x 2 root root 4096 Apr 1 2019 ./ drwx------ 8 root root 4096 May 2 14:06 ../ -rw-r--r-- 1 root root 2126 Apr 1 2019 reg.netlab.com.crt -rw------- 1 root root 3272 Apr 1 2019 reg.netlab.com.key
重启registry容器
k8s@master:~$ systemctl restart docker
至此,自签证书更新完毕!
测试k8s@master:~/shiyu$ docker pull reg.netlab.com/tensorflow-cpu Using default tag: latest latest: Pulling from tensorflow-cpu Digest: sha256:68da50778a5f80e0676c4ca617299444fc71677a2d83cacccaf7a08d08cc1df6 Status: Image is up to date for reg.netlab.com/tensorflow-cpu:latest
以上为个人经验,希望能给大家一个参考,也希望大家多多支持开心学习网。如有错误或未考虑完全的地方,望不吝赐教。