使用 ssh 进行远程交付
我们在开启一台虚拟机server5进行交付,同样使用容器的方式。
安装docker,并配置:
[root@server5 run]# cat /etc/sysctl.d/bridge.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
[root@server5 run]# cat /etc/hosts
172.25.254.3 server3 reg.caoaoyuan.org
[root@server5 run]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://vo5twm71.mirror.aliyuncs.com"]
}
[root@server5 run]# ls /etc/docker/certs.d/reg.caoaoyuan.org/ca.crt # 证书
/etc/docker/certs.d/reg.caoaoyuan.org/ca.crt
我们将通过jinkens的 ssh插件进行远程交付,将代码部署到类生产环境中。
我们首先要安装ssh插件。
这个插件可以让我们在远程主机执行shell命令。安装完成后jenkins的系统配置就会多出:
测试连接成功。
然后我们打开之前做的docker项目,让他去在server5上进行交付。
这里我们之前的配置执行shell命令是本地的,所以我们使用远程执行shell命令。
它会从指定的harbor仓库去拉取镜像,然后执行docker run。
然后触发一次。
server1上更改代码:
[root@server1 demo]# vim index.html
hhhhhhhhhhhhhhh
hhhhhhhhhhhhhhh
hhhhhhhhhhhhhhh
hhhhhhhhhhhhhhh
[root@server1 demo]# git commit -a -m "add index.html v5"
[master 2509044] add index.html v5
1 file changed, 4 insertions(+), 4 deletions(-)
[root@server1 demo]# git push -u origin master
构建成功。
[root@server5 run]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0efe29507964 myweb "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp webserver
[root@server5 run]# curl localhost
hhhhhhhhhhhhhhh
hhhhhhhhhhhhhhh
hhhhhhhhhhhhhhh
hhhhhhhhhhhhhhh
交付成功。
使用ansible参数化构建交付。
上面我们一直使用的都是使用容器进行交付,接下来我们使用ansible交付一些二进制的文件。
这里我们也使用server4 和server5 做交付的主机。开不起来了,内存都满,简易开启新的虚拟机。
我们需要做:
- Jenkins服务器提前部署好到目标主机的ssh免密,并安装ansible软件包。
- 由于是以jenkins用户调用ansible命令,所以需要设置jenkins用户到目标主机的
免密。 - 新建playbook代码仓库
首先切换到jenkins 用户,获得密钥:
[root@server2 docker]# usermod -s /bin/bash jenkins # 不然jenkins用户无法切换
[root@server2 docker]# su - jenkins
-bash-4.2$ ssh-keygen
-bash-4.2$
-bash-4.2$ cd .ssh/
-bash-4.2$ ls
id_rsa id_rsa.pub known_hosts
-bash-4.2$ ssh-copy-id server4:
jenkins@server4's password:
Permission denied, please try again. # 被拒绝是因为jenkings只是普通用户
[root@server4 ~]# useradd -u 1000 ansible # 4 和 5上创建ansible用户用来免密登陆
[root@server4 ~]# passwd ansible
passwd: all authentication tokens updated successfully.
[root@server5 run]# useradd -u 1000 ansible
[root@server5 run]# passwd ansible
passwd: all authentication tokens updated successfully.
# jenkins用户做免密:
-bash-4.2$ ssh-copy-id ansible@172.25.254.4
-bash-4.2$ ssh-copy-id ansible@172.25.254.5
-bash-4.2$ ssh ansible@172.25.254.4
[ansible@server4 ~]$ logout
Connection to 172.25.254.4 closed.
-bash-4.2$ ssh ansible@172.25.254.5
[ansible@server5 ~]$ logout
Connection to 172.25.254.5 closed. # 可以了
由于ansible用户是没有权限的所以做visudo
[root@server5 run]# visudo
ansible ALL=(ALL) NOPASSWD: ALL
[root@server4 ~]# visudo
ansible ALL=(ALL) NOPASSWD: ALL
然后我们去新建playbook代码仓库:
[root@server1 ~]# git clone git@172.25.254.1:root/playbook.git
Cloning into 'playbook'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
[root@server1 ~]# cd playbook/
[root@server1 playbook]# ls # 拉取仓库
README.md
然后在jenkins上也新建一个项目ansible。
我们先只给它设置一个git,去抓取源码。手动触发一次:
-bash-4.2$ pwd
/var/lib/jenkins
-bash-4.2$ cd workspace
-bash-4.2$ ls
demo demo@tmp docker
-bash-4.2$ ls
ansible ansible@tmp demo demo@tmp docker
-bash-4.2$ cd ansible
-bash-4.2$ ls
README.md
## jenkins就同步过来了
- 首先在jenkins主机安装ansible,可以使用阿里云的epel仓库。
[root@server2 yum.repos.d]# yum install ansible -y
- 然后去gitlab主机写一个
inventory
和配置文件
和playbook
:
[root@server1 playbook]# pwd
/root/playbook
[root@server1 playbook]# vim ansible.cfg
[defaults]
remote_user = ansible # 远程用户
command_warnings = False # 忽略警告
[privilege_escalation] # sudo 配置
become=True
become_method=sudo
become_user=root
become_ask_pass=False
[root@server1 playbook]# mkdir inventory
[root@server1 playbook]# ls
ansible.cfg inventory README.md
[root@server1 playbook]# cd inventory/
[root@server1 inventory]# vim test
[test]
172.25.254.4
[root@server1 inventory]# vim prod
[prod]
172.25.254.5 # 分别指定两个主机
[root@server1 playbook]# vim playbook.yml
---
- hosts: all
tasks:
- name: install apache
yum:
name: httpd
state: present
- name: start apache
service:
name: httpd # 安装启动apache。
state: started
[root@server1 playbook]# git add .
[root@server1 playbook]# git commit -m "add playbook"
[root@server1 playbook]# git remote -v
origin git@172.25.254.1:root/playbook.git (fetch)
origin git@172.25.254.1:root/playbook.git (push)
[root@server1 playbook]# git push -u origin master
# 添加上传
然后去jenkins上的ansible项目进行配置,
首先要在jenkins上安装Build with Parameters
和Extended Choice Parameter
和Persistent Parameter
插件。然后重启jenkins。
quansible项目中配置:
选择选项参数。
这里的test
和prod
都是deploy的值,指的inventory中的两个文件。
然后再构建这里选择 shell
执行,因为我们是已经做过ssh免密
的。因为使用jenkins
用户,它把这些文件都放在 workspace
里,所以我们要先进入目录,在使用参数化的方式构建, ${deploy}
就是我们上面设置的变量,里面有test
和prod
两个值.
-bash-4.2$ cd workspace/
-bash-4.2$ ls
ansible ansible@tmp demo demo@tmp docker
-bash-4.2$ cd ansible
-bash-4.2$ ls
ansible.cfg inventory playbook.yml README.md
进行测试:
点击build with parmeters
,选择在test
环境部署,点击开始构建。
jenkins 主就就会先抓取gitlab上的文件,然后在进入到工作目录,在执行ansble-playbook。
server4测试环境就部署好了。
生产环境部署一次:
这里我们只部署了一台主机,在生产环境我们可以部署多台。