Jenkins集成Kubernetes 部署springboot项目

文章目录

准备部署的yml文件

yml 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: test
  name: pipeline
  labels:
    app: pipeline
spec:
  replicas: 2
  selector:
    matchLabels:
      app: pipeline
  template:
    metadata:
      labels:
        app: pipeline
    spec:
      containers:
        - name: pipeline
          image: 192.168.56.112:80/repository/pipeline-test:1.0.0
          imagePullPolicy: Always
          ports:
            - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  namespace: test
  labels:
    app: pipeline
  name: pipeline
spec:
  selector:
    app: pipeline
  ports:
    - port: 8081
      targetPort: 8080
  type: NodePort
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  namespace: test
  name: pipeline
spec:
  ingressClassName: ingress
  rules:
    - host: batman.pipeline.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: pipeline
                port:
                  number: 8081

Harbor私服配置

在尝试用kubernetes的yml文件启动pipeline服务时,会出现Kubernetes无法拉取镜像的问题,这里需要在kubernetes所在的Linux中配置Harbor服务信息,并且保证Kubernetes可以拉取Harbor上的镜像

  • 设置免密登陆k8s节点

    k8s 111上执行

    mkdir /root/.ssh

    然后再 jenkins 112中执行如下操作

    1、采用dsa加密生成密钥,也可以采用rsa加密

    ssh-keygen -t dsa

    然后一路回车。

    上面将密钥生成在默认路径/root/.ssh下,然后将其内容输出到/root/.ssh/authorized_keys中

    cat /root/.ssh/id_dsa.pub >> /root/.ssh/authorized_keys

    然后将公钥复制到其他两个需要免密钥登录的k8s 111中

    scp /root/.ssh/authorized_keys 192.168.56.111:/root/.ssh/

    ssh 192.168.56.111

  • 解决拉取私有镜像的权限问题

    kubectl create secret docker-registry myharbor
    --docker-server=192.168.56.112:80
    --docker-username=admin
    --docker-password=Harbor12345
    --docker-email=943104990@qq.com
    -n test

  • pod spec 配置imagePullSecrets

    spec:
    containers:
    - name: pipeline
    image: 192.168.56.112:80/repository/pipeline-test:1.0.0
    imagePullPolicy: Always
    ports:
    - containerPort: 8080
    imagePullSecrets:
    - name: myharbor

测试使用效果

kubectl apply -f pipeline.yml

访问 http://192.168.56.111:30674/user/2

Jenkins远程调用

  • 将pipeline.yml配置到Gitlab中

  • 配置Jenkins的目标服务器,可以将yml文件传输到K8s的Master上

  • 设置执行kubectl的脚本到Jenkinsfile

    apiVersion: apps/v1
    kind: Deployment
    metadata:
    namespace: test
    name: pipeline
    labels:
    app: pipeline
    spec:
    replicas: 2
    selector:
    matchLabels:
    app: pipeline
    template:
    metadata:
    labels:
    app: pipeline
    spec:
    containers:
    - name: pipeline
    image: 192.168.56.112:80/repository/pipeline-test:1.0.0
    imagePullPolicy: Always
    ports:
    - containerPort: 8080
    imagePullSecrets:
    - name: myharbor

    apiVersion: v1
    kind: Service
    metadata:
    namespace: test
    labels:
    app: pipeline
    name: pipeline
    spec:
    selector:
    app: pipeline
    ports:
    - port: 8081
    targetPort: 8080
    type: NodePort

    pipeline {
    agent any
    environment{
    harborHost = '192.168.56.112:80'
    harborRepo = 'repository'
    harborUser = 'admin'
    harborPasswd = 'Harbor12345'
    port = 8080
    host_port = 8888
    }
    stages {

          stage('拉取Git代码') {
              steps {
                  checkout scmGit(branches: [[name: '${tag}']], extensions: [], userRemoteConfigs: [[credentialsId: 'gitee', url: 'https://gitee.com/qfxcoffee/shield.git']])
              }
          }
    
          stage('构建代码') {
              steps {
                  sh 'cd arthas-study && /var/jenkins_home/maven/bin/mvn clean package -DskipTests'
              }
          }
    
          stage('制作自定义镜像并发布Harbor') {
              steps {
                  sh '''cd arthas-study/docker
                        mv ../target/arthas-study.jar ./arthas-study.jar
                  docker build -t ${JOB_NAME}:${tag} ./'''
    
                  sh '''docker login -u ${harborUser} -p ${harborPasswd} ${harborHost}
                  docker tag ${JOB_NAME}:${tag} ${harborHost}/${harborRepo}/${JOB_NAME}:${tag}
                  docker push ${harborHost}/${harborRepo}/${JOB_NAME}:${tag}'''
              }
          }
    

    // stage('目标服务器拉取镜像并运行') {
    // steps {
    // sshPublisher(publishers: [sshPublisherDesc(configName: 'testEnv', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "/usr/bin/deploy.sh $harborHost $harborRepo $JOB_NAME $tag $port $host_port", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
    // }
    // }
    stage('将yml文件传到k8s-master上') {
    steps {
    sshPublisher(publishers: [sshPublisherDesc(configName: 'k8s', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'arthas-study/pipeline.yml')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
    }
    }
    stage('远程执行k8s-master的kubectl命令'){
    steps {
    // sh 'ssh root@192.168.56.111 kubectl apply -f /test/arthas-study/pipeline.yml'
    sshPublisher(publishers: [sshPublisherDesc(configName: 'k8s', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "kubectl apply -f /test/arthas-study/pipeline.yml", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])

              }
          }
      }
    

    }

  • 查看效果

参考文章

微服务架构学习(二)之CentOS安装minikube

相关推荐
鸭梨山大。42 分钟前
Jenkins 任意文件读取(CVE-2024-23897)修复及复现
安全·中间件·jenkins
苹果醋344 分钟前
2020重新出发,MySql基础,MySql表数据操作
java·运维·spring boot·mysql·nginx
小蜗牛慢慢爬行1 小时前
如何在 Spring Boot 微服务中设置和管理多个数据库
java·数据库·spring boot·后端·微服务·架构·hibernate
两张不够花1 小时前
Jenkins 持续集成部署
运维·jenkins
azhou的代码园1 小时前
基于JAVA+SpringBoot+Vue的制造装备物联及生产管理ERP系统
java·spring boot·制造
码农土豆1 小时前
PaddlePaddle飞桨Linux系统Docker版安装
linux·docker·paddlepaddle
wm10431 小时前
java web springboot
java·spring boot·后端
路在脚下@9 小时前
spring boot的配置文件属性注入到类的静态属性
java·spring boot·sql
啦啦右一9 小时前
Spring Boot | (一)Spring开发环境构建
spring boot·后端·spring
森屿Serien9 小时前
Spring Boot常用注解
java·spring boot·后端