jenkins自动化搭建-redis集群

jenkins的部署请参考以前的文章
Linux下用tomcat部署jenkins
部署jenkins的机器当做推送机对远程部署的机器进行免密,请参考以前的文章
Linux下实现免密登录
编写shell自动化搭建-redis集群脚本,脚本内容如下
redis_create_remote_sync.sh

#!/bin/bash

#init param(sync tar.gz)
remoteTargetPath=$1
redisClusterIp=$2
redisPort=$3

sourcePath=/root/tools/redis/redis-6.2.6.tar.gz
shellFileName=redis_cluster_auto_config.sh
shellPath=/root/tools/shell/redis/remote/jenkins/$shellFileName

function remote_sync(){
	#IFS  Internal Field Separator是shell脚本中特殊变量,默认分隔符为空格
	OLD_IFS=$IFS 
	IFS=,
	arrayIp=($redisClusterIp) 
	IFS=$OLD_IFS

        for ip in ${arrayIp[@]}
	do
	{
		#remote create targetPath
		ssh root@$ip "rm -rf $remoteTargetPath && mkdir -p $remoteTargetPath"

		#sync tar.gz to remote linux
		scp $sourcePath root@$ip:$remoteTargetPath
		ssh root@$ip "cd $remoteTargetPath && tar -zxvf *.tar.gz && rm -rf *.tar.gz && mv * redis"  

		#sync shell to remote linux for exec
		scp $shellPath root@$ip:$remoteTargetPath

		#exec remote shell
		ssh root@$ip "chmod 755 $remoteTargetPath/$shellFileName"
		ssh root@$ip "sh $remoteTargetPath/$shellFileName $remoteTargetPath/redis ${arrayIp[0]} $redisPort"
	}&
	done
	wait
}

remote_sync

redis_cluster_auto_config.sh

#!/bin/bash
source /etc/profile

#init param
redisPath=$1
redisMasterIp=$2
redisPort=$3

function redis_server_install()
{
	cd $redisPath
	make && make install
	yes | cp $redisPath/redis.conf /etc/redis.conf
}

function redis_server_config()
{
	#config NETWORK
        sed -i 's/bind 127.0.0.1 -::1/bind 0.0.0.0/'  /etc/redis.conf

	#config port
        sed -i 's/port 6379/port '$redisPort'/g' /etc/redis.conf
        
        #config SNAPSHOTTING
        sed -i 's/daemonize no/daemonize yes/g'  /etc/redis.conf
        sed -i 's/# save 3600 1/save 3600 1/g'  /etc/redis.conf

	#config slave bind master ip and port
	localIp=$(ifconfig -a | grep inet | grep -v 127.0.0.1 | grep -v inet6 | awk '{print $2}')

	if [ $redisMasterIp != $localIp ]; then
		sed -i 's/# replicaof <masterip> <masterport>/replicaof '$redisMasterIp' '$redisPort'/g' /etc/redis.conf
	fi

	#config mem
	memFree=$(awk '($1 == "MemFree:"){print $2*1024}' /proc/meminfo)
        buffers=$(awk '($1 == "Buffers:"){print $2*1024}' /proc/meminfo)
        cached=$(awk '($1 == "Cached:"){print $2*1024}' /proc/meminfo)
        totalMem=`echo "$memFree + $buffers + $cached" | bc`
        redisMem=`echo "scale=0; $totalMem/2" | bc`
        sed -i 's/# maxmemory <bytes>/maxmemory '$redisMem'/g'  /etc/redis.conf	
}

function redis_server_start(){
	#server start
	pidCount=$(lsof -i:$redisPort | grep -v COMMAND | grep LISTEN | wc -l)
	if [  $pidCount -eq 1 ];then
		lsof -i:$redisPort |grep -v COMMAND|grep LISTEN | cut -c 9-16 |  xargs kill -9 > /dev/null 2>&1
	fi

	redis-server /etc/redis.conf &
}

function redis_server_monitor(){
	#server monitor
	localIp=$(ifconfig -a | grep inet | grep -v 127.0.0.1 | grep -v inet6 | awk '{print $2}')	

        if [ $localIp == $redisMasterIp ];then
                masterCount=$(redis-cli -p $redisPort info | grep role:master | wc -l)
		if [  $masterCount -eq 1 ];then
			echo "redis master已经启动"
        	fi
        else
		slaveCount=$(redis-cli -p $redisPort info | grep role:slave | wc -l)
        	if [  $slaveCount -eq 1 ];then
                        echo "redis slave已经启动"
                fi
	fi
}

redis_server_install
redis_server_config
redis_server_start
redis_server_monitor

将脚本放在指定目录/root/tools/shell/redis/remote/jenkins下
将redis-6.2.6.tar.gz安装包放在指定目录/root/tools/redis下
redis-6.2.6.tar.gz安装包下载
链接:https://pan.baidu.com/s/1hUun0hz4uLGjctwqa4yCJg
提取码:cntc
jenkins新建任务步骤如下
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

time sh /root/tools/shell/redis/remote/jenkins/redis_create_remote_sync.sh $remoteTargetPath $redisClusterIp $redisPort

保存后就算完成jenkins的配置了
执行构建任务
在这里插入图片描述在这里插入图片描述查看构建日志
在这里插入图片描述在这里插入图片描述在这里插入图片描述如有控制台中文乱码的话,请参考以前的文章
解决jenkins控制台中文乱码问题

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐