docker部署nginx,tomcat 练习
docker部署nginx步骤
- docker pull nginx
- docker run -d --name nginx01 -p 3344:80 nginx #设置端口映射,暴露到公网
- 使用curl localhost:3344 进行测试 ,会跳出nginx欢迎界面说明跑起来成功了
拓扑图如下:
思考:弊端是每次需要在运行到容器里进行部署,比较麻烦,是否可以容器外部提供映射路径,镜像里改,容器里也自动修改??
其实是可以的,后期需要用到 -v 数据卷的技术.
docker部署redis
redis配置文件中有一处要注意!!!
daemonize设置为no 或者注释.因为这个守护进程会和docker run -d冲突,会导致容器一直启动失败
docker run -p 6379:6379 --name myredis --privileged=true
-v /young/redis/redis.conf:/etc/redi.redis.conf
-v /young/reds/data:/data
-d redis:6.0.8
redis-server /etc/redi.redis.conf
docker部署tomcat步骤
- docker run -d -p 3355:8080 --name tomcat01 tomcat
- docker exec -it tomcat01 /bin/bash
我们会发现这个容器部署好后,tomcat无法访问.
因为阿里的镜像有些事阉割版,把非必要部分都去除了.但是可以发现tomcat下的webapps.dist里有网页部署测试文件,
把这里的文件复制到webapps下, cp webapps.dist/* -r webapps/
就可以访问了
docker部署mysql
mysql注意数据,设置,日志的容积卷
docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=192406 --privileged=true -v /root/young/mysql/log:/var/log/mysql -v /root/young/mysql/data:/var/lib/mysql -v /root/young/mysql/conf:/etc/mysql/conf.d --name mysql_master mysql:5.7
docker部署mysql主从复制
主
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=192406 --privileged=true -v /root/young/mysql/log:/var/log/mysql -v /root/young/mysql/data:/var/lib/mysql -v /root/young/mysql/conf:/etc/mysql/conf.d --name mysql_master mysql:5.7
[mysqld]
server-id=1
log-bin=mall-mysql-bin
log-error=mysql-error
character-set-server=utf8
collation-server=utf8_general_ci
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
数据库安全设置初始化
mysql_secure_installation
设置从库连接账号 密码
grant replication slave on *.* to "slave"@"%" identified by "12345678";
查看mysql的bin position 1007
从
docker run -d -p 3308:3308 -e MYSQL_ROOT_PASSWORD=192406 --privileged=true -v /root/young/mysql_slave/log:/var/log/mysql -v /root/young/mysql_slave/data:/var/lib/mysql -v /root/young/mysql_slave/conf:/etc/mysql/conf.d --name mysql_slave mysql:5.7
数据库安全设置初始化
mysql_secure_installation
配置文件
[mysqld]
server-id=2
binlog-ignore-db=mysql
log-bin=mall-mysql_slave-bin
log-error=mysql-error
character-set-server=utf8
collation-server=utf8_general_ci
read_only=1
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
字符编码校验
show variables like '%character%';
从库配置主机的参数
change master to master_host='192.168.0.28',master_port=3306,master_user='slave',master_password='12345678',master_log_file='mall-mysql-bin.000002',master_log_pos=1169,master_connect_retry=30;
查看从库的同步状态
show slave status \G;
这一步主要查看Slave_IO_Running和Slave_SQL_Running 因为还未开始同步,应该是no
开启同步
start slave;
再次查看从库同步状态,在主库里创建一个库,表.来从库这查看确认下
docker容器自动化安全设置初始化
我们在mysql容器化后 输入mysql_secure_installation对mysql进行初始化,比如设置root密码,删除隐匿账号和test数据库
这个实验是分2次做的,第一次是在mysql容器里 用apt-get update && apt-get install expect
用expect进行自动化交互部署的.
这里使用了 expect "xxx" {send "aa\r"}的格式交互过不去,采用了下面这个格式就可以了
#!/usr/bin/expect
spawn mysql_secure_installation
expect "Press y|Y for Yes, any other key for No:"
send "\r"
expect "New password"
send "112345678\r"
expect "Re-enter new password"
send "12345678\r"
expect "Remove anonymous users? (Press y|Y for Yes, any other key for No) :"
send "y\r"
expect "Disallow root login remotely? (Press y|Y for Yes, any other key for No)"
send "n\r"
expect "Remove test database and access to it? (Press y|Y for Yes, any other key for No)"
send "y\r"
expect "Reload privilege tables now? (Press y|Y for Yes, any other key for No) : "
send "y\r"
interact
搞定了第一步,我们就可以再进一步,比如把expect语法写入单独的.exp文件中,后续进行shell 调用. 也可以把该语法直接写入shell语句中,具体调用办法看
https://www.cnblogs.com/Young-shi/p/16093347.html
下面例子是采用了后者,把exp语句写入了shell中运行的,
我们先用docker run -p 3308:3306 -e MYSQL_ALLOW_EMPTY_PASSWORD=yes --privileged=true -d --name m2 mysql:5.7
,设置免密登陆,减少代码量,然后在重置脚本中重新设置root密码为12345678
#!/bin/bash
apt-get update && apt-get install -y vim expect #这一句只是为了测试,可去除
/usr/bin/expect <<-aaa
spawn mysql_secure_installation
expect "Press y|Y for Yes, any other key for No:"
send "\r"
expect "New password"
send "12345678\r"
expect "Re-enter new password"
send "12345678\r"
expect "Remove anonymous users? (Press y|Y for Yes, any other key for No) :"
send "y\r"
expect "Disallow root login remotely? (Press y|Y for Yes, any other key for No)"
send "n\r"
expect "Remove test database and access to it? (Press y|Y for Yes, any other key for No)"
send "y\r"
expect "Reload privilege tables now? (Press y|Y for Yes, any other key for No) : "
send "y\r"
expect EOF
aaa
docker部署elasticsearch
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2
但是elasticsearch 很耗内存 docker stats 查看内存 cpu使用情况
然后对elasticsearch进行内存的限制 用-e命令修改配置文件
docker run -d --name elasticsearch02 -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.6.2
ES_JAVA_OPTS="-Xms64m -Xmx512m" 是设置最大占用512内存,最小64MB
可视化 练习用 portainer() 一款docker图形化管理界面
docker run -d -p 8088:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer