美文网首页
docker容器磁盘扩容

docker容器磁盘扩容

作者: sweet_yx | 来源:发表于2018-01-15 14:33 被阅读0次

    一、配置文件里更改容器创建时的默认磁盘大小

    ubutntu

    [root@ip-10-10-125-7~]#cat/etc/sysconfig/docker-storage

    DOCKER_STORAGE_OPTIONS="--storage-opt dm.loopdatasize=2000G -storage-opt dm.loopmetadatasize=10G --storage-opt dm.fs=ext4 --storage-opt dm.basesize=20G"
    
    参数:
    
    dm.loopdatasize=2000G是指存放数据的数据库空间为2t,默认是100g
    
    dm.loopmetadatasize=10G是存放Metadata数据空间为10g,默认是2g
    
    dm.fs=ext4是指容器磁盘分区为ext4
    
    dm.basesize=20G是指容器根分区默认为20g,默认是10g
    

    redhat

    docker run -dit --name es --storage-opt size=30G 
    

    新版本dm字段有了改变:

    DOCKER_STORAGE_OPTIONS="--storage-driver devicemapper --storage-opt dm.loopdatasize=2000G --storage-opt dm.loopmetadatasize=10G --storage-opt dm.fs=ext4 --storage-opt dm.basesize=20G"
    
    

    二、容器的动态扩容脚本

    #!/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
    

    相关文章

      网友评论

          本文标题:docker容器磁盘扩容

          本文链接:https://www.haomeiwen.com/subject/jparoxtx.html