Github : github.com/black-ant
CASE 备份 : gitee.com/antblack/ca…
每次换便宜的服务器都要导致环境重新部署一遍,十分麻烦。
于是把每次部署的脚本整理了一下,方便在服务器上快速进行部署。
基于 CentOS~~~
通常情况下 yum 是默认集成的,如果出现 yum 未集成的情况,按照如下步骤进行安装 :
sudo yum version
// S2 : 安装 epel-release软件仓库
sudo yum install epel-release
// S3 : 安装 yum
sudo yum install yum
// S4 : 更新 yum
sudo yum update
// 其他问题 :
failovermethod=priority in /etc/yum.repos.d/CentOS-Epel.repo;
Configuration: OptionBinding with id “failovermethod” does not exist
– 1: sudo vim /etc/yum.repos.d/CentOS-Epel.repo
– 2: 注释掉 # failovermethod=priority
– 3: sudo yum update 更新系统
Failed to download metadata for repo ‘appstream’:
Cannot prepare internal mirrorlist: No URLs in mirrorlist
– 由于 centOS 停止维护带来的问题,需要修改 mirro地址
– 1: 进入 repo 管理 cd /etc/yum.repos.d/
– 2: 修改全局地址
– sed -i ‘s/mirrorlist/#mirrorlist/g’ /etc/yum.repos.d/CentOS-*
– sed -i ‘s|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g’ /etc/yum.repos.d/CentOS-*
– 3:更新缓存 : yum makecache
– 4:更新 yum : yum update -y
Yum 版本
yum search java|grep jdk
yum search java-1.8.0-openjdk
// S2 : 安装 JDK
sudo yum install java-1.8.0-openjdk-devel.x86_64
// S3 : 查询 JDK 安装情况
java -version
yum list installed | grep docker
// 安装必要的软件包
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
// 添加 docker 源 (选择合适的源,以下选其一即可,选错可能会 timeout)
sudo yum-config-manager –add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum-config-manager –add-repo=http://mirrors.tencent.com/docker-ce/linux/centos/docker-ce.repo
sudo yum-config-manager –add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
// 安装docker
sudo yum install docker-ce
// 启动docker
sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl restart docker
// 查看docker服务状态
systemctl status docker
sudo curl -L “https://github.com/docker/compose/releases/download/v2.2.2/docker-compose-$(uname -s)-$(uname -m)” -o /usr/local/bin/docker-compose
// 准备处理文件夹
sudo chmod +x /usr/local/bin/docker-compose
但是,众所周知的原因,Github 访问不是很快
pip3 install -U pip setuptools
pip3 install docker-compose
// 查看 docker compose 版本
docker-compose –version
不是所有的服务器都需要配置这个,腾讯云的默认好像就是关闭的,阿里轻量好像要配置
# 默认防火墙 是关闭的 (not running)
firewall-cmd –state
# 启动防火墙
# 没有任何提示 则启动成功
systemctl start firewalld
# 添加规则 (–permanent 代表重启也生效)
# 提示 success 则成功
firewall-cmd –permanent –zone=public –add-port=8848/tcp
firewall-cmd –permanent –zone=public –add-port=443/tcp
firewall-cmd –permanent –zone=public –add-port=3336/tcp
firewall-cmd –permanent –zone=public –add-port=5601/tcp
firewall-cmd –permanent –zone=public –add-port=8443/tcp
// 查看防火墙规则
# 重新加载防火墙配置
# 提示 success 则成功
firewall-cmd –reload
# 至此结束
# 查看所有打开的端口
firewall-cmd –zone=public –list-ports
firewall-cmd –list-all
# 关闭防火墙
systemctl stop firewalld
yum install curl
// git
yum -y install git
ssh-keygen -t rsa -C “test123123@qq.com”
mkdir /home/rocket
// 2. 创建 docker compose
mkdir rocketmq-docker
cd rocketmq-docker
touch docker-compose.yml
// 3. 编辑 docker-compose.yml
version: ‘3’
services:
namesrv:
image: rocketmqinc/rocketmq:latest
container_name: rocketmq-namesrv
command: sh mqnamesrv
ports:
– “9876:9876”
volumes:
– https://www.jb51.net/article/data/namesrv/logs:/root/logs
broker:
image: rocketmqinc/rocketmq:latest
container_name: rocketmq-broker
command: sh mqbroker -c /rocketmq/broker.conf
depends_on:
– namesrv
ports:
– “10909:10909”
– “10911:10911”
– “10001:10001”
– “10002:10002”
– “8080:8080”
environment:
NAMESRV_ADDR: namesrv:9876
volumes:
– https://www.jb51.net/article/data/broker/logs:/root/logs
– https://www.jb51.net/article/broker.conf:/rocketmq/broker.conf
// 4. 创建 broker.conf
brokerClusterName=DefaultCluster
brokerName=broker-a
brokerId=0
deleteWhen=04
fileReservedTime=48
brokerRole=ASYNC_MASTER
flushDiskType=ASYNC_FLUSH
listenPort=10911
storePathRootDir=/root/store
storePathCommitLog=/root/store/commitlog
autoCreateTopicEnable=true
brokerIP1=10.10.101.80
// 5. 运行 docker
docker-compose up -d
// 查询整体运行情况
docker ps
// 发送消息
补充要点 :
https://hub.docker.com/r/rocketmqinc/rocketmq/tags
// broker.conf 中需要配置你的宿主机IP,否则 nameserver 拿到的会有问题
安装 docker console
unzip rocketmq-all-4.8.0-bin-release.zip
cd rocketmq-all-4.8.0-bin-release/bin
// 使用
sh mqadmin sendMessage -n localhost:9876 -t testTopic -p “Hello, RocketMQ!”
安装 RocketMQ Console
https://github.com/apache/rocketmq-dashboard.git
// 注意添加 入站规则
TCP:5601,9876,10909,10911,10001,10002
yum install redis
// S2 : 允许远程访问
vi /etc/redis.conf
bind 127.0.0.1 -> 加上 # 注释
protected-mode yes 改为 protected-mode no
// S3 : 启动redis
service redis start
//————————
// 停止redis
service redis stop
// 查看redis运行状态
service redis status
// 查看redis进程
ps -ef | grep redis
图方便短期用直接选择 Docker ,长期用慎选,镜像没选好很容易变肉鸡。
docker pull mysql
// S2 : 运行容器 (密码适当复杂点)
docker run -d –name antMySQL -e MYSQL_ROOT_PASSWORD=test9786366 -p 3306:3306 mysql
// S3 : 查看运行情况
docker ps
docker exec -it antMySQL bash
mysql -uroot -ptest9786366
S1 : 准备 docker-compose.yml
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.12.0
container_name: elasticsearch
environment:
– ‘cluster.name=elasticsearch’
– ‘discovery.type=single-node’
– ‘ES_JAVA_OPTS=-Xms1024m -Xmx1024m’
volumes:
– esdata:/usr/share/elasticsearch/data
ports:
– ‘9200:9200’
– ‘9300:9300’
networks:
– elk
kibana:
image: docker.elastic.co/kibana/kibana:7.12.0
container_name: kibana
volumes:
– /etc/localtime:/etc/localtime
– /home/es/kibana.yml:/usr/share/kibana/config/kibana.yml
links:
– elasticsearch:es
environment:
– ELASTICSEARCH_URL=http://81.22.11.111:9200
– ‘elasticsearch.hosts=http://es:9200’
– I18N_LOCALE=zh-CN
ports:
– ‘5601:5601’
networks:
– elk
depends_on:
– elasticsearch
networks:
elk:
name: elk
driver:
bridge
volumes:
esdata:
driver: local
S2 : 准备 kibana.yml
server.host: ‘0.0.0.0’
elasticsearch.hosts: [‘http://11.22.33.111:9200’]
monitoring.ui.container.elasticsearch.enabled: true
S3 : 启动
docker-compose –compatibility up -d
https://blog.51cto.com/u_14129797/5694302
这里还是使用 docker 安装,包含各种监控插件 @参考地址
mkdir nacos-docker
cd nacos-docker
touch docker-compose.yml
// S2 : 配置 docker-compose
找了很多案例,这一个应该是最完整的,提供了 prometheus 和 grafana. 不过里面有些东西已经找不到了,这里补充下 :
@ docker-compose部署nacos单机版(简洁优化版)
services:
nacos:
image: nacos/nacos-server:2.0.4
container_name: nacos
restart: always
environment:
PREFER_HOST_MODE: hostname #如果支持主机名可以使用hostname,否则使用ip,默认也是ip
SPRING_DATASOURCE_PLATFORM: mysql #数据源平台 仅支持mysql或不保存empty
MODE: standalone
MYSQL_SERVICE_HOST: 127.0.0.1
MYSQL_SERVICE_DB_NAME: nacos
MYSQL_SERVICE_PORT: 3306
MYSQL_SERVICE_USER: root
MYSQL_SERVICE_PASSWORD: root
NACOS_APPLICATION_PORT: 8848
JVM_XMS: 512m
JVM_MMS: 320m
volumes:
– https://www.jb51.net/article/docker/nacos/standalone-logs/:/home/nacos/logs
– https://www.jb51.net/article/docker/nacos/plugins/:/home/nacos/plugins
– https://www.jb51.net/article/docker/nacos/conf/application.properties:/home/nacos-docker/application.properties
ports:
– “8848:8848”
prometheus:
container_name: prometheus
image: prom/prometheus:latest
volumes:
– https://www.jb51.net/article/docker/nacos/prometheus/prometheus-standalone.yaml:/etc/prometheus/prometheus.yml
ports:
– “9090:9090”
depends_on:
– nacos
restart: on-failure
grafana:
container_name: grafana
image: grafana/grafana:latest
ports:
– 3000:3000
restart: on-failure
这里还可以编排mysql进去,已经有数据库了,这里就不要了不需要 prometheus 和 grafana 只取 nacos 即可application.properties 我给的空的
prometheus-standalone.yaml
scrape_interval: 15s #拉取 targets 的默认时间间隔
#scrape_timeout: 10s #拉取一个 target 的超时时间。
evaluation_interval: 15s #执行 rules 的时间间隔。
#external_labels: #额外的属性,会添加到拉取的数据并存到数据库中。
# Alertmanager configuration
alerting:
alertmanagers:
– static_configs:
– targets:
# – alertmanager:9093
rule_files:
# – “first_rules.yml”
# – “second_rules.yml”
# A scrape configuration containing exactly one endpoint to scrape:
# Here it’s Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
– job_name: ‘prometheus’
scheme: http
# metrics_path defaults to ‘/metrics’
# scheme defaults to ‘http’.
static_configs: #因为使用了网桥所以targets可以直接使用容器名而不用ip
– targets: [‘prometheus:9090’]
– job_name: ‘nacos’
metrics_path: ‘/nacos/monitor/prometheus’
static_configs:
– targets: [‘nacos:8848’]
除了 2.0.4 还有很多其他的版本 nacos/nacos-server Tags | Docker Hub , 这里我对这个版本比较熟悉
docker-compose –compatibility up -d
// S4 : 查看结果
docker-compose ps
docker-compose stop
docker-compose down: 停止并删除应用程序的容器
docker-compose ps: 显示当前正在运行的应用程序的容器状态
docker-compose logs: 显示应用程序的容器日志
docker-compose build: 构建应用程序的镜像
docker-compose restart: 重启应用程序的容器
docker-compose exec: 在容器中执行命令
docker-compose stop: 停止应用程序的容器
docker-compose rm: 删除已停止的应用程序的容器
docker search nacos
基本上常用的就在这里了,其他的以后碰到了再补充进去.
其他比较复杂的就是 jenkins ,K8S 这些,安装可能就不是走 yum了,会稍微复杂一些。有时间单章来发,更多关于Linux项目部署的资料请关注脚本之家其它相关文章!