Jenkins----docker部署Jenkins

Jenkins

  • 一、Jenkins简介
  • 二、Jenkins部署
  • 三、kubernetes部署

一、Jenkins简介

Jenkins是一款开源 CI&CD 软件,用于自动化各种任务,包括构建、测试和部署软件。

Jenkins 支持各种运行方式,可通过系统包、Docker 或者通过一个独立的 Java 程序。

详细介绍见官方文档:https://www.jenkins.io/zh/doc/

二、Jenkins部署

docker run -d --name jenkins -u root -p 8080:8080 -p 50000:50000 -v /data/docker/jenkins/:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock  jenkins/jenkins:lts

查看日志如下:

docker logs jenkins -f
...
Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:

133885fb50244c1fb7e2d2bf5ca30825   # 这就是初始admin密码

This may also be found at: /var/jenkins_home/secrets/initialAdminPassword

*************************************************************
*************************************************************
*************************************************************
...

获取管理员密码,查看日志如上面所示,或者查看容器内/var/jenkins_home/secrets/initialAdminPassword文件(也可以查看本地挂载目录下的initialAdminPassword文件)

docker exec -it jenkins cat /var/jenkins_home/secrets/initialAdminPassword

访问http://localhost:8080/,会看到如下界面,然后将获取的管理员密码复制粘贴到登录框中点击继续即可:
Jenkins----docker部署Jenkins_第1张图片
进入Jenkins后会有插件安装的过程,由于默认插件源在国外服务器,多数网络无法顺利下载,需要改国内插件源地址,然后重启Jenkins再次进入插件安装界面即可:

docker exec -it jenkins bash
cd cd /var/jenkins_home/updates/
sed -i 's/http:\/\/updates.jenkins-ci.org\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' default.json 
sed -i 's/http:\/\/www.google.com/https:\/\/www.baidu.com/g' default.json
docker restart jenkins

Jenkins----docker部署Jenkins_第2张图片
更换源后下载速度将会很快
Jenkins----docker部署Jenkins_第3张图片
插件安装完毕后,会进入创建第一个管理员用户界面,填写一些用户信息,点击保存并完成进入下一步
Jenkins----docker部署Jenkins_第4张图片
实例配置,实验的话就不用修改了,生产环境使用配置成分配的域名即可
Jenkins----docker部署Jenkins_第5张图片
点击开始使用Jenkins进入工作台,至此安装完毕
Jenkins----docker部署Jenkins_第6张图片
Jenkins----docker部署Jenkins_第7张图片

三、kubernetes部署

参考:https://github.com/jenkinsci/kubernetes-plugin/tree/master/src/main/kubernetes

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: jenkins
  labels:
    name: jenkins
spec:
  selector:
    matchLabels:
      name: jenkins
  serviceName: jenkins
  replicas: 1
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      name: jenkins
      labels:
        name: jenkins
    spec:
      terminationGracePeriodSeconds: 10
      serviceAccountName: jenkins
      containers:
        - name: jenkins
          image: jenkins/jenkins:lts-alpine
          imagePullPolicy: Always
          ports:
            - containerPort: 8080
            - containerPort: 50000
          resources:
            limits:
              cpu: 1
              memory: 1Gi
            requests:
              cpu: 0.5
              memory: 500Mi
          env:
            - name: LIMITS_MEMORY
              valueFrom:
                resourceFieldRef:
                  resource: limits.memory
                  divisor: 1Mi
            - name: JAVA_OPTS
              value: -Xmx$(LIMITS_MEMORY)m -XshowSettings:vm -Dhudson.slaves.NodeProvisioner.initialDelay=0 -Dhudson.slaves.NodeProvisioner.MARGIN=50 -Dhudson.slaves.NodeProvisioner.MARGIN0=0.85
          volumeMounts:
            - name: jenkins-home
              mountPath: /var/jenkins_home
          livenessProbe:
            httpGet:
              path: /login
              port: 8080
            initialDelaySeconds: 60
            timeoutSeconds: 5
            failureThreshold: 12
          readinessProbe:
            httpGet:
              path: /login
              port: 8080
            initialDelaySeconds: 60
            timeoutSeconds: 5
            failureThreshold: 12
      securityContext:
        fsGroup: 1000
  volumeClaimTemplates:
  - metadata:
      name: jenkins-home
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi
---
apiVersion: v1
kind: Service
metadata:
  name: jenkins
spec:
  selector:
    name: jenkins
  ports:
    - name: http
      port: 80
      targetPort: 8080
      nodePort: 30800
      protocol: TCP
    - name: agent
      port: 50000
      protocol: TCP
  type: NodePort
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: jenkins
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: jenkins
rules:
- 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"]
  verbs: ["get","list","watch"]
- apiGroups: [""]
  resources: ["events"]
  verbs: ["watch"]
- apiGroups: [""]
  resources: ["secrets"]
  verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:
  name: jenkins
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: jenkins
subjects:
- kind: ServiceAccount
  name: jenkins

你可能感兴趣的:(Jenkins,Docker)