使用docker运行zabbix-server
共 10354字,需浏览 21分钟
·
2020-12-12 03:54
容器间的互联
# 创建一个nginx容器
docker run -d -p 80:80 nginx
# 创建容器,做link,并进入容器中
docker run -it --link quirky_brown:web01 centos-ssh /bin/bash
# 在容器中访问nginx容器可以ping通
ping web01
命令执行过程
# 启动apache容器
[root@docker01 ~]# docker run -d httpd:2.4
3f1f7fc554720424327286bd2b04aeab1b084a3fb011a785b0deab6a34e56955
^[[A[root@docker01 docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f1f7fc55472 httpd:2.4 "httpd-foreground" 6 seconds ago Up 5 seconds 80/tcp determined_clarke
# 拉取一个busybox 镜像
[root@docker01 ~]# docker pull busybox
# 启动容器
[root@docker01 ~]# docker run -it --link determined_clarke:web busybox:latest /bin/sh
/ #
# 使用新的容器访问最初的web容器
/ # ping web
PING web (172.17.0.2): 56 data bytes
64 bytes from 172.17.0.2: seq=0 ttl=64 time=0.058 ms
^C
--- web ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.058/0.058/0.058 ms
启动zabbix容器
docker run --name mysql-server -t \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="zabbix_pwd" \
-e MYSQL_ROOT_PASSWORD="root_pwd" \
-d mysql:5.7 \
--character-set-server=utf8 --collation-server=utf8_bin
docker run --name zabbix-java-gateway -t \
-d zabbix/zabbix-java-gateway:latest
docker run --name zabbix-server-mysql -t \
-e DB_SERVER_HOST="mysql-server" \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="zabbix_pwd" \
-e MYSQL_ROOT_PASSWORD="root_pwd" \
-e ZBX_JAVAGATEWAY="zabbix-java-gateway" \
--link mysql-server:mysql \
--link zabbix-java-gateway:zabbix-java-gateway \
-p 10051:10051 \
-d zabbix/zabbix-server-mysql:latest
docker run --name zabbix-web-nginx-mysql -t \
-e DB_SERVER_HOST="mysql-server" \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="zabbix_pwd" \
-e MYSQL_ROOT_PASSWORD="root_pwd" \
--link mysql-server:mysql \
--link zabbix-server-mysql:zabbix-server \
-p 80:80 \
-d zabbix/zabbix-web-nginx-mysql:latest
关于zabbix API
关于zabbix API可以参考官方文档:
https://www.zabbix.com/documentation/3.4/zh/manual/api
# 获取token
[root@docker02 ~]# curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "Admin",
"password": "zabbix"
},
"id": 1
}' http://10.0.0.100/api_jsonrpc.php
{"jsonrpc":"2.0","result":"d3be707f9e866ec5d0d1c242292cbebd","id":1}
docker 仓库(registry)
创建一个普通仓库
docker run -d -p 5000:5000 --restart=always --name registry -v /opt/myregistry:/var/lib/registry registry
[root@docker01 ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://registry.docker-cn.com"],
"insecure-registries": ["10.0.0.100:5000"]
}
重启docker让修改生效
[root@docker01 ~]# systemctl restart docker.service
[root@docker01 ~]# docker tag busybox:latest 10.0.0.100:5000/clsn/busybox:1.0
[root@docker01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos6-ssh latest 3c2b1e57a0f5 18 hours ago 393MB
httpd 2.4 2e202f453940 6 days ago 179MB
10.0.0.100:5000/clsn/busybox 1.0 5b0d59026729 8 days ago 1.15MB
[root@docker01 ~]# docker push 10.0.0.100:5000/clsn/busybox
带basic认证的仓库
[root@docker01 clsn]# yum install httpd-tools -y
mkdir /opt/registry-var/auth/ -p
htpasswd -Bbn clsn 123456 > /opt/registry-var/auth/htpasswd
docker run -d -p 5000:5000 -v /opt/registry-var/auth/:/auth/ -e "REGISTRY_AUTH=htpasswd" -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd registry
# 登陆用户
[root@docker01 ~]# docker login 10.0.0.100:5000
Username: clsn
Password: 123456
Login Succeeded
# 推送镜像到仓库
[root@docker01 ~]# docker push 10.0.0.100:5000/clsn/busybox
The push refers to repository [10.0.0.100:5000/clsn/busybox]
4febd3792a1f: Pushed
1.0: digest: sha256:4cee1979ba0bf7db9fc5d28fb7b798ca69ae95a47c5fecf46327720df4ff352d size: 527
#认证文件的保存位置
[root@docker01 ~]# cat .docker/config.json
{
"auths": {
"10.0.0.100:5000": {
"auth": "Y2xzbjoxMjM0NTY="
},
"https://index.docker.io/v1/": {
"auth": "Y2xzbjpIenNAMTk5Ng=="
}
},
"HttpHeaders": {
"User-Agent": "Docker-Client/17.12.0-ce (linux)"
}
}
至此,一个简单的docker镜像仓库搭建完成
docker-compose编排工具
安装docker-compose
# 下载pip软件
yum install -y python2-pip
# 下载 docker-compose
pip install docker-compose
国内开启pip 下载加速:
mkdir ~/.pip/
cat > ~/.pip/pip.conf <<'EOF'
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
EOF
编排启动镜像
[root@docker01 ~]# mkdir /opt/my_wordpress/
[root@docker01 ~]# cd /opt/my_wordpress/
[root@docker01 my_wordpress]# vim docker-compose.yml
version: '3'
services:
db:
image: mysql:5.7
volumes:
- /data/db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- /data/web_data:/var/www/html
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
[root@docker01 my_wordpress]# docker-compose up
#启动方法:docker-compose up
#后台启动方法:docker-compose up -d
4、浏览器上访问http://10.0.0.100:8000
进行wordpress的安装即可
haproxy代理后端docker容器
[root@docker01 my_wordpress]# cat docker-compose.yml
version: '3'
services:
db:
image: mysql:5.7
volumes:
- /data/db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- /data/web_data:/var/www/html
ports:
- "80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
[root@docker01 my_wordpress]# docker-compose scale wordpress=2
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
Starting mywordpress_wordpress_1 ... done
Creating mywordpress_wordpress_2 ... done
[root@docker01 ~]# yum install haproxy -y
[root@docker01 ~]#cp /etc/haproxy/haproxy.cfg{,.bak}
[root@docker01 ~]# vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats level admin #支持命令行控制
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
listen stats
mode http
bind 0.0.0.0:8888
stats enable
stats uri /haproxy-status
stats auth admin:123456
frontend frontend_www_example_com
bind 10.0.0.100:8000
mode http
option httplog
log global
default_backend backend_www_example_com
backend backend_www_example_com
option forwardfor header X-REAL-IP
option httpchk HEAD / HTTP/1.0
balance roundrobin
server web-node1 10.0.0.100:32768 check inter 2000 rise 30 fall 15
server web-node2 10.0.0.100:32769 check inter 2000 rise 30 fall 15
systemctl start haproxy
systemctl enable haproxy
http://10.0.0.100:8888/haproxy-status
可以看到后端节点的监控状况,
安装socat 直接操作socket控制haproxy
yum install socat.x86_64 -y
[root@docker01 web_data]# echo "help"|socat stdio /var/lib/haproxy/stats
echo "disable server backend_www_example_com/web-node2"|socat stdio /var/lib/haproxy/stats
echo "enable server backend_www_example_com/web-node3"|socat stdio /var/lib/haproxy/stats
5、编写php测试页,放到/data/web_data下,在浏览器中访问可以查看当前的节点
[root@docker01 web_data]# vim check.php
<html>
<head>
<title>PHP测试title>
head>
<body>
echo 'Hello World
'; ?>
echo "访问的服务器地址是:"."" .$_SERVER['SERVER_ADDR'].""."
";
echo"访问的服务器域名是:"."" .$_SERVER['SERVER_NAME'].""."
";
?>
body>
html>
重启docker服务,容器全部退出的解决办法
在启动是指定自动重启
docker run --restart=always
修改docker默认配置文件
# 添加上下面这行
"live-restore": true
docker server配置文件 /etc/docker/daemon.json 参考
[root@docker02 ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://registry.docker-cn.com"],
"graph": "/opt/mydocker", # 修改数据的存放目录到/opt/mydocker/,原/var/lib/docker/
"insecure-registries": ["10.0.0.100:5000"],
"live-restore": true
}
[root@docker01 ~]# systemctl restart docker.service
Docker网络类型
docker的网络类型
Bridge默认docker网络隔离基于网络命名空间,在物理机上创建docker容器时会为每一个docker容器分配网络命名空间,并且把容器IP桥接到物理机的虚拟网桥上。
不为容器配置网络功能
此模式下创建容器是不会为容器配置任何网络参数的,如:容器网卡、IP、通信路由等,全部需要自己去配置。
[root@docker01 ~]# docker run -it --network none busybox:latest /bin/sh
/ # ip a
1: lo: mtu 65536 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
与其他容器共享网络配置(Container)
此模式和host模式很类似,只是此模式创建容器共享的是其他容器的IP和端口而不是物理机,此模式容器自身是不会配置网络和端口,创建此模式容器进去后,你会发现里边的IP是你所指定的那个容器IP并且端口也是共享的,而且其它还是互相隔离的,如进程等。
[root@docker01 ~]# docker run -it --network container:mywordpress_db_1 busybox:latest /bin/sh
/ # ip a
1: lo:mtu 65536 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
105: eth0@if106:mtu 1500 qdisc noqueue
link/ether 02:42:ac:12:00:03 brd ff:ff:ff:ff:ff:ff
inet 172.18.0.3/16 brd 172.18.255.255 scope global eth0
valid_lft forever preferred_lft forever
使用宿主机网络
此模式创建的容器没有自己独立的网络命名空间,是和物理机共享一个Network Namespace,并且共享物理机的所有端口与IP,并且这个模式认为是不安全的。
[root@docker01 ~]# docker run -it --network host busybox:latest /bin/sh
查看网络列表
[root@docker01 ~]# docker network list
NETWORK ID NAME DRIVER SCOPE
b15e8a720d3b bridge bridge local
345d65b4c2a0 host host local
bc5e2a32bb55 mywordpress_default bridge local
ebf76eea91bb none null local
用PIPEWORK为docker容器配置独立IP
参考文档: blog.csdn.net/design321/article/details/48264825
官方网站: github.com/jpetazzo/pipework
宿主环境:centos7.2
wget https://github.com/jpetazzo/pipework/archive/master.zip
unzip master.zip
cp pipework-master/pipework /usr/local/bin/
chmod +x /usr/local/bin/pipework
yum install bridge-utils.x86_64 -y
# 修改eth0配置,让br0实现桥接
[root@docker01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
BRIDGE=br0
[root@docker01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0
TYPE=Bridge
BOOTPROTO=static
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=10.0.0.100
NETMASK=255.255.255.0
GATEWAY=10.0.0.254
DNS1=223.5.5.5
# 重启网络
[root@docker01 ~]# /etc/init.d/network restart
pipework br0 $(docker run -d -it -p 6880:80 --name httpd_pw httpd) 10.0.0.220/24@10.0.0.254
[root@docker01 ~]# curl 10.0.0.220
It works!
[root@docker01 ~]# ping 10.0.0.220 -c 1
PING 10.0.0.220 (10.0.0.220) 56(84) bytes of data.
64 bytes from 10.0.0.220: icmp_seq=1 ttl=64 time=0.043 ms
4、再运行一个容器,设置网路类型为none:
pipework br0 $(docker run -d -it --net=none --name test httpd:2.4) 10.0.0.221/24@10.0.0.254
[root@docker01 ~]# curl 10.0.0.221
<html><body><h1>It works!h1>body>html>
pipework br0 testduliip 172.16.146.113/24@172.16.146.1
pipework br0 testduliip01 172.16.146.112/24@172.16.146.1
cnblogs.com/CloudMan6/p/7270551.html
Docker跨主机通信之macvlan
[root@docker01 ~]# docker network create --driver macvlan --subnet 10.1.0.0/24 --gateway 10.1.0.254 -o parent=eth0 macvlan_1
33a1f41dcc074f91b5bd45e7dfedabfb2b8ec82db16542f05213839a119b62ca
ip link set eth0 promisc on
[root@docker02 ~]# docker run -it --network macvlan_1 --ip=10.1.0.222 busybox /b
docker企业级镜像仓库harbor
[root@docker01 harbor]# pwd
/opt/harbor
[root@docker01 harbor]# docker-compose stop
cd /opt && https://storage.googleapis.com/harbor-releases/harbor-offline-installer-v1.3.0.tgz
tar xf harbor-offline-installer-v1.3.0.tgz
[root@docker01 harbor]# vim harbor.cfg
···
hostname = 10.0.0.100
harbor_admin_password = Harbor12345
···
[root@docker01 harbor]# ./install.sh
浏览器访问 http://10.0.0.11
添加一个项目
4、镜像推送到仓库的指定项目
[root@docker02 ~]# docker tag centos:6.8 10.0.0.100/clsn/centos6.8:1.0
[root@docker02 ~]#
[root@docker02 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 5b0d59026729 8 days ago 1.15MB
10.0.0.100/clsn/centos6.8 1.0 6704d778b3ba 2 months ago 195MB
centos 6.8 6704d778b3ba 2 months ago 195MB
[root@docker02 ~]# docker login 10.0.0.100
Username: admin
Password:
Login Succeeded
[root@docker02 ~]# docker push 10.0.0.100/clsn/centos6.8
The push refers to repository [10.0.0.100/clsn/centos6.8]
e00c9229b481: Pushing 13.53MB/194.5MB
6、在web界面里查看
使用容器的建议
9. 不要将数据存放在容器内
关于Docker容器的监控
容器的基本信息
包括容器的数量、ID、名称、镜像、启动命令、端口等信息
容器的运行状态
统计各状态的容器的数量,包括运行中、暂停、停止及异常退出
统计容器的CPU使用率、内存使用量、块设备I/O使用量、网络使用情况等资源的使用情况