1、拉取镜像
拉取influxdb镜像 docker pull influxdb
查看已经拉取的镜像docker images
2、简单使用
docker环境下运行influxdb
docker run -d -p 8086:8086 --name myinfluxdb influxdb
-d:容器在后台运行
-p:将容器内端口映射到宿主机端口,格式为 宿主机端口:容器内端口;8086是influxdb的HTTP API端口
–name:容器名称
最后是镜像名称+tag,镜像为influxdb,tag的值0.8.8指定了要运行的版本,默认是latest
这时候我们已经在docker中启动了influxdb,使用的是默认配置项和默认的数据存储空间,并且没有在docker中的influxdb上创建数据库,这样的docker很显然是不能用于生产环境的,因此我们要解决两个问题:
1、创建宿主目录
创建宿主机上的influxdb环境目录mkdir -p /home/influxdb/data /home/influxdb/conf /home/influxdb/meta /home/influxdb/wal
2、拷贝配置
1)可以先启动influxdb的docker环境,从docker中拷贝配置,这种方式可以在没有influxdb环境时做,简单说下操作:
查看influxdb的容器id:docker ps -a
拷贝配置到/home/influxdb/conf
下:docker cp f 容器ID:/etc/nginx/nginx.conf /home/nginx/conf
2)从别的地方获取配置(宿主机上有influxdb)
执行命令导出influxdb的配置,进入/usr/bin/
后,执行influxd config > /home/default.conf
将配置导出到/home目录下。
3)已经导出的配置信息
reporting-disabled = false
bind-address = "127.0.0.1:8088"
[meta]
dir = "/var/lib/influxdb/meta"
retention-autocreate = true
logging-enabled = true
[data]
dir = "/var/lib/influxdb/data"
index-version = "inmem"
wal-dir = "/var/lib/influxdb/wal"
wal-fsync-delay = "0s"
query-log-enabled = true
cache-max-memory-size = 1073741824
cache-snapshot-memory-size = 26214400
cache-snapshot-write-cold-duration = "10m0s"
compact-full-write-cold-duration = "4h0m0s"
max-series-per-database = 1000000
max-values-per-tag = 100000
max-concurrent-compactions = 0
max-index-log-file-size = 1048576
trace-logging-enabled = false
[coordinator]
write-timeout = "10s"
max-concurrent-queries = 0
query-timeout = "0s"
log-queries-after = "0s"
max-select-point = 0
max-select-series = 0
max-select-buckets = 0
[retention]
enabled = true
check-interval = "30m0s"
[shard-precreation]
enabled = true
check-interval = "10m0s"
advance-period = "30m0s"
[monitor]
store-enabled = true
store-database = "_internal"
store-interval = "10s"
[subscriber]
enabled = true
http-timeout = "30s"
insecure-skip-verify = false
ca-certs = ""
write-concurrency = 40
write-buffer-size = 1000
[http]
enabled = true
bind-address = ":8086"
auth-enabled = false
log-enabled = true
write-tracing = false
pprof-enabled = true
https-enabled = false
https-certificate = "/etc/ssl/influxdb.pem"
https-private-key = ""
max-row-limit = 0
max-connection-limit = 0
shared-secret = ""
realm = "InfluxDB"
unix-socket-enabled = false
bind-socket = "/var/run/influxdb.sock"
max-body-size = 25000000
access-log-path = ""
[logging]
format = "auto"
level = "info"
suppress-logo = false
[ifql]
enabled = false
log-enabled = true
bind-address = ":8082"
[[graphite]]
enabled = false
bind-address = ":2003"
database = "graphite"
retention-policy = ""
protocol = "tcp"
batch-size = 5000
batch-pending = 10
batch-timeout = "1s"
consistency-level = "one"
separator = "."
udp-read-buffer = 0
[[collectd]]
enabled = false
bind-address = ":25826"
database = "collectd"
retention-policy = ""
batch-size = 5000
batch-pending = 10
batch-timeout = "10s"
read-buffer = 0
typesdb = "/usr/share/collectd/types.db"
security-level = "none"
auth-file = "/etc/collectd/auth_file"
parse-multivalue-plugin = "split"
[[opentsdb]]
enabled = false
bind-address = ":4242"
database = "opentsdb"
retention-policy = ""
consistency-level = "one"
tls-enabled = false
certificate = "/etc/ssl/influxdb.pem"
batch-size = 1000
batch-pending = 5
batch-timeout = "1s"
log-point-errors = true
[[udp]]
enabled = false
bind-address = ":8089"
database = "udp"
retention-policy = ""
batch-size = 5000
batch-pending = 10
read-buffer = 0
batch-timeout = "1s"
precision = ""
[continuous_queries]
log-enabled = true
enabled = true
query-stats-enabled = false
run-interval = "1s"
3、启动和DB创建
1)启动
配置完成后,使用命令在后台启动influxdb的docker镜像,我们就可以使用influxdb了
docker run -d -p 8083:8083 -p 8086:8086 --name influxdb -v /home/influxdb/conf/influxdb.conf:/etc/influxdb/influxdb.conf -v /home/influxdb/data:/var/lib/influxdb/data -v /home/influxdb/meta:/var/lib/influxdb/meta -v /home/influxdb/wal:/var/lib/influxdb/wal influxdb
说明:
-p 将宿主的8083/8086端口(:左)映射到docker镜像的8083端口(:右),8083端口为web管理端,默认不开启
-v 将宿主的目录(:左)挂载到docker镜像的目录(:右),注意右边的路径,是固定的
启动了influxdb之后,我们需要手动创建一个db,这个需要进入到docker镜像中
2)DB创建
执行``进入docker镜像
docker exec -it 容器ID /bin/bash
进入到/usr/bin/
,执行./influx
启动influxdb的客户端,创建客户端(怎么创建DB)
1、使用rest端口测试
完成以上我们就可以使用influxdb的docker镜像了,下面是我的两个测试deme,用go写的。
1)写数据demo
package main
import (
"fmt"
"github.com/influxdata/influxdb1-client/v2"
"math/rand"
"time"
)
func main() {
addr := "http://172.20.36.121:8086"
c, err := client.NewHTTPClient(client.HTTPConfig{
Addr: addr,
})
if err != nil {
fmt.Println("Error creating InfluxDB Client: ", err.Error())
}
defer c.Close()
//写数据
go func(c client.Client) {
for {
insert(c)
time.Sleep(time.Second * 1)
}
}(c)
for {
time.Sleep(time.Second)
}
}
func query(c client.Client) {
sql := `SELECT "value" FROM "cpu" WHERE ("host" = 'server_1') AND time >= now() - 1h `
//q := client.NewQuery(`SELECT "host","region","value" FROM "cpu"`, "mydb", "")
q := client.NewQuery(sql, "mydb", "")
response, err := c.Query(q)
if err != nil && response.Error() == nil {
fmt.Printf("query data failed,err:%v", err)
}
for key, result := range response.Results {
_ = key
//fmt.Println("key", ":", key)
//fmt.Println("err", ":", result.Err)
//fmt.Println("message", ":", result.Messages)
for _, row := range result.Series {
//fmt.Println("name", ":", row.Name)
//fmt.Println("tags", ":", row.Tags)
fmt.Println("columns", ":", row.Columns)
//fmt.Println("partial", ":", row.Partial)
for key1, value := range row.Values {
fmt.Println(key1, ":", value)
}
}
}
}
func insert(c client.Client) {
bp, _ := client.NewBatchPoints(client.BatchPointsConfig{
Database: "mydb",
Precision: "s",
})
// Create a point and add to batch
server := rand_host()
region := rand_region()
tags := map[string]string{"host": server, "region": region}
fields := map[string]interface{}{
"value": rand_value(),
}
pt, err := client.NewPoint("cpu", tags, fields, time.Now())
if err != nil {
fmt.Println("Error: ", err.Error())
}
bp.AddPoint(pt)
// Write the batch
if err := c.Write(bp); err != nil {
fmt.Printf("写入数据时失败,错误:%v", err)
}
}
func ping(c client.Client) {
if _, _, err := c.Ping(0); err != nil {
fmt.Println("Error pinging InfluxDB Cluster: ", err.Error())
}
}
//var str_hosts []string = []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
var str_hosts []string = []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
var str_regions []string = []string{"us_east", "us_south", "us_east", "us_north"}
func rand_host() string {
key := rand.New(rand.NewSource(time.Now().UnixNano())).Intn(len(str_hosts))
return "server_" + str_hosts[key]
}
func rand_region() string {
key := rand.New(rand.NewSource(time.Now().UnixNano())).Intn(len(str_regions))
return str_regions[key]
}
func rand_value() float64 {
return rand.New(rand.NewSource(time.Now().UnixNano())).Float64()
}
2)查数据demo
package main
import (
"fmt"
"github.com/influxdata/influxdb1-client/v2"
)
func main() {
addr := "http://172.20.36.121:8086"
c, err := client.NewHTTPClient(client.HTTPConfig{
Addr: addr,
})
if err != nil {
fmt.Println("Error creating InfluxDB Client: ", err.Error())
}
defer c.Close()
query(c)
}
func query(c client.Client) {
sql := `SELECT * FROM "cpu" `
//q := client.NewQuery(`SELECT "host","region","value" FROM "cpu"`, "mydb", "")
q := client.NewQuery(sql, "mydb", "")
response, err := c.Query(q)
if err != nil && response.Error() == nil {
fmt.Printf("query data failed,err:%v", err)
}
for key, result := range response.Results {
_ = key
//fmt.Println("key", ":", key)
//fmt.Println("err", ":", result.Err)
//fmt.Println("message", ":", result.Messages)
for _, row := range result.Series {
//fmt.Println("name", ":", row.Name)
//fmt.Println("tags", ":", row.Tags)
fmt.Println("columns", ":", row.Columns)
//fmt.Println("partial", ":", row.Partial)
for key1, value := range row.Values {
fmt.Println(key1, ":", value)
}
}
}
}
查到的结果:
宿主机上data目录结构:du -h --max-depth=1 ./*
2、使用udp端口
使用udp端口时需要注意几个问题:
1)修改配置中默认的DataBase,在udp模式下使用的是udp
,所以要修改成自己的database
2)启动docker时需要手动指定监听udp的8089端口,指令:-p 8089:8089/udp
3)指定配置启动
上面结束之后我们已经基本满足了docker的使用,下面整理了一些简单的自动化处理,从建立influxdb的docker到用户的创建等(原贴)。
1、安装脚本:docker_influxdb_install.sh
#! /bin/bash
#file:docker_influxdb_install.sh
#company:cvnavi.com
#author:Pengjunlin
echo "当前执行文件......$0"
IS_EXISTS_INFLUXDB_IMAGE_NAME="false"
IS_EXISTS_INFLUXDB_CONTAINER="false"
IS_EXISTS_INFLUXDB_CONTAINER_RUNGING="false"
START_CONTAINER_CHECK_MAX_TIMES=3
START_CONTAINER_CHECK_CURRENT=1
# ========================下载镜像======================================
for i in [ `docker images ` ]; do
if [[ "$i" == "docker.io/influxdb" || "$i" == "influxdb" ]]; then
echo "$i"
IS_EXISTS_INFLUXDB_IMAGE_NAME="true"
break
fi
done
if [[ $IS_EXISTS_INFLUXDB_IMAGE_NAME == "true" ]]; then
echo "本地已存在influxdb:latest镜像,不再重新下载......."
else
echo "本地不存在influxdb:latest镜像,正在下载......."
docker pull influxdb:latest
fi
# ====================创建镜像===========================================
if [[ $IS_EXISTS_INFLUXDB_CONTAINER == "false" ]]; then
echo "检查influxdb容器是否创建......"
for i in [ `docker ps -a` ]; do
if [[ "$i" == "influxdb" ]]; then
IS_EXISTS_INFLUXDB_CONTAINER="true"
break
fi
done
if [[ $IS_EXISTS_INFLUXDB_CONTAINER == "false" ]]; then
cp docker_influxdb_create_user.sh /etc/influxdb/scripts/docker_influxdb_create_user.sh
chmod a+x /etc/influxdb/scripts/docker_influxdb_create_user.sh
if [[ -f "/etc/influxdb/scripts/docker_influxdb_create_user.sh" ]]; then
echo "检查到influxdb容器尚未创建!"
# 执行容器创建
# 运行容器实例 --privileged=true 获取管理员权限
echo "创建influxdb容器实例..."
sudo docker run -d -p 38083:8083 -p 38086:8086 --name influxdb --restart always --privileged=true -v /etc/influxdb/scripts:/etc/influxdb/scripts influxdb:latest
# 休10秒钟
echo "休眠等待10s以便Docker完成容器运行......"
sleep 10s
echo "进入influxdb容器: docker exec -it influxdb /bin/bash -c 'sh /etc/influxdb/scripts/docker_influxdb_create_user.sh'"
# 进入容器并执行脚本:
docker exec -it influxdb /bin/bash -c "sh /etc/influxdb/scripts/docker_influxdb_create_user.sh"
# 删除执行文件
rm -f /etc/influxdb/scripts/docker_influxdb_create_user.sh
echo "influxdb容器已创建完毕!"
IS_EXISTS_INFLUXDB_CONTAINER_RUNGING=true
else
echo "/etc/influxdb/scripts/docker_influxdb_create_user.sh文件不存在,docker需要用此文件创建influxdb容器实例并创建用户."
exit 1
fi
else
echo "检查到influxdb容器已创建!"
fi
fi
# ===================启动或重启容器================================
if [[ $IS_EXISTS_INFLUXDB_CONTAINER == "true" && $IS_EXISTS_INFLUXDB_CONTAINER_RUNGING == "false" ]]; then
echo "下面最多执行三次influxdb容器检查重启..."
while [[ $START_CONTAINER_CHECK_CURRENT -le $START_CONTAINER_CHECK_MAX_TIMES ]]; do
echo "检查influxdb容器状态......$START_CONTAINER_CHECK_CURRENT"
for i in [ `docker ps ` ]; do
if [[ "$i" == "influxdb" ]]; then
IS_EXISTS_INFLUXDB_CONTAINER_RUNGING="true"
break
fi
done
if [[ $IS_EXISTS_INFLUXDB_CONTAINER_RUNGING == "false" ]]; then
echo "检查到influxdb容器当前不在运行状态!"
echo "启动influxdb容器...."
docker start influxdb
for i in [ `docker ps ` ]; do
if [[ "$i" == "influxdb" ]]; then
IS_EXISTS_INFLUXDB_CONTAINER_RUNGING="true"
break
fi
done
if [[ $IS_EXISTS_INFLUXDB_CONTAINER_RUNGING == "true" ]]; then
echo "influxdb容器已经在运行!"
break
fi
else
echo "influxdb容器已经在运行!"
break
fi
START_CONTAINER_CHECK_CURRENT=$((START_CONTAINER_CHECK_CURRENT+1))
done
if [[ $IS_EXISTS_INFLUXDB_CONTAINER_RUNGING == "false" ]]; then
echo "检查到influxdb容器当前仍未运行,请联系相关人员进行处理!"
exit 1
fi
fi
2、数据库操作脚本:docker_influxdb_create_user.sh
#!/bin/bash
#file:docker_influxdb_create_user.sh
#company:cvnavi.com
#author:Pengjunlin
echo "当前执行文件......$0"
INFLUXDB_DATABASE_NAME="rtvsweb"
INFLUXDB_USER_NAME="admin"
INFLUXDB_USER_PWD="admin"
# influxdb数据库相关配置
echo "influxdb数据库相关配置"
influx -version
# 查询数据库列表
echo "查询数据库列表"
influx -execute "show databases"
# 删除数据库
echo "删除数据库$INFLUXDB_DATABASE_NAME"
influx -execute "drop database $INFLUXDB_DATABASE_NAME"
# 创建数据库
echo "创建数据库$INFLUXDB_DATABASE_NAME"
influx -execute "create database $INFLUXDB_DATABASE_NAME"
# 创建用户并授权
echo "创建$INFLUXDB_USER_NAME用户并授权"
influx -execute "create user "$INFLUXDB_USER_NAME" with password '$INFLUXDB_USER_PWD' with all privileges"
# 查询用户列表
echo "查询用户列表"
influx -execute "use $INFLUXDB_DATABASE_NAME" -execute "show users"
五、其他
1、数据备份
influxd backup -database
2、数据还原
influxd backup -database