首页 > 代码库 > Docker虚拟化技术系列之-自动化部署管理
Docker虚拟化技术系列之-自动化部署管理
随着企业Docker容器越来越多,如果要靠手工去管理docker,肯定非常吃力,而且也不现实,这就需要我们把日常的操作变成自动化管理,通过脚本工具,将工作减轻到最小,解放运维人员,如下为企业Docker自动化部署脚本V1版本,如果需要更多其他的功能,可以自己定制和修改。
如下自动化管理脚本功能:
1)通过YUM自动安装Docker平台;
2)通过该脚本,可以自动配置桥接网络;
3)自动创建docker容器,给予centos操作系统;
4)自动收集创建好的Docker容器,集中管理;
5)通过PIPEWORK工具手动指定独立IP地址;
概要:该脚本能够自动安装并配置docker虚拟化及桥接网络,同时使用pipework这个软件来配置我们的容器IP,能够实现容器的一个简单的管理:
#!/bin/bash #auto install docker and Create VM #by jfedu.net wugk 2015-02-05 #Define PATH Varablies ####################### IPADDR=`ifconfig |grep "Bcast"|awk ‘{print $2}‘|cut -d: -f2|grep "192.168"|head -1` GATEWAY=`route -n|grep "UG"|awk ‘{print $2}‘|grep "192.168"|head -1` IPADDR_NET=`ifconfig |grep "Bcast"|awk ‘{print $2}‘|cut -d: -f2|grep "192.168"|head -1|awk -F. ‘{print $1"."$2"."$3"."}‘` LIST="/root/docker_vmlist.csv" if [ -f /etc/init.d/docker ];then sed -i -e ‘/^exec/d‘ -e ‘/^prog/aexec=\"/usr/bin/\$prog --storage-driver devicemapper --storage-opt dm.loopdatasize=2000G --storage-opt dm.loopmetadatasize=10G --storage-opt dm.fs=ext4 --storage-opt dm.basesize=40G\"‘ /etc/init.d/docker fi for i in `seq 1 253`;do ping -c 1 ${IPADDR_NET}${i} ;[ $? -ne 0 ]&& DOCKER_IPADDR="${IPADDR_NET}${i}" &&break;done >>/dev/null 2>&1 echo "##################" echo -e "Dynamic get docker IP,The Docker IP address\n\n$DOCKER_IPADDR" if [ ! -e /etc/sysconfig/network-scripts/ifcfg-eth0 ];then echo -e "\033[32mThe ifcfg-eth0 Interface is not exist,Please exit or change scripts.\nYou can to exec\nsed -i \‘s/eth0/eth0/g\‘ $0\033[0m" exit 0 fi NETWORK=( HWADDR=`ifconfig eth0 |egrep "HWaddr|Bcast" |tr "\n" " "|awk ‘{print $5,$7,$NF}‘|sed -e ‘s/addr://g‘ -e ‘s/Mask://g‘|awk ‘{print $1}‘` IPADDR=`ifconfig eth0 |egrep "HWaddr|Bcast" |tr "\n" " "|awk ‘{print $5,$7,$NF}‘|sed -e ‘s/addr://g‘ -e ‘s/Mask://g‘|awk ‘{print $2}‘` NETMASK=`ifconfig eth0 |egrep "HWaddr|Bcast" |tr "\n" " "|awk ‘{print $5,$7,$NF}‘|sed -e ‘s/addr://g‘ -e ‘s/Mask://g‘|awk ‘{print $3}‘` GATEWAY=`route -n|grep "UG"|awk ‘{print $2}‘` ) if [ -z "$1" -o -z "$2" ];then echo -e "\033[32m---------------------------------\033[0m" echo -e "\033[32mPlease exec $0 CPU(C) MEM(G),example $0 4 8\033[0m" exit 0 fi #CPU=`expr $2 - 1` #if [ ! -e /usr/bin/bc ];then # yum install bc -y >>/dev/null 2>&1 #fi CPU_ALL=`cat /proc/cpuinfo |grep processor|wc -l` if [ ! -f $LIST ];then CPU_COUNT=$1 CPU_1="0" CPU1=`expr $CPU_1 + 0` CPU2=`expr $CPU1 + $CPU_COUNT - 1` if [ $CPU2 -gt $CPU_ALL ];then echo -e "\033[32mThe System CPU count is $CPU_ALL,not more than it.\033[0m" exit fi else CPU_COUNT=$1 CPU_1=`cat $LIST|tail -1|awk -F"," ‘{print $4}‘|awk -F"-" ‘{print $2}‘` CPU1=`expr $CPU_1 + 1` CPU2=`expr $CPU1 + $CPU_COUNT - 1` if [ $CPU2 -gt $CPU_ALL ];then echo -e "\033[32mThe System CPU count is $CPU_ALL,not more than it.\033[0m" exit fi fi MEM_F=`echo $2 \* 1024|bc` MEM=`printf "%.0f\n" $MEM_F` DISK=40 USER=$3 REMARK=$4 ping $DOCKER_IPADDR -c 1 >>/dev/null 2>&1 if [ $? -eq 0 ];then echo -e "\033[32m---------------------------------\033[0m" echo -e "\033[32mThe IP address to be used,Please change other IP,exit.\033[0m" exit 0 fi if [ ! -e /etc/init.d/docker ];then rpm -e epel-release --nodeps rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm yum install docker-io -y yum install device-mapper* -y mkdir -p /export/docker/ cd /var/lib/ ;rm -rf docker ;ln -s /export/docker/ . mkdir -p /var/lib/docker/devicemapper/devicemapper #dd if=/dev/zero of=/var/lib/docker/devicemapper/devicemapper/data bs=1G count=0 seek=2000 /etc/init.d/docker start sed -i -e ‘/^exec/d‘ -e ‘/^prog/aexec=\"/usr/bin/\$prog --storage-driver devicemapper --storage-opt dm.loopdatasize=2000G --storage-opt dm.loopmetadatasize=10G --storage-opt dm.fs=ext4 --storage-opt dm.basesize=40G\"‘ /etc/init.d/docker if [ $? -ne 0 ];then echo "Docker install error ,please check." exit fi fi cd /etc/sysconfig/network-scripts/ mkdir -p /data/backup/`date +%Y%m%d-%H%M` yes|cp ifcfg-eth* /data/backup/`date +%Y%m%d-%H%M`/ if [ -e /etc/sysconfig/network-scripts/ifcfg-br0 ];then echo else cat >ifcfg-eth0 <<EOF DEVICE=eth0 BOOTPROTO=none ${NETWORK[0]} NM_CONTROLLED=no ONBOOT=yes TYPE=Ethernet BRIDGE="br0" ${NETWORK[1]} ${NETWORK[2]} ${NETWORK[3]} USERCTL=no EOF cat >ifcfg-br0 <<EOF DEVICE="br0" BOOTPROTO=none ${NETWORK[0]} IPV6INIT=no NM_CONTROLLED=no ONBOOT=yes TYPE="Bridge" ${NETWORK[1]} ${NETWORK[2]} ${NETWORK[3]} USERCTL=no EOF /etc/init.d/network restart fi echo ‘Your can restart Ethernet Service: /etc/init.d/network restart !‘ echo ‘---------------------------------------------------------‘ cd - #######create docker container service docker status >>/dev/null if [ $? -ne 0 ];then /etc/init.d/docker restart fi NAME="Docker_`echo $DOCKER_IPADDR|awk -F"." ‘{print $(NF-1)"_"$NF}‘`" #IMAGES=`docker images|grep -v "REPOSITORY"|grep -v "none"|head -1|awk ‘{print $1}‘` IMAGES=`docker images|grep -v "REPOSITORY"|grep -v "none"|grep "nginx"|head -1|awk ‘{print $1}‘` CID=$(docker run -itd --privileged --cpuset-cpus=${CPU1}-${CPU2} -m ${MEM}m --net=none --name=$NAME $IMAGES /bin/bash) if [ -z $IMAGES ];then echo "Plesae Download Docker Centos Images,you can to be use docker search centos,and docker pull centos6.5-ssh,exit 0" if [ ! -f /export/jfedu_new_base_img_6.5_v83_nginx.tar ];then echo "Please exec -it scp jfedu_new_base_img_6.5_v83_nginx.tar for docker server." exit fi docker load --input /export/jfedu_new_base_img_6.5_v83_nginx.tar fi if [ ! -f /usr/local/bin/pipework ];then yum install wget unzip zip -y wget https://github.com/jpetazzo/pipework/archive/master.zip unzip master cp pipework-master/pipework /usr/local/bin/ chmod +x /usr/local/bin/pipework rm -rf master fi ip netns >>/dev/null if [ $? -ne 0 ];then rpm -e iproute --nodeps wget -c https://repos.fedorapeople.org/openstack/EOL/openstack-grizzly/epel-6/iproute-2.6.32-130.el6ost.netns.2.x86_64.rpm rpm -ivh --nodeps --force iproute-2.6.32-130.el6ost.netns.2.x86_64.rpm fi pipework br0 $NAME $DOCKER_IPADDR/24@$IPADDR docker ps -a |grep "$NAME" if [ ! -e $LIST ];then echo "编号,容器ID,容器名称,CPU,内存,硬盘,容器IP,宿主机IP,使用人,备注" >$LIST fi ################### NUM=`cat $LIST |grep -v CPU|tail -1|awk -F, ‘{print $1}‘` if [[ $NUM -eq "" ]];then NUM="1" else NUM=`expr $NUM + 1` fi ################## echo -e "\033[32mCreate virtual client Successfully.\n$NUM `echo $CID|cut -b 1-12`,$NAME,$CPU1-$CPU2,${MEM}M,${DISK}G,$DOCKER_IPADDR,$IPADDR,$USER,$REMARK\033[0m" if [ -z $USER ];then USER="NULL" REMARK="NULL" fi echo $NUM,`echo $CID|cut -b 1-12`,$NAME,$CPU1-$CPU2,${MEM}M,${DISK}G,$DOCKER_IPADDR,$IPADDR,$USER,$REMARK >>$LIST rm -rf /root/docker_vmlist_* iconv -c -f utf-8 -t gb2312 $LIST -o /root/docker_vmlist_`date +%H%M`.csv
脚本演示效果如下:
如上提示结果,有ERROR错误提示,是因为当前系统已经有重名的Docker,但是IP地址不同,可以通过修改脚本避免该问题!没有任何报错,则创建Docker成功!
Docker虚拟化技术系列之-自动化部署管理
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。