第2章 Jenkins Server的安装部署方式

2020-07-24 18:07:58 浏览数 (1)

第2章 Jenkins Server的安装部署方式

  • 1.1 安装依赖
  • 1.2 安装部署
    • 1.2.1 使用Tomcat部署
    • 1.2.2 CentOS环境部署
    • 1.2.3 MacOS环境部署
    • 1.2.4 基于Docker部署
    • 1.2.5 基于Kubernetes部署
    • 1.2.6 基于Windows部署
    • 1.2.7 软件资源下载
  • 1.3 配置

本章阐述Jenkins Server的安装部署方式。基于Tomcat、CentOS、MacOS、Docker、Kubernetes、Windows部署。部署后的基础配置。


1.1 安装依赖

Jenkins是基于Java开发的一种持续集成工具,可以在安装Java Runtime Environment的任何机器独立运行。部署前需要安装Java开发环境。要求是JDK8 ,目前已经支持JDK11。关于JDK的安装可以通过yum或者源码包方式。

rpm方式

代码语言:javascript复制
rpm -ivh jdk-xxxxx.rpm
yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel

源码包方式

代码语言:javascript复制
tar zxf jdk-xxxx.tar.gz -C /usr/local
vim /etc/profile

export JAVA_HOME=/usr/local/jdkxxx
export PATH=$PATH:$JAVA_HOME/bin

source /etc/profile

1.2 安装部署

1.2.1 使用Tomcat部署

如果使用war包部署建议采用tomcat,当然Jenkins的war包可以直接通过java -jar jenkins.war启动。我们可以部署一个tomcat服务然后将jenkins.war包放到tomcat的webapps目录下。最后通过浏览器http://tomcatserver/jenkins访问。

代码语言:javascript复制
wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-9/v9.0.24/bin/apache-tomcat-9.0.24.tar.gz
tar xf apache-tomcat-9.0.24.tar.gz -C /usr/local/
cp jenkins.war apache-tomcat-9.0.24/webapps/

# 启动
/usr/local/tomcat/bin/catalina.sh start

# 停止
/usr/local/tomcat/bin/shutdown.sh

1.2.2 CentOS环境部署

普遍在centos系统部署Jenkins,可以下载jenkins的最新rpm包部署。

代码语言:javascript复制
wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat-stable/jenkins-2.235.2-1.1.noarch.rpm

rpm -ivh jenkins-2.235.2-1.1.noarch.rpm

systemctl start jenkins
systemctl enable jenkins

1.2.3 MacOS环境部署

在Mac OS X机器上安装Jenkins CI服务器,可以手动下载pkg包安装或者通过brew自动安装。

手动下载pkg包安装,根据向导进一步安装。

使用brew部署

代码语言:javascript复制
brew install jenkins-lts


######################################################################## 100.0%
==> Downloading http://mirrors.jenkins.io/war-stable/2.235.2/jenkins.war
==> Downloading from http://mirror.serverion.com/jenkins/war-stable/2.235.2/jenki
######################################################################## 100.0%
==> Installing dependencies for jenkins-lts: openjdk@11
==> Installing jenkins-lts dependency: openjdk@11
==> Pouring openjdk@11-11.0.7 10.high_sierra.bottle.tar.gz
==> Caveats
For the system Java wrappers to find this JDK, symlink it with
  sudo ln -sfn /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk

openjdk@11 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have openjdk@11 first in your PATH run:
  echo 'export PATH="/usr/local/opt/openjdk@11/bin:$PATH"' >> /Users/zeyang/.bash_profile

For compilers to find openjdk@11 you may need to set:
  export CPPFLAGS="-I/usr/local/opt/openjdk@11/include"

==> Summary
?  /usr/local/Cellar/openjdk@11/11.0.7 10: 650 files, 295.7MB
==> Installing jenkins-lts
==> /usr/local/opt/openjdk@11/bin/jar xvf jenkins.war
==> Caveats
Note: When using launchctl the port will be 8080.

To have launchd start jenkins-lts now and restart at login:
  brew services start jenkins-lts
Or, if you don't want/need a background service you can just run:
  jenkins-lts
==> Summary
?  /usr/local/Cellar/jenkins-lts/2.235.2: 7 files, 66.4MB, built in 11 seconds
==> Caveats
==> openjdk@11
For the system Java wrappers to find this JDK, symlink it with
  sudo ln -sfn /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk

openjdk@11 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have openjdk@11 first in your PATH run:
  echo 'export PATH="/usr/local/opt/openjdk@11/bin:$PATH"' >> /Users/zeyang/.bash_profile

For compilers to find openjdk@11 you may need to set:
  export CPPFLAGS="-I/usr/local/opt/openjdk@11/include"

==> jenkins-lts
Note: When using launchctl the port will be 8080.

To have launchd start jenkins-lts now and restart at login:
  brew services start jenkins-lts
Or, if you don't want/need a background service you can just run:
  jenkins-lts
  
  
   
# Start.
brew services start jenkins-lts

# Stop.
brew services stop jenkins-lts

1.2.4 基于Docker部署

代码语言:javascript复制
docker pull jenkins/jenkins:lts

docker run -d -v jenkins_home:/var/jenkins_home -p 8080:8080 -p 50000:50000 jenkins/jenkins:lts


## 参数解释
-d 后台运行
-v 持久化jenkins数据目录
-p 端口绑定  server:8080  agent:50000

1.2.5 基于Kubernetes部署

首先创建一个namespace jenkins ,然后通过kubectl create -f jenkins.yml部署。

这是一个完整的Deployment文件。

代码语言:javascript复制
kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    k8s-app: jenkins
  name: jenkins
  namespace: devops
spec:
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s-app: jenkins
  template:
    metadata:
      labels:
        k8s-app: jenkins
      namespace: devops
      name: jenkins
    spec:
      containers:
        - name: jenkins
          image: jenkins/jenkins:2.211
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 30080
              name: web
              protocol: TCP
            - containerPort: 30081
              name: agent
              protocol: TCP
          resources:
            limits:
              cpu: 1000m
              memory: 2Gi
            requests:
              cpu: 500m
              memory: 512Mi
          livenessProbe:
            httpGet:
              path: /login
              port: 30080
            initialDelaySeconds: 60
            timeoutSeconds: 5
            failureThreshold: 12
          readinessProbe:
            httpGet:
              path: /login
              port: 30080
            initialDelaySeconds: 60
            timeoutSeconds: 5
            failureThreshold: 12
          volumeMounts:
            - name: jenkins-home
              mountPath: /var/lib/jenkins
          env:
            - name: JENKINS_HOME
              value: /var/lib/jenkins
            - name: JENKINS_OPTS 
              value: --httpPort=30080
            - name: JENKINS_SLAVE_AGENT_PORT
              value: "30081"
      volumes:
        - name: jenkins-home
          hostPath: 
            path: /data/devops/jenkins
            type: Directory
      serviceAccountName: jenkins
---
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    k8s-app: jenkins
  name: jenkins
  namespace: devops
---
kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: jenkins
  name: jenkins
  namespace: devops
spec:
  type: NodePort
  ports:
    - name: web
      port: 30080
      targetPort: 30080
      nodePort: 30080
    - name: slave
      port: 30081
      targetPort: 30081
      nodePort: 30081
  selector:
    k8s-app: jenkins
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
 name: jenkins
 namespace: devops
rules:
 - apiGroups: [""]
   resources: ["pods","configmaps","namespaces"]
   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: ["secrets"]
   verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:
 name: jenkins
 namespace: devops
roleRef:
 apiGroup: rbac.authorization.k8s.io
 kind: Role
 name: jenkins
subjects:
 - kind: ServiceAccount
   name: jenkins
   namespace: devops

jenkins端口可以自定义,这里使用的是30080端口和30081端口。持久化的目录采用的是hostpath方式,这种方式仅能适合本地测试。建议如果生产还是采用共享存储pvc来持久化。否则节点漂移到其他节点会出现数据丢失。

1.2.6 基于Windows部署

下载软件包通过向导安装即可。windows这里基本上很少用的了。

1.2.7 软件资源下载

JDK下载链接:https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html

Mac pkg软件包下载:https://mirrors.tuna.tsinghua.edu.cn/jenkins/osx-stable/

Jenkins war包下载链接:https://mirrors.tuna.tsinghua.edu.cn/jenkins/war-stable/

Jenkins rpm包下载链接:https://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat-stable/

Jenkins Docker hub:https://hub.docker.com/r/jenkins/jenkins

k8s yaml:https://github.com/zeyangli/devops-on-k8s/blob/master/devops/jenkins.yaml

windows 下载:https://mirrors.tuna.tsinghua.edu.cn/jenkins/windows-stable/


1.3 配置

解锁:当您第一次访问Jenkins的时候,系统会要求您使用自动生成的密码对其进行解锁。解锁秘钥可以通过$JENKINS_HOME/secrets/initialAdminPassword文件获取。还可以通过在启动日志中获取。

安装插件:分别是安装默认的插件和自定义选择要安装的插件,推荐还是使用默认的插件安装。

创建用户:在这里创建一个管理员账号,到此jenkins的初始化配置已完成。


配置更新站点:由于使用官方的站点速度相对很慢,这里采用清华大学的jenkins更新站点。

这块经常出现问题:当改完后其实还是很慢,因为清华源代理的json文件的内容中还是指向的外网地址。推荐几种方法:

方法1:使用jenkins中文社区的插件源。安装中文社区插件(插件搜索关键字chinese),然后点击页面最下方超链接。最后会有提示使用插件源。

方法2: 配置代理域名转发

代码语言:javascript复制
location /download {
    proxy_pass http://mirrors.tuna.tsinghua.edu.cn/jenkins/;
}

方法3:进入 jenkins 工作目录,目录下面有一个 updates 的目录,下面有一个 default.json 文件,我们执行下面的命令替换插件地址:替换完成后,需要重启 Jenkins。

代码语言:javascript复制
$ 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

我们一般应用最多的应该是在linux或者docker中使用。在linux环境中jenkins默认的配置文件在/etc/sysconfig/jenkins中,默认的启动用户为jenkins,默认端口为8080。后期都是可以基于配置文件进行修改的。

到此我们就完成了,Jenkins server节点的安装部署和基本的配置了。后期我们来分享关于agent节点的部署。

分享结束

关于作者

泽阳,DevOps领域实践者。专注于企业级DevOps运维开发技术实践分享,主要以新Linux运维技术、DevOps技术课程为主。丰富的一线实战经验,课程追求实用性获得多数学员认可。课程内容均来源于企业应用,在这里既学习技术又能获取热门技能,欢迎您的到来!

0 人点赞