因為之前寫過基於K8S部署jenkins master/slave平臺,在這個的基礎上構建微服務到K8S叢集中
Jenkins-slave構建微服務專案到K8S叢集
1、微服務專案上傳到git倉庫
這個就不多說了
2、更改註冊中心eureka的配置檔案
進入到專案程式碼中,更改微服務中eureka-service的配置檔案application.yaml(需要和開發對一下,具體還要加哪些引數)
server: port: 8761 spring: application: name: eureka-service eureka: instance: prefer-ip-address: false client: region: default service-url: defaultZone: http://eureka-0.eureka.default:${server.port}/eureka/,http://eureka-1.eureka.default:${server.port}/eureka/ register-with-eureka: true fetch-registry: true
其他應用程式往eureka註冊,需要在各自的application.yaml裡新增下面的eureka資訊
eureka: instance: prefer-ip-address: false client: region: default service-url: defaultZone: http://eureka-0.eureka:8761/eureka/,http://eureka-1.eureka:8761/eureka/ register-with-eureka: true fetch-registry: true
3、建立statefulset資原始檔部署eureka,ingress做負載均衡
(將所以要建立的資源都合併到一個yaml檔案,上傳到git倉庫 專案程式碼的eureka目錄下)
#建立Service為headless apiVersion: v1 kind: Service metadata: namespace: NS labels: app: eureka name: eureka spec: clusterIP: None selector: app: eureka ports: - port: 8761 protocol: TCP targetPort: 8761 status: loadBalancer: {} --- #建立StatefulSet apiVersion: apps/v1 kind: StatefulSet metadata: name: eureka namespace: NS spec: selector: matchLabels: app: eureka serviceName: "eureka" replicas: RSCOUNT template: metadata: labels: app: eureka spec: imagePullSecrets: - name: SECRET_NAME containers: - name: eureka image: IMAGE_NAME ports: - containerPort: 8761 --- #建立ingress apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: eureka-ingress annotations: kubernetes.io/ingress.class: "nginx" spec: rules: - host: eureka.test.com http: paths: - path: / pathType: Prefix backend: service: name: eureka port: number: 8761
4、在jenkins建立流水線任務
新建任務——eureka 流水線——然後進入專案配置,Jenkinsfile如下:
def registry = "10.48.14.50:8888" def project = "dev" def app_name = "eureka-service" def jar_path = "build/libs" def jar_name = "${app_name}-0.0.1-SNAPSHOT.jar" def jar_port = "8761" def image_name = "${registry}/${project}/${app_name}:${BUILD_NUMBER}" def git_address = "http://10.48.14.100:30080/001/xiangmu.git" // 認證 def secret_name = "registrypullauth" def docker_registry_auth = "b07ed5ba-e191-4688-9ed2-623f4753781c" def git_auth = "a5ec87ae-87a1-418e-aa49-53c4aedcd261" def k8s_auth = "3cd3f414-a0e2-4bc0-8808-78c64e6ad7d2" def JAVA_OPTS = "-Xms128m -Xmx256m -Dfile.encoding=UTF8 -Duser.timezone=GMT+08 -Dspring.profiles.active=test" pipeline { agent { kubernetes { label "jenkins-slave" yaml """ kind: Pod metadata: name: jenkins-slave spec: containers: - name: jnlp image: "${registry}/library/jenkins-slave-jdk:latest" imagePullPolicy: Always env: - name: TZ value: Asia/Shanghai volumeMounts: - name: docker-cmd mountPath: /usr/bin/docker - name: docker-sock mountPath: /var/run/docker.sock - name: gradle-cache mountPath: /root/.gradle volumes: - name: docker-cmd hostPath: path: /usr/bin/docker - name: docker-sock hostPath: path: /var/run/docker.sock - name: gradle-cache hostPath: path: /tmp/gradle """ } } parameters { choice (choices: ['1', '2', '3', '5', '7'], description: '副本數', name: 'ReplicaCount') choice (choices: ['dev','test','prod','default'], description: '名稱空間', name: 'Namespace') } stages { stage('拉取程式碼'){ steps { checkout([$class: 'GitSCM', branches: [[name: "${params.Branch}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_address}"]] ]) } } stage('程式碼編譯'){ steps { sh """ java -version pwd cd ${app_name} pwd gradle -v gradle clean build -x test """ } } stage('構建映象'){ steps { withCredentials([usernamePassword(credentialsId: "${docker_registry_auth}", passwordVariable: 'password', usernameVariable: 'username')]) { sh """ echo ' FROM ${registry}/library/java:11 MAINTAINER liang RUN apk add -U tzdata && \ ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime ADD ${jar_path}/${jar_name} / EXPOSE ${jar_port} CMD java -jar $JAVA_OPTS /${jar_name} '> ${app_name}/Dockerfile docker build -t ${image_name} ${app_name} docker login -u ${username} -p ${password} ${registry} docker push ${image_name} """ } } } stage('部署到K8S平臺'){ steps { configFileProvider([configFile(fileId: "${k8s_auth}", targetLocation: 'admin.kubeconfig')]) { sh """ pwd sed -i 's#IMAGE_NAME#${image_name}#' ${app_name}/deploy.yaml sed -i 's#SECRET_NAME#${secret_name}#' ${app_name}/deploy.yaml sed -i 's#RSCOUNT#${ReplicaCount}#' ${app_name}/deploy.yaml sed -i 's#NS#${Namespace}#' ${app_name}/deploy.yaml kubectl apply -f ${app_name}/deploy.yaml -n ${Namespace} --kubeconfig=admin.kubeconfig sleep 10 kubectl get pod -n ${Namespace} --kubeconfig=admin.kubeconfig """ } } } } }
構建專案:可以選擇啟動的副本數、專案程式碼分支、k8s名稱空間(dev、test、prod:代表開發、測試、生產環境)
檢視構建日誌:
瀏覽器訪問eureka: 需要新增dns記錄或者host檔案(根據自己的實際情況新增) http://eureka.test.com
5、構建其他應用程式部署到K8S
修改微服務閘道器gateway的配置檔案application.yaml:
server: port: 8130 spring: application: name: gateway-service eureka: instance: prefer-ip-address: false client: region: default service-url: defaultZone: http://eureka-0.eureka:8761/eureka/,http://eureka-1.eureka:8761/eureka/ register-with-eureka: true fetch-registry: true
建立deploy資源 部署gateway,ingress做負載均衡
apiVersion: v1 kind: Service metadata: namespace: NS labels: app: gateway name: gateway spec: selector: app: gateway ports: - port: 8130 protocol: TCP targetPort: 8130 status: loadBalancer: {} --- #建立Deployment apiVersion: apps/v1 kind: Deployment metadata: name: gateway namespace: NS spec: selector: matchLabels: app: gateway replicas: RSCOUNT template: metadata: labels: app: gateway spec: imagePullSecrets: - name: SECRET_NAME containers: - name: gateway image: IMAGE_NAME ports: - containerPort: 8130 --- #建立ingress apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: gateway-ingress annotations: kubernetes.io/ingress.class: "nginx" spec: rules: - host: gateway.test.com http: paths: - path: / pathType: Prefix backend: service: name: gateway port: number: 8130
jenkins中建立gateway專案,使用eureka的Jenkinsfile,只需要修改其中的變數就可以了,這裡就不寫了
......
應用程式構建完成後,檢視是否註冊到eureka:
再檢視eureka日誌,說明成功了
大概就是這樣,部署微服務的其他應用程式也是一樣。
需要注意的是:專案中application.yaml的引數要問開發人員(有些程式需要連資料庫)