• 【Docker从入门到入土 6】Consul详解+Docker https安全认证(附证书申请方式)


    一、服务注册与发现的概念

    服务注册与发现是微服务架构中不可或缺的重要组件。

    1.1 cmp问题

    在这里插入图片描述

    起初服务都是单节点的,不保障高可用性,也不考虑服务的压力承载,服务之间调用单纯的通过接口访问。

    直到后来出现了多个节点的分布式架构,起初的解决手段是在服务前端负载均衡,这样前端必须要知道所有后端服务的网络位置,并配置在配置文件中。

    这里就会有几个问题:
    ●如果需要调用后端服务A-N,就需要配置N个服务的网络位置,配置很麻烦
    ●后端服务的网络位置变化,都需要改变每个调用者的配置

    1.2 服务注册与发现

    img

    既然有这些问题,那么服务注册与发现就是解决这些问题的

    后端服务A-N可以把当前自己的网络位置注册到服务发现模块,服务发现就以K-V的方式记录下来,K一般是服务名,V就是IP:PORT。

    服务发现模块定时的进行健康检查,轮询查看这些后端服务能不能访问的了。

    前端在调用后端服务A-N的时候,就跑去服务发现模块问下它们的网络位置,然后再调用它们的服务

    这样的方式就可以解决上面的问题了,前端完全不需要记录这些后端服务的网络位置,前端和后端完全解耦!

    二、Consul ----- 服务自动发现和注册

    2.1 简介

    Consul是google开源的一个使用go语言开发的,实现服务自动注册和发现的一种工具

    2.2 为什么要用consul?

    为了解决后端应用服务器集群节点数量很多,前端负载均衡器配置和管理会很麻烦的问题

    负载均衡器的节点配置条目数量会很多,后端节点的网络位置发生了变化还需要修改所有调用这些后端节点的负载均衡器配置等问题)

    2.3 consul的架构

    consul由两种模式组成:cilent模式server模式

    在这里插入图片描述

    Client模式 :可用于接收后端服务发来的注册信息,并转发给server节点,没有持久化能力。

    Server模式:可用于接受后端服务/client模式的节点发来的注册信息,还可在server节点之前同步注册信息,具有持久化注册信息到本地的能力。

    Server模式下还有个特殊的节点: server-leader节点

    在这里插入图片描述

    Server-Leader节点负责同步注册信息给其它的server节点,并对各个节点做健康检查

    Leader基于Raft算法进行选举,在Leader选举过程中,整个集群都无法对外提供服务。

    2.3 Consul-template

    Consul-Template是基于Consul的自动替换配置文件的应用。

    Consul-Template是一个守护进程,用于实时查询Consul集群信息,并更新文件系统上任意数量的指定模板,生成配置文件。

    更新完成以后,可以选择运行 shell 命令执行更新操作,重新加载 Nginx。

    三、consul架构部署

    在这里插入图片描述

    ServerIPUsage
    Consul服务器192.168.2.102运行consul服务、nginx服务、consul-template守护进程
    Registrator服务器192.168.2.103运行registrator容器、运行nginx容器
    systemctl stop firewalld.service
    setenforce 0
    
    • 1
    • 2

    3.1 Consul服务器

    不需要安装docker

    Step1 建立 Consul 服务

    1.建立工作目录,上传并解压代码包

    mkdir /opt/consul
    cp consul_0.9.2_linux_amd64.zip /opt/consul
    
    cd /opt/consul
    unzip consul_0.9.2_linux_amd64.zip
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    mv consul /usr/local/bin/
    
    • 1

    2.设置代理,在后台启动 consul 服务端

    #启动consul server
    consul agent \
    -server \
    -bootstrap \
    -ui \
    -data-dir=/var/lib/consul-data \
    -bind=192.168.2.102 \
    -client=0.0.0.0 \
    -node=consul-server01 &> /var/log/consul.log &
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述

    #查看相关端口
    netstat -natp | grep consul
    
    • 1
    • 2

    在这里插入图片描述

    #启动consul后默认会监听5个端口:
    8300:replication、leader farwarding的端口
    8301:lan cossip的端口
    8302:wan gossip的端口
    8500:web ui界面的端口
    8600:使用dns协议查看节点信息的端口
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    Step2 查看集群信息

    #查看members状态
    consul members
    
    • 1
    • 2

    在这里插入图片描述

    #查看集群状态
    consul operator raft list-peers
    
    • 1
    • 2

    在这里插入图片描述

    consul info | grep leader
    
    • 1

    在这里插入图片描述

    Step3 通过 http api 获取集群信息

    curl 127.0.0.1:8500/v1/status/peers 			
    #查看集群server成员
    curl 127.0.0.1:8500/v1/status/leader			
    #集群 server-leader
    curl 127.0.0.1:8500/v1/catalog/services		
    #注册的所有服务
    curl 127.0.0.1:8500/v1/catalog/nginx			
    #查看 nginx 服务信息
    curl 127.0.0.1:8500/v1/catalog/nodes			
    #集群节点详细信息
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在这里插入图片描述

    Step4 测试能否访问consul的web界面

    浏览器访问
    http://192.168.2.102:8500
    
    • 1
    • 2

    在这里插入图片描述

    3.2 Registrator服务器

    容器服务自动加入 Nginx 集群

    Step1 安装 Gliderlabs/Registrator

    Gliderlabs/Registrator 可检查容器运行状态自动注册,还可注销 docker 容器的服务到服务配置中心。

    目前支持 Consul、Etcd 和 SkyDNS2。

    docker run -d \
    --name=registrator \
    --net=host \
    -v /var/run/docker.sock:/tmp/docker.sock \
    --restart=always \
    gliderlabs/registrator:latest \
    --ip=192.168.2.103 \
    consul://192.168.2.102:8500
    
    # registrator为容器名
    # host为网络模式
    # always为重启策略
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述

    Step2 测试服务发现功能是否正常

    docker run -itd -p:83:80 --name test-01 -h test01 nginx
    docker run -itd -p:84:80 --name test-02 -h test02 nginx
    docker run -itd -p:88:80 --name test-03 -h test03 httpd
    docker run -itd -p:89:80 --name test-04 -h test04 httpd				#-h:设置容器主机名
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    Step3 验证 http 和 nginx 服务是否注册到 consul

    通过consul web界面验证

    浏览器中,输入 http://192.168.2.102:8500,在 Web 页面中“单击 NODES”,然后单击“consurl-server01”,会出现 5 个服务。
    在这里插入图片描述

    在consul server 通过curl命令测试

    #通过curl命令查询到Consul上注册的一些服务
    curl 127.0.0.1:8500/v1/catalog/services 
    
    • 1
    • 2

    在这里插入图片描述

    3.3 配置consul-template

    在consul服务器上操作。

    Step1 准备 template nginx 模板文件

    vim /opt/consul/nginx.ctmpl
    
    #定义nginx upstream一个简单模板
    upstream http_backend {
      {{range service "nginx"}}
       server {{.Address}}:{{.Port}};
       {{end}}
    }
    
    #定义一个server,监听8000端口,反向代理到upstream
    server {
        listen 8000;
        server_name localhost 192.168.2.102;
        access_log /var/log/nginx/kgc.com-access.log;							#修改日志路径
        index index.html index.php;
        location / {
            proxy_set_header HOST $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Client-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://http_backend;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    Step2 安装nginx

    #安装依赖
    yum -y install pcre-devel zlib-devel gcc gcc-c++ make
    #新建用户
    useradd -M -s /sbin/nologin nginx
    
    • 1
    • 2
    • 3
    • 4
    tar zxvf nginx-1.24.0.tar.gz -C /opt/
    
    cd /opt/nginx-1.24.0/
    #编译安装
    ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install
    
    ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Step3 配置 nginx

    vim /usr/local/nginx/conf/nginx.conf
    
    ......
    http {
         include       mime.types;
         include  vhost/*.conf;       				#添加虚拟主机目录
         default_type  application/octet-stream;
    ......
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    #创建虚拟主机目录
    mkdir /usr/local/nginx/conf/vhost
    
    #创建日志文件目录
    mkdir /var/log/nginx
    
    #启动nginx
    nginx
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    Step4 配置并启动 template

    #解压代码包
    unzip consul-template_0.19.3_linux_amd64.zip -d /opt/
    
    cd /opt/
    mv consul-template /usr/local/bin/
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    #在前台启动 template 服务,启动后不要按 ctrl+c 中止 consul-template 进程。
    consul-template --consul-addr 192.168.2.102:8500 \
    --template "/opt/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/byyb.conf:/usr/local/nginx/sbin/nginx -s reload" \
    --log-level=info
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    #另外打开一个终端查看生成配置文件
    cd /usr/local/nginx/conf/vhost/
    cat byyb.conf
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    Step5 访问 template-nginx

    docker ps -a
    
    • 1

    在这里插入图片描述

    docker exec -it 4aafb2347a01 bash
    echo "this is test1 web" > /usr/share/nginx/html/index.html
    
    
    docker exec -it 26274cd201c7 bash
    echo "this is test2 web" > /usr/share/nginx/html/index.html
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    浏览器访问:http://192.168.2.102:8000/ ,并不断刷新。

    注:访问失败的话,需要开启路由转发。

    net.ipv4.ip_forward = 1
    
    • 1

    在这里插入图片描述

    Step6 增加一个 nginx 容器节点

    1)增加一个 nginx 容器节点,测试服务发现及配置更新功能

    在registor节点添加

    docker run -itd -p:85:80 --name test-05 -h test05 nginx
    
    • 1

    在这里插入图片描述

    2)查看子配置文件

    cd /usr/local/nginx/conf/vhost/
    cat byyb.conf
    
    • 1
    • 2

    在这里插入图片描述

    3)在新容器节点添加测试页面,测试能否负载均衡

    docker ps -a
    
    docker exec -it c6715b2533bf bash
    echo "this is test1 web" > /usr/share/nginx/html/index.html
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    浏览器访问:http://192.168.2.102:8000/ ,并不断刷新
    在这里插入图片描述

    可以看到,请求正常轮询到各个容器节点

    3.4 补充知识,consul多节点

    consul -leave  脱离节点
           -enable-script-checks=true :设置检查服务为可用
           -datacenter : 数据中心名称
           -join :加入到已有的集群中
    
    • 1
    • 2
    • 3
    • 4
    #添加一台已有docker环境的服务器192.168.2.104/24加入已有的群集中
    consul agent \
    -server \
    -ui \
    -data-dir=/var/lib/consul-data \
    -bind=192.168.2.104 \
    -client=0.0.0.0 \
    -node=consul-server02 \
    -enable-script-checks=true  \
    -datacenter=dc1  \
    -join 192.168.2.102 &> /var/log/consul.log &
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    consul members
    
    • 1

    四、Docker安全

    4.1 Docker 安全基线标准(注意事项)

    1)尽量不用 --privileged 运行容器(授权容器root用户用户宿主机的root权限);

    2)尽量不用 --network host 运行容器(使用host网络模式共享宿主机的网络命名空间);

    3)尽量不在容器中运行 ssh 服务;

    4)尽量不把宿主机系统的关键敏感挂载到容器中。

    4.2 常用的安全配置方法

    1)尽量使用最小化的镜像

    2)尽量以单一进程运行容器

    3)尽量在容器中使用最新版本的应用

    4)尽量安装最新版本的docker

    5)尽量使用官方的镜像或自已构建镜像;

    6)尽量给容器分配独立的文件系统

    7)尽量以资源限制的方式运行容器 ;-m --cpu-quota- --cpuset-cpus -device-write-bps

    8)尽量以只读的方式挂载数据卷(持久化容器数据到宿主机时除外); -v 宿主机目录:容器目录:ro

    9)尽量设置容器重启次数 ;--restart on-failure:N

    10)尽量以最低权限运行容器。

    五、Docker https安全认证

    5.1 背景

    为了防止链路劫持、会话劫持等问题导致 Docker 通信时被中间人攻击,c/s 两端应该通过 TLS 加密方式通讯。

    5.2 使用证书访问的工作流程

    或者说,https请求访问的过程
    在这里插入图片描述

    服务端会事先通过 CA 签发服务器端证书和私钥

    1)客户端发起 https 请求到服务端的 443 端口;

    2)服务器会先返回一个包含公钥、证书有效期、CA机构等信息的证书给客户端;

    3)客户端会先通过 CA 验证服务端证书的有效性,如果证书有效,则会在客户端随机生成一个会话密钥并通过服务端发来的公钥加密后,再发给服务端;

    4)服务端会用私钥解密获取客户端发来的会话密钥,之后双方即可通过会话密码加密/解密来实现密文通信。

    5.3 如何获取证书?

    1)在阿里云、腾讯云、华为云等云服务商申请一年有效期的免费证书或者购买付费的证书

    2)在服务器本地使用 openssl、mkcert、cfssl、certbot(Let’s Encrypt)等工具生成私有证书

    5.4 使用OpenSSL创建自签名证书的步骤

    创建自签名证书大致分为三步, 创建CA证书, 创建自签名请求, CA签名。

    1)创建CA私钥和证书

    #创建 CA 私钥文件
    openssl genrsa -out ca.key 2048
    
    #2048为位长
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    #创建 CA 证书文件
    openssl req -new -x509 -days 3650 -key ca.key -out ca.pem  
    这是 OpenSSL 工具生成自签名 X.509 证书的命令。让我逐个解释:
    
    # `openssl req`: 执行 OpenSSL 工具,使用 "req" 子命令来创建证书请求。
    
    # `-new`: 表示创建一个新的证书请求(Certificate Signing Request)。
    
    #`-x509`: 表示创建自签名 X.509 证书。
    
    #`-days 3650`: 表示生成的证书有效期为 3650 天(大约 10 年)。
    
    #`-key ca.key`: 指定用来签名证书的密钥文件为 "ca.key"。
    
    #`-out ca.pem`: 表示将生成的证书输出到文件 "ca.pem"。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在这里插入图片描述

    2)创建服务端自签名请求文件

    #创建服务端私钥文件
    openssl genrsa -out server.key 2048  
    
    • 1
    • 2

    在这里插入图片描述

    #创建服务端证书自签名请求文件
    openssl req -new -key server.key -out server.csr   
    
    • 1
    • 2

    在这里插入图片描述

    3)基于CA证书、私钥和服务端自签名请求文件,签发服务端证书

    #创建服务端证书文件
    openssl x509 -req -days 3650 -in server.csr -CA ca.pem -CAkey ca.key -CAcreateserial -out server.pem 
    
    • 1
    • 2

    在这里插入图片描述
    在这里插入图片描述

    4)证书的使用测试

    我们使用自签名证书实现通过https访问harbor私有仓库

    私有仓库的部署过程,详见我的另一篇博客【Docker从入门到入土 4】使用Harbor搭建Docker私有仓库 ,这里只标明额外的配置。

    #编辑配置文件
    vim /usr/local/harbor/harbor.yml
    ...
    
     13 https:
     14   # https port for harbor, default is 443
     15   port: 443
     16   # The path of cert and key files for nginx
     17   certificate: /opt/ct/server.pem
     18   private_key: /opt/ct/server.key
    ....
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述
    在这里插入图片描述

    cd /usr/local/harbor/
    
    ./prepare
    
    ./install.sh
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    #浏览器访问
    https://192.168.2.102
    
    • 1
    • 2

    在这里插入图片描述

    在这里插入图片描述

    5.6 nginx如何支持https?

    1)编译安装时需要添加 --with-http_ssl_module 模板;

    2)修改配置文件,添加 ssl 配置

    http {
        server {
            #SSL 访问端口号为 443
            listen   443 ssl;
            # 填写绑定证书的域名
            server_name  域名;
            root html;
            index index.html index.htm;
    		# 指定SSL证书和私钥路径
            ssl_certificate     /usr/local/nginx/conf/cert/xxxxx.pem;
            ssl_certificate_key  /usr/local/nginx/conf/cert/xxxxx.key;
    
            # ssl_session_cache    shared:SSL:1m;
            ssl_session_timeout  5m;
            ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; 
    		# 指定SSL服务器端支持的协议版本
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2;  
            ssl_prefer_server_ciphers  on;
    
            location /welcome {
                root   html;
                index  index.html index.htm;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 相关阅读:
    计算机毕业设计Java公立医院绩效考核系统(源码+系统+mysql数据库+Lw文档)
    猫12分类:使用多线程爬取图片的Python程序
    uniapp原生插件开发实战——Android打开文件到自己的app
    第三届软件测试火焰杯比赛开启,可以报名啦!
    nacos
    【C++】构造函数初始化列表 ③ ( 构造函数 的 初始化列表 中 为 const 成员变量初始化 )
    481、神奇字符串
    Debian 11.5.0 安装流程
    猿创征文|Docker部署Oracle 19C及最佳实践
    深入理解final关键字
  • 原文地址:https://blog.csdn.net/q2524607033/article/details/134025339