CI:持续集成
CD:持续部署
在jenkins容器中安装ansible
由于该项目的jenkins容器是alpine高山版本,直接用apk添加
apk add ansible
改写宿主机中docker-compose.yaml文件
version: '3.3'
services:
mysql:
#build: ./docker-cmdb/mysql
image: auto-cmdb_mysql
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
container_name: "cicd_mysql"
volumes:
- ./cmdbdata/mysql/data:/var/lib/mysql
- ./cmdbdata/mysql/init:/docker-entrypoint-initdb.d/
restart: always
environment:
MYSQL_ROOT_PASSWORD: QFedu123!
expose:
- "3306"
networks:
- jumpserver-test
nginx:
build: ./docker-cmdb/nginx
#image: auto-cmdb_nginx
ports:
- "9200:80"
container_name: "cicd_nginx"
volumes:
- ./cmdbdata/nginx/allstatic:/allstatic
- ./cmdbdata/nginxconf.d:/etc/nginx/conf.d
networks:
- jumpserver-test
redis:
#build: ./docker-cmdb/redis
image: auto-cmdb_redis
expose:
- "6379"
container_name: "cicd_redis"
networks:
- jumpserver-test
rabbit:
#build: ./docker-cmdb/rabbitmq
image: auto-cmdb_rabbit
expose:
- "5672"
container_name: "cicd_rabbit"
environment:
- RABBITMQ_DEFAULT_VHOST=zxhvhost
- RABBITMQ_DEFAULT_USER=zxh
- RABBITMQ_DEFAULT_PASS=zxh123
networks:
- jumpserver-test
cmdb:
build: ./docker-cmdb/cmdbcompose
#image: auto-cmdb_cmdb
#ports:
# - "127.0.0.1:9190:80"
# - "127.0.0.1:9122:22"
expose:
- "80"
- "22"
#container_name: "cicd_cmdb"
networks:
- jumpserver-test
tty: true
depends_on:
- mysql
- redis
- rabbit
- nginx
command:
- /bin/bash
- -c
- |
cd /opt/cmdb
python3 manage.py makemigrations
python3 manage.py migrate
uwsgi --ini conf/qf_uwsgi.ini
gitlab:
image: gitlab-ce
restart: always
hostname: 'gitlab.qfedu.com'
environment:
GITLAB_OMNIBUS_CONFIG: |
external_url 'http://10.0.122.110:9280'
unicorn['worker_timeout'] = 60
unicorn['worker_processes'] = 3 # worker进程数
unicorn['worker_memory_limit_min'] = "200 * 1 << 20" # worker最小内存
unicorn['worker_memory_limit_max'] = "300 * 1 << 20" # worker最大内存
sidekiq['concurrency'] = 10
postgresql['shared_buffers'] = "128MB"
postgresql['max_worker_processes'] = 8
networks:
- jumpserver-test
ports:
- '9280:9280'
volumes:
- './gitlab/config:/etc/gitlab'
- './gitlab/logs:/var/log/gitlab'
- './gitlab/data:/var/opt/gitlab'
jenkins:
image: jenkinsci_blueocean
restart: always
privileged: true
user: root
volumes:
- './jenkins/jenkins_home:/var/jenkins_home'
- '/var/run/docker.sock:/var/run/docker.sock'
networks:
- jumpserver-test
ports:
- "9980:8080"
networks:
jumpserver-test:
driver: bridge
注意:改写前先停掉服务
docker-compose stop
-
将ports改为
expose,不指定哪台cmdb,避免端口冲突 -
将
container_name关闭,让默认生成
image
-
改写完后再启动
docker-compose up --scale cmdb=2
image.png
在新生成的两个cmdb容器上配置ssh
ssh-keygen生成密钥对
image
ssh-keygen -A 生成系统密钥
image
/usr/sbin/sshd 打开22端口
passwd 设置密码
image
在jenkins的容器上创建一个/etc/ansible/下的hosts文件
/etc/ansible/hosts
[prod]
auto-cmdb_cmdb_1
[prod:vars]
name=prod-zxh
[dev]
auto-cmdb_cmdb_2
[dev:vars]
name=dev-zxh
port=2222
[test]
auto-cmdb_cmdb_1
[test:vars]
port=6666
在jenkins容器里,执行命令,与cmdb项目容器建立互信
由于jenkins的 publish over ssh 插件版本所需密钥对版本比opensshserver低
如果要制作“较旧的” OpenSSH格式,请执行:
ssh-keygen -t rsa -b 4096 -m PEM
之后建立免密登录web服务器
ssh-copy-id cicd-cmdb_cmdb_1
ssh-copy-id cicd-cmdb_cmdb_2
再Jenkins中创建一个自由风格的任务:
1. 添加描述信息
image
2.选择参数化构建过程
Name:事件名称
Description:添加描述信息,加以区分不同参数事件
Parameter Type:选择以分支或标签进行构建(此处以分支为例)
image
3.添加一个选项参数模块
在执行时会出现一个下拉选项,可选择自定义选项的内容
image
4.再添加一个文本参数
在执行时会出现一个文本框,可自定义文本参数的默认值
image
5.源码管理,配置git仓库
需要在 gitlab 那儿添加 jenkins 的公钥
image.png
Repository URL:gitlab的ssh链接
注意:@后面写的时gitlab容器的镜像名
image.png
image.png
image.png
6.构建触发器和构建环境
webhook方式触发
在gitlab上需要配置
image.png
image.png
使用 publish over SSH 执行更新:
需要与web服务器互信
在系统配置里 将jenkins的私钥添加进去
image.png
image.png
ansible playbook 执行更新方式
image.png
/playbook/playbook01.yml内容
---
- hosts: all
gather_facts: no
remote_user: root
tasks:
- name: cp
copy: src=/var/jenkins_home/workspace/ansible-playbook-01/cmdb.tar dest=/a
- name: tar cmdb
shell: tar -cf /opt/back/cmdb.$(date +%F-%H-%M).tar.gz /opt/cmdb/
- name: tar -xf
shell: tar xf /app/code/cmdb.tar -C /app/code
- name: cp
shell: \cp -rf /app/code/* /opt/cmdb/
使用 Ad-Hoc 基于参数化选择gitlab代码分支或者标签进行构建
根据上文定义的参数变量,可自定义shell内容,此处便于观看,echo打印了相应参数
echo "本次构建的版本是 ${build_version}"
echo "本次选择的 gitlab 分支是 ${branch_name}"
echo "本次选择部署的主机组是 ${deploy_hosts}"
Host pattern:添加主机组
Inventory:添加资产清单,需指定host文件在jenkins容器的具体目录
Module:指定ansible执行模块,此处用的debug模块
Module arguments or command to execute:等价于ansible命令中-a 后面的具体命令操作
image.png
写完任务配置内容后,保存然后点击参数构建Build with Parameters,填入相应信息
image













网友评论