如何在 Ubuntu 20.04 上使用 Docker 安装 Metabase

介绍

Metabase是一个基于Java的开源商业智能(BI)工具,可以连接到许多不同的数据库,并具有一个问题构建工具,可以隐藏大型SQL查询的复杂性。

在本教程中,您将使用 Docker Compose 安装 Metabase,然后安装 Nginx 作为您的 Metabase 网站的反向代理程序。接下来,您将通过使用 Certbot 启用安全的 HTTPS 连接,从 Let's Encrypt Certificate Authority下载和配置 TLS 证书。

前提条件

要完成本教程,您将需要:

  • 具有 2 个或更多 CPU 的 Ubuntu 20.04 服务器,具有 sudo 访问的非根用户和防火墙。 要设置此功能,请遵循我们的 初始服务器设置与 Ubuntu 20.04 教程。
  • Docker 已安装。

<$>[info] 注: 如果您正在使用 DigitalOcean 的 One-Click Docker Image,您可以跳过这些前提条件。

在您选择的区域启动新 Docker 图像,然后登录为 root 用户,然后继续使用教程。

最后,要启用 TLS,您需要一个域名,指向您的服务器的公共 IP 地址。这应该是example.commetabase.example.com。如果您使用的是 DigitalOcean,请参阅我们的 DNS Quickstart 有关在您的控制面板中创建域资源的信息。

一旦你有所有前提条件,继续到 步骤1 ,在那里你将安装docker-compose包。

步骤 1 – 安装 Docker Compose

若要安装docker-compose命令行工具,请先更新您的包列表:

1sudo apt update

然后使用apt安装包:

1sudo apt install docker-compose

<$>[注] :您还可以安装比 Ubuntu 20.04 附带的更新的 Docker Compose 包。

如果您选择使用此版本的 Docker Compose,则需要在本指南中代替docker compose作为命令而不是docker-compose

您可以通过运行以下命令确认该包已安装:

1docker-compose --version

您应该获得如下类型的输出:

1[secondary_label Output]
2docker-compose version 1.25.0, build unknown
3docker-py version: 4.1.0
4CPython version: 3.8.10

一旦您确认 Docker Compose 已安装在您的服务器上,您将在本教程的下一步中使用它来配置和启动 Metabase。

步骤 2 — 使用 Docker Compose 运行元数据库

要开始创建您的Metabase容器,在您的主目录中创建一个名为Metabase的目录,以存储您将在本教程中创建的文件。

运行以下命令来创建目录:

1mkdir ~/metabase

然后导航它:

1cd ~/metabase

现在,使用nano或您喜爱的编辑器,打开一个名为docker-compose.yml的新空白YAML文件:

1nano docker-compose.yml

您将使用此文件与docker-compose命令启动您的Metabase容器。

 1[label docker-compose.yml]
 2version: '3'
 3services:
 4  metabase:
 5    image: metabase/metabase:latest
 6    ports:
 7      - "127.0.0.1:3000:3000"
 8    volumes:
 9      - data:/metabase
10
11volumes:
12  data:

该文件定义了一个名为metabase服务,该服务运行了Metabase应用程序。该服务还引用了一个命名的卷,它用于存储在运行容器实例外的数据。

如果你正在使用nano,按CTRL+O,然后按RETURN,然后按CTRL+X退出。

您现在已经准备好使用docker-compose命令启动元数据库容器:

1docker-compose up -d

子命令告诉docker-compose启动容器和相关的卷(s)和网络(s)在你的docker-compose.yml文件中定义。 -d旗(即daemonize)告诉docker-compose在背景中运行容器,以便命令不会接管你的终端。

1[secondary_label Output]
2Creating network "metabase_default" with the default driver
3Creating volume "metabase_data" with default driver
4Pulling metabase (metabase/metabase:latest)...
5latest: Pulling from metabase/metabase
6. . .

<$>[info] 如果您想在任何时候停止您的Metabase容器,请在您的 `~/Metabase'目录中运行以下命令:

1docker-compose stop

容器将被停止. 卷中的配置和数据将保留,以便您可以使用docker-compose up -d命令重新启动容器。

完成后,Metabase 应该运行,您可以通过使用curl命令来检测网页服务器在127.0.0.1:3000运行:

1curl --head http://localhost:3000

这将只从响应中打印 HTTP 标题:

 1[secondary_label Output]
 2HTTP/1.1 200 OK
 3Date: Tue, 15 Mar 2022 19:26:22 GMT
 4X-Frame-Options: DENY
 5X-XSS-Protection: 1; mode=block
 6Last-Modified: Tue, 15 Mar 2022 19:26:22 GMT
 7Strict-Transport-Security: max-age=31536000
 8Set-Cookie: metabase.DEVICE=3525ed6d-13e0-4ad0-8fdb-f1bc426706dc;HttpOnly;Path=/;Expires=Sat, 15 Mar 2042 19:26:22 GMT;SameSite=Lax
 9X-Permitted-Cross-Domain-Policies: none
10Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate
11X-Content-Type-Options: nosniff
12Content-Security-Policy: default-src 'none'; script-src 'self' 'unsafe-eval' https://maps.google.com https://apis.google.com https://*.googleapis.com *.gstatic.com https://www.google-analytics.com   'sha256-lMAh4yjVuDkQ9NqkK4H+YHUga+anpFs5JAuj/uZh0Rs=' 'sha256-ib2/2v5zC6gGM6Ety7iYgBUvpy/caRX9xV/pzzV7hf0=' 'sha256-JJa56hyDfUbgNfq+0nq6Qs866JKgZ/+qCq2pkDJED8k='; child-src 'self' https://accounts.google.com; style-src 'self' 'unsafe-inline'; font-src 'self' ; img-src * 'self' data:; connect-src 'self' metabase.us10.list-manage.com www.google-analytics.com https://sp.metabase.com ; manifest-src 'self';  frame-ancestors 'none';
13Content-Type: text/html;charset=utf-8
14Expires: Tue, 03 Jul 2001 06:00:00 GMT
15Content-Length: 0
16Server: Jetty(9.4.43.v20210629)

200 OK响应意味着应用程序正在响应端口3000设置cookie服务器标注标题确认应用程序是在Jetty Web 服务器上运行的Metabase。

接下来,我们将设置 Nginx 来向 Metabase 容器代理公共流量。

步骤 3:安装和配置 Nginx

将 Nginx 等 Web 服务器放在您的 Metabase 服务器前面可以通过下载缓存、压缩和静态文件来提高性能,从而实现更高效的流程。我们将安装 Nginx 并将其配置为 reverse proxy 请求到 Metabase,这意味着它将负责处理用户向 Metabase 返回的请求。

首先,更新您的包列表,然后使用apt安装 Nginx:

1sudo apt install nginx

允许公共流量到端口 80 和 443 (HTTP 和 HTTPS) 使用 Nginx 完整 UFW 应用程序配置文件:

1sudo ufw allow "Nginx Full"
1[secondary_label Output]
2Rule added
3Rule added (v6)

接下来,在 /etc/nginx/sites-available 目录中打开一个新的 Nginx 配置文件,我们将称之为 `metabase.conf',但您可以使用不同的名称:

1sudo nano /etc/nginx/sites-available/metabase.conf

将下列内容粘贴到新配置文件中,确保将your_domain_here替换为您已配置的域,以指向您的Metabase服务器。

 1[label /etc/nginx/sites-available/metabase.conf]
 2server {
 3    listen 80;
 4    listen       [::]:80;
 5    server_name your_domain_here;
 6
 7    access_log  /var/log/nginx/metabase.access.log;
 8    error_log   /var/log/nginx/metabase.error.log;
 9
10    location / {
11      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
12      proxy_set_header X-Real-IP $remote_addr;
13      proxy_set_header X-Forwarded-Host $host;
14      proxy_set_header X-Forwarded-Proto https;
15      proxy_pass http://localhost:3000;
16  }
17}

此配置目前仅限于HTTP,因为我们将让Certbot在下一步负责配置TLS,其余的配置文件会设置日志位置,然后传输所有流量,以及一些重要的代理标题,以及http://localhost:3000,我们在上一步启动的Metabase容器。

保存并关闭文件,然后通过将其链接到 /etc/nginx/sites-enabled/ 来启用配置:

1sudo ln -s /etc/nginx/sites-available/metabase.conf /etc/nginx/sites-enabled/

使用nginx -t来验证配置文件语法是否正确:

1sudo nginx -t
1[secondary_label Output]
2nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
3nginx: configuration file /etc/nginx/nginx.conf test is successful

最后,用新的配置重新加载nginx服务:

1sudo systemctl reload nginx.service

您的元数据库网站现在应该在简单的HTTP上可用。 加载http://your_domain_here(您可能需要点击安全警告),它将看起来像这样:

Screenshot of Metabase's "Welcome to Metabase" page, with a button that says "Let's get started".

现在你已经有了网站并在 HTTP 上运行,现在是时候使用 Certbot 和 Let's Encrypt 证书确保连接了。

步骤 4 – 安装 Certbot 和设置 TLS 证书

借助Certbot和Let's Encrypt免费证书授权,将TLS加密添加到您的Metabase应用程序只需要两个命令。

首先,安装 Certbot 及其 Nginx 插件:

1sudo apt install certbot python3-certbot-nginx

接下来,在--nginx模式下运行certbot,并指定您在 Nginxserver_name配置指令中使用的相同域:

1sudo certbot --nginx -d your_domain_here

您将被要求同意 Let's Encrypt 服务条款,并输入电子邮件地址。

之后,您将被问及是否要将所有HTTP流量重定向到HTTPS。

之后,Let’s Encrypt 将确认您的请求,Certbot 将下载您的证书:

 1[secondary_label Output]
 2Congratulations! You have successfully enabled https://metabase.example.com
 3
 4You should test your configuration at:
 5https://www.ssllabs.com/ssltest/analyze.html?d=metabase.example.com
 6- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 7
 8IMPORTANT NOTES:
 9 - Congratulations! Your certificate and chain have been saved at:
10   /etc/letsencrypt/live/metabase.example.com/fullchain.pem
11   Your key file has been saved at:
12   /etc/letsencrypt/live/metabase.example.com/privkey.pem
13   Your cert will expire on 2022-05-09. To obtain a new or tweaked
14   version of this certificate in the future, simply run certbot again
15   with the "certonly" option. To non-interactively renew *all* of
16   your certificates, run "certbot renew"
17 - Your account credentials have been saved in your Certbot
18   configuration directory at /etc/letsencrypt. You should make a
19   secure backup of this folder now. This configuration directory will
20   also contain certificates and private keys obtained by Certbot so
21   making regular backups of this folder is ideal.
22 - If you like Certbot, please consider supporting our work by:
23
24   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
25   Donating to EFF:                    https://eff.org/donate-le

Certbot 将自动重新加载 Nginx 与新的配置和证书. 在您的浏览器中重新加载您的网站,如果您选择了重定向选项,它应该自动切换到 HTTPS。

您的网站现在是安全的,它是安全的继续基于Web的设置步骤。

步骤5:设置代码库

回到您的网页浏览器,重新加载页面. 您现在应该通过安全的 https:// 连接打开 Metabase 的数据库配置页面. 现在您可以通过点击 让我们开始 按钮完成配置过程。

您将被要求输入以下细节:

  • 你最喜欢的语言是什么? 如果你的语言在列表中,请选择它,然后点击以下屏幕截图:

Screenshot of Metabase's on-boarding language selection page

  • 我们应该叫你什么? 填写所有字段,并确保在安全的地方记录您的密码。

Screenshot of Metabase's on-boarding user creation page

  • ** 添加您的数据:** 点击 pane 底部的 ** I will add my data later** 选项

Screenshot of Metabase's Add your data page with a highlighted circle around the "I'll add my data later" link

  • 使用数据偏好: 选择是否希望与Metabase共享使用数据,以帮助改进该工具。单击 ** 完成** ,一旦您已经做出选择。
  • 你已经设置了! 如果您想从Metabase项目获得更新,请添加您的电子邮件地址,否则您可以忽略此字段。

最后,点击 ** 带我到 Metabase ** 按钮. 您将到达一页,上面写着问候,Sammy,上面包含一组预配置的 ** X-Rays** ,您可以点击它来探索您加载的样本数据。

Screenshot of Metabase's dashboard page with links to example "X-Rays" that you can click to explore sample data

点击任何 X-Ray 以查看 Metabase 中的表的详细信息. 您可以使用 X-Ray 页面上的控件来过滤数据,或探索相关的数据库字段和表。

当你准备好尝试创建自己的 ** 问题** (这就是 Metabase 用来描述查询的术语),点击页面右上角的 ** 浏览数据** 网格图标。

你的屏幕将看起来如下:

Screenshot of Metabase's Question creation page showing a table of example review data

现在点击页面右上角的 Summarize 按钮,然后将 ** Summarize by** 字段更改为 ** Average of Rating** . Metabase 将根据评级字段中的表格中的数据计算此值。

Screenshot of Metabase's Question creation page showing how to create a graph of average product ratings

点击右下角的 **完成 ** 按钮,您将在页面上显示平均产品评级的图表,如下面的屏幕截图:

Screenshot of an example Question that shows a line graph of average product ratings grouped by month

您可以使用 ** 过滤器** 、 ** 可视化** 和 ** 设置** 按钮进行实验,以更改结果图表上的图表、数据和标签。

结论

在本教程中,您使用 Docker Compose 启动了 Metabase BI 工具,然后设置了 Nginx 反向代理,并使用 Let's Encrypt TLS 证书保护它。

您现在已经准备好开始将 Metabase 连接到外部数据库以查询自己的数据. Metabase 支持各种数据源(https://www.metabase.com/datasources/),包括传统的 SQL 数据库以及分析引擎和面向文档的数据库。

有关使用 Metabase 的更多信息,请参阅 官方 Metabase 文档

Published At
Categories with 技术
comments powered by Disqus