[TOC]
0x00 前言简述
InfluxDB 介绍
Q: 什么是InfluxDB?
InfluxDB 采用Go语言开发是一个开源时间序列平台, 是一个可编程且高性能的时间序列数据库,具有跨 OSS、云和企业产品的通用 API。
简单描述: Influxdb是由Golang 构建的时序数据库,由于由Go语言构建使得其跨平台部署相对方便。
Q: InfluxDB 有何用?
描述: InfluxDB 主要用于存储和查询数据、度量、事件和实时分析的可扩展数据存储,在后台处理数据以用于 ETL 或监控和警报目的、用户仪表板以及可视化和探索数据等的 API。
常用于物联网、分析和云应用程序的时间序列数据平台以及存储大量资源监控数据指标, 所以通常其还会与Grafana联用进行数据的展示。
Q: InfluxDB 时间序列平台的优点有那些?
- 用于实时应用程序的强大 API 和 工具集, 可以用你喜欢的语言(InfluxDB API、Arduino、C#、Go、Java、JavaScript、Kotlin、Node.js、PHP、Python、R、Ruby、Scala 和 Swift)编写代码
- 高性能时间序列引擎
- 庞大的云和开源开发人员社区
- 内存占用较少
Q: 版本区别说明
描述: InfluxDB 当前主要发型版分为社区版本(免费) 和 企业版本(收费), 通常我们使用社区版本即可, 它包含了基本的时间序列工具包,社区版本当前master分支为 2.x 版本, 但是对于想使用 1.x 版本可以在Github或者Docker镜像中找寻到其。InfluxDB 2.x 相比较于 InfluxDB 1.x 来说性能有很大的提升并且将 influxCLI 不再打包在来自influxdb需要单独下载, 其次是有关系型数据库基础的朋友可能比较喜欢 InfluxDB 1.x 因为其查询更加贴近,而 2.x 版本中增删改查都有较大的变化。1
2
3
4
5# InfluxDB 1.x
show databases;
# InfluxDB 2.x
influx write -b primary -o primary -p 't01,building=boli,floor=702a temp=24.51651036342'
温馨提示: 2019 年 1 月 11 日左右,这个仓库上的 master 将成为 InfluxDB 2.0 代码,infludata/platform 的内容将移至此存储库。如果你依赖于 master,你应该更新你的依赖来跟踪 maxter-1.x 分支。
温馨提示: InfluxDB 1.x 数据存储在数据库(database)中,InfluxDB OSS 2.2中,数据存储在桶(bucket)中。
InfluxDB 参考来源
influxdb 官网地址: https://influxdata.com/
influxdb 帮助文档: https://docs.influxdata.com/influxdb/latest/introduction/get-started/
influxdb Release: https://github.com/influxdata/influxdb/releases
influxdb v2.x 客户端工具 :https://github.com/influxdata/influx-cli
0x01 安装部署
描述: 我们可以采用多种方式进行安装部署 InfluxData , 例如 Docker 映像、Debian 包、RPM 包和 InfluxDB 的压缩包等方式安装。
温馨提示: influx 命令行界面 (CLI) 客户端作为单独的二进制文件提供在同一位置。
使用 Docker 安装 IndluxDB 2.x
安装步骤
步骤 01.直接运行docker run创建一个influxdb2容器。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16mkdir /opt/influxdb-docker-data-volume && cd $_
docker run --name influxdb2 -d -p 8086:8086 \
-e DOCKER_INFLUXDB_INIT_MODE=setup \
-e DOCKER_INFLUXDB_INIT_USERNAME=admin \
-e DOCKER_INFLUXDB_INIT_PASSWORD=weiyigeek.top \
-e DOCKER_INFLUXDB_INIT_ORG=weiyigeek \
-e DOCKER_INFLUXDB_INIT_BUCKET=primary \
--volume $PWD:/var/lib/influxdb2 \
influxdb:2.2.0
# ENV 解释:
DOCKER_INFLUXDB_INIT_MODE=setup # 指其定为初始化模式。
DOCKER_INFLUXDB_INIT_USERNAME=admin # 指定UI登陆用户名
DOCKER_INFLUXDB_INIT_PASSWORD=password # 指定UI登陆密码
DOCKER_INFLUXDB_INIT_ORG=org # 创建初始org
DOCKER_INFLUXDB_INIT_BUCKET=bucket # 创建初始bucket
参考地址:
使用 Helm 在 Kubernetes 安装 InfluxDB 2.x
描述: 此处为了方便快捷安装部署influxdb,我在Kubernetes集群环境中采用helm
方式(安装使用请参考【 https://blog.weiyigeek.top/2021/6-10-617.html 】)进行InfluxData
的安装部署。
实践操作
步骤 01.添加Chart仓库并在其仓库中搜索influxdb1
2
3
4
5helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update && helm search repo influxdb
# NAME CHART VERSION APP VERSION DESCRIPTION
# bitnami/influxdb 5.3.1 2.2.0 InfluxDB(TM) is an open source time-series data...
helm show values bitnami/influxdb > influxdb.yml
步骤 03.执行如下命令安装influxdb需指定修改后的Chart图表相关values的influxdb.yml文件。1
2
3
4
5
6
7
8
9
10
11helm install prometheus-influxdb bitnami/influxdb -f influxdb.yml --version 5.3.1 -n devtest --debug --create-namespace
# ** Please be patient while the chart is being deployed **
# InfluxDB™ can be accessed through following DNS names from within your cluster:
InfluxDB™: prometheus-influxdb.devtest.svc.cluster.prod (port 8086)
InfluxDB™ Prometheus Metrics: prometheus-influxdb-metrics.devtest.svc.cluster.prod (port 9122)
# To connect to your database run the following commands:
kubectl run prometheus-influxdb-client --rm --tty -i --restart='Never' --namespace devtest \
--image docker.io/bitnami/influxdb:2.2.0-debian-10-r41 --privileged=true \
--command -- bash
# To connect to your database from outside the cluster execute the following commands:
kubectl port-forward svc/prometheus-influxdb 8086:8086
步骤 04.验证部署的influxdb的Pod以及Service,然后进入prometheus-influxdb的Pod里shell终端中.1
2
3
4
5
6
7
8
9
10kubectl get pod,svc -n devtest -l app.kubernetes.io/component=influxdb
# NAME READY STATUS RESTARTS AGE
# pod/prometheus-influxdb-5fbc6d5f9d-ww6ck 1/1 Running 5 (4m49s ago) 10m
# NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
# service/prometheus-influxdb ClusterIP 10.98.134.121 <none> 8086/TCP,8088/TCP 10m
# service/prometheus-influxdb-metrics ClusterIP 10.109.222.86 <none> 9122/TCP 10m
kubectl exec -n devtest -it prometheus-influxdb-5fbc6d5f9d-ww6ck -- bash
I have no name!@prometheus-influxdb-5fbc6d5f9d-ww6ck:/$
步骤 06.在prometheus-influxdb-5fbc6d5f9d-ww6ck终端中执行influx命令。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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90# Pod 中关于 INFLUXDB 的环境变量
I have no name!@prometheus-influxdb-5fbc6d5f9d-ww6ck:/$ env | egrep "^INFLUXDB"
INFLUXDB_ADMIN_USER_TOKEN=1VreF2TZuek7V6hmnquF
INFLUXDB_ADMIN_ORG=primary # 缺省
INFLUXDB_CREATE_USER_TOKEN=no
INFLUXDB_ADMIN_USER=admin
INFLUXDB_HTTP_AUTH_ENABLED=true
INFLUXDB_ADMIN_BUCKET=primary # 缺省
INFLUXDB_ADMIN_USER_PASSWORD=iSzWSexDso
# influx 客户端工具版本
$ influx version
Influx CLI dev (git: none) build_date: 2022-04-11T15:46:15Z
# influxdb 监控检测
$ influx ping
OK
# influxdb 服务配置
$ influx server-config
{
"assets-path": "",
"bolt-path": "/bitnami/influxdb/influxd.bolt", # 持久化关注点
"e2e-testing": false,
"engine-path": "/bitnami/influxdb", # 持久化关注点
"feature-flags": null,
"flux-log-enabled": false,
"hardening-enabled": false,
"http-bind-address": "0.0.0.0:8086",
"http-idle-timeout": 180000000000,
"http-read-header-timeout": 10000000000,
"http-read-timeout": 0,
"http-write-timeout": 0,
"influxql-max-select-buckets": 0,
"influxql-max-select-point": 0,
"influxql-max-select-series": 0,
"log-level": "info",
"metrics-disabled": false,
"nats-max-payload-bytes": 0,
"nats-port": 0,
"no-tasks": false,
"pprof-disabled": false,
"query-concurrency": 1024,
"query-initial-memory-bytes": 0,
"query-max-memory-bytes": 0,
"query-memory-bytes": 0,
"query-queue-size": 1024,
"reporting-disabled": false,
"secret-store": "bolt",
"session-length": 60,
"session-renew-disabled": false,
"sqlite-path": "/bitnami/influxdb/influxd.sqlite", # 持久化关注点
"storage-cache-max-memory-size": 1073741824,
"storage-cache-snapshot-memory-size": 26214400,
"storage-cache-snapshot-write-cold-duration": "10m0s",
"storage-compact-full-write-cold-duration": "4h0m0s",
"storage-compact-throughput-burst": 50331648,
"storage-max-concurrent-compactions": 0,
"storage-max-index-log-file-size": 1048576,
"storage-no-validate-field-size": false,
"storage-retention-check-interval": "30m0s",
"storage-series-file-max-concurrent-snapshot-compactions": 0,
"storage-series-id-set-cache-size": 0,
"storage-shard-precreator-advance-period": "30m0s",
"storage-shard-precreator-check-interval": "10m0s",
"storage-tsm-use-madv-willneed": false,
"storage-validate-keys": false,
"storage-wal-fsync-delay": "0s",
"storage-wal-max-concurrent-writes": 0,
"storage-wal-max-write-delay": 600000000000,
"storage-write-timeout": 10000000000,
"store": "disk",
"testing-always-allow-setup": false,
"tls-cert": "",
"tls-key": "",
"tls-min-version": "1.2",
"tls-strict-ciphers": false,
"tracing-type": "",
"ui-disabled": false,
"vault-addr": "",
"vault-cacert": "",
"vault-capath": "",
"vault-client-cert": "",
"vault-client-key": "",
"vault-client-timeout": 0,
"vault-max-retries": 0,
"vault-skip-verify": false,
"vault-tls-server-name": "",
"vault-token": ""
}
步骤 07.查看influxdb提供的UI界面, 此处我们可编辑svc将其通过NodePort方式保留服务, 也可通过port-forward临时转发其服务端口, 然后便可通过浏览器访问mater节点+32506端口进行访问。1
2
3
4
5
6
7
8
9# 方式1.NodePort
$ kubectl edit svc -n devtest prometheus-influxdb
$ kubectl get svc -n devtest prometheus-influxdb
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
prometheus-influxdb NodePort 10.98.134.121 <none> 8086:32506/TCP,8088:30441/TCP 6d3h
# 方式2.port-forward
kubectl port-forward -n devtest --address 0.0.0.0 svc/prometheus-influxdb 32506:8086
# Forwarding from 0.0.0.0:32506 -> 8086
温馨提示: 此处登陆密码为前面env环境中获取的INFLUXDB_ADMIN_USER
与INFLUXDB_ADMIN_USER_PASSWORD
环境变量值(admin/iSzWSexDso)。

WeiyiGeek.influxdb-UI
步骤 08.安装并使用客户端工具连接创建的prometheus-influxdb远程influxdb数据库。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15# 方式1.创建一个 influxdb-client 容器
kubectl run prometheus-influxdb-client --namespace devtest --rm --tty -i --restart='Never' --image docker.io/bitnami/influxdb:2.2.0-debian-10-r41 --privileged=true -- bash
# 方式2.下载安装部署influx二进制客户端到宿主机上
wget -c https://dl.influxdata.com/influxdb/releases/influxdb2-client-2.3.0-linux-amd64.tar.gz && tar -zxvf influxdb2-client-2.3.0-linux-amd64.tar.gz
cp influxdb2-client-2.3.0-linux-amd64/influx /usr/local/bin/influx
# 启用命令自动补全
# CLI支持为bash、zsh和powershell生成完成,要启用单个shell会话的完成(临时生效),请运行以下命令之一:
# For bash:
source <(influx completion bash)
# For zsh:
source <(influx completion zsh)
# For pwsh:
Invoke-Expression ((influx completion powershell) -join "`n`")
温馨提示: 在V2.x版本及其之后客户端工具已经从influxdb中分离开成为一个独立的项目(https://github.com/influxdata/influx-cli/
)。
温馨提示: 如果希望自动补全在其它shell会话中也可以将该命令加入到~/.bash_profile
文件中使得下次打开shell终端可以自动补全。1
2
3
4tee -a ~/.bash_profile <<'EOF'
source <(influx completion bash)
EOF
source ~/.bash_profile
步骤 09.安装完成后使用 influx命令 添加需要连接的 influxdb 数据库。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22# 方式1.使用 influx config 命令进行 influxdb 数据库连接设置。
# -a 激活状态
# -n 连接名称
# -t 指定认证Token
# -o 组织名称
# -u InfluxDB服务器的URL
# 在容器里面
influx config create -a -n prometheus-influxdb -t 1VreF2TZuek7V6hmnquF -o primary -u http://prometheus-influxdb:8086
# 在宿主机上
influx config create -a -n prometheus-influxdb -t 1VreF2TZuek7V6hmnquF -o primary -u http://10.98.134.121:8086
# 查看当前激活的influxdb连接对象
$ influx config
Active Name URL Org
* prometheus-influxdb http://10.98.134.121:8086 primary
$ influx ping
OK
# 方式2.使用 influx setup 命令进行数据库连接初始化设置。
# 删除influxdb连接字符串
$ influx config rm prometheus-influxdb
Active Name URL Org Deleted
* prometheus-influxdb http://10.98.134.121:8086 true
步骤 10.influx 2.x 数据库数据增删改查。1
2
3
4
5
6
7# 数据增加
influx write -b primary -o primary 'airSensors,sensor_id=home temperature=71.18922021239435,humidity=35.096794192432846,co=0.49012238573499495'
influx write -b primary -o primary 'airSensors,sensor_id=home temperature=72.18922021239435,humidity=36.096794192432846,co=0.50012238573499495'
influx write -b primary -o primary 'airSensors,sensor_id=home temperature=73.18922021239435,humidity=37.096794192432846,co=0.51012238573499495'
# 数据删除
influx delete -b primary -o primary -p sensor_id=home --start '2022-06-05T08:26:39.455Z' --stop '2022-06-10T09:50:39.455Z'

WeiyiGeek.利用Influxdb中DataExplorer查看插入的数据
温馨提示: 使用 influx query
命令后如果想要退出查询, 则可以按下 ctrl + d
。
使用 kubectl 在 Kubernetes 部署 InfluxDB 1.X
描述: 在k8s集群中可能我们常常会使用kubectl客户端工具指定资源清单的进行apply以部署相应资源,此处我们将演示以资源清单方式部署 InfluxDB 1.X。
温馨提示: 当前【2022年6月7日 19:51:14】1.X 最新的InfluxDB版本为 1.8.10。
步骤 01.准备的部署influxdb的资源清单如下: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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77# influxdb-Deployment.yaml
tee influxdb-Deployment.yaml <<'EOF'
apiVersion: v1
kind: Service
metadata:
labels:
app: influxdb
name: influxdb
spec:
ports:
- name: http
nodePort: 30083
port: 8083
protocol: TCP
targetPort: 8083
- name: api
nodePort: 30083
port: 8086
protocol: TCP
targetPort: 8086
- name: graphite
nodePort: 30003
port: 2003
protocol: TCP
targetPort: 2003
selector:
app: influxdb
sessionAffinity: None
type: NodePort
apiVersion: apps/v1
kind: Deployment
metadata:
name: influxdb
labels:
app: influxdb
spec:
replicas: 1
selector:
matchLabels:
app: influxdb
template:
metadata:
labels:
app: influxdb
spec:
containers:
- image: influxdb:1.8.10
imagePullPolicy: IfNotPresent
name: influxdb
ports:
- containerPort: 8083
name: influx
protocol: TCP
- containerPort: 8086
name: api
protocol: TCP
- containerPort: 2003
name: graphite
protocol: TCP
volumeMounts:
- mountPath: /etc/influxdb
name: config-volume
- mountPath: /var/lib/influxdb/
name: data
restartPolicy: Always
terminationGracePeriodSeconds: 30
volumes:
- name: config-volume
configMap:
defaultMode: 420
name: influxdb-config
- name: data
hostPath:
path: /nfsdisk-31/datastore/influxdb/data
type: DirectoryOrCreate
EOF
步骤 02.准备influxdb使用的配置文件并存放到configMap资源控制器下。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18tee influxdb.conf <<'EOF'
[meta]
dir = "/var/lib/influxdb/meta"
[data]
dir = "/var/lib/influxdb/data"
engine = "tsm1"
wal-dir = "/var/lib/influxdb/wal"
# Configure the graphite api
[[graphite]]
enabled = true
# If not set, is actually set to bind-address.
bind-address = ":2003"
# store graphite data in this database
database = "appdb"
EOF
$ kubectl create cm influxdb-config --from-file=influxdb.conf
configmap/influxdb-config created
步骤 03.使用kubectl工具按照influxdb-Deployment.yaml资源清单中的内容进行安装部署, 并且查看其部署情况。1
2
3
4
5
6
7
8
9
10$ kubectl apply -f influxdb-Deployment.yaml
# service/influxdb created
# deployment.apps/influxdb created
$ kubectl get pod,svc -l app=influxdb
# NAME READY STATUS RESTARTS AGE
# pod/influxdb-f54d77bc6-cp46r 1/1 Running 0 59s
# NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
# service/influxdb NodePort 10.101.187.102 <none> 8083:30083/TCP,8086:30086/TCP,2003:30003/TCP 33h
步骤 04.进入到influxdb-f54d77bc6-cp46r的Pod容器内部shell终端,并使用influx客户端连接到默认的 influxdb 服务端口上。
1 | $ kubectl exec -it influxdb-f54d77bc6-cp46r bash |
0x02 InfluxDB2.X 数据查询
描述: 在 InfluxDB2.x 中提供了两种语法查询数据,Flux 和 InfluxQL。
Flux - 功能给你性脚本语言
描述: Flux 一门新的功能性数据脚本语言(Flux Script),旨在将查询、处理、分析和对数据的操作统一为一个语法,这里仅介绍针对查询需要的一些常用函数和操作,了解更多Flux见Flux官方文档。
基础规定, 每个查询语句必须包含数据源,时间区间和过滤器, 对应关键字分别为 from,range,filter。实际上不包含filter也是可以的,可以理解为filter过滤条件就是all,所以说Flux查询遵循如下几个步骤.
- 指定数据源
- 时间范围
- 过滤数据
- 对数据整形、聚合函数(重新组织数据结构)
- 输出结果
1 | # 1.指定数据存放的bucket名称 |

WeiyiGeek.使用Flux进行influxDB数据查询
Flux 标准库(部分)
该部分列举常用flux标准库函数,根据用法可举一反三使用所有标准库函数
buckets() :返回当前组中所有的桶
from(bucket: “demo-bucket”) : 指定从那个bucket查询数据
range() : 指定时间区间range接收两个参数start和stop, 其中stop可以省略缺省值是当前时间,而start不能省略否则会报语法错误。
1 | #示例1 |
- filter() : 对数据进行过滤,接收一个判断函数,其类似于类似于java中的lamda, 例如
(r) => (r.recordProperty comparisonOperator comparisonExpression)
。
1 | # 1.判断函数返回一个boolean类型值, 只有符合条件的记录才会被返回。 |
- contains(value, set) :判断value是否包含在set中。
1 | #示例 |
window(very, period, offset) : 该函数根据时间将数据分组。
very 指定每个时间窗口的时间,例如 every = 5 那么 时间窗口可以是 0m-5m,5m-10m,10m-15m。默认为 period 的值
period 明确在每个时间窗口中需要从时间窗口起始到多久的数据,例如时间窗口为10m-15m period = 3 则只取10m-13m的数据,13m-15m的数据会被抛弃。默认为 every 的值
offset 指定每个时间窗口的时间偏移量,例如时间窗口为10m-15m offset = 3 那么会取 13m-15m的数据,10m-13m的数据会被抛弃,但注意当offset = every 时 offset不生效。且offset大于every 时 生效offset = offset % everymin() : column 求最小值的列默认为_value , 取每个分组中最小值
- max() : column 求最大值的列默认为_value , 取每个分组中最大值
1 | # 例如, 获取 student 表中 score 字段的最大值。 |
bottom(n:10, columns: [“_value”]) :根据columns进行排序并返回最上层n条记录。
count(column: “_value”) :统计_value列中的数据个数。
columns(column: “_value”) : 返回所有列标签名并存储在指定的列中默认为_value 通过column参数指定.
cumulativeSum(columns: [“_value”]) : 根据给定列对该列数据进行累加,模拟输出忽略 _start, _stop 等列.
1
2
3
4
5
6
7
8
9
10
11
12
13# 原始数据:
_measurement _field 62
temperature wxm 62
temperature wxm 65
temperature wxm 81.5
temperature wxm 81.5
# cumulativeSum函数执行后:
_measurement _field 62
temperature wxm 62
temperature wxm 127 # 62 + 65
temperature wxm 208.5 # 62 + 65 + 81.5
temperature wxm 290 # 62 + 65 + 81.5 + 81.5group(columns: [“host”, “_measurement”], mode:”by”) : 按那些列进行分组操作默认值为[]。
1 | # mode |
derivative(unit: 1s, nonNegative: true, columns: [“_value”], timeColumn: “_time”) :计算后续非空记录之间每单位时间的更改率。输出表架构将与输入表相同
unit 求多长时间内的变化速率。变化速率=(下一个值-上一个值)/(下一个时间-上一个时间)* unit 默认值1s
nonNegative 变化速率是否可以是负值,如果是负数influxdb会假定前一个值为0 默认值true
columns 指定计算变化速率的列 默认值 [“_value”]
timeColumn 手动指定时间列 默认值 “_time”difference(nonNegative: false, columns: [“_value”]) : 计算指定列中后续非null记录之间的差异。
nonNegative 是否允许差值为负数,如果是负数influxdb会假定前一个值为0 默认值false, 计算相邻两行的差值(next-pre)
keepFirst 是否保留第一行,默认值为falsedistinct(column: “host”): 按指定列去重.
InfluxQL - 关系型数据库查询
描述: InfluxDB 1.x数据存储在数据库(database)中,InfluxDB OSS 2.2中,数据存储在桶(bucket)中, 由于InfluxQL使用了1.x数据模型,在使用InfluxQL进行查询之前,必须将桶映射到一个数据库和保留策略(DBRP)。可以这样理解 InfluxQL 只有数据库才能使用,如果想要在桶上也能使用只有将桶映射成数据库。
使用InfluxQL查询桶数据,需要完成以下步骤:
- 1、确认桶有映射;
- 2、映射未映射的桶;
- 3、使用InfluxQL查询已映射的桶;
1 | # 1.查询所有的DBRP(DataBase Retention Police)映射 |