(1)使用Docker-compose实现Tomcat+Nginx负载均衡
建立配置文件目录
编写配置文件编写配置文件
default.conf
docker-compose.yml
index.html
Tomcat1!
Tomcat2!
Tomcat3!
test.py
运行docker-compose构建容器
验证默认的轮询策略
修改default.conf为权重策略
(2) 使用Docker-compose部署javaweb运行环境
文件目录
default.conf
docker-compose.yml
version: "3" #版本
services: #服务节点
tomcat: #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
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
networks:
webnet:
ipv4_address: 15.22.0.7
networks: #网络设置
webnet:
driver: bridge #网桥模式
ipam:
config:
-
subnet: 15.22.0.0/24 #子网
修改连接的ip和端口
ifconfig -a命令查看ip地址
在 /webapps/ssmgrogshop_war/WEB-INF/classes/jdbc.properties 修改ip和端口
启动容器
docker-compose up
登陆酒店管理系统
可进行增加客房等数据库操作
修改nginx配置文件,反向代理tomcat
upstream tomcats {
server tomcat00:5050;
server tomcat00:5051;
server tomcat00:5052;
}
修改yml文件
tomcat01:
image: tomcat
hostname: hostname
container_name: tomcat01
ports:
- "5051:8080" #后面访问网页的时候要选择对应的端口号5050
volumes: #数据卷
- "./webapps:/usr/local/tomcat/webapps"
- ./wait-for-it.sh:/wait-for-it.sh
networks: #网络设置静态IP
webnet:
ipv4_address: 15.22.0.16
tomcat02:
image: tomcat
hostname: hostname
container_name: tomcat02
ports:
- "5052:8080" #后面访问网页的时候要选择对应的端口号5050
volumes: #数据卷
- "./webapps:/usr/local/tomcat/webapps"
- ./wait-for-it.sh:/wait-for-it.sh
networks: #网络设置静态IP
webnet:
ipv4_address: 15.22.0.17
nginx:
depends_on:
- tomcat00
- tomcat01
- tomcat02
(3)使用Docker搭建大数据集群环境
1.拉取ubuntu镜像
创建目录,用于向Docker内部的Ubuntu系统传输文件
docker pull ubuntu
cd ~
mkdir build
sudo docker run -it -v /home/ubuntu/build:/root/build --name ubuntu ubuntu
创建并运行容器
2.更新软件源以及安装vim 和安装sshd
apt-get update
apt-get install vim
apt-get install ssh
/etc/init.d/ssh start
vim ~/.bashrc
#在该文件中最后一行添加如下内容
/etc/init.d/ssh start
3.配置sshd
ssh-keygen -t rsa #一直回车
cd ~/.ssh
cat id_rsa.pub >> authorized_keys
ssh localhost #测试免密登录
4.安装JDK
apt-get 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 #使.bashrc生效
java -version #验证java
5.保存镜像文件
#另开一个终端
docker commit fd1fc69d75a3 ubuntu/jdkinstalled
docker images
6.安装Hadoop
将预先下载好的压缩包放到创建的目录(容器宿主机共享目录)下以便后续安装
cd /root/build
tar -zxvf hadoop-3.1.3.tar.gz -C /usr/local #将hadoop压缩包放入本地build文件夹中
cd /usr/local/hadoop-3.1.3
./bin/hadoop version # 验证安装
7.配置Hadoop集群
hadoop-env.sh
cd /usr/local/hadoop-3.1.3/etc/hadoop
vim hadoop-env.sh
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/ #末尾加入
修改/usr/local/hadoop/etc/hadoop下的文件
-
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>
-
进入脚本目录
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
8.运行Hadoop集群
保存镜像
sudo docker commit 850572f6a724 ubuntu/hadoopinstalled
从三个终端分别开启三个容器运行ubuntu/hadoop镜像
# 第一个终端
docker run -it -h master --name master ubuntu/hadoopinstalled
# 第二个终端
docker run -it -h slave01 --name slave01 ubuntu/hadoopinstalled
# 第三个终端
docker run -it -h slave02 --name slave02 ubuntu/hadoopinstalled
三个终端分别打开/etc/hosts,根据各自ip修改为如下形式
172.17.0.5 master
172.17.0.6 slave01
172.17.0.7 slave02
在master结点测试ssh;连接到slave结点
ssh slave01
ssh slave02
exit 退出
修改master上workers文件;
vim /usr/local/hadoop-3.1.3/etc/hadoop/workers
将localhost修改为如下所示
slave01
slave02
9.测试Hadoop集群
cd /usr/local/hadoop-3.1.3
bin/hdfs namenode -format # 格式化文件系统
sbin/start-dfs.sh # 开启NameNode和DataNode服务
sbin/start-yarn.sh # 开启ResourceManager和NodeManager服务
jps # 查看服务状态
-
建立HDFS文件夹
./bin/hdfs dfs -mkdir -p /user/root/input
./bin/hdfs -put ./etc/hadoop/*.xml /user/root/input # 将xml复制到input下,作为示例程序输入
./bin/hdfs dfs -ls /user/root/input
-
执行实例程序
./bin/hadoop jar ./share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jar grep input output 'dfs[a-z.]+'
-
获取输出内容
hdfs dfs -cat output/*
(4)总结
实验花费时间:实验一2小时+实验二5小时+实验三4小时+写博客1.5小时
遇到的问题:
1.更新vim和sshd时速度贼慢,后来就去/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
2.从保存的镜像文件里重新开一个容器后,在安装hadoop-3.1.3时提示下面错误
解决办法:停止容器,重启容器,再进入容器就可以了