• CICD 持续集成与持续交付——jenkins


    部署

    软件下载:https://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat/

    1. [root@cicd2 ~]# rpm -ivh jdk-11.0.15_linux-x64_bin.rpm
    2. [root@cicd2 ~]# yum install -y fontconfig
    3. [root@cicd2 ~]# rpm -ivh jenkins-2.432-1.1.noarch.rpm

    启动服务

    [root@cicd2 ~]# systemctl enable --now jenkins.service
    [root@cicd2 ~]# netstat -antlp|grep :8080

    登录

    192.168.92.22:8080

    初始密码

    [root@cicd2 ~]# cat /var/lib/jenkins/secrets/initialAdminPassword

    安装推荐插件

    无需新建用户,直接使用admin账户

    配置

    修改密码

    新建项目

    在jenkins主机上安装git工具

    [root@cicd2 ~]# yum install -y git

    创建密钥并上传gitlab

    1. [root@cicd2 ~]# ssh-keygen
    2. [root@cicd2 ~]# cat .ssh/id_rsa.pub

    添加gitlab认证凭据

    复制私钥

    配置ssh

    1. [root@cicd2 ~]# vim /etc/ssh/ssh_config
    2. StrictHostKeyChecking no

    构建触发器

    构建任务

    查看控制台输出

    实时触发

    安装gitlab插件

    配置项目触发器

    配置gitlab

    再回到demo项目下配置

    测试推送

    添加jenkins节点

    新建虚拟机cicd3

    安装jdk和git

    1. [root@cicd3 ~]# rpm -ivh jdk-11.0.15_linux-x64_bin.rpm
    2. [root@cicd3 ~]# yum install -y git

    配置解析

    [root@cicd3 ~]# cat /etc/hosts

    在节点管理中添加节点

    配置从节点

    cicd3 ssh认证

    关闭master节点的构建任务数

    关闭git主机校验

    最后测试构建,构建任务会在docker1节点上运行

    自动化构建docker镜像

    在cicd3上安装docker-ce

    1. [root@cicd3 ~]# cd /etc/yum.repos.d/
    2. [root@cicd3 yum.repos.d]# cat docker.repo

    [root@cicd3 yum.repos.d]# yum install -y docker-ce

    修改内核参数

    [root@cicd3 ~]# vim /etc/sysctl.d/docker.conf
    1. net.bridge.bridge-nf-call-iptables = 1
    2. net.bridge.bridge-nf-call-ip6tables = 1
    3. net.ipv4.ip_forward = 1
    1. [root@cicd3 ~]# sysctl --system
    2. [root@cicd3 ~]# systemctl enable --now docker

    配置docker默认仓库

    [root@cicd3 ~]# vim /etc/docker/daemon.json
    1. {
    2. "registry-mirrors": ["https://reg.westos.org"]
    3. }

    [root@cicd3 ~]# systemctl  restart docker

    拷贝仓库证书

    1. [root@k8s1 ~]# cd /etc/docker/
    2. [root@k8s1 docker]# scp -r certs.d/ 192.168.92.23:/etc/docker/
    [root@cicd3 ~]# ls /etc/docker/certs.d/reg.westos.org/ca.crt

    测试

    [root@cicd3 ~]# docker pull nginx

    登录私有harbor仓库

    [root@cicd3 ~]# docker login reg.westos.org

    安装CloudBees Docker Build and Publish插件

    配置项目构建

    在server1上提交Dockerfile

    [root@cicd1 dockerfile]# cat Dockerfile

    1. [root@cicd1 demo]# git status -s
    2. [root@cicd1 demo]# git add  Dockerfile
    3. [root@cicd1 demo]# git commit -m "add Dockerfile"
    4. [root@cicd1 demo]# git push -u origin main

    此时gitlab会主动触发jenkins构建任务,观察jenkins的任务输出

    通过ssh插件交付任务

    jenkins安装ssh插件

    进入系统配置,添加ssh主机

    新建ssh项目

    当docker项目成功运行后触发ssh项目

    构建后查看输出

    RBAC

    安装插件

    修改默认授权策略

    新建测试用户

    新建角色

    用户授权

    使用不同的用户登录,测试权限是否正确

    pipeline

    安装ssh agent 插件

    新建流水线项目 docker_image_build

    复制一下脚本并做相应修改

    1. pipeline {
    2. agent any
    3. stages {
    4. stage('check out') {
    5. steps {
    6. git credentialsId: 'e44734dd-bdce-4a18-9722-bc51ca25ddd6', url: 'git@192.168.92.21:root/dockerfile.git', branch: 'main'
    7. }
    8. }
    9. stage('docker build') {
    10. steps {
    11. sh '''
    12. cd $WORKSPACE
    13. docker build -t reg.westos.org/library/webserver:${BUILD_NUMBER} .
    14. '''
    15. }
    16. }
    17. stage('docker push') {
    18. steps {
    19. sh '''
    20. REPOSITORY=reg.westos.org/library/webserver:${BUILD_NUMBER}
    21. docker tag $REPOSITORY reg.westos.org/library/webserver:latest
    22. docker login reg.westos.org -u admin -p westos
    23. docker push $REPOSITORY
    24. docker push reg.westos.org/library/webserver:latest
    25. '''
    26. }
    27. }
    28. stage('docker deploy') {
    29. steps {
    30. sshagent(credentials: ['044d5700-a59d-4f63-a241-7530117879c3']) {
    31. sh '''
    32. ssh -o StrictHostKeyChecking=no root@192.168.92.23 """
    33. docker ps -a |grep myapp && docker rm -f myapp
    34. docker rmi reg.westos.org/library/webserver:latest
    35. docker run -d --name myapp -p 80:80 reg.westos.org/library/webserver:latest """
    36. '''
    37. }
    38. }
    39. }
    40. }
    41. }

    注意:ssh需要使用ssh免密认证

    jenkins结合ansible参数化构建

    主机环境

    主机

    IP

    角色

    cicd2

    192.168.92.22

    jenkins、ansible

    cicd1

    192.168.92.21

    测试机test、devops sudo

    cicd3

    192.168.92.23

    测试机prod、devops sudo

    安装ansible

    [root@cicd2 ~]# vim /etc/yum.repos.d/ansible.repo
    1. [ansible]
    2. name=epel
    3. baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/x86_64/
    4. gpgcheck=0
    [root@cicd2 ~]# yum install -y ansible

    devops是测试机的ssh免密用户,并且配置sudo

    1. [root@cicd1 ~]# useradd devops
    2. [root@cicd1 ~]# echo westos | passwd --stdin devops
    3. [root@cicd1 ~]# visudo

    ​cicd3同上配置

    在ansible主机上以jenkins身份配置ssh免密到所有测试机

    1. [root@cicd2 ~]# usermod -s /bin/bash jenkins
    2. [root@cicd2 ~]# su - jenkins
    3. -bash-4.2$ ssh-keygen
    4. -bash-4.2$ ssh-copy-id devops@192.168.92.21
    5. -bash-4.2$ ssh-copy-id devops@192.168.92.23

    新建gitlab项目

    克隆项目

    [root@cicd1 ~]# git clone git@192.168.92.21:root/playbook.git

    1. [root@cicd1 ~]# cd playbook/
    2. [root@cicd1 playbook]# vim ansible.cfg
    1. [defaults]
    2. command_warnings=False
    3. remote_user=devops
    4. [privilege_escalation]
    5. become=True
    6. become_method=sudo
    7. become_user=root
    8. become_ask_pass=False
    1. [root@cicd1 playbook]# mkdir inventory
    2. [root@cicd1 playbook]# cd inventory/
    1. [root@cicd1 inventory]# vim test
    2. [test]
    3. 192.168.92.21 http_port=8000
    1. [root@cicd1 inventory]# vim prod
    2. [prod]
    3. 192.168.92.23 http_port=8080
    1. [root@cicd1 inventory]# cd ..
    2. [root@cicd1 playbook]# vim playbook.yaml
    1. ---
    2. - hosts: all
    3. tasks:
    4. - name: install the latest version of Apache
    5. yum:
    6. name: httpd
    7. state: latest
    8. - name: configure apache
    9. template:
    10. src: httpd.conf.j2
    11. dest: /etc/httpd/conf/httpd.conf
    12. notify: restart apache
    13. - name: Start service httpd, if not started
    14. service:
    15. name: httpd
    16. state: started
    17. enabled: yes
    18. handlers:
    19. - name: restart apache
    20. service:
    21. name: httpd
    22. state: restarted

    1. [root@cicd1 playbook]# yum install -y httpd
    2. [root@cicd1 playbook]# cp /etc/httpd/conf/httpd.conf .
    3. [root@cicd1 playbook]# mv httpd.conf httpd.conf.j2
    4. [root@cicd1 playbook]# vim httpd.conf.j2

    推送项目

    1. [root@cicd1 playbook]# git add .
    2. [root@cicd1 playbook]# git status -s
    3. [root@cicd1 playbook]# git commit -m "add playbook.yaml"
    4. [root@cicd1 playbook]# git push -u origin main

    jenkins新建项目playbook

    选择参数构建

    控制台输出

  • 相关阅读:
    怎么在电脑桌面上添加待办事项?
    JDBC封装增删改操作
    安装部署ELK,并收集tomcat,nginx日志
    .NET 全能 Cron 表达式解析库(支持 Cron 所有特性)
    vivado SLR
    微信小程序设计之主体文件app-json-window
    HttpServletRequest 类
    多版本node的安装与切换详细操作
    net基于asp.net的二手商品的交易系统-二手网站-计算机毕业设计
    OpenGL编程学习笔记——交互与直线算法
  • 原文地址:https://blog.csdn.net/dgffd/article/details/134495709