暂时列个架子,后续慢慢补充。。。
项目结构
1.注册登陆阿里云账号(步骤略)
购买ECS、购买域名
实名认证-创建域名模板-认证备案
2.服务器基础环境搭建(准备三台ECS、步骤略)
安装docker、安装docker-compose、镜像加速地址设置、修改yum源配置、阿里云组策略开放端口、内网ip设置
3.安装基本环境(内容太多,后续单独放一篇文章内)
ELK、nacos集群、mysql主从、redis、FastDFS集群、jenkins+nexus、mongo+mongo-express、sentinel、seata、nginx等等
nacos集群
version: '3'
services:
# nacos-server服务注册与发现,配置中心服务
docker-nacos-server:
image: nacos/nacos-server:1.0.1
container_name: nacos-server-1
ports:
- "8848:8848"
- "9555:9555"
networks:
- nacos_net
restart: on-failure
privileged: true
environment:
PREFER_HOST_MODE: ip #如果支持主机名可以使用hostname,否则使用ip,默认也是ip
SPRING_DATASOURCE_PLATFORM: mysql #数据源平台 仅支持mysql或不保存empty
NACOS_SERVER_IP: 10.211.55.17 #多网卡情况下,指定ip或网卡
NACOS_SERVERS: 10.211.55.17:8848 10.211.55.18:8848 10.211.55.19:8848 #集群中其它节点[ip1:port ip2:port ip3:port]
MYSQL_MASTER_SERVICE_HOST: 10.211.55.17 #mysql配置,Master为主节点,Slave为从节点
MYSQL_MASTER_SERVICE_PORT: 3306
MYSQL_MASTER_SERVICE_DB_NAME: nacos
MYSQL_MASTER_SERVICE_USER: root
MYSQL_MASTER_SERVICE_PASSWORD: mysql1234
MYSQL_SLAVE_SERVICE_HOST: 10.211.55.17
MYSQL_SLAVE_SERVICE_PORT: 3306
MYSQL_SLAVE_SERVICE_DB_NAME: nacos
MYSQL_SLAVE_SERVICE_USER: root
MYSQL_SLAVE_SERVICE_PASSWORD: mysql1234
#JVM调优参数
#JVM_XMS: #-Xms default :2g
#JVM_XMX: #-Xmx default :2g
#JVM_XMN: #-Xmn default :1g
#JVM_MS: #-XX:MetaspaceSize default :128m
#JVM_MMS: #-XX:MaxMetaspaceSize default :320m
#NACOS_DEBUG: n #是否开启远程debug,y/n,默认n
#TOMCAT_ACCESSLOG_ENABLED: true #是否开始tomcat访问日志的记录,默认false
volumes:
- ./cluster-logs/nacos1:/home/nacos/logs #日志输出目录
- ../init.d/custom.properties:/home/nacos/init.d/custom.properties #../init.d/custom.properties内包含很多自定义配置,可按需配置
networks:
nacos_net:
driver: bridge
version: '3'
services:
# nacos-server服务注册与发现,配置中心服务
docker-nacos-server:
image: nacos/nacos-server:1.0.1
container_name: nacos-server-3
ports:
- "8848:8848"
- "9555:9555"
networks:
- nacos_net
restart: on-failure
privileged: true
environment:
PREFER_HOST_MODE: ip #如果支持主机名可以使用hostname,否则使用ip,默认也是ip
SPRING_DATASOURCE_PLATFORM: mysql #数据源平台 仅支持mysql或不保存empty
NACOS_SERVER_IP: 10.211.55.18 #多网卡情况下,指定ip或网卡
NACOS_SERVERS: 10.211.55.17:8848 10.211.55.18:8848 10.211.55.19:8848 #集群中其它节点[ip1:port ip2:port ip3:port]
MYSQL_MASTER_SERVICE_HOST: 10.211.55.17 #mysql配置,Master为主节点,Slave为从节点
MYSQL_MASTER_SERVICE_PORT: 3306
MYSQL_MASTER_SERVICE_DB_NAME: nacos
MYSQL_MASTER_SERVICE_USER: root
MYSQL_MASTER_SERVICE_PASSWORD: mysql1234
MYSQL_SLAVE_SERVICE_HOST: 10.211.55.17
MYSQL_SLAVE_SERVICE_PORT: 3306
#JVM调优参数
#JVM_XMS: #-Xms default :2g
#JVM_XMX: #-Xmx default :2g
#JVM_XMN: #-Xmn default :1g
#JVM_MS: #-XX:MetaspaceSize default :128m
#JVM_MMS: #-XX:MaxMetaspaceSize default :320m
#NACOS_DEBUG: n #是否开启远程debug,y/n,默认n
#TOMCAT_ACCESSLOG_ENABLED: true #是否开始tomcat访问日志的记录,默认false
volumes:
- ./cluster-logs/nacos3:/home/nacos/logs #日志输出目录
- ../init.d/custom.properties:/home/nacos/init.d/custom.properties #../init.d/custom.properties内包含很多自定义配置,可按需配置
networks:
nacos_net:
driver: bridge
version: '3'
services:
# nacos-server服务注册与发现,配置中心服务
docker-nacos-server:
image: nacos/nacos-server:1.0.1
container_name: nacos-server-3
ports:
- "8848:8848"
- "9555:9555"
networks:
- nacos_net
restart: on-failure
privileged: true
environment:
PREFER_HOST_MODE: ip #如果支持主机名可以使用hostname,否则使用ip,默认也是ip
SPRING_DATASOURCE_PLATFORM: mysql #数据源平台 仅支持mysql或不保存empty
NACOS_SERVER_IP: 10.211.55.19 #多网卡情况下,指定ip或网卡
NACOS_SERVERS: 10.211.55.17:8848 10.211.55.18:8848 10.211.55.19:8848 #集群中其它节点[ip1:port ip2:port ip3:port]
MYSQL_MASTER_SERVICE_HOST: 10.211.55.17 #mysql配置,Master为主节点,Slave为从节点
MYSQL_MASTER_SERVICE_PORT: 3306
MYSQL_MASTER_SERVICE_DB_NAME: nacos
MYSQL_MASTER_SERVICE_USER: root
MYSQL_MASTER_SERVICE_PASSWORD: mysql1234
MYSQL_SLAVE_SERVICE_HOST: 10.211.55.17
MYSQL_SLAVE_SERVICE_PORT: 3306
#JVM调优参数
#JVM_XMS: #-Xms default :2g
#JVM_XMX: #-Xmx default :2g
#JVM_XMN: #-Xmn default :1g
#JVM_MS: #-XX:MetaspaceSize default :128m
#JVM_MMS: #-XX:MaxMetaspaceSize default :320m
#NACOS_DEBUG: n #是否开启远程debug,y/n,默认n
#TOMCAT_ACCESSLOG_ENABLED: true #是否开始tomcat访问日志的记录,默认false
volumes:
- ./cluster-logs/nacos3:/home/nacos/logs #日志输出目录
- ../init.d/custom.properties:/home/nacos/init.d/custom.properties #../init.d/custom.properties内包含很多自定义配置,可按需配置
networks:
nacos_net:
driver: bridge
version: '3'
services:
#jenkins setting
jenkins:
container_name: dev_jenkins
image: jenkins/jenkins:lts
#host port:container port
ports:
- "8081:8080"
- "51000:50000"
#volumes path|host path:container path
volumes:
- /usr/local/jenkins:/var/jenkins_home
environment:
TZ: 'Asia/Shanghai'
restart: always
#nexus setting
nexus:
container_name: dev_nexus
image: sonatype/nexus3
#host port:container port
ports:
- "8080:8080"
- "50000:50000"
#volumes path|host path:container path
volumes:
- /usr/local/nexus:/nexus-data
environment:
TZ: 'Asia/Shanghai'
restart: always
暂略。。。
nacos集群创建好(需要将配置持久化至Mysql否则容器重启配置丢失),并创建prod、dev、test命名空间,增加相关配置
4.安装前端环境(或docker安装,同时编译打包前端项目到镜像推送私仓)
node npm vue yarn
wget https://nodejs.org/dist/v10.15.0/node-v10.15.0.tar.gz
tar zxvf node-v10.15.0.tar.gz
node 软连接
【安装node npm参考 https://blog.csdn.net/cpongo3/article/details/89157498】
npm安装yarn
yarn设置淘宝镜像
npm install -g yarn --registry=https://registry.npm.taobao.org
yarn config set registry https://registry.npm.taobao.org -g
yarn必要时设置环境变量
export PATH=/usr/local/nodejs/bin/:$PATH(临时生效,关闭shell失效)
或 vi /etc/profile (对所有用户永久生效)
加上 export PATH=/usr/local/nodejs/bin/:$PATH
source /etc/profile 立即生效
git clone项目之后需要npm install
启动vue项目
进入项目根目录(目录会有package.json文件,记录node插件依赖、项目等信息),进行安装yarn(会在目录创建node_models目录,存放npm插件),并运行工程
yarn install
yarn run serve
5.开通阿里云镜像服务,创建开发、测试、生产镜像命名空间,yum安装git,安装maven,docker build镜像push私仓,jenkins自动化发布
镜像build(进入项目pom目录,-f参数指定项目下的Dockerfile)
docker image build --no-cache -t registry.cn-zhangjiakou.aliyuncs.com/gx-v1-dev/gx-auth:1.2.0 -f ./gx-auth/src/main/docker/Dockerfile .
指定镜像打tag
sudo docker tag dd022772a116 registry.cn-zhangjiakou.aliyuncs.com/gx-v1-dev/gx-auth:1.2.0
登陆阿里云镜像仓库【需要输入密码】
sudo docker login --username=[个人用户名] registry.cn-zhangjiakou.aliyuncs.com
镜像push到阿里云镜像仓库指定命名空间
sudo docker tag [ImageId] registry.cn-zhangjiakou.aliyuncs.com/[个人镜像地址]:[镜像版本号]
例如
sudo docker push registry.cn-zhangjiakou.aliyuncs.com/gx-v1-dev/gx-auth:1.2.0
通过构建jenkins的pipeline脚本,实现自动化镜像打包及部署(需要配置凭证)
pipeline {
agent any
environment {
//project名称
project_name="gx-cloud"
//应用名称,jar包不带后缀
app_name="gx-auth-1.0-SNAPSHOT"
// 项目打包后rar包目录,一般在Srv下或者Shell下,需要根据自己的项目灵活改变,跟项目里面dockerfile文件下的目录一样
app_war_path = "gx-auth/target/{app_name}.jar"
app_version="1.0"
//Docker镜像名称
docker_name="gx-auth"
//镜像版本,不用动
image_tag="${app_version}.${BUILD_NUMBER}"
//镜像仓库命名空间,开发dev,测试test
docker_space="gx-v1-dev"
//DockerFile文件路径
docker_file_path="gx-auth/src/main/docker/Dockerfile"
//Git仓库地址
git_url = "xxxx"
//镜像仓库地址,不用动
repo_url="【镜像仓库地址】/${docker_space}/${docker_name}"
}
parameters {
string(name: 'branch', defaultValue: '【需要发版的分支名称】', description: '发布分支')
}
stages {
stage('Git Clone') {
steps {
script {
echo "checkout from ${branch}"
echo "${image_tag}"
checkout([$class: 'GitSCM', branches: [[name: "${branch}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '【jenkins的凭证id】', url: "$git_url"]]])
echo "checkout sucess"
}
}
}
stage('Maven Build') {
steps {
script {
sh'''
export MAVEN_HOME=/usr/local/apache-maven-3.6.2
/usr/local/apache-maven-3.6.2/bin/mvn clean package install -f ${project_name}/pom.xml -Dmaven.test.skip=true
'''
}
}
}
stage('Docker Build') {
steps {
script {
docker_result = sh returnStdout: true ,script: "docker images -q ${docker_name}";
echo "********docker_result${docker_result}"
if (docker_result!='') {
sh " docker rmi --force \$(docker images -q ${docker_name})"
}
sh "docker image build --no-cache -t ${docker_name}:latest -f ${docker_file_path} . "
withCredentials([usernamePassword(credentialsId: '【jenkins的凭证id】', passwordVariable: 'DOCKER_PASSWORD', usernameVariable: 'DOCKER_USER')]) {
sh "docker login -u ${DOCKER_USER} -p ${DOCKER_PASSWORD} 【镜像仓库地址】";
}
sh "docker tag ${docker_name}:latest 【镜像仓库地址】/${docker_space}/${docker_name}:${app_version}.${BUILD_NUMBER}"
sh "docker push 【镜像仓库地址】/${docker_space}/${docker_name}:${app_version}.${BUILD_NUMBER}"
sh "docker tag ${docker_name}:latest 【镜像仓库地址】/${docker_space}/${docker_name}:latest"
sh "docker push 【镜像仓库地址】/${docker_space}/${docker_name}:latest"
docker_result = sh returnStdout: true ,script: "docker images -q ${docker_name}";
echo "********docker_result${docker_result}"
if (docker_result!='') {
sh " docker rmi --force \$(docker images -q ${docker_name})"
}
}
}
}
stage('Deploy To K8s') {
steps {
script {
result = sh returnStdout: true ,script: "kubectl get deployment --selector=app=${docker_name} --kubeconfig='/root/.kube/dev_config' --namespace=default"
if(result==''){
sh'''
echo "deploy application"
sed -e "s#{TAG}#${image_tag}#g;s#{APOLLO_META}#${apollo_meta}#g;s#{REPO_URL}#${repo_url}#g;s#{APP_ID}#${app_id}#g;s#{APP_NAME}#${app_name}#g;s#{IMAGE_NAME}#${docker_name}#g" /root/deployment/k8s-deployment.yml > /root/deployment.yml
kubectl apply -f /root/deployment.yml --kubeconfig="/root/.kube/dev_config"
'''
}else{
sh "echo 'update application'"
sh "kubectl patch deployment ${docker_name} -p '{\"spec\":{\"template\":{\"spec\":{\"containers\":[{\"image\":\"${repo_url}:${image_tag}\",\"name\":\"${docker_name}\"}]}}}}' --kubeconfig='/root/.kube/dev_config' --namespace=default"
}
}
}
}
}
post {
success {
sh "echo 'success'"
}
failure {
echo "failure"
}
unstable {
echo "unstable"
}
}
}
最后一步需要k8s设置相关及配置中心地址等信息,取消,改为脚本在应用服务器启动应用容器(可通过jenkins配置应用服务器,并上传ssh公钥实现免密登陆)
6.增加nginx进行转发,域名解析设置
www.sssgg.fun 首页 80端口 匹配 门户网站(door.sssgg.fun)
www.home.sssgg.fun 用户项目
www.config.sssgg.fun 监控和配置项目
www.admin.sssgg.fun 后台管理项目
开发环境(测试环境dev改为sit)
www.dev.sssgg.fun 首页 80端口 匹配 门户网站
www.dev.home.sssgg.fun 用户项目
www.dev.config.sssgg.fun 监控和配置项目
www.dev.admin.sssgg.fun 后台管理项目