Devops 開發運維高級篇之Jenkins+Docker+SpringCloud微服務持續集成——部署方案優化
Devops 開發運維高級篇之Jenkins+Docker+SpringCloud微服務持續集成——部署方案優化
之前我們做的方案部署都是只能選擇一個微服務部署并只有一臺生產服務器,每個微服務只有一個實例,容錯率低
如何去解決?
- 在一個Jenkins工程中可以選擇多個微服務同時發布
- 在一個Jenkins工程中可以選擇多臺生產服務器同時部署
- 每個微服務都是以集群高可用形式部署
Jenkins+Docker+SpringCloud集群部署流程說明
修改所有微服務配置:
再開一臺生產服務器 裝有docker環境
從jenkins服務器拷貝公鑰去這一臺生成服務器2的
然后修改docker的私服地址:
記得重啟docker!
注冊中心配置:(原來的單機版改成集群版)
# 集群版 spring: application: name: EUREKA-HA --- server: port: 10086 spring: # 指定profile=eureka-server1 profiles: eureka-server1 eureka: instance: # 指定當profile=eureka-server1時,主機名是eureka-server1 hostname: 20.0.0.60 client: service-url: # 將自己注冊到eureka-server1、eureka-server2這個Eureka上面去 defaultZone: http://20.0.0.60:10086/eureka/,http://20.0.0.40:10086/eureka/ --- server: port: 10086 spring: profiles: eureka-server2 eureka: instance: hostname: 20.0.0.40 client: service-url: defaultZone: http://20.0.0.60:10086/eureka/,http://20.0.0.40:10086/eureka/
其它微服務的配置:
eureka: client: service-url: defaultZone: http://20.0.0.60:10086/eureka,http://20.0.0.40:10086/eureka/ #上面的這個是eureka訪問地址 instance: lease-renewal-interval-in-seconds: 5 # 每隔5秒發送一次心跳 lease-expiration-duration-in-seconds: 10 # 10秒不發送就過期 prefer-ip-address: true
全部修改完就去吧代碼提交到Gitlab中!push!
編寫deployCluster.sh部署腳本,放到兩臺生產服務器中路徑:/opt/jenkins_shell/deployCluster.sh
#! /bin/sh #接收外部參數 harbor_url=$1 harbor_project_name=$2 project_name=$3 tag=$4 port=$5 profile=$6 imageName=$harbor_url/$harbor_project_name/$project_name:$tag echo "$imageName" #查詢容器是否存在,存在則刪除 containerId=`docker ps -a | grep -w ${project_name}:${tag} | awk '{print $1}'` if [ "$containerId" != "" ] ; then #停掉容器 docker stop $containerId #刪除容器 docker rm $containerId echo "成功刪除容器" fi #查詢鏡像是否存在,存在則刪除 imageId=`docker images | grep -w $project_name | awk '{print $3}'` if [ "$imageId" != "" ] ; then #刪除鏡像 docker rmi -f $imageId echo "成功刪除鏡像" fi # 登錄Harbor docker login -u lvbu -p Lvbu1234 $harbor_url # 下載鏡像 docker pull $imageName # 啟動容器 docker run -di -p $port:$port $imageName $profile echo "容器啟動成功"
然后就是更改Jenkinsfile 代碼??!增加循環構建編譯打包!還有集群部署的代碼!
//git的憑證 def git_auth="d5bb0e98-15f2-477f-8db7-2c33ecc6c644" //git的URL def git_url="git@20.0.0.20:root/tensquare_back.git" //鏡像標簽 def tag="latest" //harbor的url地址 def harbor_url="20.0.0.50:85" //鏡像倉庫名 def harbor_name="tensquare" //harbor的憑證 def harbor_auth="da853891-2b06-4f40-ad1f-f833b0cd96b7" node { //獲取當前選擇項目名稱 def selectedProjectNames="${project_name}".split(",") //獲取當前選擇服務器 def selectedServers="${publish_server}".split(",") stage('pull code') { checkout([$class: 'GitSCM', branches: [[name: "*/${branch}"]], extensions: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_url}"]]]) } stage('check code') { for(int i=0;i<selectedProjectNames.length;i++){ //項目信息 tensquare_eureka_server@10086 def projectInfo=selectedProjectNames[i] //當前的項目名稱 def currentProjectName="${projectInfo}".split("@")[0] //當前的項目端口 def currentProjectPort="${projectInfo}".split("@")[1] //定義SonarQubeScanner工具 def scannerHome = tool 'sonar-scanner' //引用SonarQube系統環境 withSonarQubeEnv('sonarqube') { sh """ cd ${currentProjectName} ${scannerHome}/bin/sonar-scanner """ } } } //添加公共子工程 stage('make install public sub project') { sh "mvn -f tensquare_common clean install" } //打包微服務項目,制作鏡像 stage('make package') { for(int i=0;i<selectedProjectNames.length;i++){ //項目信息 tensquare_eureka_server@10086 def projectInfo=selectedProjectNames[i] //當前的項目名稱 def currentProjectName="${projectInfo}".split("@")[0] //當前的項目端口 def currentProjectPort="${projectInfo}".split("@")[1] sh "mvn -f ${currentProjectName} clean package dockerfile:build" //定義鏡像名稱 def imageName="${currentProjectName}:${tag}" //對鏡像打標簽 sh "docker tag ${imageName} ${harbor_url}/${harbor_name}/${imageName}" //鏡像推送到harbor withCredentials([usernamePassword(credentialsId: "${harbor_auth }", passwordVariable: 'password', usernameVariable: 'username')]) { // 登錄harbor sh "docker login -u ${username} -p ${password} ${harbor_url}" //鏡像上傳 sh "docker push ${harbor_url}/${harbor_name}/${imageName}" sh "echo 鏡像上傳成功" } //遍歷所有服務器,分別部署 for (int j=0;j<selectedServers.length;j++){ //獲取當前服務器名稱 def currentServerName=selectedServers[j] //調用不同服務器模塊內容--spring.profiles.active=eureka-server1/eureka-server2 def activeProfile="--spring.profiles.active=" //根據不同的服務器名稱調用不同的服務器配置信息 if (currentServerName=="master_server"){ activeProfile=activeProfile+"eureka-server1" }else if (currentServerName=="slave_server"){ activeProfile=activeProfile+"eureka-server2" } //部署應用 sshPublisher(publishers: [sshPublisherDesc(configName: "${currentServerName}" , transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "/opt/jenkins_shell/deployCluster.sh ${harbor_url} ${harbor_name} ${currentProjectName} ${tag} ${currentProjectPort} ${activeProfile}", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)]) } } } }
然后繼續提交push到gitlab中?。?!
設計Jenkins集群項目的構建參數
安裝Extended Choice Parameter插件
創建流水線項目:
添加參數:字符串參數:分支名稱:
然后插件下好了就會有多選框:項目名稱:
然后繼續修改配置另一臺生成服務器的:
新增多選框:
保存后
然后把gitlab的后端項目ssh復制一下url 下面會用到!
然后就可以開始構建看效果圖:
然后就開始構建:
最后結果圖!!!
訪問兩臺生產服務器地址:20.0.0.60:10086 、 20.0.0.40:10086
另一臺的生成服務器:看容器:
Nginx+Zuul集群實現高可用網關
docker2服務器上安裝nginx
#下載依賴源下載nginx yum install epel-release -y yum -y install nginx
修改配置:
vim /etc/nginx/nginx.confn
內容如下:
upstream zuulServer{ server 20.0.0.60:10020 weight=1; server 20.0.0.40:10020 weight=1; }
location / { proxy_pass http://zuulServer/; }
保存后重啟nginx:
systemctl restart nginx
然后去修改nginx前端的訪問地址:
然后提交推送:
再次構建前端項目:
訪問集群網站:
自古英雄多磨難