栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

【项目自动发布】基于Docker/单节点Rancher/GitLab搭建简易的CI/CD流水线(备忘+补充完善)

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【项目自动发布】基于Docker/单节点Rancher/GitLab搭建简易的CI/CD流水线(备忘+补充完善)

前言

最近面试好像也经常问到一个问题: 你们项目是怎么发布的
传统的Java项目都是本地打包成 jar包 或者 war包, 上传到服务器, 然后通过shell脚本的方式启动的
要求我们具备一定的shell脚本编写能力。

这里介绍一种目前比较流行的发布方式(所谓DevOps?), 也是我目前项目在用的。基于Gitlab内置的CI/CD功能, (所谓CI/CD, 没有了解过的可以先简单理解为自动构建, 自动打包, 自动发布到仓库) 以及rancher的容器管理功能。
rancher是基于k3s, k8s的简略版, 这里博主也还没有深入学习k8s, 暂时跳过。

大概的发布流程就是, gitlab仓库中配置 CI/CD, 项目文件中配置 gitlab-ci.yml配置文件 和 gitlab-runner(所谓CI/CD的执行者), 当你推送最新代码到gitlab仓库, 就自动触发流水线(pipeline), 根据你的 gitlab-ci.yml 配置进行项目jar包的构建(build), 发布到maven仓库, docker镜像(image)的构建, 并将最新镜像发布到docker仓库。
然后我们就可以通过在rancher拉取最新镜像, 重新启动一个pod。这样就实现了一个自动构建, 自动发布的一个过程。

Docker安装

docker的安装, rancher的安装也可以参考这篇博文 -> 单节点rancher快速部署

*了解 - 部署Nginx(非Rancher方式)

其实Rancher自带了一个负载均衡器, Nginx Ingress, 通过Ingress就可以配置服务的路由, SSL证书, 包括 uri->端口 的映射等等, 所以这里其实不必要单独部署一个nginx。

但是通过配置nginx也可以对rancher的 ingress实际做了哪些东西有一个摸得着看得见的认识, 不然我们只会通过ui配置rancher, 却不会使用nginx, 就不太优雅了。

docker run --name nginx -v nginx-html:/usr/share/nginx/html  -v nginx-conf:/etc/nginx -d -p 80:80 -p 443:443 nginx:1.20
nginx配置

这里贴上我的一个配置

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    gzip  on;

    include /etc/nginx/conf.d/*.conf;
    
    client_max_body_size 100m;        # 文件上传你大小, 默认1m
    proxy_ssl_session_reuse off;      # 关闭代理ssl验证
    
    proxy_set_header   Host                $host;
    proxy_set_header   X-Real-IP           $remote_addr;
    proxy_set_header   X-Forwarded-For     $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Proto   $scheme;
    
    # 开启websocket
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    
    # 开启websocket支持
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }
    
    server_tokens off;
    
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;                                   #可参考此 SSL 协议进行配置
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;    #可按照此加密套件配置,写法遵循 openssl 标准
    ssl_prefer_server_ciphers on;
    
    # 负载均衡
    #upstream halo {   
    #    server 124.223.63.15:30001;
    #    server 124.223.63.15:30002 backup;
    #}
    
    # chenyao.icu halo博客
    server{
        keepalive_requests 200;      #单连接请求上限次数。
        listen 80;                   #监听端口
        server_name chenyao.icu;     #监听域名       
        location  / {                #地址匹配
           #proxy_pass  http://halo; #请求转向halo 定义的服务器列表
           
           #root path;               #静态资源根目录
           #index index.html;        #设置静态首页        
           #deny 127.0.0.1;          #拒绝的ip
           #allow 172.18.5.54;       #允许的ip  
           
           # 重定向到443
           return 301 https://$host$request_uri;         
        }
    }
    server {
        listen 443 ssl;
        server_name chenyao.icu;                         #填写您的证书绑定的域名,例如:www.cloud.tencent.com
        #root /usr/local/lighthouse/softwares/wordpress; #填写您的网站根目录,例如:/usr/local/lighthouse/softwares/wordpress
        #index index.php index.html;
        
        access_log logs/chenyaoicu.log;
        error_log logs/chenyaoicu.error.log;
        ssl_certificate chenyao.icu/fullchain.cer;        #填写您的证书文件名称,例如:cloud.tencent.com_bundle.crt
        ssl_certificate_key chenyao.icu/chenyao.icu.key;  #填写您的私钥文件名称,例如:cloud.tencent.com.key
        
        location / {
            #proxy_pass http://halo;
            proxy_pass http://10.0.16.3:32140;
        }
        
        # 静态资源反向代理 
        # 请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
        location ~ .*.(js|css|gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$
        {
            proxy_pass http://10.0.16.3:32140;
            expires      10d;
        }
    } 
    
    
    # rancher.chenyao.icu Rancher
    server{
        keepalive_requests 200;            #单连接请求上限次数。
        listen 80;                         #监听端口
        server_name rancher.chenyao.icu;   #监听域名       
        location  / {                      #地址匹配        
           # 重定向到443
           return 301 https://$host$request_uri;         
        }
    }
    server {
        listen 443 ssl;
        server_name rancher.chenyao.icu;

        access_log logs/rancher.chenyaoicu.log;
        error_log logs/rancher.chenyaoicu.error.log;
        ssl_certificate rancher.chenyao.icu/fullchain.cer;
        ssl_certificate_key rancher.chenyao.icu/rancher.chenyao.icu.key;
        
        location / {
            proxy_pass https://10.0.16.3:1443;
        }
    }
    
    
    # nexus.chenyao.icu Nexus
    server{
        keepalive_requests 200;          #单连接请求上限次数。
        listen 80;                       #监听端口
        server_name nexus.chenyao.icu;   #监听域名       
        location  / {                    #地址匹配        
           # 重定向到443
           return 301 https://$host$request_uri;         
        }
    }
    server {
        listen 443 ssl;
        server_name nexus.chenyao.icu;

        access_log logs/nexus.chenyaoicu.log;
        error_log logs/nexus.chenyaoicu.error.log;
        ssl_certificate nexus.chenyao.icu/fullchain.cer;
        ssl_certificate_key nexus.chenyao.icu/nexus.chenyao.icu.key;
        
        location / {
            proxy_pass http://10.0.16.3:32719;
        }
        
    }
    
    # gitlab.chenyao.icu GitLab
    server{
        keepalive_requests 200;          #单连接请求上限次数。
        listen 80;                       #监听端口
        server_name gitlab.chenyao.icu;  #监听域名       
        location  / {                    #地址匹配        
           # 重定向到443
           return 301 https://$host$request_uri;         
        }
    }
    server {
        listen 443 ssl;
        server_name nexus.chenyao.icu;

        access_log logs/gitlab.chenyaoicu.log;
        error_log logs/gitlab.chenyaoicu.error.log;
        ssl_certificate gitlab.chenyao.icu/fullchain.cer;
        ssl_certificate_key gitlab.chenyao.icu/gitlab.chenyao.icu.key;
        
        location / {
            proxy_pass http://10.0.16.3:1080;
        }
    }
}

SSL证书(HTTPS访问, 非必须) FreeSSL (推荐, 可免费申请的ssl证书)

https://freessl.cn/acme-deploy?domains=chenyao.icu%2Cwww.chenyao.icu

acme.sh --install-cert -d chenyao.icu 
--key-file       /var/lib/docker/volumes/nginx-conf/_data/chenyao.icu/chenyao.icu.key  
--fullchain-file /var/lib/docker/volumes/nginx-conf/_data/chenyao.icu/fullchain.cer 
--reloadcmd     "docker restart nginx"

acme.sh --install-cert -d rancher.chenyao.icu 
--key-file       /var/lib/docker/volumes/nginx-conf/_data/rancher.chenyao.icu/rancher.chenyao.icu.key  
--fullchain-file /var/lib/docker/volumes/nginx-conf/_data/rancher.chenyao.icu/fullchain.cer 
--reloadcmd     "docker restart nginx"

acme.sh --install-cert -d gitlab.chenyao.icu 
--key-file       /var/lib/docker/volumes/nginx-conf/_data/gitlab.chenyao.icu/gitlab.chenyao.icu.key  
--fullchain-file /var/lib/docker/volumes/nginx-conf/_data/gitlab.chenyao.icu/fullchain.cer 
--reloadcmd     "docker restart nginx"

acme.sh --install-cert -d nexus.chenyao.icu 
--key-file       /var/lib/docker/volumes/nginx-conf/_data/nexus.chenyao.icu/nexus.chenyao.icu.key  
--fullchain-file /var/lib/docker/volumes/nginx-conf/_data/nexus.chenyao.icu/fullchain.cer 
--reloadcmd     "docker restart nginx"

acme.sh --install-cert -d *.chenyao.icu 
--key-file       /var/lib/docker/volumes/nginx-conf/_data/*.chenyao.icu/*.chenyao.icu.key  
--fullchain-file /var/lib/docker/volumes/nginx-conf/_data/*.chenyao.icu/fullchain.cer 
--reloadcmd     "docker restart nginx"
错误: HTTPS 访问的页面上不允许出现 HTTP 请求
已阻止载入混合活动内容“http://nexus.chenyao.icu/static/rapture/app.js?_v=3.27.0-03&_e=OSS”

指向“http://nexus.chenyao.icu/static/rapture/app.js?_v=3.27.0-03&_e=OSS”的