分类

链接

2023 年 5 月
1234567
891011121314
15161718192021
22232425262728
293031  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > DevOps, Docker, k8s > 正文
共享办公室出租
使用traefik做为docker网关(负载均衡/滚动更新)
DevOps, Docker, k8s 暂无评论 阅读(257)

安装docker:

yum install docker-ce

创建docker-compose.yml:

version: '3'

services:
  traefik:
    # The official v2 Traefik docker image
    image: traefik:v2.10
    # Enables the web UI and tells Traefik to listen to docker
    command: --api.insecure=true --providers.docker
    ports:
      # The HTTP port
      - "80:80"
      # The Web UI (enabled by --api.insecure=true)
      - "8080:8080"
    volumes:
      # So that Traefik can listen to the Docker events
      - /var/run/docker.sock:/var/run/docker.socky

 

附nginx.config:

server
{
    listen 80;
    server_name _;
    index index.html index.htm index.php;
    root  /data; 

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
    }

    location ~ .*\.(js|css)?$
    {
        expires      12h;
    }

    location ~ /\.
    {
        deny all;
    }

}

放证书的 dynamic.yml:

tls:
  certificates:
    - certFile: "/etc/certs/hk.peos.cn.cer"
      keyFile: "/etc/certs/hk.peos.cn.key"

 

 

启动traefik:

docker compose up -d

 

访问dashboard:   http://localhost:8080

 

支持各种插件:

 

Traefik 检测新服务并为您创建router

现在我们已经启动并运行了一个 Traefik 实例,我们将部署新服务。

编辑文件并在文件末尾添加以下内容。docker-compose.yml:

version: '3'
services:
  whoami:
    # A container that exposes an API to show its IP address
    image: traefik/whoami
    labels:
      - "traefik.http.routers.whoami.rule=Host(`whoami.docker.localhost`)"

上面的定义:一个简单的Web服务,输出有关部署它的机器的信息(其IP地址,主机等)。whoami

使用以下命令启动服务:whoami

docker-compose up -d whoami

返回浏览器 (http://localhost:8080/api/rawdata),看到 Traefik 已自动检测到新容器并更新了自己的配置。 

当 Traefik 检测到新服务时,它会创建相应的路由,以便您可以调用它们......我看看!(在这里,我们使用卷曲)

curl -H Host:whoami.docker.localhost http://127.0.0.1

Shows the following output:

<span class="hljs-attr">Hostname:</span> <span class="hljs-string">a656c8ddca6c</span>
<span class="hljs-attr">IP:</span> <span class="hljs-number">172.27</span><span class="hljs-number">.0</span><span class="hljs-number">.3</span>
<span class="hljs-comment">#...</span>

完整的docker-compose.yml:

version: '3'

services:
  traefik:
    # The official v2 Traefik docker image
    image: traefik:v2.10
    # Enables the web UI and tells Traefik to listen to docker
    labels:
      - traefik.enable=false
    command: 
      # insecure
      - --api.insecure=true 
      - --providers.docker
      # Create an entrypoint "http" listening on port 80
      - --entrypoints.http.address=:80
      # Create an entrypoint "https" listening on port 443
      - --entrypoints.https.address=:443
      - --providers.file.filename=/etc/traefik/dynamic.yml
      - --providers.file.watch=true
    ports:
      # The HTTP port
      - "80:80"
      - "443:443"
      # The Web UI (enabled by --api.insecure=true)
      - "8080:8080"
    volumes:
      # So that Traefik can listen to the Docker events
      - /var/run/docker.sock:/var/run/docker.sock
      # put certs here
      - ./traefik/certs:/etc/certs:ro
      #- ./traefik/traefik.yml:/etc/traefik/traefik.yml:ro
      - ./traefik/dynamic.yml:/etc/traefik/dynamic.yml:ro
     
  whoami: 
      # A container that exposes an API to show its IP address 
    image: traefik/whoami 
    deploy: 
      mode: replicated
      replicas: 3
      placement:
        max_replicas_per_node: 1
      update_config:
        # 同时升级[回滚]的容器数
        parallelism: 1
        # 升级[回滚]的时间间隔
        delay: 10s
        # 有两个stop-first(默认)和start-first,一个是先停止旧的,一个是先启动新的,之后覆盖旧的
        order: stop-first 
    labels: 
      - traefik.enable=true
      - traefik.http.routers.whoami-http.rule=PathPrefix(`/prod`)
      #- traefik.http.routers.whoami-http.entrypoints=http
      
      - traefik.http.routers.whoami-https.rule=PathPrefix(`/prod`)
      #- traefik.http.routers.whoami-https.entrypoints=https
      - traefik.http.routers.whoami-https.tls=true
  nginx:
    image: nginx
    container_name: nginx
    environment:
      - USER_UID=1000
      - USER_GID=1000 
    restart: unless-stopped
    labels:
      - traefik.enable=true
      - traefik.http.routers.nginx-http.rule=Host(`hk.peos.cn`)
      - traefik.http.middlewares.redirect.redirectscheme.scheme=https
      - traefik.http.routers.nginx-http.middlewares=redirect
      #- traefik.http.routers.nginx-http.entrypoints=http
      
      - traefik.http.routers.nginx-https.rule=Host(`hk.peos.cn`)
      #- traefik.http.routers.nginx-https.entrypoints=https
      - traefik.http.routers.nginx-https.tls=true
    volumes:
      - ./nginx/conf:/etc/nginx/conf.d
      - ./nginx/data:/data

 

 

 

测试滚动更新:

先修改一image标签:

docker image tag traefik/whoami whoami:v2.0

再修改docker-compose中image为:whoami:v2.0

再更新:

docker compose up -d

 

over.

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:使用traefik做为docker网关(负载均衡/滚动更新) | Bruce's Blog

发表评论

留言无头像?