极客时间运维进阶训练营第二十一周作业

1、部署 mall-swarm 项目所需要的 MySQL、Redis、RabbitMQ、Nacos 等中间件

root@mall-middleware:~# mkdir mall
root@mall-middleware:~# cd mall/
root@mall-middleware:~/mall# git clone  https://github.com/macrozheng/mall-swarm.git


#####################
mkdir /apps
mv middware/ /apps/
cd /apps/middware/

root@mall-middleware:/apps/middware# cat docker-compose.yaml
version: '3'
services:
  mysql:
    image: mysql:5.7
    container_name: mysql
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root #设置root帐号密码
    ports:
      - 3306:3306
    volumes:
      - /mydata/mysql/data:/var/lib/mysql #数据文件挂载
      #- /mydata/mysql/conf:/etc/mysql #配置文件挂载
      - /mydata/mysql/log:/var/log/mysql #日志文件挂载
  redis:
    image: redis:7
    container_name: redis
    command: redis-server --appendonly yes
    volumes:
      - /mydata/redis/data:/data #数据文件挂载
    ports:
      - 6379:6379
  nginx:
    image: nginx:1.22
    container_name: nginx
    volumes:
      #- /mydata/nginx/conf:/etc/nginx #配置文件目录挂载
      - /mydata/nginx/html:/usr/share/nginx/html #静态资源根目录挂载
      - /mydata/nginx/logs:/var/log/nginx #日志文件挂载
    ports:
      - 80:80
  rabbitmq:
    image: rabbitmq:3.9.11-management
    container_name: rabbitmq
    volumes:
      - /mydata/rabbitmq/data:/var/lib/rabbitmq #数据文件挂载
    ports:
      - 5672:5672
      - 15672:15672
  elasticsearch:
    image: elasticsearch:7.17.3
    container_name: elasticsearch
    environment:
      - "cluster.name=elasticsearch" #设置集群名称为elasticsearch
      - "discovery.type=single-node" #以单一节点模式启动
      - "ES_JAVA_OPTS=-Xms512m -Xmx1024m" #设置使用jvm内存大小
    volumes:
      - /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins #插件文件挂载
      - /mydata/elasticsearch/data:/usr/share/elasticsearch/data #数据文件挂载
    ports:
      - 9200:9200
      - 9300:9300
  logstash:
    image: logstash:7.17.3
    container_name: logstash
    environment:
      - TZ=Asia/Shanghai
    volumes:
      - /mydata/logstash/logstash.conf:/usr/share/logstash/pipeline/logstash.conf #挂载logstash的配置文件
    depends_on:
      - elasticsearch #kibana在elasticsearch启动之后再启动
    links:
      - elasticsearch:es #可以用es这个域名访问elasticsearch服务
    ports:
      - 4560:4560
      - 4561:4561
      - 4562:4562
      - 4563:4563
  kibana:
    image: kibana:7.17.3
    container_name: kibana
    links:
      - elasticsearch:es #可以用es这个域名访问elasticsearch服务
    depends_on:
      - elasticsearch #kibana在elasticsearch启动之后再启动
    environment:
      - "elasticsearch.hosts=http://es:9200" #设置访问elasticsearch的地址
    ports:
      - 5601:5601
  mongo:
    image: mongo:4
    container_name: mongo
    volumes:
      - /mydata/mongo/db:/data/db #数据文件挂载
    ports:
      - 27017:27017
  minio:
    image: minio/minio
    container_name: minio
    command: server /data --console-address ":9001" #指定数据目录及console运行端口启动
    volumes:
      - /mydata/minio/data:/data #数据目录挂载
    environment:
      - "MINIO_ROOT_USER=minioadmin"
      - "MINIO_ROOT_PASSWORD=minioadmin"
    ports:
      - 9090:9000
      - 9001:9001

docker-compose  pull

mkdir -pv /mydata/elasticsearch
chmod 777  /mydata/elasticsearch -R #  或chmod 1000.1000  /mydata/elasticsearch # id 查询镜像的启动用户查出
docker-compose  up -d

cp /root/mall/mall-swarm/document/elk/logstash.conf /mydata/logstash/logstash.conf

root@mall-middleware:/mydata/logstash# cat /mydata/logstash/logstash.conf
input {
  tcp {
    mode => "server"
    host => "0.0.0.0"
    port => 4560
    codec => json_lines
    type => "debug"
  }
  tcp {
    mode => "server"
    host => "0.0.0.0"
    port => 4561
    codec => json_lines
    type => "error"
  }
  tcp {
    mode => "server"
    host => "0.0.0.0"
    port => 4562
    codec => json_lines
    type => "business"
  }
  tcp {
    mode => "server"
    host => "0.0.0.0"
    port => 4563
    codec => json_lines
    type => "record"
  }
}
filter{
  if [type] == "record" {
    mutate {
      remove_field => "port"
      remove_field => "host"
      remove_field => "@version"
    }
    json {
      source => "message"
      remove_field => ["message"]
    }
  }
}
output {
  elasticsearch {
    hosts => "es:9200"
    index => "mall-%{type}-%{+YYYY.MM.dd}"
  }
}
View Code

1.1 配置rabbitmq

 

 

 

 

 

 

 

 

 1.2 配置mysql

root@mall-middleware:/apps/middware# docker cp /root/mall/mall-swarm/document/sql/mall.sql mysql:/root/
root@mall-middleware:/apps/middware#

root@mall-middleware:/apps/middware# docker exec -it mysql sh
root@mall-middleware:/apps/middware# docker exec -it mysql sh
# ls /root
mall.sql
# mysql -uroot -proot
create database mall character set utf8;
grant all privileges on *.* to 'reader' @'%' identified by '123456';
use mall;
source /root/mall.sql

# 验证
root@k8s-master1:~# mysql -uroot -proot -h 172.31.7.118

mysql> show databases ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mall               |
View Code

1.3 es 初始化

# 安装中文分词
root@mall-middleware:/usr/local/src# wget  https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.17.3/elasticsearch-analysis-ik-7.17.3.zip

root@mall-middleware:~# mv elasticsearch-analysis-ik-7.17.3.zip  /mydata/elasticsearch/plugins/
root@mall-middleware:~# cd /mydata/elasticsearch/plugins/
root@mall-middleware:/mydata/elasticsearch/plugins# unzip elasticsearch-analysis-ik-7.17.3.zip -d analysis-ik
root@mall-middleware:/mydata/elasticsearch/plugins# mv elasticsearch-analysis-ik-7.17.3.zip /tmp/
root@mall-middleware:/mydata/elasticsearch/plugins# docker logs -f elasticsearch   # 确保无异常

1.4 minio 初始化

 

 

 

 1.5 安装nacos

root@mall-middleware:/apps/middware# docker-compose -f /apps/middware/nacos/nacos-docker/example/standalone-derby.yaml  up -d
# 打开nacos 指标暴露接口
root@mall-middleware:/apps/middware# docker exec -it nacos-standalone bash
[root@24e99e7d273b nacos]# echo "management.endpoints.web.exposure.include=*" >>  /home/nacos/conf/application.properties

[root@24e99e7d273b nacos]# exit
exit
root@mall-middleware:/apps/middware# docker restart  nacos-standalone
nacos-standalone
View Code

2、修改 mall-swarm 项目配置文件中的中间件地址为当前环境的地址

root@k8s-master1:~# mkdir mall-yaml-files
root@k8s-master1:~# cd mall-yaml-files/
root@k8s-master1:~/mall-yaml-files#  git clone  https://github.com/macrozheng/mall-swarm.git
root@k8s-master1:~/mall-yaml-files# cd mall-swarm-master/
root@k8s-master1:~/mall-yaml-files/mall-swarm-master#

# 查找localhost 字样的文件
root@k8s-master1:~/mall-yaml-files/mall-swarm-master# grep localhost ./* -R
./config/admin/mall-admin-dev.yaml:    url: jdbc:mysql://localhost:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false
./config/admin/mall-admin-dev.yaml:    host: localhost # Redis服务器地址
./config/admin/mall-admin-dev.yaml:  endpoint: http://localhost:9000 #MinIO服务所在地址
./config/admin/mall-admin-dev.yaml:  host: localhost
./config/demo/mall-demo-dev.yaml:    url: jdbc:mysql://localhost:3306/mall?useUnicode=true&char

root@k8s-master1:~/mall-yaml-files/mall-swarm-master# grep localhost /root/mall-yaml-files/mall-swarm-master/* -R

# 批量修改为中间件ip

root@k8s-master1:~/mall-yaml-files/mall-swarm-master# grep localhost /root/mall-yaml-files/mall-swarm-master/* -R|cut -d  ":"  -f 1  | xargs sed -i s'/localhost/172.31.7.118/g'

# 修改配置文件中的配置参数,确保正确,此处dev prd均设置为 172.31.7.118
root@k8s-master1:~/mall-yaml-files/mall-swarm-master/config# ls
admin  demo  gateway  portal  search

# 检查

~
root@k8s-master1:~/mall-yaml-files/mall-swarm-master/config# grep 3306 ./* -R
./admin/mall-admin-dev.yaml:    url: jdbc:mysql://172.31.7.118:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false
./admin/mall-admin-prod.yaml:    url: jdbc:mysql://172.31.7.118:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false
./demo/mall-demo-dev.yaml:    url: jdbc:mysql://172.31.7.118:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false
./demo/mall-demo-prod.yaml:    url: jdbc:mysql://172.31.7.118:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false
./portal/mall-portal-dev.yaml:    url: jdbc:mysql://172.31.7.118:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false
./portal/mall-portal-prod.yaml:    url: jdbc:mysql://172.31.7.118:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false
./search/mall-search-dev.yaml:    url: jdbc:mysql://172.31.7.118:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
./search/mall-search-prod.yaml:    url: jdbc:mysql://172.31.7.118:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=fal

root@k8s-master1:~/mall-yaml-files/mall-swarm-master/config# grep -B5 6379 ./* -R
./admin/mall-admin-dev.yaml-    username: root
./admin/mall-admin-dev.yaml-    password: root
./admin/mall-admin-dev.yaml-  redis:
./admin/mall-admin-dev.yaml-    host: 172.31.7.118 # Redis服务器地址
./admin/mall-admin-dev.yaml-    database: 0 # Redis数据库索引(默认为0)
./admin/mall-admin-dev.yaml:    port: 6379 # Redis服务器连接端口
--
./admin/mall-admin-prod.yaml-    username: root
./admin/mall-admin-prod.yaml-    password: root
./admin/mall-admin-prod.yaml-  redis:
./admin/mall-admin-prod.yaml-    host: 172.31.7.118 # Redis服务器地址
./admin/mall-admin-prod.yaml-    database: 0 # Redis数据库索引(默认为0)
./admin/mall-admin-prod.yaml:    port: 6379 # Redis服务器连接端口
--
./gateway/mall-gateway-dev.yaml-spring:
./gateway/mall-gateway-dev.yaml-  redis:
./gateway/mall-gateway-dev.yaml-    host: 172.31.7.118 # Redis服务器地址
./gateway/mall-gateway-dev.yaml-    database: 0 # Redis数据库索引(默认为0)
./gateway/mall-gateway-dev.yaml:    port: 6379 # Redis服务器连接端口
--
./gateway/mall-gateway-prod.yaml-spring:
./gateway/mall-gateway-prod.yaml-  redis:
./gateway/mall-gateway-prod.yaml-    host: 172.31.7.118 # Redis服务器地址
./gateway/mall-gateway-prod.yaml-    database: 0 # Redis数据库索引(默认为0)
./gateway/mall-gateway-prod.yaml:    port: 6379 # Redis服务器连接端口
--
./portal/mall-portal-dev.yaml-      port: 27017
./portal/mall-portal-dev.yaml-      database: mall-port
./portal/mall-portal-dev.yaml-  redis:
./portal/mall-portal-dev.yaml-    host: 172.31.7.118 # Redis服务器地址
./portal/mall-portal-dev.yaml-    database: 0 # Redis数据库索引(默认为0)
./portal/mall-portal-dev.yaml:    port: 6379 # Redis服务器连接端口
--
./portal/mall-portal-prod.yaml-      port: 27017
./portal/mall-portal-prod.yaml-      database: mall-port
./portal/mall-portal-prod.yaml-  redis:
./portal/mall-portal-prod.yaml-    host: 172.31.7.118 # Redis服务器地址
./portal/mall-portal-prod.yaml-    database: 0 # Redis数据库索引(默认为0)
./portal/mall-portal-prod.yaml:    port: 6379 # Redis服务器连接端口
# 将如下文件的api接口改为 http://mall-gateway-service:8201
mall-gateway/src/main/resources/application.yml
View Code

3、基于 maven+JDK 1.8 编译 mall-swarm 项目

# 制备编译环境
# 宿主机安装java8环境
apt install -y openjdk-8-jdk
root@k8s-master1:~/mall-yaml-files/mall-swarm-master# java -version
openjdk version "1.8.0_362"
OpenJDK Runtime Environment (build 1.8.0_362-8u362-ga-0ubuntu1~20.04.1-b09)
OpenJDK 64-Bit Server VM (build 25.362-b09, mixed mode)
# 安装maven
root@k8s-master1:/usr/local/src# tar xf apache-maven-3.6.3-bin.tar.gz
root@k8s-master1:/usr/local/src# mkdir /apps -pv
mkdir: created directory '/apps'
root@k8s-master1:/usr/local/src# mv apache-maven-3.6.3/ /apps/
root@k8s-master1:/usr/local/src# ln -s /apps/apache-maven-3.6.3/ /apps/apache-maven
echo "export PATH=/apps/apache-maven/bin/:$PATH" >> /etc/profile
source  /etc/profile

root@k8s-master1:/apps# mvn --version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)  ## 确保mvn 可以正常运行
Maven home: /apps/apache-maven
Java version: 1.8.0_362, vendor: Private Build, runtime: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "5.4.0-146-generic", arch: "amd64", family: "unix"
# 设置镜像源
oot@k8s-master1:/apps/apache-maven/conf# vim settings.xml
<mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>
</mirror>

# 导入并处理docker 镜像
docker load -i mall-java-8-image.tar.gz
docker tag java:8 harbor.iclinux.com/pub-images/java:8
docker push  harbor.iclinux.com/pub-images/java:8
root@k8s-master1:~/mall-yaml-files/mall-swarm-master# vim pom.xml
248                                 <build>
249                                     <!--定义基础镜像-->
250                                     <from>harbor.iclinux.com/pub-images/java:8</from>
251                                     <args>
252                                         <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
253                                     </args>


# 修改 k8s yaml 文件

/root/mall-yaml-files/mall-swarm-master/document/k8s
sed -i s'/192.168.3.101/172.31.7.118/g' mall-admin-deployment.yaml
sed -i s'/192.168.3.101/172.31.7.118/g' mall-gateway-deployment.yaml
sed -i s'/192.168.3.101/172.31.7.118/g' mall-portal-deployment.yaml
sed -i s'/192.168.3.101/172.31.7.118/g' mall-auth-deployment.yaml
sed -i s'/192.168.3.101/172.31.7.118/g' mall-monitor-deployment.yaml
sed -i s'/192.168.3.101/172.31.7.118/g' mall-search-deployment.yaml

root@k8s-master1:~/mall-yaml-files/mall-swarm-master/document/k8s# grep 8848 ./*
./mall-admin-deployment.yaml:              value: http://172.31.7.118:8848
./mall-admin-deployment.yaml:              value: http://172.31.7.118:8848
./mall-auth-deployment.yaml:              value: http://172.31.7.118:8848
./mall-auth-deployment.yaml:              value: http://172.31.7.118:8848
./mall-gateway-deployment.yaml:              value: http://172.31.7.118:8848
./mall-gateway-deployment.yaml:              value: http://172.31.7.118:8848
./mall-monitor-deployment.yaml:              value: http://172.31.7.118:8848
./mall-monitor-deployment.yaml:              value: http://172.31.7.118:8848
./mall-portal-deployment.yaml:              value: http://172.31.7.118:8848
./mall-portal-deployment.yaml:              value: http://172.31.7.118:8848
./mall-search-deployment.yaml:              value: http://172.31.7.118:8848
./mall-search-deployment.yaml:              value: http://172.31.7.118:8848


# 编译打包
mvn clean install package -Dmaven.test.skip=true
View Code

4、在 k8s 运行 mall-swarm 项目

root@k8s-master1:~/mall-yaml-files/mall-swarm-master/mall-admin/target/docker/mall/mall-admin/1.0-SNAPSHOT/build# cat Dockerfile
FROM harbor.iclinux.com/pub-images/java:8
MAINTAINER macrozheng
COPY maven /
ENTRYPOINT ["java", "-jar","-Dspring.profiles.active=prod","/mall-admin-1.0-SNAPSHOT.jar"]

## 镜像上传公司镜像仓库
docker tag mall/mall-auth:1.0-SNAPSHOT harbor.iclinux.com/magedu/mall-auth:1.0-SNAPSHOT
docker tag mall/mall-gateway:1.0-SNAPSHOT harbor.iclinux.com/magedu/mall-gateway:1.0-SNAPSHOT
docker tag mall/mall-monitor:1.0-SNAPSHOT harbor.iclinux.com/magedu/mall-monitor:1.0-SNAPSHOT
docker tag mall/mall-portal:1.0-SNAPSHOT harbor.iclinux.com/magedu/mall-portal:1.0-SNAPSHOT
docker tag mall/mall-search:1.0-SNAPSHOT harbor.iclinux.com/magedu/mall-search:1.0-SNAPSHOT
docker tag mall/mall-admin:1.0-SNAPSHOT harbor.iclinux.com/magedu/mall-admin:1.0-SNAPSHOT


docker push harbor.iclinux.com/magedu/mall-auth:1.0-SNAPSHOT
docker push harbor.iclinux.com/magedu/mall-gateway:1.0-SNAPSHOT
docker push harbor.iclinux.com/magedu/mall-monitor:1.0-SNAPSHOT
docker push harbor.iclinux.com/magedu/mall-portal:1.0-SNAPSHOT
docker push harbor.iclinux.com/magedu/mall-search:1.0-SNAPSHOT
docker push harbor.iclinux.com/magedu/mall-admin:1.0-SNAPSHOT

## 部署
- 逐个修改yaml文件的镜像地址
root@k8s-master1:~/mall-yaml-files/mall-swarm-master/document/k8s# find .  -name "*deployment.yaml"
./mall-admin-deployment.yaml
./mall-gateway-deployment.yaml
./mall-monitor-deployment.yaml
./mall-portal-deployment.yaml
./mall-search-deployment.yaml
./mall-auth-deployment.yaml

- 检测
root@k8s-master1:~/mall-yaml-files/mall-swarm-master/document/k8s# grep -B3 image ./*
./mall-admin-deployment.yaml-      containers:
./mall-admin-deployment.yaml-        - name: mall-admin
./mall-admin-deployment.yaml-          # 指定Docker Hub中的镜像地址
./mall-admin-deployment.yaml:          image: harbor.iclinux.com/magedu/mall-admin:1.0-SNAPSHOT
--
./mall-auth-deployment.yaml-      containers:
./mall-auth-deployment.yaml-        - name: mall-auth
./mall-auth-deployment.yaml-          # 指定Docker Hub中的镜像地址
./mall-auth-deployment.yaml:          image: harbor.iclinux.com/magedu/mall-auth:1.0-SNAPSHOT
--
./mall-gateway-deployment.yaml-      containers:
./mall-gateway-deployment.yaml-        - name: mall-gateway
./mall-gateway-deployment.yaml-          # 指定Docker Hub中的镜像地址
./mall-gateway-deployment.yaml:          image: harbor.iclinux.com/magedu/mall-gateway:1.0-SNAPSHOT
--
./mall-monitor-deployment.yaml-      containers:
./mall-monitor-deployment.yaml-        - name: mall-monitor
./mall-monitor-deployment.yaml-          # 指定Docker Hub中的镜像地址
./mall-monitor-deployment.yaml:          image: harbor.iclinux.com/magedu/mall-monitor:1.0-SNAPSHOT
--
./mall-portal-deployment.yaml-      containers:
./mall-portal-deployment.yaml-        - name: mall-portal
./mall-portal-deployment.yaml-          # 指定Docker Hub中的镜像地址
./mall-portal-deployment.yaml:          image: harbor.iclinux.com/magedu/mall-portal:1.0-SNAPSHOT
--
./mall-search-deployment.yaml-      containers:
./mall-search-deployment.yaml-        - name: mall-search
./mall-search-deployment.yaml-          # 指定Docker Hub中的镜像地址
./mall-search-deployment.yaml:          image: harbor.iclinux.com/magedu/mall-search:1.0-SNAPSHOT

- 部署
root@k8s-master1:~/mall-yaml-files/mall-swarm-master/document/k8s# kubectl  apply -f .

root@k8s-master1:~/mall-yaml-files/mall-swarm-master/document/k8s# kubectl get pods
NAME                                       READY   STATUS    RESTARTS      AGE
mall-admin-deployment-654657b78-pbs6n      1/1     Running   0             2m45s
mall-auth-deployment-56b5df8667-lfzgf      1/1     Running   0             2m45s
mall-gateway-deployment-84df795d49-79n7g   1/1     Running   0             2m45s
mall-monitor-deployment-77c455c7fd-qnwbr   1/1     Running   0             2m45s
mall-portal-deployment-d4698976c-jm66f     1/1     Running   0             2m45s
mall-search-deployment-7877b54575-wn658    1/1     Running   0             2m45s
View Code

 

 

 1 # cat mall-monitor-service.yaml
 2 apiVersion: v1
 3 kind: Service
 4 metadata:
 5   name: mall-monitor-service
 6   namespace: default
 7 spec:
 8   type: ClusterIP
 9   selector:
10     app: mall-monitor
11   ports:
12     - name: http
13       protocol: TCP
14       port: 8101
15       targetPort: 8101
16 
17 root@k8s-master1:~/mall-yaml-files/mall-swarm-master/document/k8s# vim mall-monitor-service.yaml
18 apiVersion: v1
19 kind: Service
20 metadata:
21   name: mall-monitor-service
22   namespace: default
23 spec:
24   type: NodePort
25   selector:
26     app: mall-monitor
27   ports:
28     - name: http
29       protocol: TCP
30       port: 8101
31       targetPort: 8101
32       nodePort: 38101
33 
34 root@k8s-master1:~/mall-yaml-files/mall-swarm-master/document/k8s# kubectl apply -f mall-monitor-service.yaml
35 service/mall-monitor-service configured

登录地址:http://172.31.7.111:38101/login 账号macro\123456

 

 

 

5、编译前端服务 mall-admin-web

root@k8s-master1:/apps# ln -sv /apps/node-v12.14.0-linux-x64 /apps/node
'/apps/node' -> '/apps/node-v12.14.0-linux-x64'

echo 'export PATH=/apps/node/bin/:$PATH'>>  /etc/profile

root@k8s-master1:/apps# source  /etc/profile
root@k8s-master1:/apps# npm version
{
  npm: '6.13.4',
  ares: '1.15.0',
  brotli: '1.0.7',
  cldr: '35.1',
  http_parser: '2.8.0',
  icu: '64.2',
  llhttp: '1.1.4',
  modules: '72',
  napi: '5',
  nghttp2: '1.39.2',
  node: '12.14.0',
  openssl: '1.1.1d',
  tz: '2019c',
  unicode: '12.1',
  uv: '1.33.1',
  v8: '7.7.299.13-node.16',
  zlib: '1.2.11'
}
root@k8s-master1:/apps# node -v
v12.14.0
root@k8s-master1:/apps# npx -v
6.13.4
root@k8s-master1:/apps#

mkdir mall-admin-web
cd mall-admin-web
git clone https://github.com/macrozheng/mall-admin-web.git

# 修改配置文件
root@k8s-master1:~/mall-admin-web/mall-admin-web/config# vim index.js
 16     host: '0.0.0.0', // can be overwritten by process.env.HOST

root@k8s-master1:~/mall-admin-web/mall-admin-web/config# vim dev.env.js
  BASE_API: '"https://admin-api.macrozheng.com"'

# 设置npm 源
npm config set registry https://registry.npm.taobao.org

root@k8s-master1:~/mall-admin-web/mall-admin-web/config# npm config list
; cli configs
metrics-registry = "https://registry.npm.taobao.org/"

# 安装基本环境
npm install -g npm-install-peers

# 安装nodejs sass 环境
apt install -y python2.7
ln -sfv /usr/bin/python2.7  /usr/bin/python2

SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/  npm install --unsafe-perm node-sass

npm install --unsafe-perm

# 编译
npm run dev
# 此时可以登录进行测试  http://172.31.7.101:8090/#/login    # admin\macro123
npm run build
npm run build
# 编译后文件位置: /root/mall-admin-web/mall-admin-web/dist
View Code

 

 

6、构建前端服务 mall-admin-web 镜像,并在 K8S 运行前端服务 mall-admin-web 服务

# 打包
cd /root/mall-admin-web/mall-admin-web/dist/
tar czf  mall-admin-web.tar.gz ./
cp mall-admin-web.tar.gz  /root/4.mall-admin-web-dockerfile-and-yaml/dockerfile/mall-admin-web/docker-images

# 制作nginx 镜像
root@k8s-master1:~/4.mall-admin-web-dockerfile-and-yaml/dockerfile/mall-admin-web/docker-images# cat Dockerfile
FROM ubuntu:20.04

RUN rm -rf /etc/localtime && ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && apt update && apt  install iproute2  ntpdate tcpdump telnet traceroute  gcc openssh-server lrzsz tree  openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev  iotop unzip zip make libgetopt-argvfile-perl vim -y

ADD openresty-1.21.4.1.tar.gz /usr/local/src/
RUN cd /usr/local/src/openresty-1.21.4.1/ && ./configure --prefix=/apps/openresty \
  --with-luajit \
  --with-pcre \
  --with-http_iconv_module \
  --with-http_realip_module \
  --with-http_sub_module \
  --with-http_stub_status_module  \
  --with-stream \
  --with-stream_ssl_module && make && make install

RUN mkdir /apps/openresty/nginx/conf/conf.d -pv && mkdir /data
ADD skywalking-nginx-lua-0.6.0 /data/skywalking-nginx-lua-0.6.0
ADD nginx.conf /apps/openresty/nginx/conf/nginx.conf
ADD conf.d /apps/openresty/nginx/conf/conf.d
ADD mall-admin-web.tar.gz /apps/openresty/nginx/html/



CMD ["/apps/openresty/nginx/sbin/nginx"]


# 修改nginx 配置文件
root@k8s-master1:~/4.mall-admin-web-dockerfile-and-yaml/dockerfile/mall-admin-web/docker-images# vim conf.d/myserver.con
    server {
        listen 80;
        server_name  mall.magedu.com;

# 制作镜像并上传仓库
root@k8s-master1:~/4.mall-admin-web-dockerfile-and-yaml/dockerfile/mall-admin-web/docker-images# cat build-command.sh
#!/bin/bash
docker build -t harbor.iclinux.com/magedu/mall-admin-web:v1 .

docker push harbor.iclinux.com/magedu/mall-admin-web:v1
root@k8s-master1:~/4.mall-admin-web-dockerfile-and-yaml/dockerfile/mall-admin-web/docker-images# bash build-command.sh

# 验证镜像

root@k8s-master1:~/4.mall-admin-web-dockerfile-and-yaml/dockerfile/mall-admin-web/docker-images# docker run  -it --rm -p80:80  harbor.iclinux.com/magedu/mall-admin-web:v1


root@k8s-master1:~/4.mall-admin-web-dockerfile-and-yaml/yaml# cat mall-admin-web-deployment.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    app: magedu-mall-web-admin-deployment-label
  name: magedu-mall-web-admin-deployment
  #namespace: magedu
spec:
  replicas: 1
  selector:
    matchLabels:
      app: magedu-mall-web-admin-selector
  template:
    metadata:
      labels:
        app: magedu-mall-web-admin-selector
    spec:
      containers:
      - name: magedu-mall-web-admin-container
        #image: registry.cn-hangzhou.aliyuncs.com/zhangshijie/mall-admin-web:v1-official-api
        image: harbor.iclinux.com/magedu/mall-admin-web:v1
        imagePullPolicy: Always
        ports:
        - containerPort: 80
          protocol: TCP
          name: http
        - containerPort: 443
          protocol: TCP
          name: https
        env:
        - name: "password"
          value: "123456"
        - name: "age"
          value: "20"
        resources:
          limits:
            cpu: 500m
            memory: 512Mi
          requests:
            cpu: 500m
            memory: 256Mi
root@k8s-master1:~/4.mall-admin-web-dockerfile-and-yaml/yaml# cat mall-admin-web-service.yaml
---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: magedu-mall-web-admin-service-label
  name: magedu-mall-web-admin-service
  #namespace: magedu
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 32080
  - name: https
    port: 443
    protocol: TCP
    targetPort: 443
    nodePort: 32091
  selector:
    app: magedu-mall-web-admin-selector
root@k8s-master1:~/4.mall-admin-web-dockerfile-and-yaml/yaml# kubectl  apply -f .
deployment.apps/magedu-mall-web-admin-deployment created
service/magedu-mall-web-admin-service created

root@k8s-master1:~/4.mall-admin-web-dockerfile-and-yaml/yaml# kubectl  get pod
NAME                                               READY   STATUS    RESTARTS      AGE
magedu-mall-web-admin-deployment-fc889cd5f-jbv9p   1/1     Running   0             2m4s
mall-admin-deployment-654657b78-pbs6n              1/1     Running   0             9h
mall-auth-deployment-56b5df8667-lfzgf              1/1     Running   0             9h
mall-gateway-deployment-84df795d49-79n7g           1/1     Running   0             9h
mall-monitor-deployment-77c455c7fd-qnwbr           1/1     Running   0             9h
mall-portal-deployment-d4698976c-jm66f             1/1     Running   0             9h
mall-search-deployment-7877b54575-wn658            1/1     Running   0             9h
net-test1                                          1/1     Running   7 (15d ago)   29d
tomcat-deployment-6786cbbb46-26dpd                 1/1     Running   0             3d12h
root@k8s-master1:~/4.mall-admin-web-dockerfile-and-yaml/yaml# kubectl  get svc
NAME                            TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
kubernetes                      ClusterIP   10.100.0.1       <none>        443/TCP                      29d
magedu-mall-web-admin-service   NodePort    10.100.148.186   <none>        80:32080/TCP,443:32091/TCP   2m12s
mall-admin-service              ClusterIP   10.100.87.147    <none>        8080/TCP                     9h
mall-auth-service               ClusterIP   10.100.221.42    <none>        8401/TCP                     9h
mall-gateway-service            NodePort    10.100.128.230   <none>        8201:30201/TCP               9h
mall-monitor-service            NodePort    10.100.55.223    <none>        8101:38101/TCP               9h
mall-portal-service             ClusterIP   10.100.174.164   <none>        8085/TCP                     9h
mall-search-service             ClusterIP   10.100.39.158    <none>        8081/TCP                     9h
tomcat-service                  NodePort    10.100.62.16     <none>        80:31080/TCP                 3d12h

# 修改haproxy
# vim /etc/haproxy/haproxy.cfg
listen magedu-wordpress-80
 bind 172.31.7.189:80
 mode tcp
 #balance leastconn
 server 172.31.7.111 172.31.7.111:32080 check inter 2000 fall 3 rise 5
# systemctl restart haproxy.service
# 设置访问 dns解析
172.31.7.189 mall.magedu.com

# 查看openresty 日志
root@k8s-master1:~/4.mall-admin-web-dockerfile-and-yaml/yaml# kubectl exec -it  magedu-mall-web-admin-deployment-fc889cd5f-jbv9p -- bash
root@magedu-mall-web-admin-deployment-fc889cd5f-jbv9p:/# tail -f /apps/openresty/nginx/logs/*.log
View Code

 

posted @ 2023-03-31 17:48  john221100  阅读(31)  评论(0编辑  收藏  举报