系统综合实验第四次作业
一、使用Docker-compose实现Tomcat+Nginx负载均衡
(1)nginx反向代理原理
客户端对代理是无感知的,客户端不需要任何配置就可以访问,客户端将请求发送到反向代理服务器,由反向代理服务器去选择目标服务器获取数据后,在返回给客户端,此时反向代理服务器和目标服务器对外就是一个服务器,暴露的是代理服务器地址,隐藏了真实服务器IP地址。
(2)nginx代理tomcat集群
- 文件结构
├── default.conf
├── docker-compose.yml
├── nginx [error opening dir]
├── tomcat1
│ └── index.html
├── tomcat2
│ └── index.html
└── tomcat3
└── index.html
- docker-compose.yml
version: "3"
services:
nginx:
image: nginx
container_name: "nginx-tomcat"
ports:
- 80:8085
volumes:
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf # 挂载配置文件
depends_on:
- tomcat01
- tomcat02
- tomcat03
tomcat01:
image: tomcat
container_name: "tomcat01"
volumes:
- ./tomcat1:/usr/local/tomcat/webapps/ROOT # 挂载web目录
tomcat02:
image: tomcat
container_name: "tomcat02"
volumes:
- ./tomcat2:/usr/local/tomcat/webapps/ROOT
tomcat03:
image: tomcat
container_name: "tomcat03"
volumes:
- ./tomcat3:/usr/local/tomcat/webapps/ROOT
- default.conf
upstream tomcats {
server tomcat01:8080;
server tomcat02:8080;
server tomcat03:8080;
}
server {
listen 8085;
server_name localhost;
location / {
proxy_pass http://tomcats; # 请求转向tomcats
}
}
写好配置后docker-compose up -d即可访问localhost
(3)nginx的负载均衡策略
- 轮询策略测试负载均衡
- 权重策略测试负载均衡
先修改default.conf
upstream tomcats {
server tomcat01:8080; #默认weight=1
server tomcat02:8080 ;
server tomcat03:8080 weight=3;
}
server {
listen 8085;
server_name localhost;
location / {
proxy_pass http://tomcats; # 请求转向tomcats
}
}
这里修改完一定要重启容器,不然策略不会改变。。。很难受
由于03的权重比较大,所以出现的频率比较大
二、使用Docker-compose部署javaweb运行环境
(1)文件结构
├── docker-compose.yml
├── default.conf
├── docker-entrypoint.sh
├── grogshop.sql
├── Dockerfile
└── webapps
├── docs
├── examples
├── host-manager
├── manager
├── ROOT
├── ssmgrogshop_war
└── ssmgrogshop_war.war
- 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"
#红色的外部访问端口不修改的情况下,要把Linux的MySQL服务停掉
#service mysql stop
#反之,将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
vim ~/javaweb1/webapps/ssmgrogshop_war/WEB-INF/classes/jdbc.properties
(3)启动容器,浏览器访问页面
sudo docker-compose up -d
http://主机ip地址:8080/ssmgrogshop_war
(4)测试两个tomcat服务器负载均衡
http://主机ip地址:8080/ssmgrogshop_war
http://主机ip地址:5050/ssmgrogshop_war
(5)在前端页面进行数据库操作
-
增
-
删
-
改
-
查
三、使用Docker搭建大数据集群环境
(1)搭建hadoop环境
- 文件结构
├── Dockerfile
├── build
│ └── hadoop-3.1.3.tar.gz
└── sources.list
- Dockerfile
#Base images 基础镜像
FROM ubuntu:18.04
#MAINTAINER 维护者信息
MAINTAINER lhw
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无密码登陆
ssh-keygen -t rsa # 一直按回车即可
cd ~/.ssh
cat id_rsa.pub >> authorized_keys
安装jdk,Hadoop
apt-get install openjdk-8-jdk
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 # 在文件末尾添加以下五行,配置Java、hadoop环境变量:
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生效
验证安装是否成功
java -version
hadoop version
(3)配置hadoop集群
cd /usr/local/hadoop-3.1.3/etc/hadoop
vim hadoop-env.sh
vim core-site.xml
vim hdfs-site.xml
vim mapred-site.xml
vim yarn-site.xml
- hadoop-env.sh
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/ # 在任意位置添加
- core-site.xml
<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
<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
<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
<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>
- 进入脚本目录
cd /usr/local/hadoop-3.1.3/sbin
- 对于start-dfs.sh和stop-dfs.sh文件,添加下列参数
HDFS_DATANODE_USER=root
HADOOP_SECURE_DN_USER=hdfs
HDFS_NAMENODE_USER=root
HDFS_SECONDARYNAMENODE_USER=root
- 对于start-yarn.sh和stop-yarn.sh,添加下列参数
YARN_RESOURCEMANAGER_USER=root
HADOOP_SECURE_DN_USER=yarn
YARN_NODEMANAGER_USER=root
(4)构建镜像并运行主机
docker commit 容器ID ubuntu/hadoop
# 第一个终端
docker run -it -h master --name master ubuntu/hadoop
# 第二个终端
docker run -it -h slave01 --name slave01 ubuntu/hadoop
# 第三个终端
docker run -it -h slave02 --name slave02 ubuntu/hadoop
- 修改/etc/hosts
172.17.0.2 master
172.17.0.3 slave01
172.17.0.4 slave02
- 测试ssh
- master主机上修改workers
vim /usr/local/hadoop-3.1.3/etc/hadoop/workers
slave01
slave02
- 开启服务
start-dfs.sh
start-yarn.sh
jps查看
(6)运行hadoop示例程序
hdfs namenode -format # 格式化文件系统
sbin/start-all.sh #启动所有服务
- 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/* #查看运行结果
- wordcount测试
hdfs dfs -rm root #删除上一次运行的输入和输出
hdfs dfs -mkdir -p /user/root/input #新建input文件夹
vim txt1.txt #在当前目录下新建txt1.txt
vim txt2.txt #在当前目录下新建txt2.txt
hdfs dfs -put ./*.txt input #将新建的文本文件放入input文件夹
hadoop jar /usr/local/hadoop-3.1.3/share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jar wordcount input output #运行示例程序wordcount
hdfs dfs -cat output/* #查看运行结果
(4)实验报告
发表一篇博客,记录主要的过程,无需每个步骤一一截图;
提交可以运行的相关配置文件以及测试程序包;
记录实验过程的主要问题和解决方法,分享经验和感想;
记录完成作业所花的时间。