赞
踩
还是惯例,先贴官网地址
Docker基于CS(client-server)架构,Docker client简单来说就是执行docker ps, docker image list
等命令的客户端,Docker daemon即运行在后台的dockerd进程,负责处理请求、运行、管理容器等工作,下图来自Docker官网,简单展示了Docker的架构。
既然是CS架构,Docker客户端就要连接Docker服务,二者可以作为同一主机上的两个进程,也可以是不同主机上的两个进程进行通信,都是使用REST API。另外,Docker Compose也是另一个单独的Docker client。
Docker daemon(dockerd)监听API请求并管理各种Docker对象,镜像、容器、网络和卷(volumes)。Docker daemon也可以与其他的daemon交互,管理docker服务。
Docker client(docker)是我们操作docker的接口,执行的docker命令被发送到Docker Daemon,处理后返回结果呈现在前端。Docker client可以同时与多个Docker Daemon交互。
Docker提供的图形化桌面应用,有Windows、Linux、Mac版本,涵盖了Docker Daemon、Docker client、Docker Compose、Docker Content Trust, Kubernetes,和Credential Helper,其中Windows是基于WSL的。
存储Docker镜像的地方,Pypi,Maven仓库类似。
配置Docker Daemon有两种方式
/etc/docker/daemon.json
/usr/lib/systemd/system/docker.service
/etc/systemd/system/docker.service.d/
下所有的conf配置文件可以同时使用两种方式,dockerd会将启动参数和JSON文件中的配置合并,前提是两个位置的配置不能冲突,不能出现相同的配置选项(key)。
Docker配置远程连接
默认情况下,dockerd在unix套接字上监听本地docker client连接。配置远程连接有加密(基于TLS)和不加密两种方式,都可以通过systemd或者daemon.json完成,两种配置不能有冲突,否则会导致dockerd启动失败。
无需任何认证,任何客户端均可以与dockerd通信,不安全,不推荐。
mkdir /root/tls
cd /root/tls
# 环境变量,后面使用
HOST=服务器IP
openssl genrsa -aes256 -out ca-key.pem 4096
以下是示例输出
Generating RSA private key, 4096 bit long modulus
…++
…++
e is 65537 (0x10001)
Enter pass phrase for ca-key.pem:
Verifying - Enter pass phrase for ca-key.pem:
openssl req -new -x509 -days 365 -key ca-key.pem -sha256 -out ca.pem
Enter pass phrase for ca-key.pem:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.’, the field will be left blank.
-----
Country Name (2 letter code) [AU]:
State or Province Name (full name) [Some-State]:Queensland
Locality Name (eg, city) []:Brisbane
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Docker Inc
Organizational Unit Name (eg, section) []:Sales
Common Name (e.g. server FQDN or YOUR name) []:$HOST
Email Address []:Sven@home.org.au
$HOST即上面设置的服务器IP
openssl genrsa -out server-key.pem 4096
# Generating RSA private key, 4096 bit long modulus
#.....................................................................++
#.................................................................................................++
#e is 65537 (0x10001)
openssl req -subj "/CN=$HOST" -sha256 -new -key server-key.pem -out server.csr
echo subjectAltName = DNS:$HOST,IP:$HOST,IP:127.0.0.1 >> extfile.cnf
echo extendedKeyUsage = serverAuth >> extfile.cnf
openssl x509 -req -days 365 -sha256 -in server.csr -CA ca.pem -CAkey ca-key.pem \
-CAcreateserial -out server-cert.pem -extfile extfile.cnf
# Signature ok
# subject=/CN=your.host.com
# Getting CA Private Key
# Enter pass phrase for ca-key.pem:
openssl genrsa -out key.pem 4096
# Generating RSA private key, 4096 bit long modulus
# .........................................................++
................++
# e is 65537 (0x10001)
openssl req -subj '/CN=client' -new -key key.pem -out client.csr
echo extendedKeyUsage = clientAuth > extfile-client.cnf
openssl x509 -req -days 365 -sha256 -in client.csr -CA ca.pem -CAkey ca-key.pem \
-CAcreateserial -out cert.pem -extfile extfile-client.cnf
# Signature ok
# subject=/CN=client
# Getting CA Private Key
# Enter pass phrase for ca-key.pem:
rm -v client.csr server.csr extfile.cnf extfile-client.cnf
chmod -v 0400 ca-key.pem key.pem server-key.pem
chmod -v 0444 ca.pem server-cert.pem cert.pem
最后有以下几个文件
-r-------- 1 root root 3326 Aug 19 08:27 ca-key.pem
-r--r--r-- 1 root root 2130 Aug 19 08:27 ca.pem
-rw-r--r-- 1 root root 41 Aug 19 08:44 ca.srl
-r--r--r-- 1 root root 1883 Aug 19 08:44 cert.pem
-r-------- 1 root root 3243 Aug 19 08:43 key.pem
-r--r--r-- 1 root root 1948 Aug 19 08:30 server-cert.pem
-r-------- 1 root root 3243 Aug 19 08:28 server-key.pem
/etc/docker
下cp -v {ca,cert,key}.pem /etc/docker
vim /etc/docker/daemon.json
,添加以下内容{
"hosts": ["unix:///var/run/docker.sock", "tcp://0.0.0.0:2376"],
"tls": true,
"tlscacert": "/etc/docker/ca.pem",
"tlscert": "/etc/docker/server-cert.pem",
"tlskey": "/etc/docker/server-key.pem",
"tlsverify": true
}
vim /etc/systemd/system/docker.service.d/exec-start-conflit.conf
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd --containerd=/run/containerd/containerd.sock
vim /etc/systemd/system/docker.service.d/exec-start-conflit.conf
[Service]
ExecStart=
ExecStart= --tlsverify --tls --tlscacert=/etc/docker/ca.pem --tlscert=/etc/docker/server-cert.pem --tlskey=/etc/docker/server-key.pem -H unix:///var/run/docker.sock -H tcp://0.0.0.0:2376 --containerd=/run/containerd/containerd.sock
两种方式配置完后,都需要重启docker
systemctl daemon-reload
systemctl restart docker
# 检查2376端口是否打开
netstat -nltp | grep 2376
# 本地是否正常执行
docker ps
拷贝ca.pem、cert.pem、key.pem
到客户端主机指定文件夹。
docker --tlsverify \
--tlscacert=ca.pem \
--tlscert=cert.pem \
--tlskey=key.pem \
-H=$HOST:2376 version
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。