K8S+Jenkins自动化构建微服务项目(后续)

虚幻大学 xuhss 135℃ 0评论

Python微信订餐小程序课程视频

https://blog.csdn.net/m0_56069948/article/details/122285951

Python实战量化交易理财系统

https://blog.csdn.net/m0_56069948/article/details/122285941阅读目录


因为之前写过基于K8S部署jenkins master/slave平台,在这个的基础上构建微服务到K8S集群中

回到顶部## Jenkins-slave构建微服务项目到K8S集群

1、微服务项目上传到git仓库

  这个就不多说了

2、更改注册中心eureka的配置文件

进入到项目代码中,更改微服务中eureka-service的配置文件application.yaml(需要和开发对一下,具体还要加哪些参数)

?

| 123456789101112131415 | server:port: 8761spring: application: name: eureka-service eureka:instance:prefer-ip-address: falseclient:region: defaultservice-url:defaultZone: http://eureka-0.eureka.default:${server.port}/eureka/,http://eureka-1.eureka.default:${server.port}/eureka/register-with-eureka: truefetch-registry: true |

其他应用程序往eureka注册,需要在各自的application.yaml里添加下面的eureka信息

?

| 123456789 | eureka:instance:prefer-ip-address: falseclient:region: defaultservice-url:defaultZone: http://eureka-0.eureka:8761/eureka/,http://eureka-1.eureka:8761/eureka/register-with-eureka: truefetch-registry: true |

3、创建statefulset资源文件部署eureka,ingress做负载均衡

(将所以要创建的资源都合并到一个yaml文件,上传到git仓库 项目代码的eureka目录下)

?

| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 | #创建Service为headlessapiVersion: v1kind: Servicemetadata:namespace: NSlabels:app: eurekaname: eurekaspec:clusterIP: Noneselector:app: eurekaports:- port: 8761protocol: TCPtargetPort: 8761status:loadBalancer: {} ---#创建StatefulSetapiVersion: apps/v1kind: StatefulSetmetadata:name: eurekanamespace: NSspec:selector:matchLabels:app: eurekaserviceName: "eureka"replicas: RSCOUNTtemplate:metadata:labels:app: eurekaspec:imagePullSecrets:- name: SECRET_NAMEcontainers:- name: eurekaimage: IMAGE_NAMEports:- containerPort: 8761 ---#创建ingressapiVersion: networking.k8s.io/v1kind: Ingressmetadata:name: eureka-ingressannotations:kubernetes.io/ingress.class: "nginx"spec:rules:- host: eureka.test.comhttp:paths:- path: /pathType: Prefixbackend:service:name: eurekaport: number: 8761 |

4、在jenkins创建流水线任务

新建任务——eureka 流水线——然后进入项目配置,Jenkinsfile如下:

?

| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 | 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: Podmetadata:name: jenkins-slavespec:containers:- name: jnlpimage: "${registry}/library/jenkins-slave-jdk:latest"imagePullPolicy: Alwaysenv: - name: TZvalue: Asia/ShanghaivolumeMounts:- name: docker-cmdmountPath: /usr/bin/docker- name: docker-sockmountPath: /var/run/docker.sock- name: gradle-cachemountPath: /root/.gradlevolumes:- name: docker-cmdhostPath:path: /usr/bin/docker- name: docker-sockhostPath:path: /var/run/docker.sock- name: gradle-cachehostPath: 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 -versionpwdcd ${app_name}pwdgradle -vgradle clean build -x test""" }} stage('构建镜像'){steps {withCredentials([usernamePassword(credentialsId: "${docker_registry_auth}", passwordVariable: 'password', usernameVariable: 'username')]) {sh """echo 'FROM ${registry}/library/java:11MAINTAINER liangRUN apk add -U tzdata && \ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtimeADD ${jar_path}/${jar_name} /EXPOSE ${jar_port}CMD java -jar $JAVA_OPTS /${jar_name}'> ${app_name}/Dockerfiledocker 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 """pwdsed -i 's#IMAGE_NAME#${image_name}#' ${app_name}/deploy.yamlsed -i 's#SECRET_NAME#${secret_name}#' ${app_name}/deploy.yamlsed -i 's#RSCOUNT#${ReplicaCount}#' ${app_name}/deploy.yamlsed -i 's#NS#${Namespace}#' ${app_name}/deploy.yamlkubectl apply -f ${app_name}/deploy.yaml -n ${Namespace} --kubeconfig=admin.kubeconfigsleep 10kubectl get pod -n ${Namespace} --kubeconfig=admin.kubeconfig"""}}}}} |

构建项目:可以选择启动的副本数、项目代码分支、k8s命名空间(dev、test、prod:代表开发、测试、生产环境)

  ![](https://img-blog.csdnimg.cn/img_convert/6e127a9a36ae2eda36c45ad1345bf506.png)

查看构建日志:

   bb2ef23a29da8e089d2717e769fa2ce4 - K8S+Jenkins自动化构建微服务项目(后续)

    ![](https://img-blog.csdnimg.cn/img_convert/93ab65a7579e9433b2a71199250cb08e.png)
浏览器访问eureka:
  需要添加dns记录或者host文件(根据自己的实际情况添加)
 http://eureka.test.com ![](https://img2022.cnblogs.com/blog/2347845/202204/2347845-20220419163344540-114518815.png)

5、构建其他应用程序部署到K8S

修改微服务网关gateway的配置文件application.yaml:

?

| 123456789101112131415 | server:port: 8130spring: application: name: gateway-service eureka:instance:prefer-ip-address: falseclient:region: defaultservice-url:defaultZone: http://eureka-0.eureka:8761/eureka/,http://eureka-1.eureka:8761/eureka/register-with-eureka: truefetch-registry: true |

创建deploy资源 部署gateway,ingress做负载均衡

?

| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 | apiVersion: v1kind: Servicemetadata:namespace: NSlabels:app: gatewayname: gatewayspec:selector:app: gatewayports:- port: 8130protocol: TCPtargetPort: 8130status:loadBalancer: {}---#创建DeploymentapiVersion: apps/v1kind: Deploymentmetadata:name: gatewaynamespace: NSspec:selector: matchLabels:app: gatewayreplicas: RSCOUNTtemplate:metadata:labels:app: gatewayspec:imagePullSecrets:- name: SECRET_NAMEcontainers:- name: gatewayimage: IMAGE_NAMEports:- containerPort: 8130---#创建ingressapiVersion: networking.k8s.io/v1kind: Ingressmetadata:name: gateway-ingressannotations:kubernetes.io/ingress.class: "nginx"spec:rules:- host: gateway.test.comhttp:paths:- path: /pathType: Prefixbackend:service:name: gatewayport: number: 8130 |

jenkins中创建gateway项目,使用eureka的Jenkinsfile,只需要修改其中的变量就可以了,这里就不写了

......

应用程序构建完成后,查看是否注册到eureka:

   00865a090b7aaefbf3fc044ac1b12d72 - K8S+Jenkins自动化构建微服务项目(后续)

 再查看eureka日志,说明成功了

   69ad38cad2ca9a516e8fae1149a15fc8 - K8S+Jenkins自动化构建微服务项目(后续)

大概就是这样,部署微服务的其他应用程序也是一样。

需要注意的是:项目中application.yaml的参数要问开发人员(有些程序需要连数据库)

转载请注明:xuhss » K8S+Jenkins自动化构建微服务项目(后续)

喜欢 (0)

您必须 登录 才能发表评论!