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

因为之前写过基于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:代表开发、测试、生产环境)

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

查看构建日志:

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

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

浏览器访问eureka:
  需要添加dns记录或者host文件(根据自己的实际情况添加)
   http://eureka.test.com
《K8S+Jenkins自动化构建微服务项目(后续)》

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:

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

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

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

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

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

 

    原文作者:等风来~~
    原文地址: https://www.cnblogs.com/cfzy/p/16160965.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞