2020系统综合实践 第4次实践作业

1.使用Docker-compose实现Tomcat+Nginx负载均衡

(1)理解反向代理原理

正向代理是代理客户端,为客户端收发请求,使真实客户端对服务器不可见;而反向代理是代理服务器端,为服务器收发请求,使真实服务器对客户端不可见。

(2)nginx代理tomcat集群,代理2个以上tomcat

编写docker-compose.yml

version: "3"
services:
  tomcat001:
    image: tomcat:8.5.0
    ports:
      - "8083:8080"
    restart: "always"
    container_name: tomcat001
    volumes:
            - ./tomcat1:/usr/local/tomcat/webapps/ROOT 
  tomcat002:
    image: tomcat:8.5.0
    ports:
      - "8082:8080"
    container_name: tomcat002
    restart: "always"
    volumes:
            - ./tomcat2:/usr/local/tomcat/webapps/ROOT
  nginx:
    image: nginx
    volumes:
      - ./nginx/default.conf:/etc/nginx/nginx.conf
    ports:
      - "81:80"
    links:
      - tomcat001:t01
      - tomcat002:t02

编写default.conf

user  nginx;
worker_processes  1;
 
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
 
 
events {
    worker_connections  1024;
}
 
 
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    access_log  /var/log/nginx/access.log  main;
 
    sendfile        on;
    #tcp_nopush     on;
 
    keepalive_timeout  65;
 
    #gzip  on;
 
    #include /etc/nginx/conf.d/*.conf;
 
    upstream tomcat_client {
         server t01:8080 ;
         server t02:8080 ;
    } 
 
    server {
        server_name "";
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;
 
        location / {
            proxy_pass http://tomcat_client;
            proxy_redirect default;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }
}

编写两个html文件(以其中一个为例)

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>tomcat</title>
</head>
<body>
    <h1>tomcat1</h1>
</body>
</html>

文件结构如下

构建

sudo docker-compose up -d --build

访问localhost:81,轮流出现两个网页

(3)了解nginx的负载均衡策略,并至少实现nginx的2种负载均衡策略

先安装一下python

sudo apt-get install python3

1.轮询策略

python文件

import requests

url="http://localhost:81"

for i in range(0,10):
        reponse=requests.get(url)
        print(reponse.text)

两者出现的次数相当

2.权重策略

python文件

import requests

url="http://localhost:81"

context={}
for i in range(0,100):
        response=requests.get(url)
        
        if response.text in context:
                context[response.text]+=1
        else:
                context[response.text]=1

print(context)

修改default.conf

  upstream tomcat_client {
         server t01:8080 weight=3;
         server t02:8080 weight=1;
}

然后重启容器

运行python文件多次,结果均为1:3

2.使用Docker-compose部署javaweb运行环境

(1)使用老师给的javaweb参考项目

进入项目对应目录,修改连接数据库的IP

cd  /home/lzz/webapps/ssmgrogshop_war/WEB-INF/classes
vim jdbc.properties

项目结构图如下

创建并启动容器服务

docker-compose up -d

在web上访问

进行数据库的增加操作,增加一名假面骑士做为旅客

进行数据库的修改操作,将黄旭林的性别改为女

进行数据库的删除操作,删除黄旭林

(2)添加nginx反向代理服务,实现负载均衡

default.conf

upstream tomcats{
        server tt1:8080 ;
        server tt2:8080 ;
        server tt3:8080 ;

}
server {
    listen 2508;
    server_name localhost;

location / {
root   /usr/share/nginx/html;
    index  index.html index.htm;
proxy_pass http://tomcats;
}

}

docker-compose.yml

version: '2'
services:
  tomcat01:
    image: tomcat
    hostname: hostname
    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

重新构建

docker-compose up -d

5050和5051都可以访问酒店管理系统

3.使用Docker搭建大数据集群环境

(1)hadoop分布式集群环境配置,至少包含三个节点(一个master,两个slave)

pull Ubuntu镜像

docker pull ubuntu

创建build文件 运行容器

docker run -it -v /home/dyssl/build:/root/build --name ubuntu ubuntu

更新系统源软件

apt-get update

安装vim

vim apt-get install vim

安装sshd

apt-get install ssh

开启sshd服务器

/etc/init.d/ssh start

在该文件中最后一行添加如下内容,实现进入Ubuntu系统时,都能自动启动sshd服务

vim ~/.bashrc
/etc/init.d/ssh start

获取密匙

ssh-keygen -t rsa

免密登入sshd

sshd cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

安装jdk

jdk apt install openjdk-8-jdk

在文件末尾添加以下两行,配置Java环境变量

vim ~/.bashrc
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/
export PATH=$PATH:$JAVA_HOME/bin

使.bashrc生效,查看java版本

source ~/.bashrc
java -version

新打开一个终端,将该容器保存为镜像

docker commit cab159840beb ubuntu/jdk

把hadoop的安装包放在~/目录下,然后执行以下代码安装hadoop

cd /root/build
tar -zxvf hadoop-3.1.3.tar.gz -C /usr/local

验证hadoop安装

cd /usr/local/hadoop-3.1.3
./bin/hadoop version

在顶部添加如下代码

Vim hadoop-env.sh
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/

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>

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>

vim mapred-site.xml

<configuration>
	<property>
		<!--使用yarn运行MapReduce程序-->
		<name>mapreduce.framework.name</name>
		<value>yarn</value>
	</property>
	<property>
		<!--jobhistory地址host:port-->
		<name>mapreduce.jobhistory.address</name>
		<value>master:10020</value>
	</property>
	<property>
		<!--jobhistory的web地址host:port-->
		<name>mapreduce.jobhistory.webapp.address</name>
		<value>master:19888</value>
	</property>
	<property>
		<!--指定MR应用程序的类路径-->
		<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>

Vim yarn-site.xml

<configuration>
        <property>
                <name>yarn.resourcemanager.hostname</name>
                <value>master</value>
        </property>
        <property>
                <name>yarn.nodemanager.aux-services</name>
                <value>mapreduce_shuffle</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

打开新的终端保存镜像

docker commit cab159840beb 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

vim /etc/hosts根据各自ip修改成如下形式

172.17.0.3      master
172.17.0.4      slave01
172.17.0.5      slave02


在master结点测试ssh;连接到slave1结点

ssh slave01

在master结点测试ssh;连接到slave2结点

ssh slave02

修改master上workers文件

vim /usr/local/hadoop-3.1.3/etc/hadoop/workers
slave01 
slave02

在master上测试hadoop集群

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门槛真的有点高,最后还是用老师给的项目,难度一下子就降下来了,按照老师写的博客来做很快就能出结果。然后最后一个实验由于之前在大数据的实践课上做过类似的,所以做起来熟悉感比较强,踩坑就很少。总的来说耗时还是比较多,参考了很多同学的博客让我实际做起来轻松了不少

5.耗时

实验一:2小时
实验二:3小时
实验三:4小时
博客编写:1.5小时
合计:10.5小时

posted @ 2020-05-17 23:42  田野飞行员  阅读(261)  评论(0编辑  收藏  举报