随着企业Docker容器越来越多,如果要靠手工去管理docker,肯定非常吃力,而且也不现实,这就需要我们把日常的操作变成自动化管理,通过脚本工具,将工作减轻到最小,解放运维人员,如下为企业Docker自动化部署脚本V1版本,如果需要更多其他的功能,可以自己定制和修改。
如下自动化管理脚本功能:
1)通过YUM自动安装Docker平台;
2)通过该脚本,可以自动配置桥接网络;
3)自动创建docker容器,基于centos操作系统;
4)自动收集创建好的Docker容器,集中管理;
5)通过PIPEWORK工具手动指定独立IP地址;
概要:该脚本能够自动安装并配置docker虚拟化及桥接网络,同时使用pipework这个软件来配置我们的容器IP,能够实现容器的一个简单的管理:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
#!/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成功!
你若觉得作者的文章写得不错,请奖励作者几锭银子,让作者感受到来自你满满的鼓励和继续创作的动力!我们支持原创!鼓励原创!
打赏之后,文章底部显示参与打赏者的记录,可以直接通过微信扫描打赏哟,直接按打赏根据提示操作。
自定义打赏金额,友情提示打赏金额不要超过50元哦,土豪可以随意哦,51CTO谢谢您对作者的支持!谢谢你的慷慨大方!
本文转自 wgkgood 51CTO博客,原文链接:http://blog.51cto.com/wgkgood/1899883