You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

437 lines
13 KiB

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<h1><center>容器云实战</h1></center>
**作者:行癫(盗版必究)**
------
## 一:环境介绍
应用商城Mall重新实现全容器化部署原应用商城系统各模块功能及系统架构如下
| 模块 | 使用技术 | 备注 |
| :---------------: | :------: | :----------------------------------------------------: |
| mall.sql | MySQL | 网站的数据库 |
| dist目录 | Nginx | 网站的前端项目 |
| mall-shopping | web项目 | 8081端口商品/购物车/首页渲染等交互 |
| mall-user | | 8082端口提供用户相关的交互如登录、注册、个人中心等 |
| user-provider | 后端服务 | 提供用户相关服务 |
| shopping-provider | | 提供购物车、推荐商品、商品等服务 |
<img src="https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20220811223627960.png" alt="image-20220811223627960" style="zoom:50%;" />
## 二:项目部署
#### 1.数据库镜像
制作数据库MySQL镜像镜像名为mall-mysql:v1.1设置数据库支持UTF-8编码设定root用户的密码为123456并设置服务开机自启
```shell
[root@xingdian ~]# docker pull mariadb:latest
[root@xingdian ~]# docekr tag mariadb:latest chinaskill:mariadb:v1.1
```
#### 2.redis镜像
制作Redis镜像镜像名为chinaskill-redis:v1.1使用centos:centos7.9.2009基础镜像安装Redis服务设置Redis免密并关闭保护模式开放端口6379并设置服务开机自启
```shell
[root@xingdian redis]# ls
Dockerfile redis-4.0.4 redis-4.0.4.tar.gz redis.conf
```
redis下载地址https://download.redis.io/releases/redis-4.0.4.tar.gz
```shell
[root@xingdian redis]# wget https://download.redis.io/releases/redis-4.0.4.tar.gz
```
准备redis配置文件
```shell
[root@xingdian redis]# git clone https://gitee.com/xingdiancloud/redids-config.git
```
编写Dockerfile文件
```shell
[root@xingdian redis]# cat Dockerfile
FROM centos:centos7.5.1804
MAINTAINER Chinaskill
RUN yum -y install gcc make
ADD redis-4.0.4.tar.gz /opt/
RUN mv /opt/redis-4.0.4 /opt/redis
WORKDIR /opt/redis
RUN make && make install
RUN rm -rf /opt/redis/redis.conf
COPY redis.conf /opt/redis
EXPOSE 6379
CMD ["redis-server","/opt/redis/redis.conf"]
```
构建镜像
```shell
[root@xingdian redis]# docker build -t chinaskill-redis:v1.1 .
```
#### 3.zookeeper镜像
编写Dockerfile制作ZooKeeper镜像镜像名为chinaskill-zookeeper:v1.1使用centos:7.9.2009基础镜像安装ZooKeeper服务开放端口2181并设置服务开机自启
```shell
[root@xingdian zookeeper]# ls
apache-zookeeper-3.6.3-bin Dockerfile jdk-8u211-linux-x64.tar.gz
apache-zookeeper-3.6.3-bin.tar.gz jdk1.8.0_211
```
zookeeper下载地址https://dlcdn.apache.org/zookeeper/zookeeper-3.6.3/apache-zookeeper-3.6.3-bin.tar.gz
```shell
[root@xingdian zookeeper]# wget https://dlcdn.apache.org/zookeeper/zookeeper-3.6.3/apache-zookeeper-3.6.3-bin.tar.gz --no-check-certificate
```
JDK下载地址https://share.weiyun.com/LHhEjTmG
编写Dockerfile文件
```shell
[root@xingdian zookeeper]# cat Dockerfile
FROM centos:centos7.5.1804
MAINTAINER Chinaskill
ADD jdk-8u211-linux-x64.tar.gz /usr/local
ADD apache-zookeeper-3.6.3-bin.tar.gz /usr/local/
RUN yum -y install vim net-tools telnet tree git wget
WORKDIR /usr/local
ENV JAVA_HOME=/usr/local/jdk1.8.0_211
ENV PATH=$JAVA_HOME/bin:$PATH
ENV ZOOKEEPER_HOME=/usr/local/apache-zookeeper-3.6.3-bin
RUN cp $ZOOKEEPER_HOME/conf/zoo_sample.cfg $ZOOKEEPER_HOME/conf/zoo.cfg
EXPOSE 2181
CMD $ZOOKEEPER_HOME/bin/zkServer.sh start-foreground
```
构建镜像
```shell
[root@xingdian zookeeper]# docker build -t chinaskill-zookeeper:v1.1 .
```
4.kafka镜像
编写Dockerfile制作Kafka镜像镜像名为chinaskill-kafka:v1.1使用centos:centos7.9.2009基础镜像安装Kafka服务开放端口9092并设置服务开机自启
```shell
[root@nfs-harbor kafka]# ls
Dockerfile jdk-8u211-linux-x64.tar.gz kafka_2.11-2.1.0.tgz start.sh
```
kafka下载地址https://archive.apache.org/dist/kafka/2.1.0/kafka_2.11-2.1.0.tgz
```shell
[root@xingdian kafka]# wget https://archive.apache.org/dist/kafka/2.1.0/kafka_2.11-2.1.0.tgz
```
JDK下载地址https://share.weiyun.com/LHhEjTmG
编写Dockerfile文件
```shell
[root@nfs-harbor kafka]# cat start.sh
$KAFKA_HOME/bin/zookeeper-server-start.sh $KAFKA_HOME/config/zookeeper.properties &
sleep 20
$KAFKA_HOME/bin/kafka-server-start.sh $KAFKA_HOME/config/server.properties
```
```shell
[root@nfs-harbor kafka]# cat Dockerfile
FROM centos:7
ADD jdk-8u211-linux-x64.tar.gz /usr/local
ADD kafka_2.11-2.0.0.tgz /usr/local
ENV JAVA_HOME=/usr/local/jdk1.8.0_211
ENV PATH=$PATH:$JAVA_HOME/bin
ENV KAFKA_HOME=/usr/local/kafka_2.11-2.0.0
EXPOSE 9092
COPY start.sh /
CMD ["sh","/start.sh"]
```
构建镜像
```shell
[root@xingdian kafka]# docker build -t chinaskill-kafka:v1.1 .
```
#### 4.nginx镜像
```shell
[root@xingdian nginx]# ls
default.conf nginx-1.22.0
dist.tar.gz nginx-1.22.0.tar.gz
Dockerfile nginx.conf
gpmall-shopping-0.0.1-SNAPSHOT.jar shopping-provider-0.0.1-SNAPSHOT.jar
gpmall-user-0.0.1-SNAPSHOT.jar user-provider-0.0.1-SNAPSHOT.jar
jdk-8u211-linux-x64.tar.gz
```
nginx下载地址https://nginx.org/download/nginx-1.22.0.tar.gz
JDK下载地址https://share.weiyun.com/LHhEjTmG
```shell
[root@xingdian nginx]# wget https://nginx.org/download/nginx-1.22.0.tar.gz
```
nginx配置文件
```shell
[root@nfs-harbor nginx]# cat nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /opt/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /user {
proxy_pass http://127.0.0.1:8082;
}
location /shopping {
proxy_pass http://127.0.0.1:8081;
}
location /cashier {
proxy_pass http://127.0.0.1:8083;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
```
编写Dockerfile文件
```shell
[root@xingdian nginx]# cat start.sh
java -jar /opt/nginx/html/shopping-provider-0.0.1-SNAPSHOT.jar &
sleep 20
java -jar /opt/nginx/html/user-provider-0.0.1-SNAPSHOT.jar &
sleep 20
java -jar /opt/nginx/html/gpmall-shopping-0.0.1-SNAPSHOT.jar &
sleep 20
java -jar /opt/nginx/html/gpmall-user-0.0.1-SNAPSHOT.jar
```
```shell
[root@xingdian nginx]# cat Dockerfile
FROM centos:centos7.5.1804
MAINTAINER Chinaskill
ENV TZ=Asia/Shanghai
ENV LANG=en_US.UTF-8
ENV LANGUAGE=en_US:en
ENV LC_ALL=en_US.UTF-8
ADD jdk-8u211-linux-x64.tar.gz /usr/local
WORKDIR /usr/local
ENV JAVA_HOME=/usr/local/jdk1.8.0_211
RUN yum -y install gcc openssl openssl-devel pcre-devel zlib-devel make
ADD nginx-1.22.0.tar.gz /opt
WORKDIR /opt/nginx-1.22.0
RUN ./configure --prefix=/opt/nginx
RUN make && make install
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/nginx/sbin:$JAVA_HOME/bin
EXPOSE 80 443
ADD dist.tar.gz /opt/nginx/html
COPY gpmall-shopping-0.0.1-SNAPSHOT.jar /opt/nginx/html
COPY gpmall-user-0.0.1-SNAPSHOT.jar /opt/nginx/html
COPY shopping-provider-0.0.1-SNAPSHOT.jar /opt/nginx/html
COPY user-provider-0.0.1-SNAPSHOT.jar /opt/nginx/html
COPY nginx.conf /opt/nginx/conf/
RUN chmod a+x /start.sh
#RUN nohup java -jar /opt/nginx/html/shopping-provider-0.0.1-SNAPSHOT.jar &
#RUN nohup java -jar /opt/nginx/html/user-provider-0.0.1-SNAPSHOT.jar &
#RUN nohup java -jar /opt/nginx/html/gpmall-shopping-0.0.1-SNAPSHOT.jar &
#RUN nohup java -jar /opt/nginx/html/gpmall-user-0.0.1-SNAPSHOT.jar &
CMD /opt/nginx/sbin/nginx && /start.sh
```
镜像构建
```shell
[root@xingdian nginx]# docker build -t chinaskill-nginx:v1.1 .
```
#### 5.容器互联
编写docker-compose
```shell
[root@xingdian cloud]# cat docker-compose.yml
version: "3"
services:
mysql.mall:
container_name: mall-mysql
image: chinaskill-mariadb:v1.1
environment:
MARIADB_ROOT_PASSWORD: 123456
restart: always
hostname: mysql.mall
redis.mall:
container_name: mall-redis
image: chinaskill-redis:v1.1
restart: always
hostname: redis.mall
zookeeper.mall:
container_name: mall-zookeeper
image: chinaskill-zookeeper:v1.1
restart: always
hostname: zookeeper.mall
kafka.mall:
depends_on:
- zookeeper.mall
container_name: mall-kafka
image: chinaskill-kafka:v1.1
restart: always
hostname: kafka.mall
mall:
container_name: mall-nginx
image: chinaskill-nginx:v1.1
links:
- mysql.mall
- redis.mall
- zookeeper.mall
- kafka.mall
ports:
- "83:80"
```
启动
```shell
[root@xingdian cloud]# docker-compose up -d
```
查看
```shell
[root@xingdian cloud]# docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------------------
mall-kafka sh /a.sh Up 9092/tcp
mall-mysql docker-entrypoint.sh mariadbd Up 3306/tcp
mall-nginx /bin/sh -c /opt/nginx/sbin ... Up 443/tcp, 0.0.0.0:83->80/tcp,::
:83->80/tcp
mall-redis redis-server /opt/redis/re ... Up 6379/tcp
mall-zookeeper /bin/sh -c $ZOOKEEPER_HOME ... Up 2181/tcp
```
#### 6.浏览器访问
![image-20220820140224682](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20220820140224682.png)