docker实战
1.安装confluence
准备工作
1)在主机安装confluence,启动,破解成功后关闭,路径:/wiki
2)将主机上confluence进行打包
]# mkdir -p /opt/confluence ]# cd /opt/confluence ]# tar cvzf confluence.7.14.0.tar.gz /wiki
镜像
mysql镜像
从docker hub拉取mysql镜像
docker pull mysql:5.7.40
confluence镜像
编写构建confluence镜像的dockerfile
FROM centos:7 ENV TZ=Asia/Shanghai appv=7.14.0 wiki_dir=/wiki WORKDIR $wiki_dir VOLUME $wiki_dir ##容器内yum源文件,需要事先准备好 ADD CentOS-Base.repo /etc/yum.repos.d/ ADD epel.repo /etc/yum.repos.d/ ##confluence应用破解包,解压到/下 ADD confluence-${appv}.tar.gz / RUN useradd confluence ADD server.xml wiki/conf EXPOSE 18090 CMD ["wiki/bin/catalina.sh", "run"]
使用douckerfile构建
docker build -f docwiki -t confluence:7.14.0 .
容器
mysql
执行创建mysql容器的命令
]# docker run -itd --name mysql1 -e 'MYSQL_ROOT_PASSWORD=123456' --restart=always mysql:5.7.40
配置数据库
##进入容器 ]# docker exec -it mysql1 bash 登录数据库创建confluence库及用户 ]# mysql -uroot -p mysql> create database confluence character set utf8 collate utf8_bin; mysql> GRANT ALL PRIVILEGES ON confluence.* TO'wiki'@'172.17.0.%' IDENTIFIED BY'123Abc..'; mysql> SET GLOBAL TRANSACTION ISOLATION LEVEL READ COMMITTED; mysql> flush privileges;
confluence
执行创建confluence容器命令
]# docker run -itd \ --name confluence1 -p 18090:18090 \ -v /wiki/data/backups:/wiki/data/backups \ --restart=always confluence:7.14.0
登录confluence页面,配置数据库连接信息
进行数据库的构建
confluence部署成功
2.mysql容器初始化设置
mysql官方镜像有个目录:/docker-entrypoint-initdb.d,启动容器时会运行该目录下的脚本,可以用来做数据库的初始配置
docker compose文件
mysql-compose-build.yml
version: "3.8" services: mysqltest: image: mysqlproxy ##mysql新镜像名称 container_name: zabmysql ##mysql容器名称 environment: MYSQL_ROOT_PASSWORD: "123456" TZ: "Asia/Shanghai" build: context: . dockerfile: zabmysql ##mysql新镜像构建文件 ports: - 13310:13310 volumes: - "/zabmysql/data:/var/lib/mysql" - "./conf/my.cnf:/etc/my.cnf" - "/etc/localtime:/etc/localtime" restart: unless-stopped networks: - web1 networks: web1: external: true
dockerfile文件
zabmysql
FROM mysql:8.0.31 RUN mkdir /docker-entrypoint-initdb.d/data ##将sql文件复制到镜像的/docker-entrypoint-initdb.d/data目录中 ADD data/schema.sql /docker-entrypoint-initdb.d ADD data/images.sql /docker-entrypoint-initdb.d ADD data/data.sql /docker-entrypoint-initdb.d ##将初始化脚本复制到镜像的/docker-entrypoint-initdb.d目录中 ADD script/mysql_init.sh /docker-entrypoint-initdb.d RUN usermod -aG root mysql && chmod +x /docker-entrypoint-initdb.d/mysql_init.sh
初始化脚本
mysql_init.sh
#!/bin/bash mysql -uroot -p'123456' -e "create database zabbix_proxy character set utf8 collate utf8_bin;" mysql -uroot -p'123456' -e "create USER 'zabbix'@'192.168.2.%' IDENTIFIED BY '123Abc..';" mysql -uroot -p'123456' -e "GRANT ALL PRIVILEGES ON zabbix_proxy.* TO 'zabbix'@'192.168.2.%';" mysql -uroot -p'123456' zabbix_proxy < /docker-entrypoint-initdb.d/data/schema.sql mysql -uroot -p'123456' zabbix_proxy < /docker-entrypoint-initdb.d/data/images.sql mysql -uroot -p'123456' zabbix_proxy < /docker-entrypoint-initdb.d/data/data.sql
创建mysql容器
执行docker compose文件,构建新镜像,创建容器
登录容器
3.zblog开源博客容器部署
zblog版本:1.7.3
下载地址:https://www.zblogcn.com/zblogphp/
准备目录/文件
下载离线安装包
zblog文件目录:/zblog
将安装包中的文件复制到/zblog中
zblog数据库目录:/zbdata
以上目录/文件在宿主机中,没有需要自己建
编写docker compose
version: "3.8" services: zbweb: ##nginx容器名称 image: zbnginx:1.25.5 ##构建的nginx新镜像名称 container_name: zbweb build: context: ./nginx dockerfile: nginxbuild ##新nginx镜像构建文件 ports: - "20002:18080" volumes: - "/zblog:/zblog" ##zblog代码目录 - "./nginx/conf/zblog.conf:/nginx/conf/vhosts/zblog.conf" ##nginx配置文件 networks: - zbnet depends_on: - zbphp zbphp: image: zbphp:8.2.0 ##构建的php新镜像名称 container_name: zbphp ##php容器名称 build: context: ./php dockerfile: phpbuild ##新php镜像构建文件 volumes: - "/zblog:/zblog" ##zblog代码目录 - "./php/conf/php.ini:/php/php.ini" ##php配置文件 - "./php/conf/php-fpm.conf:/php/etc/php-fpm.conf" - "./php/conf/www.conf:/php/etc/php-fpm.d/www.conf" depends_on: - zbredis networks: - zbnet zbredis: image: redis:7.2.4 ##redis容器的镜像 container_name: zbredis ##redis容器名称 volumes: - "./redis/conf:/redis/redis/conf" ##redis配置文件存放目录 - "./redis/log:/redis/redis/log" ##redis日志目录 - "./redis/backup:/redis/redis/backup" ##redis持久化文件存放目录 depends_on: - zbmysql networks: - zbnet zbmysql: image: zbmysql:8.0.31 ##新mysql镜像 container_name: zbmysql build: context: ./mysql dockerfile: mysqlbuild ##新mysql镜像构建文件 volumes: - "./mysql/conf/my.cnf:/etc/my.cnf" ##mysql配置文件 - "/zbdata:/var/lib/mysql" ##mysql数据目录 networks: - zbnet networks: ##容器网络配置,使用桥接模式,网段:172.20.2.0/24 zbnet: driver: bridge ipam: driver: default config: - subnet: "172.20.0.0/16" ip_range: "172.20.2.0/24" gateway: "172.20.2.0"
编写dockerfile
zbweb:nginxbuild
FROM nginx:1.25.5 ENV TZ=Asia/Shanghai \ zb_dir=/zblog ##nginx用户uid,家目录 RUN usermod -u 1020 nginx && \ groupmod -g 1020 nginx && \ ##授予权限 mkdir ${zb_dir} && \ ##zblog目录 chown -R nginx:nginx ${zb_dir}
zbphp:phpbuild
FROM php:8.2.0 ENV TZ=Asia/Shanghai \ zb_dir=/zblog ##nginx用户uid,家目录,与nginx镜像构建相同 RUN useradd nginx -u 1020 &&\ groupmod -g 1020 nginx && \ usermod -u 1020 -d /php nginx && \ ##授予权限 mkdir ${zb_dir} && \ chown -R nginx:nginx /php && \ ##php主目录,授予nginx用户权限 chown -R nginx:nginx ${zb_dir}
zbmysql:mysqlbuild
FROM mysql:8.0.31 ENV TZ=Asia/Shanghai \ MYSQL_ROOT_PASSWORD=mysql@123 ##mysql数据库密码 ADD script/mysql_init.sh /docker-entrypoint-initdb.d ##mysql初始化脚本复制到镜像中 RUN usermod -aG root mysql ##设置用户,组权限
编写脚本
mysql初始化脚本:mysql_init.sh
#!/bin/bash mysql -uroot -p'mysql@123' -e "create database zblog character set utf8 collate utf8_bin;" mysql -uroot -p'mysql@123' -e "create USER 'zblog'@'172.20.2.%' IDENTIFIED BY '123Abc..';" mysql -uroot -p'mysql@123' -e "GRANT ALL PRIVILEGES ON zblog.* TO 'zblog'@'172.20.2.%';" mysql -uroot -p'mysql@123' -e "ALTER USER 'zblog'@'172.20.2.%' IDENTIFIED WITH mysql_native_password BY '123Abc..';"
容器构建文件目录结构:
启动zblog所有容器
执行docker compose,启动zblog各容器:
docker compose -f zblog-compose.yml up --build -d
初始化信息配置
4.部署zabbix平台
版本信息:
zabbix版本:6.4.10
mysql版本:8.0.31
nginx版本:1.25.5
php版本:8.2.0
构建zabbix镜像
zabbix server 构建dockerfile
FROM centos:7 ENV zbx_dir=/zabbix/zabbix_server \ TZ=Asia/Shanghai \ ver=6.4.10 WORKDIR $zbx_dir ADD zabbix-server-${ver}.tar.gz / ADD CentOS-Base.repo /etc/yum.repos.d ADD epel.repo /etc/yum.repos.d COPY script/ ${zbx_dir}/script RUN yum -y install libcurl libxml2 net-snmp-devel libxml2-devel libevent-devel mysql-devel curl-devel pcre* RUN useradd zabbix -u 2000 -d ${zbx_dir} && \ chown -R zabbix:zabbix $zbx_dir && \ chmod +x ${zbx_dir}/script/* ##zabbix_server_start.sh,zabbix server启动脚本 CMD ["/bin/bash", "-c", "/zabbix/zabbix_server/script/zabbix_server_start.sh && tail -100f /zabbix/zabbix_server/logs/zabbix_server.log" ]
zabbix_server_start.sh脚本
#!/bin/bash zax_dir=/zabbix/zabbix_server su - zabbix -c "${zax_dir}/sbin/zabbix_server" sleep 2 ps -ef | grep -w "${zax_dir}/sbin/zabbix_server" if [ $? -eq 0 ];then echo "success: zabbix server start" echo '' else echo '' echo "error: zabbix server start failed" exit 10 fi
zabbix mysql 构建dockerfile
FROM mysql:8.0.31 ENV TZ=Asia/Shanghai \ MYSQL_ROOT_PASSWORD=mysql@123 ##mysql_init.sh,mysql容器启动,初始化脚本 ADD script/mysql_init.sh /docker-entrypoint-initdb.d ADD my.cnf /etc RUN usermod -aG root mysql && \ chmod +x /docker-entrypoint-initdb.d/mysql_init.sh && \ mkdir /docker-entrypoint-initdb.d/data ADD data/images.sql /docker-entrypoint-initdb.d/data ADD data/schema.sql /docker-entrypoint-initdb.d/data ADD data/data.sql /docker-entrypoint-initdb.d/data
mysql_init.sh脚本
#!/bin/bash #!/bin/bash mysql -uroot -p'mysql@123' -e "create database zabbix character set utf8 collate utf8_bin;" mysql -uroot -p'mysql@123' -e "create USER 'zabbix'@'172.16.2.%' IDENTIFIED BY '123Abc..';" mysql -uroot -p'mysql@123' -e "GRANT ALL PRIVILEGES ON zabbix.* TO 'zabbix'@'172.16.2.%';" mysql -uroot -p'mysql@123' zabbix < /docker-entrypoint-initdb.d/data/schema.sql mysql -uroot -p'mysql@123' zabbix < /docker-entrypoint-initdb.d/data/images.sql mysql -uroot -p'mysql@123' zabbix < /docker-entrypoint-initdb.d/data/data.sq
zabbix容器
docker compose文件
services: zabnginx: image: zabnginx:1.25.5 container_name: zabnginx build: context: ./nginx dockerfile: nginxbuild ##nginx新镜像构建文件 ports: - "18080:18080" ##映射18080端口 volumes: - "/opt/web:/web" ##zabbix web文件目录 - "./nginx/conf/zabbix.conf:/nginx/conf/vhosts/nginx.conf" ##nginx配置文件 restart: unless-stopped networks: - zabnet depends_on: - zabphp zabphp: image: zabphp:8.2.0 container_name: zabphp build: context: ./php dockerfile: phpbuild ##php新镜像构建文件 volumes: - "/opt/web:/web" ##zabbix web文件目录,与nginx容器共享同一目录 - "./php/conf/php.ini:/php/php.ini" ##php配置文件 - "./php/conf/php-fpm.conf:/php/etc/php-fpm.conf" - "./php/conf/www.conf:/php/etc/php-fpm.d/www.conf" restart: unless-stopped networks: - zabnet depends_on: - zabserver zabserver: image: zabbix_server:6.4.10 container_name: zabserver volumes: - "./server/conf:/zabbix/zabbix_server/etc" ##zabbix server配置文件目录 ports: - "10151:10151" restart: unless-stopped networks: - zabnet depends_on: - zabjava zabjava: image: zabbix_java:6.4.10 container_name: zabjava volumes: ##zabbix java相关文件,提前复制到本地目录中 - "./java_gateway/conf/settings.sh:/zabbix/zabbix_java/sbin/zabbix_java/settings.sh" - "./java_gateway/conf/logback.xml:/zabbix/zabbix_java/sbin/zabbix_java/lib/logback.xml" networks: - zabnet depends_on: - zabmysql zabmysql: image: zabbix_mysql:8.0.31 container_name: zabmysql volumes: - "./mysql/conf/my.cnf:/etc/my.cnf" - "/data:/var/lib/mysql" restart: unless-stopped networks: - zabnet networks: zabnet: driver: bridge ipam: driver: default config: - subnet: "172.16.0.0/16" ip_range: "172.16.2.0/24" gateway: "172.16.2.1"
镜像构建文件
nginxbuild
FROM nginx:1.25.5 ENV TZ=Asia/Shanghai \ zab_dir=/web RUN usermod -u 1020 nginx && \ groupmod -g 1020 nginx && \ mkdir ${zab_dir} && \ chown -R nginx:nginx ${zab_dir}
phpbuild
FROM php:8.2.0 ENV TZ=Asia/Shanghai \ zab_dir=/web RUN useradd nginx -u 1020 &&\ groupmod -g 1020 nginx &&\ usermod -u 1020 -d /php nginx && \ mkdir ${zab_dir} && \ chown -R nginx:nginx /php && \ chown -R nginx:nginx ${zab_dir}
zabbix容器构建目录结构
compose/
├── java_gateway zabbix java gateway目录
│ └── conf
│ ├── logback.xml
│ └── settings.sh
├── mysql zabbix mysql目录
│ ├── 1
│ └── conf
│ └── my.cnf
├── nginx zabbix nginx目录
│ ├── conf
│ │ └── zabbix.conf
│ └── nginxbuild
├── php zabbix php目录
│ ├── conf
│ │ ├── php-fpm.conf
│ │ ├── php.ini
│ │ └── www.conf
│ └── phpbuild
├── server zabbix server目录
│ └── conf
│ ├── zabbix_server.conf
│ └── zabbix_server.conf.d
├── web zabbix web页面目录
├── zabbix-compose.yml zabbix容器建立compose文件
启动zabbix容器
进入compose主目录执行:
docker compose -f zabbix-compose.yml up --build -d
5.ES集群部署
说明:部署swarm集群,使用overlay网络,实现es集群各节点的通信
集群情况
节点容器名称 | 节点宿主机 | 角色 | 节点名称 (node.name) | 容器ip |
---|---|---|---|---|
estest100 | systest-server2 | master+data | ES1 | 192.168.2.100 |
estest101 | systest-server1 | data | ES2 | 192.168.2.101 |
estest102 | systest-server3 | data | ES3 | 192.168.2.102 |
部署swarm集群
swarm集群使用2377端口进行通信,因此,需要集群的主机间开放2377端口
创建swarm集群
在一台主机上执行:
docker swarm init
记住token:SWMTKN-1-18ec0dmy3nc66izfrib685g36f1mi7hrlvldye8gm13ulwq5g4-7u9tfyimhy946gvb4viq70b1k
加入swarm集群
另外的主机上执行
docker swarm join --token 'SWMTKN-1-18ec0dmy3nc66izfrib685g36f1mi7hrlvldye8gm13ulwq5g4-7u9tfyimhy946gvb4viq70b1k' 172.25.230.52:2377
SWMTKN-1-18ec0dmy3nc66izfrib685g36f1mi7hrlvldye8gm13ulwq5g4-7u9tfyimhy946gvb4viq70b1k:生成的token
172.25.230.52:2377:连接生成token的宿主机ip及端口2377
创建容器网络
执行命令:
docker network create --driver=overlay --subnet=10.9.0.0/16 --ip-range=10.9.2.0/24 --attachable esnet
网络名称:esnet
子网网段:10.9.0.0
ip地址范围:10.9.2.0-255
创建es容器
编写docker compose文件
es-compose.yml
version: "3.8" services: estest: image: docker.elastic.co/elasticsearch/elasticsearch:7.17.0 container_name: estest101 environment: - "TZ=Asia/Shanghai" ports: - 19200:19200 - 19300:19300 volumes: - /opt/es/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml - /opt/es/jvm.options:/usr/share/elasticsearch/config/jvm.options - /opt/es/elasticsearch-env:/usr/share/elasticsearch/bin/elasticsearch-env restart: always networks: - esnet networks: esnet: driver: overlay external: true
配置文件
elasticsearch.yml
cluster.name: es-cluster node.name: ES1 ##各节点名称不同 path.data: /usr/share/elasticsearch/data path.logs: /usr/share/elasticsearch/logs network.host: 0.0.0.0 http.port: 19200 transport.tcp.port: 19300 #discovery.type: single-node discovery.zen.fd.ping_interval: 10s discovery.zen.fd.ping_timeout: 10s discovery.zen.fd.ping_retries: 6 discovery.seed_hosts: ["estest100:19300", "estest101:19300", "estest102:19300"] cluster.initial_master_nodes: ["ES1", "ES2", "ES3"] ##配置集群的引导节点,集群启动成功后删除即可 node.master: true node.data: true discovery.zen.minimum_master_nodes: 2 #index.number_of_shards: 5 #index.number_of_replicas: 1 http.cors.enabled: false http.max_content_length: 100mb
启动容器
节点启动顺序:estest100==>estest101==>estest102
docker compose -f es-compose.yml up -d
查看集群情况
这里的ip是指容器的ip