基于jenkins+k8s实现devops

1、背景

由于jenkins运行在k8s上能够更好的利用动态agent进行构建。所以写了个部署教程,亲测无坑

2、部署

1、创建ns

kubectl create namespace devops

2、kubectl apply -f jenkins.yml

yaml 复制代码
apiVersion: v1
kind: ServiceAccount
metadata:
  name: jenkins
  namespace: devops
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: jenkins
rules:
  - apiGroups: ["extensions", "apps"]
    resources: ["deployments", "ingresses"]
    verbs: ["create", "delete", "get", "list", "watch", "patch", "update"]
  - apiGroups: [""]
    resources: ["services"]
    verbs: ["create", "delete", "get", "list", "watch", "patch", "update"]
  - apiGroups: [""]
    resources: ["pods"]
    verbs: ["create", "delete", "get", "list", "patch", "update", "watch"]
  - apiGroups: [""]
    resources: ["pods/exec"]
    verbs: ["create", "delete", "get", "list", "patch", "update", "watch"]
  - apiGroups: [""]
    resources: ["pods/log", "events"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["secrets"]
    verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: jenkins
  namespace: devops
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: jenkins
subjects:
  - kind: ServiceAccount
    name: jenkins
    namespace: devops
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins
  namespace: devops
spec:
  selector:
    matchLabels:
      app: jenkins
  template:
    metadata:
      labels:
        app: jenkins
    spec:
      serviceAccount: jenkins
      initContainers:
        - name: fix-permissions
          image: busybox:1.35.0
          command: ["sh", "-c", "chown -R 1000:1000 /var/jenkins_home"]
          securityContext:
            privileged: true
          volumeMounts:
            - name: jenkinshome
              mountPath: /var/jenkins_home
      containers:
        - name: jenkins
          image: jenkins/jenkins:2.414.1-lts-jdk11
          imagePullPolicy: IfNotPresent
          env:
            - name: JAVA_OPTS
              value: -Dhudson.model.DownloadService.noSignatureCheck=true
          ports:
            - containerPort: 8080
              name: web
              protocol: TCP
            - containerPort: 50000
              name: agent
              protocol: TCP
          readinessProbe:
            httpGet:
              path: /login
              port: 8080
            initialDelaySeconds: 60
            timeoutSeconds: 5
            failureThreshold: 12
          volumeMounts:
            - name: jenkinshome
              mountPath: /var/jenkins_home
            - name: localtime
              mountPath: /etc/localtime  
      volumes:
        - name: jenkinshome
          hostPath:
            path: /opt/jenkins/jenkins_data
        - name: localtime
          hostPath:
            path: /etc/localtime
---
apiVersion: v1
kind: Service
metadata:
  name: jenkins
  namespace: devops
  labels:
    app: jenkins
spec:
  selector:
    app: jenkins
  ports:
    - name: web
      port: 8080
      targetPort: web
    - name: agent
      port: 50000
      targetPort: agent
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: jenkins
  namespace: devops
spec:
  ingressClassName: nginx
  rules:
    - host: jenkins.k8s.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: jenkins
                port:
                  name: web

注意:镜像建议使用最新版本,因为jenkin平台默认提供了最新的插件,且无法选择版本,所以如果jenkins版本过低会导致插件不兼容问题

3、本地电脑配置host解析后,就可以用域名访问

4、查看pod日志获取初始化密码,也可以查看/opt/jenkins/jenkins_data/secrets/initialAdminPassword

5、安装必要插件

中文插件: Localization: Chinese

pipeline插件:Pipeline

k8s插件: Kubernetes

代码库管理插件:Git

6、配置k8s连接信息


填写 以下内容 ,然后点击测试。

k8s地址 :https://kubernetes.default.svc.cluster.local

命名空间:devops

jenkins地址:http://jenkins.devops.svc.cluster.local:8080

由于之前部署的时候已经给jenkins用户访问k8s 的devops命名空间的权限,所以这里不需要配置kubeconfig认证也可直接访问

3、编写一条pipeline

这里用一个java项目的ci过程作为案例

groovy 复制代码
def createVersion() {
    // 定义一个版本号作为当次构建的版本,输出结果 20191210175842_69
    return new Date().format('yyyyMMddHHmmss') + "_${env.BUILD_ID}"
}

pipeline{
    agent{
        kubernetes{
          defaultContainer 'maven'
          yaml '''
apiVersion: v1
kind: Pod
spec:
  containers:
    - name: maven
      image: maven:3.8.1-jdk-8
      command: ["sleep"]
      args: ["99d"]
    - name: docker
      image: docker
      command: ["sleep"]
      args: ["99d"]
      volumeMounts:
        - mountPath: /var/run/docker.sock
          name: docker-socket
  volumes:
    - name: docker-socket
      hostPath:
        path: /var/run/docker.sock
'''
        }
    }
    environment {
        tag = createVersion()
    }
    stages{
        stage("pull code"){
          steps{
            script{
              git 'https://gitee.com/uuei/java-devops-demo.git'
            }
          }
        }
        stage("mvn"){
          steps{
            script{
              sh 'mvn clean package'
            }
            container('docker') {
              script {
              sh 'docker build -t java-demo:${tag} .'
            }
        }
          }
        }
    }
}
相关推荐
JianZhen✓12 小时前
后端进阶 13 问:CI/CD、Prisma、ORM、ER 图、SSE、索引事务、Nest 生命周期一次讲透(附面试答法)
ci/cd·面试·职场和发展
xbzb14 小时前
Kubernetes ReplicaSet:副本数自愈、标签接管与 orphan 删除实测
容器·kubernetes·删除·pod·replicaset·orphan
赤生云电竞2 天前
算力调度系统架构实践:计费、配额与抢占策略设计——基于Kubernetes 1.28与YARN 3.3的参数选型
贪心算法·系统架构·kubernetes
JonaHu2 天前
AceDataCloud Coding Plan 多模型编程:本地、CI 与团队仓库的 Token 隔离实战
ai编程·devops·codex·api安全·claude code
如夏倾城2 天前
入侵检测系统(IDS)原理、部署模式与 IPS/防火墙对比
大数据·网络·人工智能·网络安全·云原生·架构·kubernetes
Z21-X2 天前
云原生运维学习笔记:Kubernetes Volume 与 Secret 实验
运维·笔记·学习·云原生·kubernetes
吴建旭 智宅焕3 天前
智能家居全国交付中执行端能力评估与分级调度架构设计:从注册数量到交付确定性的系统实现
运维·智能家居·devops
A-刘晨阳4 天前
K8S 二进制集群搭建(一主两从)v1.37.2 版本
运维·云原生·容器·kubernetes·calico·nerdctl
工程小白菜4 天前
ai-code-review 全链路:让 AI 来审 AI 的代码
ci/cd·ai编程
goyeer5 天前
【Kubernetes】Kubernetes之CRI详解
java·容器·kubernetes