Devops-day2-Jenkins自动化部署工具配置安装
文章目录
Jenkins安装
Jenkins是依赖于Java开发、且由各种组件组成的一个自动化部署工具。
准备机器 | IP | 配置要求 |
---|---|---|
jenkins | 192.168.12.83 | 内存3g+/硬盘20G+ |
- 基础优化
setenforce 0
systemctl disable --now firewalld
sed -i 's#enforcing#disabled#g' /etc/sysconfig/selinux
1.安装Jenkins
# 安装Java依赖
[root@jenkins ~]# yum install java-1.8.0-openjdk* -y
方式一
# 安装Jenkins(推荐,因为yum默认为新版本,不建议安装太新的版本)
[root@jenkins ~]# mkdir packages && cd packages
[root@jenkins packages]# wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat-stable/jenkins-2.249.1-1.1.noarch.rpm
[root@jenkins packages]# yum localinstall -y jenkins-2.249.1-1.1.noarch.rpm
方式二
# 安装Jenkins
[root@jenkins ~]# yum install jenkins
# yum方式安装需要安装认证 Key
[root@jenkins ~]# rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
方式三
# Docker方式部署
docker run -dit --name jenkins -p 8080:8080 -p 50000:50000 -u root \
-v /var/jenkins_home:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /usr/bin/docker:/usr/bin/docker \
-v /usr/local/bin/kubectl:/usr/bin/kubectl \
-v /root/.kube/admin.kubeconfig:/root/.kube/admin.kubeconfig \
-v /usr/local/apache-maven-3.5.0:/usr/local/apache-maven-3.5.0 \
-v /usr/local/jdk1.8.0_171:/usr/local/jdk1.8.0_171 \
-e MAVEN_HOME=/usr/local/maven-3.5.0/bin \
-e JAVA_HOME=/usr/local/jdk1.8.0_171/bin \
-v /etc/localtime:/etc/localtime \
-v /etc/resolv.conf:/etc/resolv.conf \
-v /etc/hosts:/etc/hosts \
-e KUBECONFIG=/root/.kube/admin.kubeconfig \
--name jenkins jenkins/jenkins:latest
#-e JAVA_OPTS="-server -Xms1024m -Xmx2048m -XX:PermSize=256m -Duser.timezone=Asia/Shanghai" \
# 限制Jenkins内存占用
PS:
# docker部署jenkins首次启动若一直加载,则修改以下配置后重启jenkins即可
#vim /var/jenkins_home/hudson.model.UpdateCenter.xml
#把 http://updates.jenkins-io/update-center.json 改成 http://mirror.xmission.com/jenkins/updates/update-center.json
2.配置Jenkins
# 切换至配置文件目录
[root@jenkins ~]# cd /var/lib/jenkins/updates
# 替换为清华大学源
[root@jenkins updates]# sed -i 's/http:\/\/updates.jenkinsci.org\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' default.json
# 替换为百度引擎
[root@jenkins updates]# sed -i 's/http:\/\/www.google.com/https:\/\/www.baidu.com/g' default.json
3.启动 Jenkins
- Jenkins是依赖于Java服务端口来访问的
# 启动Jenkins
[root@jenkins packages]# systemctl start jenkins
# 获取Java访问端口:8080
[root@jenkins ~]# netstat -lntp | grep java
tcp6 0 0 :::8080 :::* LISTEN 16969/java
# 浏览器访问:ip+端口
192.168.12.83:8080
# 获取初始密码,填入浏览器
[root@jenkins ~]# cat /var/lib/jenkins/secrets/initialAdminPassword
da091d34b9e64f30adee67192fb36152
# 依次点击(不安装插件,一会手动安装)
选择插件 --> 无 --> 安装 --> 用户名信息自填 --> url默认(有域名可用域名) --> 完成 --> 开始使用!
4.优化为中文界面
- 官网:https://www.jenkins.io/
- 优化为中文页面,网页翻译的不标准
- 可在官方搜索插件安装
# 将优化包覆盖解压至jenkins目录,覆盖原来的plugins(空目录)
[root@jenkins ~]# tar xf packages/plugins.tar.gz -C /var/lib/jenkins/
# 所有文件属主、组权限必须为jenkins,否则进行授权
[root@jenkins ~]# ll /var/lib/jenkins/plugins/
total 245288
drwxr-xr-x 7 jenkins jenkins 124 Sep 16 2020 ace-editor
-rw-r--r-- 1 jenkins jenkins 4279042 Sep 16 2020 ace-editor.jpi
drwxr-xr-x 4 jenkins jenkins 56 Sep 16 2020 antisamy-markup-formatter
-rw-r--r-- 1 jenkins jenkins 112483 Sep 16 2020 antisamy-markup-formatter.bak
-rw-r--r-- 1 jenkins jenkins 2858119 Sep 16 2020 antisamy-markup-formatter.jpi
···
# 重启Jenkins,使其配置生效,重启后需等待几秒才可访问,此时已改为中文页面!
[root@jenkins ~]# systemctl restart jenkins
5.配置网页端
登录后依次点击:
头像 --> 系统管理 -->插件管理 --> 高级 --> 最下面 --> 升级站点改为国内插件下载地址:
https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
回到最顶 --> 可更新 --> 插件管理 --> 勾选以下插件 --> 下载待重启安装
- Blue Ocean # 外部工具集成用户界面
- Docker # 云提供商集群管理和分布式构建
- Docker Pipeline # 开发DevOpsdockerpipeline
- Gitgit # 源码管理
- Git Pipeline for Blue Ocean # 外部工具集成用户界面
- GitLab # 构建触发器
- DingTalk # 钉钉 Jenkins 插件
- Kubernetes # agent云提供商集群管理和分布式构建
- Pipeline implementation for Blue Ocean #外部工具集成用户界面
- Pipeline SCM API for Blue Ocean # 这个插件是BlueOcean插件的一部分
- SSH Agent # 允许您通过Jenkins中的SSH代理提供SSH凭证来构建
- SSH Build Agents # agent代理启动器和控制器
6.忘记jenkins初始密码解决
7.开启创建用户、修改用户密码功能
8.Jenkins已安装插件获取复用脚本
可定制导出插件并导入构建到新的镜像中
目录结构
[root@k8s-master02 dockerfile]# tree
.
├── Dockerfile
├── plugins.sh
└── plugins.txt
0 directories, 3 files
Dockerfile
FROM jenkinsci/blueocean
LABEL manintainer: https://blog.csdn.net/qq_23995091
# 用最新的插件列表文件提花默认插件文件
COPY plugins.txt /usr/share/jenkins/ref
# 执行插件安装
RUN /usr/local/bin/install-plugins.sh < /usr/share/jenkins/ref/plugins.txt
plugins.sh
已安装的Jenkins插件,执行此脚本可一键导出插件名及版本号,方便一键导入使用,无需再次手动选择插件安装!
PS:
只需更改以下内容即可:
admin:123456
:Jenkins
的登录用户/密码
172.23.0.244:31001
:Jenkins
的访问地址/端口
PS:
若是K8s集群内的Jenkins容器,除此之外还可用ClusterIP的地址与端口,或高可用集群VIP
的IP
与端口号
cat > plugins.sh <<EOF
#!/usr/bin/envbash
curl -ssL "http://admin:123456@172.23.0.244:31001/pluginManager/api/xml?depth=1&xpath=/*/*/shortName|/*/*/version&wrapper=plugins" | perl -pe 's/.*?<shortName>([\w-]+).*?<version>([^<]+)()(<\/\w+>)+/\1:\2\n/g'|sed 's/ /:/' > plugins.txt
EOF
plugins.txt
查看以获取的插件列表
[root@jenkins ~]# cat plugins.txt
jjwt-api:0.11.2-9.c8b45b8bb173
sshd:3.1.0
cloudbees-folder:6.17
ant:1.13
antisamy-markup-formatter:2.7
structs:308.v852b473a2b8c
durable-task:493.v195aefbb0ff2
workflow-step-api:622.vb_8e7c15b_c95a_
token-macro:267.vcdaea6462991
workflow-cps:2648.va9433432b33c
build-timeout:1.20
handlebars:3.0.8
trilead-api:1.0.13
jaxb:2.3.0.1
credentials:1074.v60e6c29b_b_44b_
plain-credentials:1.7
workflow-job:1145.v7f2433caa07f
ssh-credentials:1.19
credentials-binding:1.27
momentjs:1.1.1
scm-api:595.vd5a_df5eb_0e39
workflow-api:1108.v57edf648f5d4
timestamper:1.16
caffeine-api:2.9.2-29.v717aac953ff3
mailer:391.ve4a_38c1b_cf4b_
script-security:1131.v8b_b_5eda_c328e
plugin-util-api:2.12.0
workflow-basic-steps:2.24
font-awesome-api:5.15.4-5
popper-api:1.16.1-2
jsch:0.1.55.2
jquery3-api:3.6.0-2
bootstrap4-api:4.6.0-3
gradle:1.38
snakeyaml-api:1.29.1
jackson2-api:2.13.1-246.va8a9f3eaf46a
git-client:3.11.0
popper2-api:2.11.0-1
pipeline-milestone-step:1.3.2
bootstrap5-api:5.1.3-4
echarts-api:5.2.2-2
display-url-api:2.3.5
workflow-support:813.vb_d7c3d2984a_0
git-server:1.10
checks-api:1.7.2
junit:1.53
pipeline-input-step:427.va6441fa17010
matrix-project:1.19
resource-disposer:0.17
branch-api:2.7.0
ws-cleanup:0.40
workflow-durable-task-step:1112.vda00e6febcc1
pipeline-stage-step:291.vf0a8a7aeeb50
command-launcher:1.6
jdk-tool:1.5
pipeline-graph-analysis:188.v3a01e7973f2c
bouncycastle-api:2.25
ace-editor:1.1
workflow-scm-step:2.13
htmlpublisher:1.28
metrics:4.0.2.8
apache-httpcomponents-client-4-api:4.5.13-1.0
pipeline-rest-api:2.20
blueocean-web:1.25.2
pipeline-stage-view:2.20
pipeline-build-step:2.15
blueocean-jwt:1.25.2
pipeline-model-api:1.9.3
pipeline-model-extensions:1.9.3
jenkins-design-language:1.25.2
workflow-cps-global-lib:552.vd9cc05b8a2e1
workflow-multibranch:706.vd43c65dec013
pipeline-stage-tags-metadata:1.9.3
blueocean-core-js:1.25.2
pipeline-model-definition:1.9.3
favorite:2.3.3
lockable-resources:2.13
workflow-aggregator:2.6
okhttp-api:4.9.3-105.vb96869f8ac3a
sse-gateway:1.24
github-api:1.301-378.v9807bd746da5
gitee:1.2.7
git:4.10.1
blueocean-events:1.25.2
github:1.34.1
blueocean-rest-impl:1.25.2
github-branch-source:2.11.4
pipeline-github-lib:1.0
blueocean-i18n:1.25.2
ssh-slaves:1.33.0
matrix-auth:3.0
pam-auth:1.6.1
ldap:2.7
email-ext:2.86
localization-support:1.1
blueocean-dashboard:1.25.2
localization-zh-cn:1.0.24
blueocean-git-pipeline:1.25.2
dashboard-view:2.18
ssh:2.6.1
blueocean:1.25.2
publish-over:0.22
publish-over-ssh:1.22
config-file-provider:3.8.2
docker-commons:1.17
nodejs:1.5.0
docker-workflow:1.26
jquery:1.12.4-1
git-parameter:0.9.14
jersey2-api:2.35-3
gitlab-plugin:1.5.27
blueocean-config:1.25.2
authorize-project:1.4.0
authentication-tokens:1.4
role-strategy:3.2.0
blueocean-commons:1.25.2
blueocean-rest:1.25.2
pubsub-light:1.16
javadoc:1.6
blueocean-pipeline-scm-api:1.25.2
variant:1.4
blueocean-pipeline-api-impl:1.25.2
blueocean-pipeline-editor:1.25.2
blueocean-github-pipeline:1.25.2
handy-uri-templates-2-api:2.1.8-1.0
cloudbees-bitbucket-branch-source:737.vdf9dc06105be
blueocean-bitbucket-pipeline:1.25.2
blueocean-personalization:1.25.2
blueocean-display-url:2.4.1
blueocean-autofavorite:1.2.4
kubernetes-client-api:5.11.2-182.v0f1cf4c5904e
kubernetes-credentials:0.9.0
kubernetes:1.31.3
view-job-filters:2.3
maven-plugin:3.16
dingding-json-pusher:2.0
dingding-notifications:2.4.4
h2-api:1.4.199
pipeline-maven:3.10.0
robot:3.0.1