docker初始化设置

#!/bin/bash

#descrition:
#配置Docker运行环境、logstash环境,并启动。支持docker version 1.11.2、1.12.1
#The log output file:/var/log/docker-env-init.log
#
#precondition:mir envionment variables example
#DOCKER_IMAGE_SERVER_IP=172.16.80.94
#DOCKER_IMAGE_SERVER_NAME=hub.chinacloud.com
#LOGSTORE_ID=0672ac9c-c328-496a-bddc-47bb93c8bc12
#ONELOG_SERVER_IP=172.16.81.41
#ONELOG_SERVER_PORT=4564
#ENV_ETCD_SERVER=172.16.70.73:2379
#公安ntp server
#NTP_SERVER_IP=10.64.1.45
#
#steps:
#1.设置VM DNS
#2.设置Docker dns & 私有仓库
#3.重启docker
#4.设置logstash并启动

VM_IP=""

#$1:$LINENO
#$2:ERROR/DEBUG/INFO
#$3:MSG
log(){
	#Time=`date "+%Y-%m-%d %H:%M:%S"`
	currentTimeStamp=`date +%s`
	echo "$currentTimeStamp sh:docker-env-init.sh lineNo:$1 $2::$3" >> /var/log/docker-env-init.log
}

confVmEnvironment (){
	iptables -F
	
	#方案一 修改/etc/resolv.conf===暂时不用
	#sed -i "/nameserver /d" /etc/resolv.conf
	#echo "nameserver $DOCKER_IMAGE_SERVER_IP" >> /etc/resolv.conf
	#方案二 设置/etc/hosts
	sed -i "/$DOCKER_IMAGE_SERVER_NAME /d" /etc/hosts
	echo "$DOCKER_IMAGE_SERVER_IP  $DOCKER_IMAGE_SERVER_NAME" >> /etc/hosts
	
	#设置时钟同步
	if [ "$NTP_SERVER_IP" != "" ]
	then
		echo "*/30 * * * * /usr/sbin/ntpdate $NTP_SERVER_IP && /sbin/hwclock -w" >> /var/spool/cron/root
		log "$LINENO" "DEBUG" "Set the NTP clock synchronization > ntp server:$NTP_SERVER_IP"
	fi
	
	#获取虚机IP && 设置logstash配置参数
	#sed "49,52/^/#/" /opt/logstash-2.3.3/conf/docker_service.conf
	sed -i "s/{ONELOG_SERVER_IP}/$ONELOG_SERVER_IP/g" /opt/logstash-2.3.3/conf/docker_service.conf
	if [ "$SCALR_EXTERNAL_IP" != "" ]
	then
		VM_IP=$SCALR_EXTERNAL_IP
	elif [ "$SCALR_INTERNAL_IP" != "" ]
	then
		#解决公安内网只有SCALR_INTERNAL_IP问题
		sed -i "s/\${SCALR_EXTERNAL_IP}/\${SCALR_INTERNAL_IP}/g" /opt/logstash-2.3.3/conf/docker_service.conf
		VM_IP=$SCALR_INTERNAL_IP
	fi
	log "$LINENO" "DEBUG" "VM_IP is $VM_IP"
	
}

confDocker(){
	DOCKER_VERSION=`docker -v`
	log "$LINENO" "DEBUG" "$DOCKER_VERSION."
	
	DOCKER_SERVICE_CONF_UPDATE_FLAG=`grep insecure-registry /usr/lib/systemd/system/docker.service`
	if [ "$DOCKER_SERVICE_CONF_UPDATE_FLAG" = "" ]
	then 
		[[ $DOCKER_VERSION =~ "1.11.2" ]] && sed -i "s#ExecStart=/usr/bin/docker daemon -H fd://#ExecStart=/usr/bin/docker daemon -H fd:// -H=unix:///var/run/docker.sock -H=0.0.0.0:2376 --insecure-registry=$DOCKER_IMAGE_SERVER_IP --insecure-registry=$DOCKER_IMAGE_SERVER_NAME#" /usr/lib/systemd/system/docker.service
		
		[[ $DOCKER_VERSION =~ "1.12.1" ]] && sed -i "s#ExecStart=/usr/bin/dockerd#ExecStart=/usr/bin/dockerd -H=unix:///var/run/docker.sock -H=0.0.0.0:2376 --insecure-registry=$DOCKER_IMAGE_SERVER_IP --insecure-registry=$DOCKER_IMAGE_SERVER_NAME#" /usr/lib/systemd/system/docker.service
	fi
}

restartDocker(){
	systemctl daemon-reload
	systemctl stop docker
	systemctl start docker
	#systemctl restart docker
}


main() {
	true > /var/log/docker-env-init.log
	log "$LINENO" "DEBUG" "shell-start.Began to initialize the docker environment."
	log "$LINENO" "DEBUG" "envionment variables[DOCKER_IMAGE_SERVER_IP=$DOCKER_IMAGE_SERVER_IP ONELOG_SERVER_IP=$ONELOG_SERVER_IP ONELOG_SERVER_PORT=$ONELOG_SERVER_PORT LOGSTORE_ID=$LOGSTORE_ID]"
	
	######1.设置VM环境#####
	confVmEnvironment
	log "$LINENO" "DEBUG" "step-1 Configuration environment successfully."
	
	######2.设置Docker dns & 私有仓库#####
	confDocker
	log "$LINENO" "DEBUG" "step-2 Configuration docker insecure-registry successfully.. >> /usr/lib/systemd/system/docker.service"
	
	######3.重启docker######
	restartDocker
	log "$LINENO" "DEBUG" "step-3 Docker restart the success."
	
	######4.启动registrator容器
	#log "$LINENO" "DEBUG" "docker pull gliderlabs/registrator:latest"
	#docker pull gliderlabs/registrator:latest
	#IMAGE_ID=`docker images | grep gliderlabs/registrator |grep latest | awk '{print $3}'`
	#if [ "$IMAGE_ID" = "" ]
	#then
	#	log "$LINENO" "ERROR" "step-4 registrator image download failure."
	#	exit 1
	#else
	#	log "$LINENO" "DEBUG" "Status:Downloaded newer image for gliderlabs/registrator:latest"
	#fi
	
	log "$LINENO" "DEBUG" "docker rm -f registrator"
	docker rm -f registrator
	#启动注册器,用于监听容器状态 -l namespace=system 表示不用删除该容器
	log "$LINENO" "DEBUG" "docker run -d --name=registrator --net=host -P --volume=/var/run/docker.sock:/tmp/docker.sock -l namespace=system --restart always gliderlabs/registrator:latest -ip=$VM_IP -ttl=90 -ttl-refresh=60 etcd://$ENV_ETCD_SERVER"
	docker run -d --name=registrator  --net=host -P --volume=/var/run/docker.sock:/tmp/docker.sock -l namespace=system --restart always gliderlabs/registrator:latest -ip=$VM_IP -ttl=90 -ttl-refresh=60 etcd://$ENV_ETCD_SERVER
	CONTAINER_ID=`docker ps |grep registrator |grep gliderlabs/registrator:latest | awk '{print $1}'`
	if [ "$CONTAINER_ID" = "" ]
	then
		log "$LINENO" "ERROR" "step-4 Failed to start the container."
		exit 1
	else
		log "$LINENO" "DEBUG" "step-4 Start registrator container successfully.CONTAINER_ID > $CONTAINER_ID"
	fi
	
	
	######5.启动logstash######
	nohup /opt/logstash-2.3.3/bin/logstash -f /opt/logstash-2.3.3/conf/docker_service.conf --allow-env &
	log "$LINENO" "DEBUG" "step-5 Logstash starting success."
	log "$LINENO" "DEBUG" "shell-finished.Initialize the docker environment successfully concluded."
	
}

main

你可能感兴趣的:(脚本库,Shell编程)