创建文件
随便找个目录创建一个start.sh 并且给这个start.sh添加可执行权限
cd ~
touch start.sh
chmod u+x start.sh
添加内容
给start.sh添加如下内容
#!/bin/bash
MySqlPassword="123456"
DockerContainerPath="/usr/docker"
updateYum() {
echo "开始更新yum"
yum -y update
yum install -y yum-utils
initSystem
echo "yum更新完成"
}
read -t 30 -p "是否更新yum? Y/N " updateYum
yum_lower=$(echo "$updateYum" | tr '[:upper:]' '[:lower:]')
if [ "$yum_lower" = "y" ]; then
updateYum
fi
getLocalIp() {
HOST_IF=$(ip route | grep default | head -n1 | cut -d' ' -f5)
HOST_IP=$(ip a | grep "$HOST_IF$" | head -n1 | awk '{print $2}' | cut -d'/' -f1)
echo "$HOST_IP"
}
createCustomNetwork() {
if ! docker network inspect myNetwork &>/dev/null; then
docker network create myNetwork
fi
}
initSystem() {
yum -y install gcc gcc-c++ autoconf automake make
yum -y install pcre-devel
yum -y install openssl openssl-devel
yum -y install zlib zlib-devel
yum -y install wget
yum -y install net-tools
yum -y install lrzsz
}
removeDocker() {
echo "开始卸载旧版本docker"
yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-selinux docker-engine-selinux docker-engine docker-ce
echo "旧版本docker卸载完成!"
}
installDocker() {
yum install -y yum-utils
if command -v docker &>/dev/null; then
docker_version=$(docker -v)
echo "docker版本: $docker_version"
else
echo "docker未安装"
removeDocker
echo "开始安装docker!"
# 阿里云
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 清华
yum-config-manager --add-repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
# 官方
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum -y install docker-ce docker-ce-cli containerd.io docker-compose-plugin
docker_version=$(docker -v)
if [ -n "$docker_version" ]; then
echo "docker安装完成"
echo "启动docker服务"
sudo systemctl start docker
sudo systemctl enable docker
echo "docker启动完成"
echo "开始安装docker-compose"
curl -L https://github.com/docker/compose/releases/download/v2.2.2/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose -v
echo "安装docker-compose完成"
else
echo "docker安装失败"
fi
fi
}
# mysql-server
installMysqlWithDocker() {
mkdir -p "$DockerContainerPath"/mysql/{conf,logs,data}
docker pull mysql
docker run -p 3306:3306 --name mysql \
--restart always \
-v "$DockerContainerPath"/mysql/conf:/etc/mysql/conf.d \
-v "$DockerContainerPath"/mysql/logs:/logs \
-v "$DockerContainerPath"/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=$MySqlPassword -d mysql
docker start mysql
}
# redis-server
installRedisWithDocker() {
mkdir -p /usr/docker/redis/data
docker run -p 6379:6379 --name redis \
--restart=always \
-v "$DockerContainerPath"/redis/redis.conf:/etc/redis/redis.conf \
-v "$DockerContainerPath"/redis/data:/data \
-d redis redis-server /etc/redis/redis.conf --appendonly yes
}
#mongo-server
installMongoWithDocker() {
mkdir -p "$DockerContainerPath"/mongodb/{data,backup}
docker run --name mongo -p 27017:27017 \
--restart=always \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=123456 \
-v "$DockerContainerPath"/mongodb/data:/data/db \
-v "$DockerContainerPath"/mongodb/backup:/data/backup -d mongo mongod --auth
}
installEmqxWithDocker() {
docker pull emqx/emqx
docker run -dit --name emqx \
-p 18083:18083 \
-p 1883:1883 \
-p 8083:8083 \
-p 8084:8084 \
emqx/emqx:latest
}
#portainer docker管理工具
installPortainerWithDocker() {
mkdir -p "$DockerContainerPath"/portainer/data
docker pull portainer/portainer
docker run -p 9000:9000 -p 8000:8000 --name portainer \
--restart=always \
-v /var/run/docker.sock:/var/run/docker.sock \
-v "$DockerContainerPath"/portainer/data:/data \
-d portainer/portainer
echo "docker管理工具:http://$(getLocalIp):/9000"
}
#常用工具工具
installItToolsWithDocker() {
docker run -d \
--name ittools \
--restart=always \
-p 7071:80 \
corentinth/it-tools:latest
echo "常用开发工具:http://$(getLocalIp):7071"
}
installJenkinsWithDocker() {
mkdir -p "$DockerContainerPath"/jenkins
docker run -d --restart=always -uroot \
-p 9095:8080 \
-p 50000:50000 \
--name jenkins \
-e TZ=Asia/Shanghai \
-v "$DockerContainerPath"/jenkins:/var/jenkins_home \
-v /etc/localtime:/etc/localtime \
-v /usr/soft/maven/apache-maven-3.9.5:/usr/local/apache-maven-3.9.5 \
-v /usr/soft/java/usr/soft/java/jdk-11.0.19:/usr/local/jdk-11.0.19 \
jenkins/jenkins
}
installMiniIoDocker() {
mkdir -p "$DockerContainerPath"/miniIo/data
docker run -p 9000:9000 -p 9090:9090 \
--name minio \
-d --restart=always \
-e "MINIO_ACCESS_KEY=minioadmin" \
-e "MINIO_SECRET_KEY=minioadmin" \
-v "$DockerContainerPath"/minio/data:/data \
minio/minio server \
/data --console-address ":9090" -address ":9000"
echo "miniIo -account:minioadmin"
echo "miniIo -password:minioadmin"
echo "miniIo -url:http://$(getLocalIp):9090/login"
}
installTdEnginDocker() {
mkdir -p "$DockerContainerPath"/tdengine/{lib,log}
docker run -p 6030:6030 -p 6041:6041 -p 6043-6049:6043-6049 -p 6043-6049:6043-6049/udp \
--name tdengine \
-d --restart=always \
-v /etc/localtime:/etc/localtime:ro \
-v "$DockerContainerPath"/tdengine/lib:/var/lib/taos:Z \
-v "$DockerContainerPath"/tdengine/log:/var/log/taos:Z \
tdengine/tdengine
echo "tdengine -port:6041"
echo "tdengine -account:root"
echo "tdengine -password:taosdata"
}
installXXLJOBDocker() {
echo "正在拉取xxl-job-admin Docker镜像..."
docker pull xuxueli/xxl-job-admin:2.4.0
mkdir -p "$DockerContainerPath"/xxl-job/{lib,log}
read -p "请输入MySQL IP地址 (留空则默认为127.0.0.1): " mysql_ip
mysql_ip=${mysql_ip:-127.0.0.1}
read -p "请输入MySQL端口 (留空则默认为3306): " db_port
db_port=${db_port:-3306}
read -p "请输入MySQL用户名: " db_user
read -s -p "请输入MySQL密码: " db_password
echo
docker run -p 8081:8080 \
--name xxl-job-admin \
-e PARAMS="--spring.datasource.url=jdbc:mysql://$mysql_ip:$db_port/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai --spring.datasource.username=$db_user --spring.datasource.password=$db_password" \
-d --restart=always \
-v "$DockerContainerPath"/xxl-job/log:/data/applogs \
xuxueli/xxl-job-admin:2.4.0
echo "XXL-JOB Admin version: 2.4.0 已部署成功."
echo "XXL-JOB Admin URL: http://$(getLocalIp):8081/xxl-job-admin"
}
installRocketMqDocker() {
echo "开始安装rocketmq NameServer..."
installRocketMqNameServerDocker
echo "开始创建rocketmq配置文件..."
createRocketMqConfig
echo "开始安装rocketmq broker..."
installRocketMqBrokerDocker
echo "开始安装rocketmq 控制台..."
installRocketMqConsoleDocker
}
installRocketMqNameServerDocker() {
mkdir -p "$DockerContainerPath"/rocketmq/nameserver/{logs,store}
docker run -d --restart=always \
--name rmqnamesrv --privileged=true \
-p 9876:9876 \
-v "$DockerContainerPath"/rocketmq/nameserver/logs:/root/logs \
-v "$DockerContainerPath"/rocketmq/nameserver/store:/root/store \
-e "MAX_POSSIBLE_HEAP=100000000" \
rocketmqinc/rocketmq \
sh mqnamesrv
}
createRocketMqConfig() {
mkdir -p "$DockerContainerPath"/rocketmq/conf
# 配置文件路径
CONFIG_FILE="$DockerContainerPath"/rocketmq/conf/broker.conf # 检查配置文件目录是否存在,如果不存在则创建
if [ ! -d "$(dirname "$CONFIG_FILE")" ]; then
mkdir -p "$(dirname "$CONFIG_FILE")"
fi # 将配置信息写入配置文件
{
echo "# 所属集群名称,如果节点较多可以配置多个"
echo "brokerClusterName=DefaultCluster"
echo "# broker名称,master和slave使用相同的名称,表明他们的主从关系"
echo "brokerName=broker-a"
echo "# 0表示Master,大于0表示不同的slave"
echo "brokerId=0"
echo "# 表示凌晨几点做消息删除动作,默认是凌晨4点"
echo "deleteWhen=04"
echo "# 在磁盘上保留消息的时长,单位是小时"
echo "fileReservedTime=48"
echo "# 有三个值:SYNC_MASTER,ASYNC_MASTER,SLAVE;同步和异步表示Master和Slave之间同步数据的机制;"
echo "brokerRole=ASYNC_MASTER"
echo "# 刷盘策略,取值为:ASYNC_FLUSH,SYNC_FLUSH表示同步刷盘和异步刷盘;SYNC_FLUSH消息写入磁盘后才返回成功状态,ASYNC_FLUSH不需要;"
echo "flushDiskType=ASYNC_FLUSH"
echo "# 设置broker节点所在服务器的ip地址"
echo "brokerIP1=$(getLocalIp)"
echo "# 磁盘使用达到95%之后,生产者再写入消息会报错 CODE: 14 DESC: service not available now, maybe disk full"
echo "diskMaxUsedSpaceRatio=95"
} > "$CONFIG_FILE"
echo "配置已写入到 $CONFIG_FILE"
}
installRocketMqBrokerDocker() {
mkdir -p "$DockerContainerPath"/rocketmq/broker/{logs,store}
docker run -d --restart=always \
--name rmqbroker \
--link rmqnamesrv:namesrv \
-p 10911:10911 \
-p 10909:10909 \
--privileged=true \
-v "$DockerContainerPath"/rocketmq/broker/logs:/root/logs \
-v "$DockerContainerPath"/rocketmq/store:/root/store \
-v "$DockerContainerPath"/rocketmq/conf/broker.conf:/opt/docker/rocketmq/broker.conf \
-e "NAMESRV_ADDR=namesrv:9876" \
-e "MAX_POSSIBLE_HEAP=200000000" \
rocketmqinc/rocketmq \
sh mqbroker -c /opt/docker/rocketmq/broker.conf
}
installRocketMqConsoleDocker() {
docker run -d \
--restart=always \
--name rmqadmin \
-e "JAVA_OPTS=-Drocketmq.namesrv.addr=$(getLocalIp):9876 \
-Dcom.rocketmq.sendMessageWithVIPChannel=false" \
-p 9999:8080 \
pangliang/rocketmq-console-ng
echo "rocket mq console addr: http://$(getLocalIp):9999/"
}
installServiceInDocker() {
local serviceName=$1
local dockerImage=$2
local params=("${@:3}")
echo "Installing $serviceName with Docker..."
# Create a custom network for the container
createCustomNetwork
docker run -d --name $serviceName --network myNetwork "${params[@]}" $dockerImage
echo "$serviceName installed."
}
choiceService() {
selected_services=() # 存储用户选择的服务的数组
# 服务菜单
PS3="请选择要安装的服务 (使用数字选择多个服务,使用空格分隔,然后按Enter): "
options=("Redis" "MySQL" "Apache" "Mongo" "EMQX" "Portainer" "Jenkins" "ItTools" "MiniIo" "Tdengine" "XXL-JOB" "ROCKET-MQ" "完成")
select choice in "${options[@]}"; do
echo "$choice"
IFS=', ' read -ra choices <<<"$REPLY" # 以空格分隔多个数字
for c in "${choices[@]}"; do
if [[ "$c" =~ ^[0-9]+$ ]]; then
selected_services+=("${options[c - 1]}")
else
echo "无效选项,请选择菜单中的服务,使用数字选择多个服务,使用空格分隔。(Enter继续)"
fi
done
if [ ${#selected_services[@]} -eq 0 ]; then
echo "请选择至少一个服务。"
else
break 2
fi
done
# 打印用户选择的服务
echo "您选择的服务: ${selected_services[@]}"
#执行用户选择的服务的安装
for service in "${selected_services[@]}"; do
case $service in
"Redis")
echo "开始安装 Redis"
installRedisWithDocker
;;
"MySQL")
echo "开始安装 MySQL"
# installServiceInDocker "mysql" "mysql:latest" "-e MYSQL_ROOT_PASSWORD=your_password"
installMysqlWithDocker
;;
"Apache")
echo "开始安装 Apache"
# 在这里添加安装 Apache 的命令
;;
"Mongo")
echo "开始安装 Mongo"
installMongoWithDocker
;;
"EMQX")
echo "开始安装 EMQX"
installEmqxWithDocker
;;
"Jenkin")
echo "开始安装 Jenkins"
installJenkinsWithDocker
;;
"Portainer")
echo "开始安装 Portainer docker管理工具"
installPortainerWithDocker
;;
"ItTools")
echo "开始安装 ItTools工具"
installItToolsWithDocker
;;
"MiniIo")
echo "开始安装 MiniIo 管理工具"
installMiniIoDocker
;;
"Tdengine")
echo "开始安装 Tdengine"
installTdEnginDocker
;;
"XXL-JOB")
echo "开始安装 XXL-JOB"
installXXLJOBDocker
;;
"ROCKET-MQ")
echo "开始安装 ROCKET-MQ"
installRocketMqDocker
;;
esac
done
}
read -t 30 -p "所有服务都是基于docker安装 是否同意? Y/N " installDocker
install_docker_answer=$(echo "$installDocker" | tr '[:upper:]' '[:lower:]')
if [ "$install_docker_answer" = "y" ]; then
installDocker
choiceService
else
echo "命令结束."
fi
保存并执行
./start.sh