【Drone入门与实践-9】traefik网关搭建

752 阅读5分钟

Treafik是基于go开发的一个网关,可以很好地结合docker,可以自动发现docker下的容器,并且支持均衡负载。特别是在docker容器水平伸缩后,不需要重启网关就可以自动发现新增的容器,或者在减少容器的时候自动调整。相比较nginx,nginx需要手工修改配置文件,还需热重启,显得略微麻烦。

Traefik还支持熔断,还有各种插件。更多的功能需要各位同学前往官网探索。

1、配置文件

这里贴出笔者的配置文件:

################################################################
#
# Configuration sample for Traefik v2.
#
# For Traefik v1: https://github.com/traefik/traefik/blob/v1.7/traefik.sample.toml
#
################################################################

################################################################
# Global configuration
################################################################
[global]
  checkNewVersion = true
  sendAnonymousUsage = true

################################################################
# Entrypoints configuration
################################################################

# Entrypoints definition
#
# Optional
# Default:
[entryPoints]
  [entryPoints.web]
    address = ":80"

  [entryPoints.websecure]
    address = ":443"

################################################################
# Traefik logs configuration
################################################################

# Traefik logs
# Enabled by default and log to stdout
#
# Optional
#
[log]

  # Log level
  #
  # Optional
  # Default: "ERROR"
  #
  # level = "DEBUG"

  # Sets the filepath for the traefik log. If not specified, stdout will be used.
  # Intermediate directories are created if necessary.
  #
  # Optional
  # Default: os.Stdout
  #
  filePath = "log/traefik.log"

  # Format is either "json" or "common".
  #
  # Optional
  # Default: "common"
  #
  # format = "json"

################################################################
# Access logs configuration
################################################################

# Enable access logs
# By default it will write to stdout and produce logs in the textual
# Common Log Format (CLF), extended with additional fields.
#
# Optional
#
[accessLog]

  # Sets the file path for the access log. If not specified, stdout will be used.
  # Intermediate directories are created if necessary.
  #
  # Optional
  # Default: os.Stdout
  #
  filePath = "/log/log.txt"

  # Format is either "json" or "common".
  #
  # Optional
  # Default: "common"
  #
  # format = "json"

################################################################
# API and dashboard configuration
################################################################

# Enable API and dashboard
[api]

  # Enable the API in insecure mode
  #
  # Optional
  # Default: false
  #
  insecure = true

  # Enabled Dashboard
  #
  # Optional
  # Default: true
  #
  dashboard = true

################################################################
# Ping configuration
################################################################

# Enable ping
[ping]

  # Name of the related entry point
  #
  # Optional
  # Default: "traefik"
  #
  # entryPoint = "traefik"

################################################################
# Docker configuration backend
################################################################

# Enable Docker configuration backend
[providers.docker]

  # Docker server endpoint. Can be a tcp or a unix socket endpoint.
  #
  # Required
  # Default: "unix:///var/run/docker.sock"
  #
  # 这台机器是我们用来部署应用程序的机器
  endpoint = "ssh://root@172.16.113.10"
  network = "app_network"

  # Default host rule.
  #
  # Optional
  # Default: "Host(`{{ normalize .Name }}`)"
  #
  # defaultRule = "Host(`{{ normalize .Name }}.docker.localhost`)"

  # Expose containers by default in traefik
  #
  # Optional
  # Default: true
  #
  # exposedByDefault = false
  #
  #
[metrics]
  [metrics.influxDB] 
    address = "influxdb:8089"
    protocol = "udp"
    #database = "traefik"

2、准备工作

在test-node-1上

# 创建存放配置文件的目录
mkdir /data/traefik/conf
# 新建配置文件,复制上面的配置文件,粘贴进去
vim /data/treafik/conf/traefik.toml

3、traefik的安装

这里我们把80端口,和443端口给traefik使用,这样我们访问我们的应用的时候就不需要每次带个端口了。

docker run -d -p 81:8080 -p 80:80 -p 443:443 \
-v /data/traefik/conf/traefik.toml:/etc/traefik/traefik.toml \
--name=traefik \
--network=app_network \
-v /root/.ssh:/root/.ssh \
traefik:v2.4.8

4、验证安装

等待安装完成后,可以在浏览器访问http://172.16.113.9:81,这是网关自带的dashboard。

image.png

⚠️别以为到这里就OK了,其实还用不了呢!到这一步,只是traefik跑起来了,但是作为网关并没有起作用。

5、配置SSH免密登录

由于我们使用了ssh的方式来和172.16.113.10上的docker进行通信,因此,我们需要配置免密登录。 在test-node-1上执行ssh-keygen,然后执行ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.113.9,这样就把公钥上传到test-node-2上了。

6、容器内安装SSH

traefik镜像中不包含ssh程序,因此我们需要手动安装一下。过程也是比较简单。 首先进入到traefik容器内部,执行apk add openssh。 安装完成后traefik就可以自动连接上test-node-2的docker了。

7、错误排查

如果出现网关的问题,比如无法自动发现容器,可以在容器内查看日志,日志文件在/log下面。

8、traefik的监控

网关是比较核心的部件,因此我们最好能快速查看到它的运行状况,下面我们让它把监控数据写入实时数据库,然后展示出来。

8.1、influxdb配置文件

我们需要手动准备配置文件,开启influxdb的upd数据端口,默认是没有开启的。

# ⚠️必须要手动创建这2个目录,否则influxdb无法写入数据。
mkdir /data/influxdb/conf
mkdir /data/influxdb/data
vim /data/influxdb/conf/influxdb.conf

配置文件内容:

[meta]
  dir = "/var/lib/influxdb/meta"

[data]
  dir = "/var/lib/influxdb/data"
  engine = "tsm1"
  wal-dir = "/var/lib/influxdb/wal"

[[udp]]
  enabled = true
  bind-address = ":8089" # the bind address
  database = "telegraf" # name of the database that will be written to
  batch-size = 5000 # will flush if this many points get buffered
  batch-timeout = "1s" # will flush at least this often even if the batch-size is not reached
  batch-pending = 10 # number of batches that may be pending in memory
  read-buffer = 0 # UDP read buffer, 0 means to use OS default
  precision = "n" # sets the default precision of points written via UDP

8.2、安装influxdb

这里我们需要安装一个influxdb来作为监控数据的存储。⚠️注意下版本号。这里使用1.8.3,暂时无法使用2.0系列的版本。

docker run -d \
--privileged=true \
-p 8089:8089 \
-p 8086:8086 \
--network=app_network \
-v /data/influxdb/data:/var/lib/influxdb \
-v /data/influxdb/conf/:/etc/influxdb \
--name influxdb influxdb:1.8.3

8.3、验证traefik是否真的把数据写入了influxdb

在portainer上进入influxdb容器,然后在命令行输入influx, 然后输入show databases;回车(可能要等几分钟才会出现)。 查看是否有默认的数据库telegraf出现即可。这个数据库名称也可以修改的。在traefik.toml中修改。 如下图: image.png

8.4、安装grafana

使用grafana来做监控数据可视化,grafana是一个图表创建工具,可以很好的支持influxdb。下面是安装命令:

docker run -d \
--name=grafana \
-p 4000:3000 \
--network=app_network \
grafana/grafana

用户名密码默认为:admin/admin

8.5、配置grafana

访问http://172.16.113.9:4000,使用默认账号和密码登录后: image.png 我们点击左侧工具条上的齿轮按钮添加数据源: image.png 我们选择influxDBimage.png 填写配置,如果数据库不存在会提示的: image.png 最终应该是提示这样的: image.png 完成后我们就可以创建dashboard了。这里需要去网上查询一下influxdb的查询语句。

9、小结

现在网关和监控基本搭建完成了。监控图表的创作还请查询网上资料。