一鍵部署 springcloud 微服務,需要用到 Jenkins K8S Docker等工具。
本文使用jenkins部署,流程如下圖
開發者將程式碼push到git
運維人員透過jenkins部署,自動到git上pull程式碼
透過maven構建程式碼
將maven構建後的jar打包成docker映像 並 push docker映像到docker registry
透過k8s發起 釋出/更新 服務 操作
其中 2~5步驟都會在jenkins中進行操作
1、開發者將程式碼PUSH到Git
這一步本文不做詳細描述
2、透過Jenkins部署,自動到Git上PULL程式碼
這裏需要用到Jenkins 的 pipeline外掛程式
2.1、 配置SSH-KEY
因為jenkins需要
pull
git上的程式碼,正常來說,程式碼都是私有的,
git clone
操作的時候會需要密碼,就不能完成自動化操作了。這裏使用
SSH-KEY
的方式,讓
git clone
操作無需密碼就能完成複制
2.1.1、生成/添加SSH公鑰
在jenkins所在環境裏執行
ssh-keygen -t ed25519 -C "[email protected]"
註意:這裏的
[email protected]
只是生成的 sshkey 的名稱,並不約束或要求具體命名為某個信箱,現網的大部份教程均講解的使用信箱生成,其一開始的初衷僅僅是為了便於辨識所以使用了信箱
按照提示完成三次回車,即可生成
ssh key
。透過檢視
~/.ssh/id_ed25519.pub
檔內容,獲取到你的
public key
得到公鑰 public key 內容
cat ~/.ssh/id_ed25519.pub
復制備用
2.1.2、將公鑰配置到git平台
git平台可以是github,gitee,也可以是自己搭建的gitlab等
我這裏使用gitee
透過倉庫主頁 「
管理
」->「
部署公鑰管理
」->「
添加部署公鑰
」 ,添加生成的 public key 添加到倉庫中。
添加成功後,到jenkins所在環境執行
ssh -T [email protected]
首次使用需要確認並添加主機到本機SSH可信列表。若返回
Hi XXX! You’ve successfully authenticated, but Gitee.com does not provide shell access.
內容,則證明添加成功
2.1.3、測試
復制你計畫的SSH連結
在jenkins所在環境
執行
git clone [email protected]:xxxx.git
2.2、配置Jenkins的pipeline 自動clone程式碼
2.2.1、Jenkins建立任務
新建任務
選擇流水線 確定
這裏勾選參數化構建,選擇字元參數,用於輸入構建程式碼的版本
預設值填master,根據自身計畫實際填寫
拉到最下面的流水線,寫pipeline指令碼,如果不知道怎麽寫,可以點選流水線語法進行參考
這是我的指令碼,
REPOSITORY
填寫計畫的ssh地址,
REPOSITORY_VERSION
是剛剛配置的構建參數
pipeline {
agent any
environment {
REPOSITORY="[email protected]:xxxxxx/cloud-demo.git"
}
stages {
stage('拉程式碼') {
steps {
echo"start fetch code from git:${REPOSITORY}"
deleteDir()
git branch: "${REPOSITORY_VERSION}", url: "${REPOSITORY}"
}
}
}
}
保存
2.2.2、測試拉程式碼流程
返回Jenkins 首頁,選擇剛剛建立的計畫
點選右邊的執行按鈕
輸入代分碼支版本
點選開始構建
看到這裏就是已經構建成功了
根據日誌提示的目錄,可以看到目錄下已經有程式碼了
3、透過maven構建程式碼
3.1、maven外掛程式安裝
因為這裏使用到了maven,jenkins需要先安裝maven工具
3.2、maven構建計畫
補充pipeline指令碼
pipeline {
agent any
environment {
REPOSITORY="[email protected]:xxxxxx/cloud-demo.git"
MODULE="cloud-demo-m-test-dubbo-service"
}
stages {
stage('拉程式碼') {
steps {
echo"start fetch code from git:${REPOSITORY}"
deleteDir()
git branch: "${REPOSITORY_VERSION}", url: "${REPOSITORY}"
}
}
stage('編譯程式碼') {
steps {
echo"start compile"
sh "cd cloud-demo-project && mvn -U clean install"
echo"compile project ................................"
sh "cd cloud-demo-dependencies && mvn -U clean install"
echo"compile dependencies ................................"
sh "cd cloud-demo-common && mvn -U clean install"
echo"compile common ................................"
sh "cd cloud-demo-m-test && mvn -U -am clean install"
echo"compile m-test ................................"
sh "cd cloud-demo-m-test/cloud-demo-m-test-common && mvn -U -am clean install"
echo"compile m-test-dubbo-api ................................"
sh "cd cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-api && mvn -U -am clean install"
echo"compile m-test-dubbo-service ................................"
sh "cd cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-service && mvn -U -am clean package"
}
}
}
}
這裏補充了
stage('編譯程式碼') {}
部份,用於maven編譯。具體編譯指令碼需要根據自己計畫實際,這個是我計畫的編譯必須步驟。
3.3、測試拉程式碼流程+構建計畫過程
重復2.2.2步驟,檢視執行結果
構建成功
4、將maven構建後的jar打包成docker映像 並 push docker映像到docker registry
在jenkins環境下,建立目錄用於存放指令碼檔
mkdir /usr/local/project/.env/cloud-demo-m-test-dubbo-service/ -p
這個目錄下存放4個檔
build.sh
Dockerfile
application.properties
bootstrap.properties
application.properties
和
bootstrap.properties
是springcloud的配置檔,內容根據自己計畫情況
buils.sh 檔內容
#!/usr/bin/env bash
REPOSITORY_VERSION=$1
GIT_REVISION=`git log -1 --pretty=format:"%h"`
TIME=`date "+%Y.%m.%d.%H.%M"`
IMAGE_NAME=192.168.31.100:5000/cloud-demo/cloud-demo-m-test-dubbo-service
IMAGE_TAG=${REPOSITORY_VERSION}-${GIT_REVISION}-${TIME}
docker build -t ${IMAGE_NAME}:${IMAGE_TAG} .
docker push ${IMAGE_NAME}:${IMAGE_TAG}
echo"${IMAGE_NAME}:${IMAGE_TAG}" > IMAGE
## 內容說明
REPOSITORY_VERSION 是需要傳入的參數,傳計畫git分支名,用於打標簽使用
GIT_REVISION 是獲取當前git的送出版本 如 c9c8525,線上問題可以根據這個版本尋找具體程式碼問題
TIME 年.月.日.時.分 記錄打包時間,也用於打標簽使用
IMAGE_NAME 映像名,這裏字首包含了192.168.31.100:5000 是因為我打包之後會push到192.168.31.100:5000,其他伺服器可以到這裏來pull映像
docker build -t ${IMAGE_NAME}:${IMAGE_TAG} . 構建映像
docker push ${IMAGE_NAME}:${IMAGE_TAG} 推播映像
echo"${IMAGE_NAME}:${IMAGE_TAG}" > IMAGE 把映像名:映像標簽 輸出到IMAGE檔裏,方便後續步驟獲取
Dockerfile 檔內容
FROM openjdk:8u342-jdk
MAINTAINER yanger [email protected]
COPY target/cloud-demo-m-test-dubbo-service-1.0-SNAPSHOT.jar /cloud-demo-m-test-dubbo-service.jar
COPY application.properties /application.properties
COPY bootstrap.properties /bootstrap.properties
ENTRYPOINT ["java", "-jar", "/cloud-demo-m-test-dubbo-service.jar"]
## 內容說明
FROM openjdk:8u342-jdk 使用openjdk:8u342-jdk 作為基礎映像
COPY 檔到映像
ENTRYPOINT ["java", "-jar", "/cloud-demo-m-test-dubbo-service.jar"] 啟動時執行 java -jar /cloud-demo-m-test-dubbo-service.jar
這裏用到了
docker registry
如果還沒有registry 請先啟動,可以用docker的方式啟動
docker run -d -p 5000:5000 --name registry registry
build.sh
檔需要可執行許可權
chmod 755 build.sh
補充pipeline指令碼
pipeline {
agent any
environment {
REPOSITORY="[email protected]:xxxxxx/cloud-demo.git"
SCRIPT_PATH="/usr/local/project/.env/cloud-demo-m-test-dubbo-service/"
}
stages {
stage('拉程式碼') {
steps {
echo"start fetch code from git:${REPOSITORY}"
deleteDir()
git branch: "${REPOSITORY_VERSION}", url: "${REPOSITORY}"
}
}
stage('編譯程式碼') {
steps {
echo"start compile"
sh "cd cloud-demo-project && /usr/local/maven/bin/mvn -U clean install"
echo"compile project ................................"
sh "cd cloud-demo-dependencies && /usr/local/maven/bin/mvn -U clean install"
echo"compile dependencies ................................"
sh "cd cloud-demo-common && /usr/local/maven/bin/mvn -U clean install"
echo"compile common ................................"
sh "cd cloud-demo-m-test && /usr/local/maven/bin/mvn -U -am clean install"
echo"compile m-test ................................"
sh "cd cloud-demo-m-test/cloud-demo-m-test-common && /usr/local/maven/bin/mvn -U -am clean install"
echo"compile m-test-dubbo-api ................................"
sh "cd cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-api && /usr/local/maven/bin/mvn -U -am clean install"
echo"compile m-test-dubbo-service ................................"
sh "cd cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-service && /usr/local/maven/bin/mvn -U -am clean package"
}
}
stage('構建映像') {
steps {
echo"start build image"
sh "cp ${SCRIPT_PATH}/build.sh cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-service/"
sh "cp ${SCRIPT_PATH}/Dockerfile cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-service/"
sh "cp ${SCRIPT_PATH}/application.properties cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-service/"
sh "cp ${SCRIPT_PATH}/bootstrap.properties cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-service/"
sh "cd cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-service/ && ./build.sh ${REPOSITORY_VERSION}"
}
}
}
}
補充了
stage(‘構建映像’){}
內容和
environment
部份加了個參數
SCRIPT_PATH
SCRIPT_PATH
參數是上面建立的資料夾路徑
stage(‘構建映像’){}
這一步是復制相應檔到計畫目錄下,並且執行build.sh指令碼
測試
可以看到映像已經打包好,並且已經push到registry了
瀏覽器存取
http://192.168.31.100:5000/v2/cloud-demo/cloud-demo-m-test-dubbo-service/tags/list
可以看到registry有
cloud-demo/cloud-demo-m-test-dubbo-service:master-7012e1d-2023.05.01.10.16
這個映像
5、透過k8s發起 釋出/更新 服務 操作
5.1、配置構建K8S資源的描述檔
在
/usr/local/project/.env/cloud-demo-m-test-dubbo-service/
目錄新增檔
cloud-demo-m-test-dubbo-service.yaml
檔內容
apiVersion:apps/v1
kind:Deployment
metadata:
creationTimestamp:null
labels:
app:cloud-demo-m-test-dubbo-service
name:cloud-demo-m-test-dubbo-service
spec:
replicas:1
selector:
matchLabels:
app:cloud-demo-m-test-dubbo-service
strategy:{}
template:
metadata:
creationTimestamp:null
labels:
app:cloud-demo-m-test-dubbo-service
spec:
containers:
-image:IMAGE_AND_TAG
name:cloud-demo-m-test-dubbo-service
resources:{}
volumeMounts:
-name:log-path
mountPath:/logs
volumes:
-name:log-path
hostPath:
path:/root/k8s/cloud-demo-m-test-dubbo-service/logs
status:{}
---
apiVersion:v1
kind:Service
metadata:
creationTimestamp:null
labels:
app:cloud-demo-m-test-dubbo-service
name:cloud-demo-m-test-dubbo-service
spec:
ports:
-port:20881
protocol:TCP
targetPort:20881
selector:
app:cloud-demo-m-test-dubbo-service
type:NodePort
status:
loadBalancer:{}
這個檔是構建K8S資源的描述檔,建立
deployment
和
service
,暴露埠20881,掛載/logs目錄到主機
其中
IMAGE_AND_TAG
是需要替換為相應
容器名:容器標簽
的
5.2、完善jenkins步驟
pipeline {
agent any
environment {
REPOSITORY="[email protected]:xxxxxx/cloud-demo.git"
SCRIPT_PATH="/usr/local/project/.env/cloud-demo-m-test-dubbo-service"
IMAGE=""
}
stages {
stage('拉程式碼') {
steps {
echo"start fetch code from git:${REPOSITORY}"
deleteDir()
git branch: "${REPOSITORY_VERSION}", url: "${REPOSITORY}"
}
}
stage('編譯程式碼') {
steps {
echo"start compile"
sh "cd cloud-demo-project && /usr/local/maven/bin/mvn -U clean install"
echo"compile project ................................"
sh "cd cloud-demo-dependencies && /usr/local/maven/bin/mvn -U clean install"
echo"compile dependencies ................................"
sh "cd cloud-demo-common && /usr/local/maven/bin/mvn -U clean install"
echo"compile common ................................"
sh "cd cloud-demo-m-test && /usr/local/maven/bin/mvn -U -am clean install"
echo"compile m-test ................................"
sh "cd cloud-demo-m-test/cloud-demo-m-test-common && /usr/local/maven/bin/mvn -U -am clean install"
echo"compile m-test-dubbo-api ................................"
sh "cd cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-api && /usr/local/maven/bin/mvn -U -am clean install"
echo"compile m-test-dubbo-service ................................"
sh "cd cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-service && /usr/local/maven/bin/mvn -U -am clean package"
}
}
stage('構建映像') {
steps {
echo"start build image"
sh "cp ${SCRIPT_PATH}/build.sh cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-service/"
sh "cp ${SCRIPT_PATH}/Dockerfile cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-service/"
sh "cp ${SCRIPT_PATH}/application.properties cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-service/"
sh "cp ${SCRIPT_PATH}/bootstrap.properties cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-service/"
sh "cd cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-service/ && ./build.sh ${REPOSITORY_VERSION}"
}
}
stage('釋出') {
steps {
echo"start deploy"
script {
IMAGE = readFile "cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-service/IMAGE"
IMAGE = IMAGE.trim()
if (IMAGE == "") {
throw new Exception("獲取映像名檔失敗,請重試")
}
}
echo"IMAGE: -- ${IMAGE}"
sh "cp ${SCRIPT_PATH}/cloud-demo-m-test-dubbo-service.yaml cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-service/"
sh "sed -i \"s#IMAGE_AND_TAG#${IMAGE}#g\" cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-service/cloud-demo-m-test-dubbo-service.yaml"
sh "kubectl apply -f cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-service/cloud-demo-m-test-dubbo-service.yaml"
}
}
}
}
添加了
stage('釋出') {}
部份
透過讀取IMAGE檔,獲取
映像名:映像標簽
如果獲取不到
映像名:映像標簽
,丟擲異常
將
cloud-demo-m-test-dubbo-service.yaml
檔復制到工作目錄
用映像名:映像標簽 替換掉
IMAGE_AND_TAG
使用kubectl命令釋出/更新服務
5.3、測試
在jenkins上構建計畫
在K8S master伺服器上,執行
kubectl get all
搞定!
來源|blog.csdn.net/qq_22917569/
article/details/130447497