ubuntu docker inflxudb(安裝 使用 備份 還原 以及python編碼) telegraf Grafana


inflxudb1.7.7

1.拉取最新的鏡像 docker pull influxdb 

2.運行實例:

docker run -d --restart=always -p 8086:8086 -p 8083:8083  -p 2003:2003  -v /root/docker/influxdb/data/:/var/lib/influxdb/data  -v /root/docker/influxdb/dump/:/var/lib/influxdb/dump/ -e INFLUXDB_GRAPHITE_ENABLED=true  -e INFLUXDB_ADMIN_ENABLED=true  -e INFLUXDB_DB=db0 -e INFLUXDB_ADMIN_USER=admin -e INFLUXDB_ADMIN_PASSWORD=admin  -e INFLUXDB_USER=telegraf -e INFLUXDB_USER_PASSWORD=telegraf --name influxdb influxdb

8086 HTTP API port
8083 Administrator interface port, if it is enabled
2003 Graphite support, if it is enabled
The above would create the database db0, create an admin user with the password admin, then create the telegraf user with your telegraf's secret password. It would then exit and leave behind any files it created in the volume that you mounted.
參考:https://hub.docker.com/_/influxdb

需要保證/root/docker/influxdb/data/是空的


3.建數據庫 

方法1 進入容器系統
docker exec -it influxdb bash
curl -G http://localhost:8086/query --data-urlencode "q=CREATE DATABASE mydb1"
或者(192.168.100.3 是物理機的ip)
curl -G http://192.168.100.3:8086/query --data-urlencode "q=CREATE DATABASE mydb2"
方法2
docker exec -it influxdb influx
create database "mydb3"
如圖(注意這里有我們初始化的db0數據庫):

基本用法如下:

create database "db_name" #創建數據庫
show databases #顯示所有的數據庫
drop database "db_name" #刪除數據庫
use db_name #使用數據庫
show measurements #顯示該數據庫中所有的表
drop measurement "measurement_name" #刪除表
#insertyour_measurement,tag_name=tag_value... column_name=column_value
insert test,host=127.0.0.1,monitor_name=test count=1 #創建表test,直接在插入數據的時候指定表名test
insert payment,device=mobile,product=Notepad,method=credit billed=33,licenses=3i #創建表payment i是integer的意思,默認數字是float
#例子
curl -i -XPOST 'http://192.168.100.3:8086/write?db=mydb3' --data-binary 'payment,device=mobile,product=Notepad,method=credit billed=33,licenses=3i'
curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=mydb3" --data-urlencode "q=select * from payment order by time desc"

python操作influxdb:

需要安裝pip3 install influxdb

 

python 代碼如下:

# -*- coding: utf-8 -*-
import time ,random
from influxdb import InfluxDBClient
#這個函數每次調用返回當前cpu的運行狀況
#因為沒有安裝psutil包,所以使用一個隨機函數代替cpu信息的獲取
def read_info():
    data_list = [{
        'measurement': 'win'
        ,'tags': {'cpu': 'i7-7700HQ'}
        ,'fields': {
            'cpu_info_user': random.random()
            ,'cpu_info_system': random.random()
            ,'cpu_info_idle': random.random()
            ,'cpu_info_interrupt': random.random()
            ,'cpu_info_dpc': random.random()
        }
    }]
    return data_list
    
if __name__ == '__main__':
    db_name = 'db0'
    #創建一個influxdb的客戶端
    client = InfluxDBClient('192.168.100.3', 8086, 'admin', 'admin', db_name) 
    #創建數據庫
    client.create_database(db_name)
    # 初始化 
    counts = 0 
    #計數,也就是數據上傳20次
    while counts <= 20:
        counts += 1 
        #write_points(points, time_precision=None, database=None, retention_policy=None, tags=None, batch_size=None, protocol=u'json')
        #write(data, params=None, expected_response_code=204, protocol=u'json')
        client.write_points(read_info()) 
        time.sleep(5)
    #查詢結果   
    #query(query, params=None, epoch=None, expected_response_code=200, database=None, raise_errors=True, chunked=False, chunk_size=0)
    result = client.query('select * from win')
    print("Result: {0}".format(result))

python參考https://influxdb-python.readthedocs.io/en/latest/examples.html 和https://zhuanlan.zhihu.com/p/37523795

備份和還原

我們先備份db0然后還原db1

docker exec -it influxdb influxd backup -portable -database db0 /tmp/db0 #備份db0到容器
docker cp influxdb:/tmp/db0 /root/docker/influxdb/dump #拷貝到主機
docker exec -it influxdb influxd restore -portable -db db0 -newdb db1 /tmp/db0 #還原到新的數據庫db1, 這里省略了拷貝到容器的步驟
或者
docker exec -it influxdb influxd backup -portable -database db0 /var/lib/influxdb/dump/db0 #備份
docker exec -it influxdb influxd restore -portable -db db0 -newdb db1 /var/lib/influxdb/dump/db0 #還原
如果指定了dump文件 備份結果如下: 備份還原請參考https://docs.influxdata.com/influxdb/v1.7/administration/backup_and_restore/

 

 

 

注意進入容器的指令是docker exec -it influxdb influx 而不是docker exec -it influxdb influxd 我在還原數據庫 就多輸入了一個d字符 老提示listen tcp 127.0.0.1:8088: bind: address already in use 還找了很多解決方法https://stackoverflow.com/questions/40844762/influxdb-port-8088

 

telegraf

這里的數據采集可以采用telegraf來完成, ubuntu下的安裝如下:

cat <<EOF | sudo tee /etc/apt/sources.list.d/influxdata.list
deb https://repos.influxdata.com/ubuntu bionic stable
EOF
sudo curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add -
sudo apt-get update
sudo apt-get -y install telegraf

參考https://computingforgeeks.com/how-to-install-and-configure-telegraf-on-ubuntu-18-04-debian-9/

修改配置:

添加服務並啟動

sudo systemctl enable --now telegraf

 systemctl status telegraf

grafana

1.拉取鏡像 docker pull grafana/grafana

2.運行實例:docker run -d --restart=always -p 3000:3000 --name grafana grafana/grafana  如果需要開啟防火牆,請執行:

firewall-cmd --zone=public --add-port=8086/tcp --permanent
firewall-cmd --zone=public --add-port=3000/tcp --permanent
systemctl restart firewalld
3.訪問grafana :http://192.168.100.3:3000 輸入用戶名和密碼 admin/admin

 

第一次進入需要修改密碼:

 

4.配置數據源(add data source)

 

 

 

5. 回到主頁添加 dashboard

 

 

 

 

 

上面的terlegraf是安裝到linux的物理機,現在我們來試一下它的docker安裝

docker pull influxdb #拉取鏡像
docker run -d --restart=always --name=telegraf -v /root/docker/telegraf/telegraf.conf:/etc/telegraf/telegraf.conf:ro telegraf #啟動實例
docker logs -f telegraf #查看日志

telegraf.conf配置

---2019-8-13補充

注意上面python操作influxdb的文件而命名不能包含influxxxx.py; 我第一次命名比較隨意,但是這次新搭建環境命名叫influxdb.py,結果程序在運行時提示 ImportError: cannot import name 'InfluxDBClient', 參考influxdb-python結果還是失敗

也修改了python路徑, sudo gedit /etc/profile 指令來修改環境變量,加入 export PATH=<你要加入的路徑>:$PATH

最后修改文件名稱就好了,。。。。。

參考地址:

https://hub.docker.com/_/influxdb

https://www.cnblogs.com/LUA123/p/9507029.html

https://towardsdatascience.com/get-system-metrics-for-5-min-with-docker-telegraf-influxdb-and-grafana-97cfd957f0ac


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM