Docker下安裝Influxdb-1.6.1和Grafana5.2.2


 

第一步、安裝Influxdb

首先啟動docker

systemctl start docker

 

然后安裝Influxdb(這里解釋一下為啥用docker,因為官網下載的話需要翻牆【fan-qiang】,真悲催)注意切換root用戶

[root@localhost admin]# docker pull influxdb
Using default tag: latest
Trying to pull repository docker.io/library/influxdb ... 
latest: Pulling from docker.io/library/influxdb
55cbf04beb70: Pull complete 
1607093a898c: Pull complete 
9a8ea045c926: Pull complete 
4c8b66fe6495: Pull complete 
9f3c67b9b082: Pull complete 
864cc6881ca8: Pull complete 
c1165c5c85e6: Pull complete 
0b5bd48b7b2b: Pull complete 
Digest: sha256:c9098612611038b6d0daddf1ed89d0144f41124b0feed765c0d31844e7f32e9f
Status: Downloaded newer image for docker.io/influxdb:latest
[root@localhost admin]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/mongo         latest              8bf72137439e        9 days ago          380 MB
docker.io/influxdb      latest              34de2bdc2d7f        13 days ago         213 MB
docker.io/centos        latest              5182e96772bf        13 days ago         200 MB
docker.io/hello-world   latest              2cb0d9787c4d        5 weeks ago         1.85 kB

 

啟動Influxdb

[root@localhost admin]# docker run -d -p 8083:8083 -p 8086:8086 --name my_influxdb influxdb
aec85244ff227e3175afcba59dc7293001428e4b21300c09c5567becac270635
[root@localhost admin]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                            NAMES
aec85244ff22        influxdb            "/entrypoint.sh in..."   8 seconds ago       Up 5 seconds        0.0.0.0:8083->8083/tcp, 0.0.0.0:8086->8086/tcp   my_influxdb

 

其實這一步可以省略8083端口,因為新版本根本就移除了web控制台部分,網上大多數教程還是0.8 或者 1.1版本的,他們那個有web控制台(其實14、15年的文章用低版本無可厚非,現在有的人2018年7月份的教程都用的0.8版本,我想問有意思嗎?) 

進入docker鏡像:

[root@localhost admin]# docker exec -it my_influxdb bash

 

進入/usr/bin目錄,這里面有Influxdb的工具

root@aec85244ff22:/usr/bin# find | grep influx  
./influx
./influx_inspect
./influx_stress
./influx_tsm
./influxd

 

查看Influxdb版本

./influx -version

 

進入Influxdb客戶端命令行

root@aec85244ff22:/usr/bin# ./influx
Connected to http://localhost:8086 version 1.6.1
InfluxDB shell version: 1.6.1
> show databases
name: databases
name
----
_internal
> exit

 創建數據庫

> create database my_test
> show databases
name: databases
name
----
_internal
my_test

 刪除數據庫

drop database [db_name]

 

使用數據庫

> use my_test
Using database my_test

 

現在寫個定時程序,不斷向數據庫添加數據

建立一個SpringBoot工程

導入依賴

入口類

package com.example.demo;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Random;

@SpringBootApplication
@EnableScheduling
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }


    @Scheduled(fixedRate = 1000)
    public void doInsert(){
        Random random = new Random();
        InfluxDBDemo.insert(random.nextInt(1000));
    }

}

 

Influxdb類

package com.example.demo;

import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.Point;


public class InfluxDBDemo {

    public static void insert(int num){
        InfluxDB db = InfluxDBFactory.connect("http://192.168.192.128:8086", "admin", "admin");
        db.setDatabase("my_test");  // 設置數據庫
        Point.Builder builder = Point.measurement("test_measurement");  // 創建Builder,設置表名
        builder.addField("count",num);  // 添加Field
        builder.tag("TAG_CODE","TAG_VALUE_" + num);    // 添加Tag
        Point point = builder.build();
        db.write(point);
    }
}

 

當你啟動,每隔1秒自動向數據庫添加

這個時候,顯示表列表

> show measurements
name: measurements
name
----
test_measurement

 

查看表

> select * from test_measurement
name: test_measurement
time                TAG_CODE      count
----                --------      -----
1534754858345040970 TAG_VALUE_655 655
1534754859072609138 TAG_VALUE_42  42
1534754860076519190 TAG_VALUE_881 881
1534754861077128121 TAG_VALUE_461 461
1534754862079956555 TAG_VALUE_374 374
1534754863079020432 TAG_VALUE_574 574
1534754864073986943 TAG_VALUE_647 647
1534754865077651294 TAG_VALUE_78  78
1534754866079569554 TAG_VALUE_688 688

 

刪除表

drop measurement 【measurement_name】

第二步、安裝Grafana

相比之下,grafana就比較友好了,因為官網上的都能下載,無論Windows還是linux。

[root@localhost admin]# docker pull grafana/grafana
Using default tag: latest
Trying to pull repository docker.io/grafana/grafana ... 
latest: Pulling from docker.io/grafana/grafana
be8881be8156: Pull complete 
728ffd1b8130: Pull complete 
426111690cea: Pull complete 
Digest: sha256:b5591419cfa3a930cecdddff0a338c03296d29b617d9f340dc72ee839dd1c5be
Status: Downloaded newer image for docker.io/grafana/grafana:latest

 

運行

[root@localhost admin]# docker run -d -p 3000:3000 --name my_grafana grafana/grafana
6c9d5d2d8422e666ca44403c5c47be3fa43308b4d2a9587ab16ad97fcffede24

 

打開防火牆端口,以便你本機能訪問虛擬機資源

[root@localhost admin]# firewall-cmd --zone=public --add-port=8086/tcp --permanent
success
[root@localhost admin]# firewall-cmd --zone=public --add-port=3000/tcp --permanent
success
[root@localhost admin]# systemctl restart firewalld

 

訪問  http://192.168.192.128:3000

賬號密碼:admin/admin,進去之后讓你修改密碼

 

進去之后

現在配置數據源

 

最后點擊

然后點擊,選擇Home

..添加dashboard

..

 ..點擊Panel Title

 

..

..

..在右上角可以設置展示效果

設置自動刷新,選擇時間范圍,然后選擇刷新間隔,注意點擊Apply。這樣每隔5s,自動刷新

..效果

 

..最后回到Home

..可以看到我們剛才創建的監控圖

 


免責聲明!

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



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