Docker守護進(jìn)程安全配置項目詳解
本文將為大家介紹docker守護進(jìn)程的相關(guān)安全配置項目。
一、測試環(huán)境
1.1 安裝 CentOS 7
CentOS Linux release 7.7.1908 (Core)
升級內(nèi)核,重啟
# yum update kernel
[root@localhost docker]# uname -a
Linux localhost 3.10.0-1062.12.1.el7.x86_64 #1 SMP Tue Feb 4 23:02:59 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost docker]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
1.2 安裝 docker ce 19.03
# yum install -y yum-utils device-mapper-persistent-data lvm2 # yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo # yum install -y docker-ce [root@localhost docker]# docker --version Docker version 19.03.8, build afacb8b
二、 守護進(jìn)程安全配置
默認(rèn)沒有配置文件,需要單獨創(chuàng)建/etc/docker/daemon.json,下面配置都是在該文件上進(jìn)行配置,本地的測試示例。
{
"icc": false,
"log-level": "info",
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file":"5",
"labels": "somelabel",
"env": "os,customer"
},
"iptables": true,
"userns-remap": "default",
"userland-proxy": false,
"experimental": false,
"selinux-enabled": true,
"live-restore": true,
"no-new-privileges": true,
"cgroup-pare
2.1 配置通過 HTTPS 和證書認(rèn)證訪問 Docker 守護進(jìn)程
服務(wù)器證書
創(chuàng)建 HOST,定義域(IP 也可以),會根據(jù)域來生成對應(yīng)的證書,一般用于注冊證書當(dāng)中的 CN:
創(chuàng)建證書目錄:
$ mkdir -p /etc/docker/dockerd/CA && cd /etc/docker/dockerd/CA
生成 key 證書,并填寫兩次 key 證書密碼:
$ openssl genrsa -aes256 -out ca-key.pem 4096
生成 ca 證書,需要輸入注冊證書基礎(chǔ)信息:
$ openssl req -new -x509 -days 365 -key ca-key.pem -sha256 -out ca.pem
創(chuàng)建 server 證書:
$ openssl genrsa -out server-key.pem 4096 $ openssl req -subj "/CN=localhsot" -sha256 -new -key server-key.pem -out server.csr
設(shè)定證書指定的 IP 地址:
$ echo subjectAltName = DNS:localhost,IP:127.0.0.1 >> extfile.cnf
將 Docker 守護程序密鑰的擴展使用屬性設(shè)置為僅用于服務(wù)器身份驗證:
$ echo extendedKeyUsage = serverAuth >> extfile.cnf
生成 server cert 證書:
$ openssl x509 -req -days 3650 -sha256 -in server.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out server-cert.pem -extfile extfile.cnf
客戶端證書
創(chuàng)建客戶端證書:(還是當(dāng)前目錄)
$ openssl genrsa -out key.pem 4096 $ openssl req -subj '/CN=localhost' -new -key key.pem -out client.csr
要使密鑰適合客戶端身份驗證,請創(chuàng)建擴展配置文件:
$ echo extendedKeyUsage = clientAuth >> extfile.cnf
生成 client cert 證書:
$ openssl x509 -req -days 3650 -sha256 -in client.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out cert.pem -extfile extfile.cnf
使用
對證書賦予相應(yīng)的權(quán)限:
$ chmod -v 0400 ca-key.pem key.pem server-key.pem $ chmod -v 0444 ca.pem server-cert.pem cert.pem [root@localhost CA]# ls ca-key.pem ca.pem ca.srl cert.pem client.csr extfile.cnf key.pem server-cert.pem server.csr server-key.pem
服務(wù)端配置 /etc/docker/daemon.json
"tls": true, "tlsverify": true, "tlscacert": "/etc/docker/CA/ca.pem", "tlscert": "/etc/docker/CA/server-cert.pem", "tlskey": "/etc/docker/CA/server-key.pem"
客戶端配置
設(shè)置客戶端證書到當(dāng)服務(wù)器上,并放置到相應(yīng)的位置:
$ cp -v {ca,cert,key}.pem ~/.docker
$ export DOCKER_HOST=tcp://$HOST:2376 DOCKER_TLS_VERIFY=1
通過如下方式模擬測試:
$ curl https://$HOST:2376/images/json \
--cert ~/.docker/cert.pem \
--key ~/.docker/key.pem \
--cacert ~/.docker/ca.pem
[{"Containers":-1,"Created":1540777343,"Id":"sha256:55e7b305dc477345434ce3bd3941940481f982eea31c8f28c0670d59c63d544b","Labels":nu
2.2 使用namespace隔離技術(shù)
namespace是一種隔離技術(shù),docker就是使用隔離技術(shù)開啟特定的namespace創(chuàng)建出一些特殊的進(jìn)程,不過使用namespace是有條件的。系統(tǒng)會創(chuàng)建dockremap,通過/etc/subuid和/etc/subuid對應(yīng)的id值,映射到容器中去;實際情況還是使用的是dockremap普通權(quán)限,達(dá)到自動隔離的效果。
首先修改 /etc/sysctl.conf
# echo "user.max_user_namespaces=15076" >> /etc/sysctl.conf
在 /etc/docker/daemon.json 增加配置項 "userns-remap": "default"
修改此項配置需要慎重,如果是已經(jīng)部署了一套docker環(huán)境,啟用此選項后,會切換到隔離環(huán)境,以前的docker容器將無法使用!
[root@localhost docker]# cat /etc/subuid dockremap:100000:65536
2.3 設(shè)置 docker 的分區(qū)
為容器創(chuàng)建單獨的分區(qū),默認(rèn)分區(qū)在\var\lib\docker\,包含本地鏡像、容器、網(wǎng)絡(luò)等相關(guān)的東西。
[root@localhost docker]# ls /var/lib/docker
100000.100000 builder buildkit containers image network overlay2 plugins runtimes swarm tmp trust volumes
可以使用 "data-root": "" 配置默認(rèn)的分區(qū)位置。
2.4 限制默認(rèn)網(wǎng)橋容器之間的流量
當(dāng)啟動 Docker 服務(wù)時候,默認(rèn)會添加一條轉(zhuǎn)發(fā)策略到 iptables 的 FORWARD 鏈上。策略為通過( ACCEPT )還是禁止( DROP ),取決于配置 --icc=true (缺省值)還是 --icc=false 。如果手動指定 --iptables=false 則不會添加 iptables 規(guī)則。
默認(rèn)情況下,默認(rèn)網(wǎng)橋上同一主機上的容器之間允許所有網(wǎng)絡(luò)通信,如果不需要,限制所有容器間的通信。 將需要通信的特定容器鏈接在一起,或者創(chuàng)建自定義網(wǎng)絡(luò),并且僅加入需要與該自定義網(wǎng)絡(luò)進(jìn)行通信的容器。
配置限制默認(rèn)網(wǎng)橋上容器之間的流量 "icc":false
2.5 配置日志
配置集中的遠(yuǎn)程日志,設(shè)置日志進(jìn)程 --log-level 級別為 info ,日志記錄格式 json,本地日志記錄
"log-level": "info",
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file":"5",
"labels": "somelabel",
"env": "os,customer"
},
配置遠(yuǎn)程日志

Docker 日志記錄驅(qū)動程序接收容器日志并將其轉(zhuǎn)發(fā)到遠(yuǎn)程目標(biāo)或文件。 默認(rèn)的日志記錄驅(qū)動程序是json-file。 它將容器日志以JSON格式存儲在本地磁盤上。 Docker具有用于記錄日志的插件體系結(jié)構(gòu),因此有用于開源工具和商業(yè)工具的插件:
Journald–將容器日志存儲在系統(tǒng)日志中.
Syslog Driver–支持UDP,TCP,TLS
Fluentd –支持將TCP或Unix套接字連接到fluentd
Splunk – HTTP / HTTPS轉(zhuǎn)發(fā)到Splunk服務(wù)器
Gelf – UDP日志轉(zhuǎn)發(fā)到Graylog2
示例 fluent
{
"log-driver": "fluentd",
"log-opts": {
"fluentd-address": "fluentdhost:24224"
}
}
使用 syslog
{
"log-driver": "syslog",
"log-opts": {
"syslog-address": "udp://1.2.3.4:1111"
}
}
2.6 設(shè)置 ulimit
{
"default-ulimits": {
"nofile": {
"Name": "nofile",
"Hard": 64000,
"Soft": 64000
}
}
}
2.7 設(shè)置 cgroup
--cgroup-parent 選項允許設(shè)置用于容器的默認(rèn)cgroup父級。 如果未設(shè)置此選項,則對于fs cgroup驅(qū)動程序,默認(rèn)為 /docker ;對于systemd cgroup驅(qū)動程序,默認(rèn)為 system.slice 。
如果cgroup有一個正斜杠( / ),則cgroup在根cgroup下創(chuàng)建,否則cgroup在守護程序cgroup下創(chuàng)建。
假設(shè)守護程序在cgroup daemoncgroup中運行,則 --cgroup-parent=/foobar 在 /sys/fs/cgroup/memory/foobar 中創(chuàng)建一個cgroup,而使用 --cgroup-parent=foobar 則創(chuàng)建 /sys/fs/cgroup/memory/daemoncgroup/foobar 中創(chuàng)建 cgroup。
systemd cgroup驅(qū)動程序?qū)ΘCcgroup-parent具有不同的規(guī)則。 Systemd按切片表示層次結(jié)構(gòu),切片的名稱對樹中的位置進(jìn)行編碼。 因此,systemd cgroup的 --cgroup-parent 應(yīng)為切片名稱。 名稱可以包含一系列用短劃線分隔的名稱,這些名稱描述了從根切片到切片的路徑。 例如, --cgroup-parent=user-a-b.slice 表示容器的內(nèi)存cgroup在 /sys/fs/cgroup/memory/user.slice/user-a.slice/user-a-b.slice/docker-<id>.scope 中創(chuàng)建。
也可以使用容器運行來設(shè)置,使用docker create和docker run上的 --cgroup-parent 選項,會優(yōu)先于守護程序上的 --cgroup-parent 選項。
2.8 配置 seccomp
使用的測試配置文件,禁止在 Docker 里使用 chmod 命令
https://github.com/docker/labs/blob/master/security/seccomp/seccomp-profiles/default-no-chmod.json [root@localhost docker]# docker run --rm -it alpine sh / # ls bin etc lib mnt proc run srv tmp var dev home media opt root sbin sys usr / # touch foo.sh / # chmod +x foo.sh chmod: foo.sh: Operation not permitted / # exit
實際可以完成禁止、允許、告警某些系統(tǒng)相關(guān)的調(diào)用,參考: https://github.com/torvalds/linux/blob/master/arch/x86/entry/syscalls/syscall_64.tbl
2.9 配置支持無守護程序的容器
--live-restore 確保 docker 守護進(jìn)程關(guān)閉時不影響容器。
測試時再關(guān)閉 docker 守護進(jìn)程后,nginx 容器仍能正常提供訪問。

2.10 禁用 docker 的實驗性功能
設(shè)置 "experimental": false
2.11 限制容器通過 suid 或 sgid 提權(quán)
no-new-privileges 安全選項可防止容器內(nèi)的應(yīng)用程序進(jìn)程在執(zhí)行期間獲得新的特權(quán)。
舉例:有一個在映像中設(shè)置了 setuid/setgid 位的程序,例如sudo,容器中的進(jìn)程也具有執(zhí)行該程序的(文件)權(quán)限,試圖通過諸如setuid/setgid之類的設(shè)施獲取特權(quán)的任何操作將被拒絕。
三、守護進(jìn)程配置示例說明(Linux)
{
"authorization-plugins": [],//訪問授權(quán)插件
"data-root": "",//docker數(shù)據(jù)持久化存儲的根目錄,默認(rèn)為/var/lib/docker
"dns": [],//DNS服務(wù)器
"dns-opts": [],//DNS配置選項,如端口等
"dns-search": [],//DNS搜索域名
"exec-opts": [],//執(zhí)行選項
"exec-root": "",//執(zhí)行狀態(tài)的文件的根目錄
"experimental": false,//是否開啟試驗性特性
"features": {},//啟用或禁用特定功能。如:{"buildkit": true}使buildkit成為默認(rèn)的docker鏡像構(gòu)建器。
"storage-driver": "",//存儲驅(qū)動器類型
"storage-opts": [],//存儲選項
"labels": [],//鍵值對式標(biāo)記docker元數(shù)據(jù)
"live-restore": true,//dockerd掛掉是否?;钊萜鳎ū苊饬薲ocker服務(wù)異常而造成容器退出)
"log-driver": "json-file",//容器日志的驅(qū)動器
"log-opts": {
"max-size": "10m",
"max-file":"5",
"labels": "somelabel",
"env": "os,customer"
},//容器日志的選項
"mtu": 0,//設(shè)置容器網(wǎng)絡(luò)MTU(最大傳輸單元)
"pidfile": "",//daemon PID文件的位置
"cluster-store": "",//集群存儲系統(tǒng)的URL
"cluster-store-opts": {},//配置集群存儲
"cluster-advertise": "",//對外的地址名稱
"max-concurrent-downloads": 3,//設(shè)置每個pull進(jìn)程的最大并發(fā)
"max-concurrent-uploads": 5,//設(shè)置每個push進(jìn)程的最大并發(fā)
"default-shm-size": "64M",//設(shè)置默認(rèn)共享內(nèi)存的大小
"shutdown-timeout": 15,//設(shè)置關(guān)閉的超時時限
"debug": true,//開啟調(diào)試模式
"hosts": [],//dockerd守護進(jìn)程的監(jiān)聽地址
"log-level": "",//日志級別
"tls": true,//開啟傳輸層安全協(xié)議TLS
"tlsverify": true,//開啟輸層安全協(xié)議并驗證遠(yuǎn)程地址
"tlscacert": "",//CA簽名文件路徑
"tlscert": "",//TLS證書文件路徑
"tlskey": "",//TLS密鑰文件路徑
"swarm-default-advertise-addr": "",//swarm對外地址
"api-cors-header": "",//設(shè)置CORS(跨域資源共享-Cross-origin resource sharing)頭
"selinux-enabled": false,//開啟selinux(用戶、進(jìn)程、應(yīng)用、文件的強制訪問控制)
"userns-remap": "",//給用戶命名空間設(shè)置 用戶/組
"group": "",//docker所在組
"cgroup-pare
總結(jié)
到此這篇關(guān)于Docker守護進(jìn)程安全配置項目詳解的文章就介紹到這了,更多相關(guān)docker守護進(jìn)程內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信