# docker run -d --name nginx-server -v /opt/nginx-server:/usr/share/nginx/html:ro nginx 664cd1bbda4ad2a71cbd09f0c6baa9b34db80db2d69496670a960be07b9521cb
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 664cd1bbda4a nginx "/docker-entrypoint.…"4 seconds ago Up 3 seconds 80/tcp nginx-server
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 74dddf51983d nginx "/docker-entrypoint.…"3 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx-server-port
# ls /opt nginx-server nginx-server-port
# echo "nginx is running" > /opt/nginx-server-port/index.html
在宿主机上访问
# docker top nginx-server-port UID PID PPID C STIME TTY TIME CMD root 2219522163015:08 ? 00:00:00 nginx: master process nginx -g daemon off; 1012238722195015:08 ? 00:00:00 nginx: worker process
# docker top nginx-server-conf UID PID PPID C STIME TTY TIME CMD root 2500524972015:38 ? 00:00:00 nginx: master process nginx -g daemon off; 1012517825005015:38 ? 00:00:00 nginx: worker process 1012517925005015:38 ? 00:00:00 nginx: worker process
三、使用Docker容器实现Tomcat部署
3.1 获取参考资料
3.2 运行tomcat应用容器
3.2.1 不暴露端口运行
# docker run -d --rm tomcat:9.0
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c20a0e781246 tomcat:9.0"catalina.sh run"27 seconds ago Up 25 seconds 8080/tcp heuristic_cori
3.2.2 暴露端口运行
# docker run -d -p 8080:8080 --rm tomcat:9.0 2fcf5762314373c824928490b871138a01a94abedd7e6814ad5f361d09fbe1de
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2fcf57623143 tomcat:9.0"catalina.sh run"3 seconds ago Up 1 second 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp eloquent_chatelet
在宿主机访问
# docker exec 2fc ls /usr/local/tomcat/webapps 里面为空,所以可以添加网站文件。
3.2.3 暴露端口及添加网站文件
# docker run -d -p 8081:8080 -v /opt/tomcat-server:/usr/local/tomcat/webapps/ROOT tomcat:9.0 f456e705d48fc603b7243a435f0edd6284558c194e105d87befff2dccddc0b63
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f456e705d48f tomcat:9.0"catalina.sh run"3 seconds ago Up 2 seconds 0.0.0.0:8081->8080/tcp, :::8081->8080/tcp cool_germain
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6d16ca21cf31 mysql:5.7"docker-entrypoint.s…"32 seconds ago Up 30 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
通过容器中客户端访问 # docker exec -it mysql mysql -uroot -proot mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type'help;' or '\h'for help. Type'\c' to clear the current input statement.
mysql>
在docker host上访问 # yum -y install mariadb
# mysql -h 192.168.255.157 -uroot -proot -P 3306 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type'help;' or '\h'for help. Type'\c' to clear the current input statement.
MySQL [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows inset (0.00 sec)
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2dbbed8e35c7 mysql:5.7"docker-entrypoint.s…"58 seconds ago Up 57 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql-master
4.2.2 MySQL主节点配置
# vim /opt/mysql-master/conf/my.cnf # cat /opt/mysql-master/conf/my.cnf [client] default-character-set=utf8
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES caf7bf3fc68f mysql:5.7"docker-entrypoint.s…"8 seconds ago Up 6 seconds 33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp mysql-slave
4.2.4 MySQL从节点配置
# vim /opt/mysql-slave/conf/my.cnf # cat /opt/mysql-slave/conf/my.cnf [client] default-character-set=utf8
# mysql -h 192.168.255.157 -uroot -proot -P 3306 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type'help;' or '\h'for help. Type'\c' to clear the current input statement.
MySQL [(none)]>
授权 MySQL [(none)]> grant replication slave on *.* to 'backup'@'%' identified by '123456';
# mysql -h 192.168.255.157 -uroot -proot -P 3307 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type'help;' or '\h'for help. Type'\c' to clear the current input statement.
MySQL [(none)]>
MySQL [(none)]> change master to master_host='mysql-master', master_user='backup', master_password='123456', master_log_file='mysql-bin.000001', master_log_pos=154, master_port=3306;
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e1c306e6e5a3 elasticsearch:7.17.0"/bin/tini -- /usr/l…"22 seconds ago Up 20 seconds 0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp elasticsearch
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fb60e73f9cd5 kibana:7.17.0"/bin/tini -- /usr/l…"2 minutes ago Up 2 minutes 0.0.0.0:5601->5601/tcp, :::5601->5601/tcp kibana
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9bd2b39cd92a redis "docker-entrypoint.s…"44 seconds ago Up 42 seconds 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp redis
192.168.255.157:6379> set test1 a OK 192.168.255.157:6379> get test1 "a"
7.4 Redis集群
安装redis-cluster;3主3从方式,从为了同步备份,主进行slot数据分片
编辑运行多个redis容器脚本文件 # vim redis-cluster.sh # cat redis-cluster.sh for port in$(seq 80018006); \ do \ mkdir -p /mydata/redis/node-${port}/conf touch /mydata/redis/node-${port}/conf/redis.conf cat << EOF >/mydata/redis/node-${port}/conf/redis.conf port ${port} cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout5000 cluster-announce-ip192.168.255.157 cluster-announce-port${port} cluster-announce-bus-port1${port} appendonly yes EOF docker run -p${port}:${port} -p1${port}:1${port} --name redis-${port} \ -v /mydata/redis/node-${port}/data:/data \ -v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \ -d redis:5.0.7 redis-server /etc/redis/redis.conf; \ done
执行脚本 # sh redis-cluster.sh
查看已运行容器 # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8d53864a98ce redis:5.0.7"docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:8006->8006/tcp, :::8006->8006/tcp, 6379/tcp, 0.0.0.0:18006->18006/tcp, :::18006->18006/tcp redis-8006 e2b5da0f0605 redis:5.0.7"docker-entrypoint.s…"2 minutes ago Up About a minute 0.0.0.0:8005->8005/tcp, :::8005->8005/tcp, 6379/tcp, 0.0.0.0:18005->18005/tcp, :::18005->18005/tcp redis-8005 70e8e8f15aea redis:5.0.7"docker-entrypoint.s…"2 minutes ago Up 2 minutes 0.0.0.0:8004->8004/tcp, :::8004->8004/tcp, 6379/tcp, 0.0.0.0:18004->18004/tcp, :::18004->18004/tcp redis-8004 dff8e4bf02b4 redis:5.0.7"docker-entrypoint.s…"2 minutes ago Up 2 minutes 0.0.0.0:8003->8003/tcp, :::8003->8003/tcp, 6379/tcp, 0.0.0.0:18003->18003/tcp, :::18003->18003/tcp redis-8003 c34dc4c423ef redis:5.0.7"docker-entrypoint.s…"2 minutes ago Up 2 minutes 0.0.0.0:8002->8002/tcp, :::8002->8002/tcp, 6379/tcp, 0.0.0.0:18002->18002/tcp, :::18002->18002/tcp redis-8002 b8cb5feffb43 redis:5.0.7"docker-entrypoint.s…"2 minutes ago Up 2 minutes 0.0.0.0:8001->8001/tcp, :::8001->8001/tcp, 6379/tcp, 0.0.0.0:18001->18001/tcp, :::18001->18001/tcp redis-8001