Jenkins部署指北

written in 2020 — details may be outdated

Contents
  1. 开始之前
  2. 安装 Docker
  3. 部署Jenkins
  4. 深入阅读

本文记录使用Docker从头建立Jenkins服务的过程。

开始之前#

本文假设你已经拥有一定的Linux基础,懂得命令行操作、ssh等工具的使用。

方便起见,本文使用Docker来部署Jenkins。其他安装方式参考深入阅读部分文章。

安装 Docker#

Docker为服务的部署提供了前所未有的便捷性,有关的介绍可以前往官网了解。

安装Docker只需按照步骤操作即可,此处放上官方教程链接:

CentOS

Debian

本文给出一个Debian发行版上的命令汇总,理论上Copy-Paste到命令行,以有sudo权限的非特权用户执行就可以完成Docker和docker-compose的安装。

# For Debian
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg2 \
software-properties-common -y
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/debian \
$(lsb_release -cs) \
stable"
sudo apt update
sudo apt-get install docker-ce docker-ce-cli containerd.io -y
sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

如果希望能以非特权用户与Docker通信,则需要将用户加入docker组中。在Debian上可以利用adduser,当然也可以使用usermod,此处简易起见,使用adduser。

sudo adduser $(whoami) docker

需要重新登录才能生效。

至此,我们已经成功地安装好了Docker,可以运行一个hello world: docker run hello-world 以验证是否安装成功并设置好用户组。

部署Jenkins#

Jenkins目前分为两个镜像jenkinsci/blueocean和jenkins/jenkins,区别在于前者预先安装了blueocean插件,后者没有。感觉blueocean还是比较有必要装的,推荐直接使用前者。

最简单的方法只需要一行,这是Docker给我们提供的便利。

docker run -p 8080:8080 jenkinsci/blueocean

访问ip:8080就可以看到jenkins的初始化界面了,我在这一步没有截图,此处借用一张网上的图。

此时我们需要到命令行寻找刚刚docker那一条命令的输出,有类似这样的

*************************************************************
*************************************************************
*************************************************************
Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:
f63c3c3247e6445ab77517c38cddfb65
This may also be found at: /var/jenkins_home/secrets/initialAdminPassword
*************************************************************
*************************************************************
*************************************************************

可以看到这就是我们要找的admin password。

输入以后就成功进入了Jenkins。至此,我们已经完成了Jenkins的安装。

当然,如果我们要在生产环境使用的话还需要做存储的持久化,使用https等。需要使用docker-compose来避免每次都要输入很多的命令行参数,我在此放一个我使用的docker-compose.yml。

version: '2.4'
volumes:
jenkins:
name: jenkins
networks:
jenkins:
name: jenkins
ipam:
config:
- subnet: 172.23.1.0/24
services:
jenkins:
image: jenkinsci/blueocean
container_name: jenkins
restart: unless-stopped
networks:
jenkins:
ipv4_address: 172.23.1.23
volumes:
- jenkins:/var/jenkins_home

我没有使用Docker的port映射,而是使用了HAProxy作为反向代理,是为了方便tls的配置和多站点共用443端口。有关反向代理和tls配置不再赘述,有兴趣可以看看下面的几条链接。

深入阅读#

Keyboard shortcuts

Everywhere
0 1 2 3 4Go to blog, categories, projects, friends, about
/Open search
?Show or hide this help
EscClose help or menu, clear selection
g g / GTop / bottom of page
Lists
j / k ↓ / ↑Move highlight down / up
Enter / oOpen the highlighted item
{ / }Previous / next group
Post
j / kScroll down / up
d / uScroll half a page down / up
] / [Next / previous heading
n / pOlder / newer post
lSwitch to the translation
b / BackspaceBack to the list, or home
yCopy the post URL

Search posts