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="[email protected]: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前端的訪問地址:
然後提交推送:
再次構建前端專案:
訪問叢集網站:
自古英雄多磨難