系统综合实践4
(1)使用Docker-compose实现Tomcat+Nginx负载均衡
理解nginx反向代理原理
反向代理(Reverse Proxy)方式是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器;并将从服务器上得到的结果返回给Internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。
nginx代理tomcat集群,代理2个以上tomcat
- 在目录下创建如下文件
docker-compose.yml
version: "3"
services:
nginx:
image: nginx
container_name: jasonginx
ports:
- "80:80"
volumes:
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf # 挂载配置文件
depends_on:
- tomcat01
- tomcat02
- tomcat03
tomcat01:
image: tomcat
container_name: jasotomcat1
volumes:
- ./tomcat1:/usr/local/tomcat/webapps/ROOT # 挂载web目录
tomcat02:
image: tomcat
container_name: jasotomcat2
volumes:
- ./tomcat2:/usr/local/tomcat/webapps/ROOT
tomcat03:
image: tomcat
container_name: jasotomcat3
volumes:
- ./tomcat3:/usr/local/tomcat/webapps/ROOT
default.conf
upstream tomcats {
server jasotomcat1:8080; # 主机名:端口号
server jasotomcat2:8080; # tomcat默认端口号8080
server jasotomcat3:8080; # 默认使用轮询策略
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://tomcats; # 请求转向tomcats
}
}
- 运行docker-compose
docker-compose up -d
- 查看容器
- 查看web端
了解nginx的负载均衡策略,并至少实现nginx的2种负载均衡策略
- 负载均衡策略1:轮询策略
import requests
url="http://127.0.0.1"
for i in range(0,10):
reponse=requests.get(url)
print(reponse.text)
- 负载均衡策略2:权重策略
default.conf
upstream tomcats {
server lyhtomcat1:8080 weight=1; # 主机名:端口号
server lyhtomcat2:8080 weight=2; # tomcat默认端口号8080
server lyhtomcat3:8080 weight=3; # 使用权重策略
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://tomcats; # 请求转向tomcats
}
}
- 重编一个爬虫查看
import requests
url="http://127.0.0.1"
count={}
for i in range(0,2000):
response=requests.get(url)
if response.text in count:
count[response.text]+=1;
else:
count[response.text]=1
for a in count:
print(a, count[a])
由于tomcat3权重大所以出现频率较高
(2) 使用Docker-compose部署javaweb运行环境
- 文件结构
default.conf
upstream tomcats {
server tomcat4:8080 weight=1;
server tomcat5:8080 weight=2;
}
server {
listen 8080;
server_name localhost;
location / {
proxy_pass http://tomcats; # 请求转向tomcats
}
}
docker-compose.yml
version: '2'
services:
tomcat01:
image: tomcat
hostname: lyhjavaweb
container_name: tomcat4
ports:
- "5050:8080"
volumes:
- "$PWD/webapps:/usr/local/tomcat/webapps"
networks:
webnet:
ipv4_address: 15.22.0.15
tomcat02:
image: tomcat
container_name: tomcat5
ports:
- "5051:8080"
volumes:
- "$PWD/webapps:/usr/local/tomcat/webapps"
networks:
webnet:
ipv4_address: 15.22.0.16
mymysql:
build: .
image: mymysql:test
container_name: mymysql
ports:
- "3306: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
ports:
- "8080:8080"
volumes:
- ./default.conf:/etc/nginx/conf.d/default.conf # 挂载配置文件
networks:
webnet:
driver: bridge
ipam:
config:
- subnet: 15.22.0.0/24
gateway: 15.22.0.2
- 修改IP
- 打开终端运行
docker-compose up -d
- 在web上查看
http://127.0.0.1:5050/ssmgrogshop_war
账号:sa 密码:123
(3)使用Docker搭建大数据集群环境
- pull Ubuntu镜像
docker pull ubuntu
- 创建build文件 运行容器
sudo docker run -it -v /home/ubuntu/build:/root/build --name ubuntu ubuntu
- 进入容器换源
cat<<EOF>/etc/apt/sources.list
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
EOF
- 更新源和安装必要软件
apt-get update
apt-get install vim
apt-get install ssh
vim ~/.bashrc #在该文件中最后一行添加如下内容,实现进入Ubuntu系统时,都能自动启动sshd服务
/etc/init.d/ssh start
- 配置ssh
cd ~/.ssh
ssh-keygen -t rsa # 一直按回车即可
cat id_rsa.pub >> authorized_keys
- 安装JDK
apt install openjdk-8-jdk
vim ~/.bashrc # 在文件末尾添加以下两行,配置Java环境变量:
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/
export PATH=$PATH:$JAVA_HOME/bin
source ~/.bashrc
java -version #查看是否安装成功
- 保存配置好的镜像文件
#打开一个新的终端
sudo su
docker ps
docker commit 容器id ubuntu/jdk
- 安装Hadoop
- 开启保存的那份镜像ubuntu/jdk
docker run -it -v /home/ubuntu/build:/root/build --name ubuntu-jdk ubuntu/jdk
- 将文件复制入共享文件夹
sudo cp /home/ubuntu/Desktop/hadoop-3.1.3.tar.gz /home/ubuntu/build
#在打开的容器里运行
cd /root/build
tar -zxvf hadoop-3.1.3.tar.gz -C /usr/local
cd /usr/local/hadoop-3.1.3
./bin/hadoop version # 验证安装
- 配置Hadoop集群
cd /usr/local/hadoop-3.1.3/etc/hadoop #进入配置文件存放目录
hadoop-env.sh
vim hadoop-env.sh
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/ # 在任意位置添加
core-site.xml
vim 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
vim hdfs-site.xml
<configuration>
<property>
<name>dfs.namenode.name.dir</name>
<value>file:/usr/local/hadoop-3.1.3/namenode_dir</value>
</property>
<property>
<name>dfs.datanode.data.dir</name>
<value>file:/usr/local/hadoop-3.1.3/datanode_dir</value>
</property>
<property>
<name>dfs.replication</name>
<value>3</value>
</property>
</configuration>
mapred-site.xml
vim mapred-site.xml
<configuration>
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
<property>
<name>mapreduce.jobhistory.address</name>
<value>master:10020</value>
</property>
<property>
<name>mapreduce.jobhistory.webapp.address</name>
<value>master:19888</value>
</property>
<property>
<name>mapreduce.application.classpath</name>
<value>/usr/local/hadoop-3.1.3/share/hadoop/mapreduce/lib/*,/usr/local/hadoop-3.1.3/share/hadoop/mapreduce/*</value>
</property>
</configuration>
yarn-site.xml
<configuration>
<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.nodename.vmem-pmem-ratio</name>
<value>2.7</value>
</property>
</configuration>
- 修改脚本
进入脚本文件存放目录:
cd /usr/local/hadoop-3.1.3/sbin
使用vim修改脚本文件
对于start-dfs.sh和stop-dfs.sh文件,添加下列参数(可以放在function{}之后):
HDFS_DATANODE_USER=root
HADOOP_SECURE_DN_USER=hdfs
HDFS_NAMENODE_USER=root
HDFS_SECONDARYNAMENODE_USER=root
对于start-yarn.sh和stop-yarn.sh,添加下列参数(可以放在function{}之后):
YARN_RESOURCEMANAGER_USER=root
HADOOP_SECURE_DN_USER=yarn
YARN_NODEMANAGER_USER=root
- 保存镜像
#打开一个新的终端
docker commit 容器id ubuntu/hadoopinstalled
从三个终端分别开启三个容器运行ubuntu/hadoopinstalled镜像,分别表示Hadoop集群中的master,slave01和slave02;
# 第一个终端
sudo docker run -it -h master --name master ubuntu/hadoopinstalled
# 第二个终端
sudo docker run -it -h slave01 --name slave01 ubuntu/hadoopinstalled
# 第三个终端
sudo docker run -it -h slave02 --name slave02 ubuntu/hadoopinstalled
三个终端分别打开/etc/hosts,根据各自ip修改为如下形式
vim /etc/hosts
172.17.0.4 master
172.17.0.5 slave01
172.17.0.6 slave02
在master结点测试ssh;连接到slave结点
ssh slave01
ssh slave02
exit #退出
修改master上workers文件;修改localhost
vim /usr/local/hadoop-3.1.3/etc/hadoop/workers
slave01
slave02
测试Hadoop集群
#在master上操作
cd /usr/local/hadoop-3.1.3
bin/hdfs namenode -format #首次启动Hadoop需要格式化
sbin/start-all.sh #启动所有服务
jps #分别查看三个终端
运行Hadoop实例程序
/bin/hdfs dfs -mkdir -p /user/hadoop/input
bin/hdfs dfs -put ./etc/hadoop/*.xml /user/hadoop/input
bin/hdfs dfs -ls /user/hadoop/input
执行实例并查看结果
bin/hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jar grep /user/hadoop/input output 'dfs[a-z.]+'
/bin/hdfs dfs -cat output/*
(4)踩坑总结
- javaweb项目本想自己肝,无奈太菜选择借鉴项目
- 项目构建完成后登录失败,检查后是default.conf没配,加上后重构解决
- 误关终端T T,导致在hadoop集群开启失败,删除容器后三个终端重开解决
(5)花费时间
学习3h+操作8h+博客1h=12h