如何在zabbix容器中引用另一個mysql容器

1.創建鏡像

1.1 創建zabbix,mysql鏡像

docker hub中找到下載和使用zabbix(zabbix-java-gateway;zabbix-server-mysql;zabbix-web-nginx-mysql)鏡像以及mysql鏡像的說明:

(1)在docker中使用 docker pull命令下載docker鏡像

 

docker pull zabbix/zabbix-server-mysql
docker pull zabbix/zabbix-agent
docker pull zabbix/zabbix-java-gateway
docker pull zabbix/zabbix-web-nginx-mysql
docker pull mysql:5.7
docker pull mysql:5.6

 

也可將鏡像導出並保存,後期離線使用(通過docker save, load命令)

目前最新的穩定版本爲  centos-3.0-latest

docker save 9b5fe32dcc8e > zabbix-server-mysql #9b5fe32dcc8e爲鏡像id  ,一下save指令同
docker save 9b5fe32dcc8e > zabbix-server-mysql.tar
docker save f11 > zabbix-java-gateway.tar
docker save 2826 > zabbix-agent.tar
docker save 1c79 > mysql_5_6.tar
docker save 7d83 > mysql_5_7.tar

 

 

2 創建容器

 

2.1創建mysql容器

使用docker run 命令創建mysql容器: (docker run [OPTIONS] IMAGE [COMMAND] [ARG...])

docker run --name mysql_server -p 3310:3306 -e MYSQL_ROOT_PASSWORD=xxx -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
 

創建好之後可以登錄試試看:mysql -P 3310 -h localhost -u root -p

 

有幾個需要注意的地方:

(1)-p 3310:3306:3310爲數據庫外部暴露端口,3306爲3310對應容器內部的端口。這個內部端口3306也可以起訂其他的,但是會被容器自動轉換一下

 

(2)-e MYSQL_ROOT_PASSWORD=xxx

該參數指定數據庫 root 用戶的密碼。類似的初始化參數使用方法一樣,具體可參考 附錄1 或者docker hub mysql

(3)--character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

指定所有表的默認編碼格式爲UTF-8

類似的配置參數可通過 docker run -it --rm mysql:tag --verbose --help 命令來獲取。

(4)如果想要是用自己的mysql配置文件,請將配置文件以.cnf爲後綴,並將文件所在文件夾映射至/etc/mysql/conf.d文件夾。

例如加入參數

-v /my/custom:/etc/mysql/conf.d

注意:

1./my/custom目錄(映射容器/etc/mysql/conf.d目錄)下的配置文件xxx.cnf會覆蓋容器中的配置文件(/etc/mysql/my.cnf)的配置

2.如果映射目錄(/my/custom)下包含多個.cnf文件,則按照官方都會起作用

 

2.2 創建zabbix容器

docker run --log-opt max-size=50mb --log-opt max-file=2 --name zabbixServer -p 10000:10051 -e DB_SERVER_HOST="172.17.0.1" -e MYSQL_USER="root" -e MYSQL_PASSWORD="xxx" -e DB_SERVER_PORT="3306" -d zabbix/zabbix-server-mysql:latest

這裏有兩種常場景,配置稍有不同:

(1)zabbix-server和mysql數據庫不在同一臺機器上,DB_SERVER_HOST 和 DB_SERVER_PORT 參數按普通客戶端能夠正常訪問的IP和端口進行填寫就行

(2)zabbix-server和mysql數據庫在同一臺機器上,zabbix-server 要能聯通 mysql 容器。這時,填寫外網的ip 與端口號就不行了。

a:這時在建立zabbix-server容器時填寫  --link mysql_server:mysql 參數(mysql_serer 爲要連接的容器名,容器id 也可以。mysql 爲這個容器在 zabbix-server容器中的別名。),然後 在zabbix-server容器中可使用 mysql_server或者mysql 作爲mysql_server容器的host。zabbix-server容器創建好之後,進入容器,cat /etc/hosts 就可查看到 IP映射關係。

 b:也可先 使用 docker network create net1 創建net1網絡,然後在建立mysql 容器時使用 --network net1 --network-alias mysql1 參數,該參數表示這個容器 使用net1 網絡,並且在網絡中自己的別名爲 mysql1作爲host.因此在創建zabbix-server時可直接使用mysql1作爲mysql容器的host,而不用 link 參數。

 

然後 docker run zabbix-server 時加入--network net1 參數使用net1 網絡。再重複a

 

docker run --name zabbixServerWeb -p 11000:80 -e DB_SERVER_HOST="172.17.0.1" -e MYSQL_USER="root" -e MYSQL_PASSWORD="xxx" -e ZBX_SERVER_HOST="172.17.0.1" -e ZBX_SERVER_PORT="10000" -e PHP_TZ="Asia/Shanghai" -e DB_SERVER_PORT="3310" -d zabbix/zabbix-web-nginx-mysql:latest

 

注意:

 

zabbix 默認用戶名、密碼: Admin      zabbix

 

DB_SERVER_HOST、ZBX_SERVER_HOST 不能設置爲127.0.0.1,因爲這樣容器會認爲是自己,而不是主機,要使用ifconfig來查看並設置主機真正的ip地址。

 

 

 

 

 

 

 

 

=============================================================>

zabbix  官方docker鏡像地址:

server-mysql:https://hub.docker.com/r/zabbix/zabbix-server-mysql/

各參數及說明:

 

Environment Variables

When you start the zabbix-server-mysql image, you can adjust the configuration of the Zabbix server by passing one or more environment variables on the docker run command line.

DB_SERVER_HOST

This variable is IP or DNS name of MySQL server. By default, value is 'mysql-server'

DB_SERVER_PORT

This variable is port of MySQL server. By default, value is '3306'.

MYSQL_USERMYSQL_PASSWORD

These variables are used by Zabbix server to connect to Zabbix database. By default, values are zabbixzabbix.

MYSQL_DATABASE

The variable is Zabbix database name. By default, value is zabbix.

ZBX_LOADMODULE

The variable is list of comma separated loadable Zabbix modules. It works with volume /var/lib/zabbix/modules. The syntax of the variable is dummy1.so,dummy2.so.

ZBX_DEBUGLEVEL

The variable is used to specify debug level. By default, value is 3. It is DebugLevel parameter in zabbix_server.conf. Allowed values are listed below:

  • 0 - basic information about starting and stopping of Zabbix processes;
  • 1 - critical information
  • 2 - error information
  • 3 - warnings
  • 4 - for debugging (produces lots of information)
  • 5 - extended debugging (produces even more information)

ZBX_TIMEOUT

The variable is used to specify timeout for processing checks. By default, value is 4.

ZBX_JAVAGATEWAY_ENABLE

The variable enable communication with Zabbix Java Gateway to collect Java related checks. By default, value is false.

 

 

 

web-mysql:https://hub.docker.com/r/zabbix/zabbix-web-nginx-mysql/

各參數及說明:

 

Environment Variables

When you start the zabbix-web-nginx-mysql image, you can adjust the configuration of the Zabbix web interface by passing one or more environment variables on the docker run command line.

ZBX_SERVER_HOST

This variable is IP or DNS name of Zabbix server. By default, value is zabbix-server.

ZBX_SERVER_PORT

This variable is port Zabbix server listening on. By default, value is 10051.

DB_SERVER_HOST

This variable is IP or DNS name of MySQL server. By default, value is 'mysql-server'

DB_SERVER_PORT

This variable is port of MySQL server. By default, value is '3306'.

MYSQL_USERMYSQL_PASSWORD

These variables are used by Zabbix web interface to connect to Zabbix database. By default, values are zabbixzabbix.

MYSQL_DATABASE

The variable is Zabbix database name. By default, value is zabbix.

PHP_TZ

The variable is timezone in PHP format. Full list of supported timezones are available on php.net. By default, value is 'Europe/Riga'.

ZBX_SERVER_NAME

The variable is visible Zabbix installation name in right top corner of the web interface.

ZBX_MAXEXECUTIONTIME

The varable is PHP max_execution_time option. By default, value is 300.

ZBX_MEMORYLIMIT

The varable is PHP memory_limit option. By default, value is 128M.

ZBX_POSTMAXSIZE

The varable is PHP post_max_size option. By default, value is 16M.

ZBX_UPLOADMAXFILESIZE

The varable is PHP upload_max_filesize option. By default, value is 2M.

ZBX_MAXINPUTTIME

The varable is PHP max_input_time option. By default, value is 300.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章