1. 程式人生 > >docker高階應用之動態擴充套件容器空間大小

docker高階應用之動態擴充套件容器空間大小

先轉載記錄一下,尚未驗證,有空看一下

------------------------------------------------------------------------------------------------------------

docker容器預設的空間是10G,如果想指定預設容器的大小(在啟動容器的時候指定),可以在docker配置檔案裡通過dm.basesize引數指定,比如

docker -d --storage-opt dm.basesize=20G

上面方法只是真的生成容器的時候進行的,並且修改後需要重啟docker,無法做到動態給執行容器指定大小,下面我介紹一下如何動態的擴充套件容器空間大小。

動態擴充套件的優點:

1、不需要修改docker配置,並且重啟docker服務;

2、可以直接對執行中的容器進行動態擴充套件(只能增,無法縮);

缺點:

1、docker所在宿主機分割槽的格式必須是ext2、ext3、ext4;

2、docker儲存引擎必須是devicemapper

儲存引擎檢視,可以使用docker info檢視

15:25:49 # docker info
Containers: 5
Images: 62
Storage Driver: devicemapper
 Pool Name: docker-8:17-37748738-pool
 Data file: /data1/docker/devicemapper/devicemapper/data
 Metadata file: /data1/docker/devicemapper/devicemapper/metadata
 Data Space Used: 21498.9 Mb
 Data Space Total: 102400.0 Mb
 Metadata Space Used: 13.7 Mb
 Metadata Space Total: 2048.0 Mb
Execution Driver: lxc-1.0.6
Kernel Version: 3.10.0-123.el7.x86_64

從上面的Storage Driver可以看到我使用的引擎。

下面是動態擴充套件的例子:

1、新建立個test容器

15:23:48 # docker run --privileged -d  -p 22 --name='test' docker.ops-chukong.com:5000/centos6-http:new /usr/bin/supervisord
1716fe941926dbd0b247b85d73e83b9465322a5005edc3c6182b59a6ac0939a7
[email protected]:/tmp
15:24:01 # docker inspect test|grep -i add
        "IPAddress": "172.17.0.18",
[email protected]
:/tmp 15:24:08 # ssh 172.17.0.18 The authenticity of host '172.17.0.18 (172.17.0.18)' can't be established. RSA key fingerprint is 39:7c:13:9f:d4:b0:d7:63:fc:ff:ae:e3:46:a4:bf:6b. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '172.17.0.18' (RSA) to the list of known hosts. [email protected]'s password: Last login: Mon Nov 17 14:10:39 2014 from 172.17.42.1 [email protected]:~ 15:24:13 # df -hT Filesystem Type Size Used Avail Use% Mounted on rootfs rootfs 9.8G 470M 8.8G 5% / /dev/mapper/docker-8:17-37748738-1716fe941926dbd0b247b85d73e83b9465322a5005edc3c6182b59a6ac0939a7 ext4 9.8G 470M 8.8G 5% / shm tmpfs 64M 0 64M 0% /dev/shm /dev/sdb1 ext4 1.8T 30G 1.7T 2% /.dockerinit /dev/sda3 ext4 518G 30G 462G 6% /etc/resolv.conf /dev/sdb1 ext4 1.8T 30G 1.7T 2% /etc/hostname /dev/sdb1 ext4 1.8T 30G 1.7T 2% /etc/hosts /dev/sdb1 ext4 1.8T 30G 1.7T 2% /.dockerenv

可以看到我執行test容器的根分割槽是ext4分割槽,容器是10G

2、使用我的指令碼動態擴充套件容器空間

指令碼內容

15:22:12 # cat dynamic_modify_docker_disk.sh
#!/bin/bash
#This script is dynamic modify docker container disk
#Author Deng Lei
if [ -z $1 ] || [ -z $2 ]; then
    echo "Usage: container_name increase_capacity"
    echo "Example: I want increase 11G to test"
    echo "The command is:   sh `basename $0` test 11"
    exit 1
																										                                                fi
if [ `docker inspect $1 &>>/dev/null &&  echo 0 || echo 1` -eq 1 ];then
    echo "The container $1 is no exist!"
	exit 1
fi
container_id=`docker inspect -f '{{ .Id }}' $1`
now_disk=`dmsetup table /dev/mapper/docker-*-$container_id|awk '{print $2}'`
disk=$(($2*1024*1024*1024/512))
if [ $disk -lt $now_disk ];then
    echo "I can't shink container $1 from $(($now_disk*512/1024/1024/1024))G to ${2}G!I only modify contanier increase disk!"
	exit 1
fi
dmsetup table /dev/mapper/docker-*-$container_id|sed "s/0 [0-9]* thin/0 $disk thin/"|dmsetup load /dev/mapper/docker-*-$container_id
dmsetup resume /dev/mapper/docker-*-$container_id
resize2fs /dev/mapper/docker-*-$container_id
if [ $? -eq 0 ];then
    echo "dynamic container $1 disk to ${2}G is success!"
else
    echo "dynamic container $1 disk to ${2}G is fail!"
fi

目前給test容器進行動態增加20G空間

15:24:40 # sh dynamic_modify_docker_disk.sh test 20
dynamic container test disk to 20G is success!
[email protected]:/tmp
15:24:46 # ssh 172.17.0.18
[email protected]'s password:
Last login: Tue Jan 20 15:24:13 2015 from 172.17.42.1
[email protected]:~
15:24:52 # df -hT
Filesystem           Type    Size  Used Avail Use% Mounted on
rootfs               rootfs   20G  475M   19G   3% /
/dev/mapper/docker-8:17-37748738-1716fe941926dbd0b247b85d73e83b9465322a5005edc3c6182b59a6ac0939a7
                     ext4     20G  475M   19G   3% /
shm                  tmpfs    64M     0   64M   0% /dev/shm
/dev/sdb1            ext4    1.8T   30G  1.7T   2% /.dockerinit
/dev/sda3            ext4    518G   30G  462G   6% /etc/resolv.conf
/dev/sdb1            ext4    1.8T   30G  1.7T   2% /etc/hostname
/dev/sdb1            ext4    1.8T   30G  1.7T   2% /etc/hosts
/dev/sdb1            ext4    1.8T   30G  1.7T   2% /.dockerenv

可以看到已經增加成功

下面在給test增加到50G

15:25:21 # sh dynamic_modify_docker_disk.sh test 50
dynamic container test disk to 50G is success!
[email protected]:/tmp
15:25:24 # ssh 172.17.0.18
[email protected]'s password:
Last login: Tue Jan 20 15:24:52 2015 from 172.17.42.1
[email protected]:~
15:25:27 # df -hT
Filesystem           Type    Size  Used Avail Use% Mounted on
rootfs               rootfs   50G  480M   47G   1% /
/dev/mapper/docker-8:17-37748738-1716fe941926dbd0b247b85d73e83b9465322a5005edc3c6182b59a6ac0939a7
                     ext4     50G  480M   47G   1% /
shm                  tmpfs    64M     0   64M   0% /dev/shm
/dev/sdb1            ext4    1.8T   30G  1.7T   2% /.dockerinit
/dev/sda3            ext4    518G   30G  462G   6% /etc/resolv.conf
/dev/sdb1            ext4    1.8T   30G  1.7T   2% /etc/hostname
/dev/sdb1            ext4    1.8T   30G  1.7T   2% /etc/hosts
/dev/sdb1            ext4    1.8T   30G  1.7T   2% /.dockerenv

也可以增加成功

但我要是像縮減到30G

15:25:45 # sh dynamic_modify_docker_disk.sh test 30
I can't shink container test from 50G to 30G!I only modify contanier increase disk!

是無法進行縮減的,僅能進行增加操作。

FAQ:

centos 7裡使用docker的時候預設儲存引擎是devicemapper

在進行動態調整docker容器磁碟空間的時候,出現

resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Device or resource busy while trying to open /dev/mapper/docker-253:1-1270544-d2d2cef71c86910467c1afdeb79c1a008552f3f9ef9507bb1e04d77f2ad5eac4
Couldn't find valid filesystem superblock.

原因是resize2fs僅能支援ext2、ext3、ext4,不支援xfs

所以建議docker伺服器的檔案系統格式調整為ext4