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

相关推荐
萨格拉斯救世主38 分钟前
jenkins使用slave节点进行node打包报错问题处理
运维·jenkins
morris13140 分钟前
【SpringBoot】Xss的常见攻击方式与防御手段
java·spring boot·xss·csp
川石课堂软件测试1 小时前
性能测试|docker容器下搭建JMeter+Grafana+Influxdb监控可视化平台
运维·javascript·深度学习·jmeter·docker·容器·grafana
阿伟*rui4 小时前
配置管理,雪崩问题分析,sentinel的使用
java·spring boot·sentinel
paopaokaka_luck5 小时前
【360】基于springboot的志愿服务管理系统
java·spring boot·后端·spring·毕业设计
Yaml47 小时前
Spring Boot 与 Vue 共筑二手书籍交易卓越平台
java·spring boot·后端·mysql·spring·vue·二手书籍
小小小妮子~7 小时前
Spring Boot详解:从入门到精通
java·spring boot·后端
hong1616887 小时前
Spring Boot中实现多数据源连接和切换的方案
java·spring boot·后端
追风林8 小时前
mac 本地docker-mysql主从复制部署
mysql·macos·docker
程序媛小果9 小时前
基于java+SpringBoot+Vue的旅游管理系统设计与实现
java·vue.js·spring boot