一、前言
本次使用Apollo V0.11.0版本,基于独立的Eureka注册中心和mariaDB做配置中心的容器化高可用集群部署,本文未涉及mariaDB的主从,有需要的小伙伴请自行做数据库主从。
二、基础环境准备
1)安装java环境
yum install java-devel
2)安装mariaDB
注意:yum install mariadb 这个命令,默认安装的是5.5.56版本,但是Apollo的sql基于高版本创建,故而我们需要安装mariaDB的10.3.4版本
vi /etc/yum.repos.d/mariadb.repo
然后新增如下
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3.4/centos7-amd64/
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
然后安装、启动和设置开机启动
yum -y install mariadb-server
systemctl start mariadb
systemctl enable mariadb
为mariadb配置远程访问权限(初始帐户名root,初始密码为空)
use mysql
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'abcdef' WITH GRANT OPTION;
FLUSH PRIVILEGES;
确认下,可以远程访问了
3)安装maven
注意:不要使用yum install maven,因为默认安装版本是Apache Maven 3.0.5 (Red Hat 3.0.5-17),版本编译apollo时候会出错,所以我们使用3.5.2版本的maven,如果使用yum install安装过的小伙伴,需要卸载原来的maven。
wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache-maven.repo
yum -y install apache-maven
4)搭建自己的Eureka注册中心
自行参考前一篇Blog《高可用注册中心Eureka搭建》
5)下载并解压 apollo
yum -y install wget
yum -y install zip
yum -y install unzip
mkdir /opt/wy
cd /opt/wy
wget https://github.com/ctripcorp/apollo/archive/master.zip
unzip master.zip
三、修改配置及编译
apollo-adminservice 设置端口 为 9001
apollo-configservice 设置端口 为 9002
apollo-portal 设置端口 为 9003
两套apollo分别部署在 192.168.8.235 和192.168.8.237 机器,共用数据库在192.168.8.237。
1)初始化数据库
在/opt/wy/apollo-master/scripts/sql 路径下有两个sql文件(apolloconfigdb.sql apolloportaldb.sql),在我们之前搭建的mariadb中执行恢复。
2)配置数据库中Eureka地址(第二条sql改成自己的ip和端口还有账号密码)
use ApolloConfigDB;
update ServerConfig set Value='http://abc:123@192.168.8.235:8761/eureka,http://abc:123@192.168.8.237:8761/eureka' where ServerConfig.Key = 'eureka.service.url';
3) 192.168.8.237 配置如下
Ⅰ、apollo-adminservice 配置
a) /opt/wy/apollo-master/apollo-adminservice/src/main/docker/Dockerfile
将EXPOSE 8090改为EXPOSE 9001
b) /opt/wy/apollo-master/apollo-adminservice/src/main/resources/adminservice.properties
将server.port= 8090改为server.port= 9001
c)/opt/wy/apollo-master/apollo-adminservice/src/main/resources/application.yml
将server:port: 8090改为server:port: 9001 (注意格式要换行,不要修改原来文件格式,只改端口号,冒号和端口之间还要有空格,因为yml文件有自己的语法要求)
d)/opt/wy/apollo-master/apollo-adminservice/src/main/scripts/startup.sh
将SERVER_PORT=8090改为SERVER_PORT=9001
e)/opt/wy/apollo-master/apollo-adminservice/src/main/resources/bootstrap.yml
新增真实IP,避免docker化后,网卡IP选择错误。
Ⅱ、apollo-configservice配置
a)/opt/wy/apollo-master/apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/ConfigServiceApplication.java
新增一行import如下
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
修改@EnableEurekaServer为@EnableEurekaClient
b)/opt/wy/apollo-master/apollo-configservice/src/main/docker/Dockerfile
将EXPOSE 8080改为EXPOSE 9002
c)/opt/wy/apollo-master/apollo-configservice/src/main/resources/configservice.properties
将server.port= 8080改为server.port= 9002
d)/opt/wy/apollo-master/apollo-configservice/src/main/resources/application.yml
将server:port: 8080改为server:port: 9002 (注意格式要换行,不要修改原来文件格式,只改端口号,冒号和端口之间还要有空格,因为yml文件有自己的语法要求)
e)/opt/wy/apollo-master/apollo-configservice/src/main/scripts/startup.sh
将SERVER_PORT=8080改为SERVER_PORT=9002
f)/opt/wy/apollo-master/apollo-configservice/src/main/resources/bootstrap.yml
新增真实IP,避免docker化后,网卡IP选择错误。(注意对齐和冒号后面的空格)
Ⅲ、apollo-portal设置
a)/opt/wy/apollo-master/apollo-portal/src/main/docker/Dockerfile
将EXPOSE 8080改为EXPOSE 9003
b)/opt/wy/apollo-master/apollo-portal/src/main/resources/application.yml将server:port: 8080改为server:port: 9003 (注意格式要换行,不要修改原来文件格式,只改端口号,冒号和端口之间还要有空格,因为yml文件有自己的语法要求)
c)/opt/wy/apollo-master/apollo-portal/src/main/scripts/startup.sh将SERVER_PORT=8080改为SERVER_PORT=9003
Ⅳ、编译配置
/opt/wy/apollo-master/scripts/build.sh
修改前:
#!/bin/sh
# apollo config db info
apollo_config_db_url=jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8
apollo_config_db_username=root
apollo_config_db_password=
apollo_portal_db_username=root
apollo_portal_db_password=
# meta server url, different environments should have different meta server addresses
dev_meta=http://localhost:8080
pro_meta=http://yetAnotherIp:8080
META_SERVERS_OPTS="-Ddev_meta=$dev_meta -Dfat_meta=$fat_meta -Duat_meta=$uat_meta -Dpro_meta=$pro_meta"
# =============== Please do not modify the following content =============== #
# go to script directory
cd "${0%/*}"
cd ..
# package config-service and admin-service
echo "==== starting to build config-service and admin-service ===="
mvn clean package -DskipTests -pl apollo-configservice,apollo-adminservice -am -Dapollo_profile=github -Dspring_datasource_url=$apollo_config_db_url -Dspring_datasource_username=$apollo_config_db_username -Dspring_datasource_password=$apollo_config_db_password
echo "==== building config-service and admin-service finished ===="
echo "==== starting to build portal ===="
mvn clean package -DskipTests -pl apollo-portal -am -Dapollo_profile=github,auth -Dspring_datasource_url=$apollo_portal_db_url -Dspring_datasource_username=$apollo_portal_db_username -Dspring_datasource_password=$apollo_portal_db_password $META_SERVERS_OPTS
echo "==== building portal finished ===="
echo "==== starting to build client ===="
mvn clean install -DskipTests -pl apollo-client -am $META_SERVERS_OPTS
echo "==== building client finished ===="
修改后
#!/bin/sh
# apollo config db info
apollo_config_db_url=jdbc:mysql://192.168.8.237:3306/ApolloConfigDB?characterEncoding=utf8
apollo_config_db_username=root
apollo_config_db_password=abcdef
# apollo portal db info
apollo_portal_db_url=jdbc:mysql://192.168.8.237:3306/ApolloPortalDB?characterEncoding=utf8
apollo_portal_db_username=root
apollo_portal_db_password=abcdef
# meta server url, different environments should have different meta server addresses
dev_meta=http://192.168.8.237:9002
META_SERVERS_OPTS="-Ddev_meta=$dev_meta"
# =============== Please do not modify the following content =============== #
# go to script directory
cd "${0%/*}"
cd ..
# package config-service and admin-service
echo "==== starting to build config-service and admin-service ===="
mvn clean package -DskipTests -pl apollo-configservice,apollo-adminservice -am -Dapollo_profile=github -Dspring_datasource_url=$apollo_config_db_url -Dspring_datasource_username=$apollo_config_db_username -Dspring_datasource_password=$apollo_config_db_password
echo "==== building config-service and admin-service finished ===="
echo "==== starting to build portal ===="
mvn clean package -DskipTests -pl apollo-portal -am -Dapollo_profile=github,auth -Dspring_datasource_url=$apollo_portal_db_url -Dspring_datasource_username=$apollo_portal_db_username -Dspring_datasource_password=$apollo_portal_db_password $META_SERVERS_OPTS
echo "==== building portal finished ===="
echo "==== starting to build client ===="
mvn clean install -DskipTests -pl apollo-client -am $META_SERVERS_OPTS
echo "==== building client finished ===="
Ⅴ、编译
cd /opt/wy/apollo-master/scripts
./build.sh
Ⅵ、启动apollo
①传统分布式启动
#启动adminservice
cd /opt/wy/apollo-master/apollo-adminservice/target
unzip -d ./test apollo-adminservice-0.11.0-SNAPSHOT-github.zip
cd test/scripts/
./startup.sh
#启动configservice
cd /opt/wy/apollo-master/apollo-configservice/target
unzip -d ./test apollo-configservice-0.11.0-SNAPSHOT-github.zip
cd test/scripts/
./startup.sh
#启动portal
cd /opt/wy/apollo-master/apollo-portal/target
unzip -d ./test apollo-portal-0.11.0-SNAPSHOT-github.zip
cd test/scripts/
./startup.sh
②docker化启动
在/opt/wy/apollo-master 目录下新建docker-compose.yml,内容如下:
version: "2"
services:
apollo-configservice: ##容器服务名
container_name: apollo-configservice ##容器名
build: apollo-configservice/src/main/docker/ ##Dockerfile路径
image: apollo-configservice:0.11.0 ##镜像名
ports:
- "9002:9002"
volumes:
- "/Users/mobin/opt/logs/100003171:/opt/logs/100003171" ##将/opt/logs/100003171目录挂载到宿主机的/Users/mobin/opt/logs/100003171方便在宿主机上查看日志
environment:
- spring_datasource_url=jdbc:mysql://192.168.8.237:3306/ApolloConfigDB?characterEncoding=utf8
- spring_datasource_username=root
- spring_datasource_password=abcdef
apollo-adminservice:
container_name: apollo-adminservice
build: apollo-adminservice/src/main/docker/
image: apollo-adminservice:0.11.0
ports:
- "9001:9001"
depends_on:
- apollo-configservice
volumes:
- "/Users/mobin/opt/logs/100003172:/opt/logs/100003172"
environment:
- spring_datasource_url=jdbc:mysql://192.168.8.237:3306/ApolloConfigDB?characterEncoding=utf8
- spring_datasource_username=root
- spring_datasource_password=abcdef
apollo-portal:
container_name: apollo-portal
build: apollo-portal/src/main/docker/
image: apollo-portal:0.11.0
ports:
- "9003:9003"
depends_on:
- apollo-adminservice
volumes:
- "/Users/mobin/opt/logs/100003173:/opt/logs/100003173"
environment:
- spring_datasource_url=jdbc:mysql://192.168.8.237:3306/ApolloPortalDB?characterEncoding=utf8
- spring_datasource_username=root
- spring_datasource_password=abcdef
拷贝编译生成文件
cp /opt/wy/apollo-master/apollo-adminservice/target/apollo-adminservice-0.11.0-SNAPSHOT-github.zip /opt/wy/apollo-master/apollo-adminservice/src/main/docker/
cp /opt/wy/apollo-master/apollo-configservice/target/apollo-configservice-0.11.0-SNAPSHOT-github.zip /opt/wy/apollo-master/apollo-configservice/src/main/docker/
cp /opt/wy/apollo-master/apollo-portal/target/apollo-portal-0.11.0-SNAPSHOT-github.zip /opt/wy/apollo-master/apollo-portal/src/main/docker/
安装docker-compose
sudo curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
使用docker-compose编译docker镜像并启动(如果失败,请确认本机器是否安装docker)
docker-compose up -d
4) 192.168.8.235 配置如下
操作同3)步骤,然后将其中
Ⅰ、/opt/wy/apollo-master/apollo-configservice/src/main/resources/bootstrap.yml
和/opt/wy/apollo-master/apollo-adminservice/src/main/resources/bootstrap.yml 的
ip-address: 192.168.8.237 改为 ip-address: 192.168.8.235
Ⅱ、 /opt/wy/apollo-master/scripts/build.sh
dev_meta=http://192.168.8.237:9002 改为 dev_meta=http://192.168.8.235:9002
5)如果docker容器化启动失败,docker container logs 容器号,查看到如下错误日志
Operation not permitted (cannot access log file)
Thu May 31 14:34:45 CST 2018 Failed to start apollo-adminservice.jar, return code: 4
两种方案解决:
1、 使用docker run重新跑容器,增加--privileged 参数
感兴趣的小伙伴可以查看Dockfile,携程有写docker run的命令。
2、修改如下配置(建议此种)
/etc/sysconfig/selinux 将SELINUX的值设置为disabled。
然后 执行 source /etc/sysconfig/selinux (或重启)
6)运行成功!
Eureka信息如下:
Apollo信息如下(初始账号为:apollo 密码为:admin)
参考资料: