❄一、使用Docker-compose实现Tomcat+Nginx负载均衡
(1)nginx反向代理原理
反向代理(Reverse Proxy)方式是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器;并将从服务器上得到的结果返回给Internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。
通常的代理服务器,只用于代理内部网络对Internet的连接请求,客户机必须指定代理服务器,并将本来要直接发送到Web服务器上的http请求发送到代理服务器中。当一个代理服务器能够代理外部网络上的主机,访问内部网络时,这种代理服务的方式称为反向代理服务。
(2)nginx代理tomcat集群
创建docker-compose文件夹,树形结构如下
相关文档配置/编写
- docker-compose.yml
version: "3.8"
services:
nginx:
image: nginx
container_name: c_ngx
ports:
- 80:2419
volumes:
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf # 挂载配置文件
depends_on:
- tomcat1
- tomcat2
- tomcat3
tomcat1:
image: tomcat
container_name: c_tc1
volumes:
- ./tomcat1:/usr/local/tomcat/webapps/ROOT # 挂载web目录
tomcat2:
image: tomcat
container_name: c_tc2
volumes:
- ./tomcat2:/usr/local/tomcat/webapps/ROOT
tomcat3:
image: tomcat
container_name: c_tc3
volumes:
- ./tomcat3:/usr/local/tomcat/webapps/ROOT
- default.conf
upstream tomcats {
server c_tc1:8080 ;
server c_tc2:8080 ;
server c_tc3:8080 ;
}
server {
listen 2419;
server_name localhost;
location / {
proxy_pass http://tomcats; # 请求转向tomcats
}
}
- 三个index.html文件
Welcome to tomcat-1 server.
Welcome to tomcat-2 server.
Welcome to tomcat-3 server.
访问localhost
(3)nginx的负载均衡策略
- 创建test.py文件并编写
import requests
url = 'http://localhost'
for i in range(1,10):
res = requests.get(url)
print(res.text)
轮询策略测试负载均衡
- 轮询策略,运行test.py文件,可以看出三个服务器出现的频率相同
python3 /home/hadoop/test.py
权重策略
- 修改default.conf,并重启容器
upstream tomcats {
server c_tc1:8080 weight=1;
server c_tc2:8080 weight=3;
server c_tc3:8080 weight=6;
}
server {
listen 2419;
server_name localhost;
location / {
proxy_pass http://tomcats; # 请求转向tomcats
}
}
- 权重策略,运行test.py文件,可以看到三个服务器出现的频率和权重值成正比
❄二、使用Docker-compose部署javaweb运行环境
参考老师的博客
(1)创建2文件夹,树形结构如下
- docker-compose.yml
version: "3" #版本
services: #服务节点
tomcat00: #tomcat 服务
image: tomcat #镜像
hostname: hostname #容器的主机名
container_name: tomcat00 #容器名
ports: #端口
- "5050:8080"
volumes: #数据卷
- "./webapps:/usr/local/tomcat/webapps"
- ./wait-for-it.sh:/wait-for-it.sh
networks: #网络设置静态IP
webnet:
ipv4_address: 15.22.0.15
tomcat01: #tomcat 服务
image: tomcat #镜像
hostname: hostname #容器的主机名
container_name: tomcat01 #容器名
ports: #端口
- "5055:8080"
volumes: #数据卷
- "./webapps:/usr/local/tomcat/webapps"
- ./wait-for-it.sh:/wait-for-it.sh
networks: #网络设置静态IP
webnet:
ipv4_address: 15.22.0.16
mymysql: #mymysql服务
build: . #通过MySQL的Dockerfile文件构建MySQL
image: mymysql:test
container_name: mymysql
ports:
- "3309:3306"
command: [
'--character-set-server=utf8mb4',
'--collation-server=utf8mb4_unicode_ci'
]
environment:
MYSQL_ROOT_PASSWORD: "123456"
networks:
webnet:
ipv4_address: 15.22.0.6
nginx:
image: nginx
container_name: "nginx-tomcat"
ports:
- 8080:8080
volumes:
- ./default.conf:/etc/nginx/conf.d/default.conf # 挂载配置文件
tty: true
stdin_open: true
depends_on:
- tomcat00
- tomcat01
networks:
webnet:
ipv4_address: 15.22.0.7
networks: #网络设置
webnet:
driver: bridge #网桥模式
ipam:
config:
-
subnet: 15.22.0.0/24 #子网
- docker-entrypoint.sh
#!/bin/bash
mysql -uroot -p123456 << EOF # << EOF 必须要有
source /usr/local/grogshop.sql;
- Dockerfile
# 这个是构建MySQL的dockerfile
FROM registry.saas.hand-china.com/tools/mysql:5.7.17
# mysql的工作位置
ENV WORK_PATH /usr/local/
# 定义会被容器自动执行的目录
ENV AUTO_RUN_DIR /docker-entrypoint-initdb.d
#复制gropshop.sql到/usr/local
COPY grogshop.sql /usr/local/
#把要执行的shell文件放到/docker-entrypoint-initdb.d/目录下,容器会自动执行这个shell
COPY docker-entrypoint.sh $AUTO_RUN_DIR/
#给执行文件增加可执行权限
RUN chmod a+x $AUTO_RUN_DIR/docker-entrypoint.sh
# 设置容器启动时执行的命令
#CMD ["sh", "/docker-entrypoint-initdb.d/import.sh"]
- default.conf
upstream tomcat123 {
server tomcat00:8080;
server tomcat01:8080;
}
server {
listen 8080;
server_name localhost;
location / {
proxy_pass http://tomcat123;
}
}
(2)修改连接数据库的Ip,端口号改成本机Ip地址
vim ~/2/webapps/ssmgrogshop_war/WEB-INF/classes/jdbc.properties
(3)启动容器
docker-compose up -d
(4)浏览器访问前端页面
http://127.0.0.1:8080/ssmgrogshop_war
❄三、使用Docker搭建大数据集群环境
参考博文
(1)搭建hadoop环境
- 拉取镜像并创建build文件,实现文件共享
docker pull ubuntu
cd ~
mkdir build
sudo docker run -it -v /home/p4/build:/root/build --name ubuntu ubuntu
- Dockerfile
#Base images 基础镜像
FROM ubuntu:18.04
#MAINTAINER 维护者信息
MAINTAINER y00
COPY ./sources.list /etc/apt/sources.list
- source.list
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# 预发布软件源,不建议启用
# deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
- 创建并运行容器
docker build -t ubuntu:18.04 .
docker run -it --name ubuntu ubuntu:18.04
(2)容器初始化
- 安装工具
apt-get update
apt-get install vim # 用于修改配置文件
apt-get install ssh # 分布式hadoop通过ssh连接
/etc/init.d/ssh start # 开启sshd服务器
vim ~/.bashrc # 在文件末尾添加/etc/init.d/ssh start,实现ssd开机自启
- 实现ssh无密访问本地sshd
ssh-keygen -t rsa
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
- 安装jdk
apt-get install openjdk-8-jdk
- 安装hadoop()
docker cp ./build/hadoop-3.1.3.tar.gz 容器ID:/root/hadoop-3.1.3.tar.gz
cd /root
tar -zxvf hadoop-3.1.3.tar.gz -C /usr/local
- 环境配置
vim ~/.bashrc # 文件末尾添加以下五行变量
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/
export PATH=$PATH:$JAVA_HOME/bin
export HADOOP_HOME=/usr/local/hadoop-3.1.3
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib
export PATH=$PATH:$HADOOP_HOME/sbin:$HADOOP_HOME/bin:$JAVA_HOME/bin
source ~/.bashrc # 使.bashrc生效
- 验证jdk,hadoop是否安装成功
java -version
hadoop version
(3)配置Hadoop集群
相关文件配置
cd /usr/local/hadoop-3.1.3/etc/hadoop
- hadoop-env.sh
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/ # 在任意位置添加
- core-site.xml
<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl" ?>
<configuration>
<property>
<name>hadoop.tmp.dir</name>
<value>file:/usr/local/hadoop-3.1.3/tmp</value>
<description>Abase for other temporary directories.</description>
</property>
<property>
<name>fs.defaultFS</name>
<value>hdfs://master:9000</value>
</property>
</configuration>
- hdfs-site.xml
<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl" ?>
<configuration>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
<property>
<name>dfs.namenode.name.dir</name>
<value>file:/usr/local/hadoop-3.1.3/tmp/dfs/name</value>
</property>
<property>
<name>dfs.datanode.data.dir</name>
<value>file:/usr/local/hadoop-3.1.3/tmp/dfs/data</value>
</property>
<property>
<name>dfs.permissions.enabled</name>
<value>false</value>
</property>
</configuration>
- mapred-site.xml
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl" ?>
<configuration>
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
<property>
<name>yarn.app.mapreduce.am.env</name>
<value>HADOOP_MAPRED_HOME=/usr/local/hadoop-3.1.3</value>
</property>
<property>
<name>mapreduce.map.env</name>
<value>HADOOP_MAPRED_HOME=/usr/local/hadoop-3.1.3</value>
</property>
<property>
<name>mapreduce.reduce.env</name>
<value>HADOOP_MAPRED_HOME=/usr/local/hadoop-3.1.3</value>
</property>
</configuration>
- yarn-site.xml
<?xml version="1.0" ?>
<configuration>
<!-- Site specific YARN configuration properties -->
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
<property>
<name>yarn.resourcemanager.hostname</name>
<value>Master</value>
</property>
<property>
<name>yarn.nodemanager.vmem-pmem-ratio</name>
<value>2.5</value>
</property>
</configuration>
- 在start-dfs.sh和stop-dfs.sh文件,添加下列参数
cd /usr/local/hadoop-3.1.3/sbin
HDFS_DATANODE_USER=root
HADOOP_SECURE_DN_USER=hdfs
HDFS_NAMENODE_USER=root
HDFS_SECONDARYNAMENODE_USER=root
- 对于statr-yarn.sh和stop-yarn.sh,添加下列参数
(4)构建镜像
docker commit 容器ID ubuntu/hadoop
(5)开启三个终端分别运行
- 查看自己容器的Ip地址
cat /etc/hosts
- 修改master的/etc/hosts
172.17.0.2 master
172.17.0.3 slave01
172.17.0.4 slave02
- 测试ssh,在master运行
ssh slave01
exit
ssh slave02
exit
- master修改workers
vim /usr/local/hadoop-3.1.3/etc/hadoop/workers
将localhost修改成
slave01
slave02
- 开启服务
start-dfs.sh
start-yarn.sh
- jps查看
(6)运行hadoop实例
- 格式化
hdfs namenode -format # 格式化文件系统
- grep测试
hdfs dfs -mkdir -p /user/root/input #新建input文件夹
hdfs dfs -put /usr/local/hadoop-3.1.3/etc/hadoop/*s-site.xml input #将部分文件放入input文件夹
hadoop jar /usr/local/hadoop-3.1.3/share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jar grep input output 'dfs[a-z.]+' #运行示例程序grep
hdfs dfs -cat output/* #查看运行结果
❄三、主要问题和解决方法
- 第二个实验运行老师的例子时发现登录超时==还没有解决
❄四、时间花费
- Tomcat+Nginx负载均衡:2.5h
- 部署javaweb运行环境:5h
- 用Docker搭建大数据集群环境:5h
- 写博客:1.5h