1.Docker容器的安全性,很大程度上依赖于Linux系统自身,评
估Docker的安全性时,主要考虑以下几个方面:
2.命名空间隔离的安全
3.控制组资源控制的安全
4.内核能力机制
5.Docker服务端防护
6.其他安全特性
1.docker与系统共享内核并且会在宿主机上产生相应的进程
[root@server1 ~]# docker run -it --name vm1 ubuntu
root@f1c5528bcddb:/# ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr
root@f1c5528bcddb:/# free -m
total used free shared buffers cached
Mem: 992 442 550 12 0 257
-/+ buffers/cache: 184 808
Swap: 2047 0 2047
[root@server1 ~]# docker inspect vm1 | grep Pid
"Pid": 3435,
"PidMode": "",
"PidsLimit": 0,
[root@server1 ~]# cd /proc/3435
[root@server1 3435]# ls
attr environ mem pagemap stat
autogroup exe mountinfo personality statm
auxv fd mounts projid_map status
cgroup fdinfo mountstats root syscall
clear_refs gid_map net sched task
cmdline io ns schedstat timers
comm limits numa_maps sessionid uid_map
coredump_filter loginuid oom_adj setgroups wchan
cpuset map_files oom_score smaps
cwd maps oom_score_adj stack
[root@server1 3435]# cd ns/
[root@server1 ns]# ll
total 0
lrwxrwxrwx 1 root root 0 Aug 7 09:51 ipc -> ipc:[4026532227]
lrwxrwxrwx 1 root root 0 Aug 7 09:51 mnt -> mnt:[4026532225]
lrwxrwxrwx 1 root root 0 Aug 7 09:50 net -> net:[4026532230]
lrwxrwxrwx 1 root root 0 Aug 7 09:51 pid -> pid:[4026532228]
lrwxrwxrwx 1 root root 0 Aug 7 09:51 user -> user:[4026531837]
lrwxrwxrwx 1 root root 0 Aug 7 09:51 uts -
查看cgroup服务路径
[root@server1 ~]# mount -t cgroup
依次查看cpu,内存和容器的目录下的文件
注意:cgroup目录下有对容器进行相应的限制的参数,如cpu,memory等,新建的容器的id会出现在相应限额的docker的目录下,在memory目录下新建目录时直接会从上级目录继承.
查看内存,发现没有限制
在linux系统中可以通过修改/etc/security/limits.conf文件来限制cpu内存
[root@server1 ~]# cd /etc/security/limits.d/
[root@server1 limits.d]# ls
20-nproc.conf
[root@server1 limits.d]# vim /etc/security/limits.conf
1.cpu独占
[root@server1 ~]# cd /sys/fs/cgroup/cpu
[root@server1 cpu]# mkdir x1
[root@server1 cpu]# cd x1/
[root@server1 x1]# ls
cgroup.clone_children cpu.cfs_quota_us
cgroup.event_control cpu.rt_period_us
cgroup.procs cpu.rt_runtime_us
cpuacct.stat cpu.shares
cpuacct.usage cpu.stat
cpuacct.usage_percpu notify_on_release
cpu.cfs_period_us tasks
[root@server1 x1]# cat cpu.cfs_period_us
100000 ##限制的长度
[root@server1 x1]# cat cpu.cfs_quota_us
-1 ##没有时间限制
[root@server1 x1]# dd if=/dev/zero of=/dev/null & ##占用cpu
[1] 3951
[root@server1 x1]# top ##查看cpu的占有百分比(只有一个cpu)
若有两个cpu需要按下列操作:
[root@server1 x1]# cd /sys/devices/system/cpu/
[root@server1 cpu]# ls
[root@server1 cpu]# cd cpu0/
[root@server1 cpu]# echo 0 > online
[root@server1 cpu]# lscpu
注意:
1.cpu.cfs_period_us:cpu分配的周期(微秒),默认为100000。
2.cpu.cfs_quota_us:表示该control group限制占用的时间(微秒),默认为-1,表示不限制。如果设为50000,表示占用50000/100000=50%的CPU。
2.对cpu资源进行限制(docker容器)
1.清除之前创建的容器
[root@server1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7c111fe00da9 ubuntu "/bin/bash" About an hour ago Up About an hour vm1
[root@server1 ~]# docker rm -f vm1
vm1
[root@server1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
、
2.对容器暂停,和解除暂停
[root@server1 ~]# cd /sys/fs/cgroup/
[root@server1 cgroup]# ls
blkio cpu,cpuacct freezer net_cls perf_event
cpu cpuset hugetlb net_cls,net_prio pids
cpuacct devices memory net_prio systemd
[root@server1 cgroup]# cd freezer/
[root@server1 freezer]# ls
cgroup.clone_children cgroup.sane_behavior release_agent
cgroup.event_control docker tasks
cgroup.procs notify_on_release
[root@server1 freezer]# cd docker/
[root@server1 docker]# ls
cgroup.clone_children freezer.self_freezing
cgroup.event_control freezer.state
cgroup.procs notify_on_release
freezer.parent_freezing tasks
[root@server1 docker]# cat freezer.state
THAWED ##显示开启状态
创建容器,并暂停
[root@server1 docker]# docker run -it --name vm1 ubuntu
root@797f2da104ec:/# [root@server1 docker]#
[root@server1 docker]# ls
797f2da104ecda43a84eb833d2ed49900e6c5216ad60497865c5d269bce4faf5
cgroup.clone_children
cgroup.event_control
cgroup.procs
freezer.parent_freezing
freezer.self_freezing
freezer.state
notify_on_release
tasks
[root@server1 docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
797f2da104ec ubuntu "/bin/bash" 29 seconds ago Up 27 seconds vm1
[root@server1 docker]# docker container pause vm1 ##暂停容器vm1
vm1
[root@server1 docker]# cd 797f2da104ecda43a84eb833d2ed49900e6c5216ad60497865c5d269bce4faf5
[root@server1 797f2da104ecda43a84eb833d2ed49900e6c5216ad60497865c5d269bce4faf5]# ls
cgroup.clone_children freezer.self_freezing
cgroup.event_control freezer.state
cgroup.procs notify_on_release
[root@server1 797f2da104ecda43a84eb833d2ed49900e6c5216ad604978[root@server1 797f2da104ecda43a84eb833d2ed49900e6c5216ad60497865c5d269bce4faf5]# cat freezer.state FROZEN ##暂停状态
[root@server1 797f2da104ecda43a84eb833d2ed49900e6c5216ad60497865c5d269bce4faf5]# cat tasks
4382 ##进程id
[root@server1 797f2da104ecda43a84eb833d2ed49900e6c5216ad60497865c5d269bce4faf5]# ps ax ##进程4382显示暂停
[root@server1 797f2da104ecda43a84eb833d2ed49900e6c5216ad60497865c5d269bce4faf5]# docker container unpause vm1
vm1
[root@server1 797f2da104ecda43a84eb833d2ed49900e6c5216ad60497865c5d269bce4faf5]# cat freezer.state
THAWED
[root@server1 797f2da104ecda43a84eb833d2ed49900e6c5216ad60497865c5d269bce4faf5]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
797f2da104ec ubuntu "/bin/bash" 7 minutes ago Up 7 minutes vm1
[root@server1 797f2da104ecda43a84eb833d2ed49900e6c5216ad60497865c5d269bce4faf5]# cat tasks
4382
[root@server1 797f2da104ecda43a84eb833d2ed49900e6c5216ad60497865c5d269bce4faf5]# ps ax
设置swap和内存总共的大小(针对docker容器)
[root@server1 ~]# docker run -it --name vm1 --memory 300MB --memory-swap 300MB ubuntu
root@4057f95e93bf:/# free -m
total used free shared buffers cached
Mem: 992 442 550 12 0 261
-/+ buffers/cache: 179 813
Swap: 2047 0 2047
root@4057f95e93bf:/# [root@server1 ~]#
[root@server1 ~]# free -m
total used free shared buff/cache available
Mem: 992 133 558 12 300 695
Swap: 2047 0 204
1.容器中的内存信息和主机中的内存信息
[root@server1 ~]# docker run -it --name vm1 --memory 300MB --memory-swap 300MB ubuntu
root@4057f95e93bf:/# free -m
total used free shared buffers cached
Mem: 992 442 550 12 0 261
-/+ buffers/cache: 179 813
Swap: 2047 0 2047
root@4057f95e93bf:/# [root@server1 ~]#
[root@server1 ~]# free -m
total used free shared buff/cache available
Mem: 992 133 558 12 300 695
Swap: 2047 0 2047
2.启用lxcfs
1.安装
[root@server1 ~]# ls
[root@server1 ~]# yum install lxcfs-2.0.5-3.el7.centos.x86_64.rpm -y
[root@server1 ~]# cd /var/lib/lxcfs/
[root@server1 lxcfs]# ls
[root@server1 lxcfs]# lxcfs /var/lib/lxcfs/ &
[2] 4802
[root@server1 lxcfs]# hierarchies:
0: fd: 5: freezer
1: fd: 6: memory
2: fd: 7: net_prio,net_cls
3: fd: 8: devices
4: fd: 9: blkio
5: fd: 10: perf_event
6: fd: 11: hugetlb
7: fd: 12: cpuacct,cpu
8: fd: 13: cpuset
9: fd: 14: pids
10: fd: 15: name=systemd
[root@server1 lxcfs]#
3.启动lxcfs之后,会在/var/lib/lxcfs目录下生成两个目录
[root@server1 ~]# cd /var/lib/lxcfs/
[root@server1 lxcfs]# ls
cgroup proc
如果在/var/lib/lxcfs目录下启动的lxcfs,之后需要重新进入才可以看到新生成的目录
[root@server1 lxcfs]# ps ax | grep lxcfs
4802 pts/0 Sl 0:00 lxcfs /var/lib/lxcfs/
测试:
创建容器,限制内存发现限制成功
[root@server1 ~]# docker run -it --name vm1 --memory 300MB --memory-swap 300MB \
> -v /var/lib/lxcfs/proc/diskstats:/proc/diskstats:rw \
> -v /var/lib/lxcfs/proc/meminfo:/proc/meminfo:rw \
> -v /var/lib/lxcfs/proc/stat:/proc/stat:rw \
> -v /var/lib/lxcfs/proc/swaps:/proc/swaps:rw \
> -v /var/lib/lxcfs/proc/uptime:/proc/uptime:rw \
> ubuntu