Thingsboard docker 部署方式连容器内postgres数据库

关键步骤:

  • thingsboard/tb-postgres实例容器暴露5432端口
  • 开放postgres 远程访问权限

1.暴露5433端口

(1)docker-compose.yml方式部署

docker-compose.yml方式部署 参考官方: 基于Docker安装ThingsBoard | ThingsBoard中文网 (ithingsboard.com)
我们需要访问postgres数据库因此需要暴露54532端口:
在docker-compose.yml配置文件中添加 - "5432:5432"

version: '2.2'
services:
  mytb:
    restart: always
    image: "thingsboard/tb-postgres"
    ports:
      - "8080:9090"
      - "5432:5432"
      - "1883:1883"
      - "5683:5683/udp"
    environment:
      TB_QUEUE_TYPE: in-memory
    volumes:
      - ~/.mytb-data:/data
      - ~/.mytb-logs:/var/log/thingsboard

其他步骤按官方文档即可完成安装。

(2)thingsboard/tb-postgres 镜像方式部署 参考官方: 基于Docker(Windows)安装ThingsBoard | ThingsBoard中文网 (ithingsboard.com)

1.创建Docker卷

docker volume create mytb-data
docker volume create mytb-logs

2.执行以下命令以直接运行docker(添加-p 5432:5432):

docker run -it -p 5432:5432 -p 9090:9090 -p 1883:1883 -p 5683:5683/udp -v mytb-data:/data -v ~/mytb-logs:/var/log/thingsboard --name mytb --restart always thingsboard/tb-postgres

(3)已经运行中的实例暴露5432端口

  • 停止容器
docker stop b1bf74622f24
  • var/lib/docker/containers/容器ID/hostconfig.json 配置中找到PortBindings节点 添加5432端口配置信息:
"5432/tcp":[{"HostIp":"","HostPort":"5432"}]
[root@aliyun db]# docker ps
CONTAINER ID   IMAGE                     COMMAND         CREATED        STATUS         PORTS                                                                                                                                                                                                                                       NAMES
b1bf74622f24   thingsboard/tb-postgres   "start-tb.sh"   17 hours ago   Up 7 minutes   0.0.0.0:1883->1883/tcp, :::1883->1883/tcp, 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp, 0.0.0.0:7070->7070/tcp, :::7070->7070/tcp, 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp, 0.0.0.0:5683-5688->5683-5688/udp, :::5683-5688->5683-5688/udp   mytb
[root@aliyun db]# vim /var/lib/docker/containers/b1bf74622f24b3b5b6044c12a3985e6fd7fe9c1f740ceebcb00148e2c6eca54d/hostconfig.json 
{"Binds":["/root/.mytb-data:/data","/root/.mytb-logs:/var/log/thingsboard"],"ContainerIDFile":"","LogConfig":{"Type":"json-file","Config":{}},"NetworkMode":"default","PortBindings":{"1883/tcp":[{"HostIp":"","HostPort":"1883"}],"5432/tcp":[{"HostIp":"","HostPort":"5432"}],"5683/udp":[{"HostIp":"","HostPort":"5683"}],"5684/udp":[{"HostIp":"","HostPort":"5684"}],"5685/udp":[{"HostIp":"","HostPort":"5685"}],"5686/udp":[{"HostIp":"","HostPort":"5686"}],"5687/udp":[{"HostIp":"","HostPort":"5687"}],"5688/udp":[{"HostIp":"","HostPort":"5688"}],"7070/tcp":[{"HostIp":"","HostPort":"7070"}],"9090/tcp":[{"HostIp":"","HostPort":"9090"}]},"RestartPolicy":{"Name":"always","MaximumRetryCount":0},"AutoRemove":false,"VolumeDriver":"","VolumesFrom":null,"CapAdd":null,"CapDrop":null,"CgroupnsMode":"host","Dns":[],"DnsOptions":[],"DnsSearch":[],"ExtraHosts":null,"GroupAdd":null,"IpcMode":"private","Cgroup":"","Links":null,"OomScoreAdj":0,"PidMode":"","Privileged":false,"PublishAllPorts":false,"ReadonlyRootfs":false,"SecurityOpt":null,"UTSMode":"","UsernsMode":"","ShmSize":67108864,"Runtime":"runc","ConsoleSize":[0,0],"Isolation":"","CpuShares":0,"Memory":0,"NanoCpus":0,"CgroupParent":"","BlkioWeight":0,"BlkioWeightDevice":[],"BlkioDeviceReadBps":null,"BlkioDeviceWriteBps":null,"BlkioDeviceReadIOps":null,"BlkioDeviceWriteIOps":null,"CpuPeriod":0,"CpuQuota":0,"CpuRealtimePeriod":0,"CpuRealtimeRuntime":0,"CpusetCpus":"","CpusetMems":"","Devices":[],"DeviceCgroupRules":null,"DeviceRequests":null,"KernelMemory":0,"KernelMemoryTCP":0,"MemoryReservation":0,"MemorySwap":0,"MemorySwappiness":null,"OomKillDisable":false,"PidsLimit":null,"Ulimits":null,"CpuCount":0,"CpuPercent":0,"IOMaximumIOps":0,"IOMaximumBandwidth":0,"MaskedPaths":["/proc/asound","/proc/acpi","/proc/kcore","/proc/keys","/proc/latency_stats","/proc/timer_list","/proc/timer_stats","/proc/sched_debug","/proc/scsi","/sys/firmware"],"ReadonlyPaths":["/proc/bus","/proc/fs","/proc/irq","/proc/sys","/proc/sysrq-trigger"]}
  • var/lib/docker/containers/容器ID/config.v2.json 配置中找到ExposedPorts节点 添加5432端口配置信息:
"5432/tcp":{}
[root@aliyun db]# vim /var/lib/docker/containers/b1bf74622f24b3b5b6044c12a3985e6fd7fe9c1f740ceebcb00148e2c6eca54d/config.v2.json 

最后重启容器

2.开放postgres 远程访问权限

postgres默认只开放本地连接,因此需要修改配置文件
thingsboard/tb-postgres 镜像,postgres配置文件在容器内的/data/db/ 下pg_hba.conf和postgresql.conf
由于我们做了文件映射:docker-compose.yml中的- ~/.mytb-data:/data或docker命令下的-v mytb-data:/data
因此,我们只需要修改宿主机的mytb-data目录下的对应配置

[root@aliyun ~]# cd /root/.mytb-data/db/
[root@aliyun db]# ls
base          pg_dynshmem    pg_logical    pg_replslot   pg_stat      pg_tblspc    pg_wal                postgresql.conf
global        pg_hba.conf    pg_multixact  pg_serial     pg_stat_tmp  pg_twophase  pg_xact               postmaster.opts
pg_commit_ts  pg_ident.conf  pg_notify     pg_snapshots  pg_subtrans  PG_VERSION   postgresql.auto.conf  postmaster.pid
[root@aliyun db]# 

(1)配置用户访问权限

[root@aliyun db]# vim pg_hba.conf 

在IPv4下新增一条

host    all             all             0.0.0.0/0               trust

(2)监听所有IP请求

[root@aliyun db]# vim postgresql.conf 

在配置文件中新增一条

listen_addresses = '*' 

3.修改完成,重启thingsboard/tb-postgres容器

如果使用云服务器,还需要注意开放5432端口

[root@aliyun db]# docker ps
CONTAINER ID   IMAGE                     COMMAND         CREATED        STATUS        PORTS                                                                                                                                                                                                                                       NAMES
b1bf74622f24   thingsboard/tb-postgres   "start-tb.sh"   17 hours ago   Up 17 hours   0.0.0.0:1883->1883/tcp, :::1883->1883/tcp, 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp, 0.0.0.0:7070->7070/tcp, :::7070->7070/tcp, 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp, 0.0.0.0:5683-5688->5683-5688/udp, :::5683-5688->5683-5688/udp   mytb
[root@aliyun db]# docker start b1b
b1b
[root@aliyun db]#

你可能感兴趣的:(Thingsboard docker 部署方式连容器内postgres数据库)