微服務遠端釋出(至少一分鐘前,你根本不知道會有這個吻。一無所有的時候,說明你該擁有的,還沒有到來。)
阿新 • • 發佈:2022-03-04
完成微服務多伺服器遠端釋出
1)配置遠端部署伺服器
從Jenkins伺服器拷貝公鑰到docker2遠端伺服器
ssh-copy-id192.168.195.182
系統配置->新增遠端伺服器
2)修改Docker配置信任Harbor私服地址
{
"registry-mirrors": ["https://zydiol88.mirror.aliyuncs.com"],
"insecure-registries": ["192.168.195.183:85"]
}
重啟Docker
3)新增引數
多選框:部署伺服器
最終效果:
4)修改Jenkinsfile構建指令碼
//git的憑證
defgit_auth="1be38991-873b-4a68-8eb6-312347fdc0a4"
//git的URL
defgit_url="[email protected]:kgc_group/tensquare_back.git"
//映象標籤
deftag="latest"
//harbor的url地址
defharbor_url="192.168.195.183:85"
//映象倉庫名
defharbor_name="tensquare"
//harbor的憑證
defharbor_auth="e8b4bf42-2a87-4611-90f7-4b4a75479b5c"
node {
//獲取當前選擇專案名稱
defselectedProjectNames="${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(inti=0;i<selectedProjectNames.length;i++){
//專案資訊tensquare_eureka_server@10086
defprojectInfo=selectedProjectNames[i]
//當前的專案名稱
defcurrentProjectName="${projectInfo}".split("@")[0]
//當前的專案埠
defcurrentProjectPort="${projectInfo}".split("@")[1]
//定義SonarQubeScanner工具
defscannerHome =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 images,push images') {
for(inti=0;i<selectedProjectNames.length;i++){
//專案資訊tensquare_eureka_server@10086
defprojectInfo=selectedProjectNames[i]
//當前的專案名稱
defcurrentProjectName="${projectInfo}".split("@")[0]
//當前的專案埠
defcurrentProjectPort="${projectInfo}".split("@")[1]
sh "mvn -f ${currentProjectName} clean package dockerfile:build"
//定義映象名稱
defimageName="${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)])
}
}
}
}
推送上傳gitlab
5)編寫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 tom -p Abcd1234 $harbor_url
# 下載映象
docker pull $imageName
# 啟動容器
docker run -di -p $port:$port $imageName $profile
echo "容器啟動成功"
兩臺生產伺服器叢集都啟動成功!!!
6)叢集效果
後臺微服務叢集成功!!!
Nginx+Zuul叢集實現高可用閘道器
1)docker2伺服器上安裝
yum install epel-release-y
yum -y install nginx
2)Nginx(已完成)埠也要修改
vi /etc/nginx/nginx.conf
upstream zuulServer{
server 192.168.153.50:10020 weight=1;
server 192.168.153.30:10020 weight=1;
}
location / {
proxy_pass http://zuulServer/;
}
內容如下:
3)重啟Nginx: systemctl restart nginx
4)修改前端Nginx的訪問地址
再次構建前端工程
叢集網站成功!!!
希望和悲傷,都是一縷光。總有一天,我們會再相遇。