租售同体的书屋项目——Docker容器化

·  阅读 408
租售同体的书屋项目——Docker容器化

一、概述

这一章就把所有的项目工程都进行容器化,方便后续进行k8s的编排。

对于docker的使用和安装,可以查看我的另一篇文章# Docker的使用&制作Golang微服务镜像并运行

二、Docker搭建所需要的基础设施mysql、redis、kafka、nginx容器

注:一般来说考虑到大存储、安全性、高性能等等问题,不建议数据库进行容器化,我这里纯粹为了方便

1.Docker部署mysql参考

2.Docker部署redis参考

3.Docker部署nginx参考

<--文件目录-->
[root@hecs-235091 nginx]# tree
.
├── conf
│   └── nginx.conf
├── logs
│   ├── access.log
│   └── error.log
└── www # 放置前端vue打包的文件
    ├── dist.zip
    ├── index.html
    └── static
        ├── css
        │   ├── app.84b8cd2a12bc1dbff045a41e8370e476.css
        │   └── app.84b8cd2a12bc1dbff045a41e8370e476.css.map
        ├── fonts
        │   ├── element-icons.535877f.woff
        │   └── element-icons.732389d.ttf
        └── js
            ├── app.6d8928826ee267ab679d.js
            ├── app.6d8928826ee267ab679d.js.map
            ├── manifest.2ae2e69a05c33dfc65f8.js
            ├── manifest.2ae2e69a05c33dfc65f8.js.map
            ├── vendor.bbccc874db8e5a606664.js
            └── vendor.bbccc874db8e5a606664.js.map
复制代码
<--创建容器命令-->
[root@hecs-235091 nginx]# docker run --name nginx -d -p 80:80 -v /home/MyProject/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/MyProject/nginx/www:/usr/share/nginx/html nginx
复制代码
<--nginx配置-->
[root@hecs-235091 nginx]# cat conf/nginx.conf 

# user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
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  600s;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
    server {
        listen       80;
        server_name  114.115.169.233;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /usr/share/nginx/html/;
            index  index.html index.htm;
        }

                location /api/ {
            proxy_pass http://114.115.169.233:8002/;
        }
        #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;
        }

        # 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;
        #}
    }
}
复制代码

4.Docker部署kafka参考

<--创建kafka虚拟网络-->
[root@hecs-235091 kafka]# docker network create --driver bridge --subnet 172.18.0.0/24 --gateway 172.18.0.1 zookeeper_kafka
复制代码
<--基于虚拟网络搭建kafka集群,使用docker compose-->
[root@hecs-235091 kafka]# cat docker-compose.yaml 
version: '3.3'

services:
  zookeeper:
    image: wurstmeister/zookeeper
    ports:
      - 2181:2181
    container_name: zookeeper
    networks:
      default:
        ipv4_address: 172.18.0.11
  kafka0:
    image: wurstmeister/kafka
    depends_on:
      - zookeeper
    container_name: kafka0
    ports:
      - 9092:9092
    environment:
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://114.115.169.233:9092
      KAFKA_LISTENERS: PLAINTEXT://:9092
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_BROKER_ID: 0
    volumes:
      - /root/data/kafka0/data:/data
      - /root/data/kafka0/log:/datalog
    networks:
      default:
        ipv4_address: 172.18.0.12
  kafka1:
    image: wurstmeister/kafka
    depends_on:
      - zookeeper
    container_name: kafka1
    ports:
      - 9093:9093
    environment:
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://114.115.169.233:9093
      KAFKA_LISTENERS: PLAINTEXT://:9093
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_BROKER_ID: 1
    volumes:
      - /root/data/kafka1/data:/data
      - /root/data/kafka1/log:/datalog
    networks:
      default:
        ipv4_address: 172.18.0.13
  kafka2:
    image: wurstmeister/kafka
    depends_on:
      - zookeeper
    container_name: kafka2
    ports:
      - 9094:9094
    environment:
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://114.115.169.233:9094
      KAFKA_LISTENERS: PLAINTEXT://:9094
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_BROKER_ID: 2
    volumes:
      - /root/data/kafka2/data:/data
      - /root/data/kafka2/log:/datalog
    networks:
      default:
        ipv4_address: 172.18.0.14
  kafka-manager:
    image: sheepkiller/kafka-manager:latest
    restart: unless-stopped
    container_name: kafka-manager
    hostname: kafka-manager
    ports:
      - "9000:9000"
    links:            # 连接本compose文件创建的container
      - kafka0
      - kafka1
      - kafka2
    external_links:   # 连接本compose文件以外的container
      - zookeeper
    environment:
      ZK_HOSTS: 114.115.169.233:2181                 ## 修改:宿主机IP
      TZ: CST-8
networks:
  default:
    external:
      name: zookeeper_kafka
复制代码

5.搭建结果展示

基础设施.png

三、项目进行容器化

<--grpc微服务使用goctl 自动生成Dockerfile文件-->
book-store@ubuntu:~/BookStore-Golang/Grpc/User$ goctl docker -go user.go 
book-store@ubuntu:~/BookStore-Golang/Grpc/Book$ goctl docker -go book.go
book-store@ubuntu:~/BookStore-Golang/Grpc/Action$ goctl docker -go action.go 
book-store@ubuntu:~/BookStore-Golang/Grpc/Order$ goctl docker -go order.go
复制代码
<--例如: User-->
FROM golang:alpine AS builder

LABEL stage=gobuilder

ENV CGO_ENABLED 0
ENV GOOS linux
ENV GOPROXY https://goproxy.cn,direct

WORKDIR /build/zero

ADD go.mod .
ADD go.sum .
RUN go mod download
COPY . .
COPY ./etc /app/etc
RUN go build -ldflags="-s -w" -o /app/user ./user.go


FROM alpine

# 自己加入如下两句命令,不然可能不能成功
RUN sed -i 's/https/http/' /etc/apk/repositories
RUN apk add curl

RUN apk update --no-cache && apk add --no-cache ca-certificates tzdata
ENV TZ Asia/Shanghai

WORKDIR /app
COPY --from=builder /app/user /app/user
COPY --from=builder /app/etc /app/etc

CMD ["./user", "-f", "etc/user.yaml"]
复制代码
<--webApi的Dockerfile也是进行仿写-->
FROM golang:alpine AS builder

LABEL stage=gobuilder

ENV CGO_ENABLED 0
ENV GOOS linux
ENV GOPROXY https://goproxy.cn,direct

WORKDIR /build/zero

ADD go.mod .
ADD go.sum .
RUN go mod download
COPY . .
COPY ./Etc /app/Etc
RUN go build -ldflags="-s -w" -o /app/bookstore ./bookstore.go


FROM alpine

RUN sed -i 's/https/http/' /etc/apk/repositories
RUN apk add curl

RUN apk update --no-cache && apk add --no-cache ca-certificates tzdata
ENV TZ Asia/Shanghai

WORKDIR /app

COPY --from=builder /app/bookstore /app/bookstore
COPY --from=builder /app/Etc /app/Etc

CMD ["./bookstore"]

复制代码
<--进行容器创建命令,用Book举例-->
root@ubuntu:/home/book-store/BookStore-Golang/Grpc/Book# docker build -t caoxunaaa/bs-book-rpc -f Dockerfile .
复制代码
<--上传到Docker Hub方便下载-->
root@ubuntu:/home/book-store/BookStore-Golang/Grpc/Book# docker push caoxunaaa/bs-book-rpc
复制代码
<--在服务器上pull镜像,并运行容器,使用-v进行配置文件的修改-->
[root@hecs-235091 kafka]# docker pull caoxunaaa/bs-book-rpc
[root@hecs-235091 kafka]# docker run -p 8081:8080 --name book-rpc -v /home/MyProject/etc_conf/etc:/app/etc -d caoxunaaa/bs-book-rpc
复制代码
<--配置文件如下,其他同理-->
[root@hecs-235091 MyProject]# cd etc_conf/
[root@hecs-235091 etc_conf]# ls
etc  Etc
[root@hecs-235091 etc_conf]# cd etc/
[root@hecs-235091 etc]# ls
action.yaml  book.yaml  order.yaml  user.yaml
[root@hecs-235091 etc]# cat book.yaml 
Name: book.rpc
ListenOn: 0.0.0.0:8080
Mysql:
  DataSource: bookstore:bookstore@tcp(114.115.169.233:3306)/bs-books?charset=utf8&parseTime=true
CacheRedis:
  - Host: 114.115.169.233:6379
    Type: node
复制代码

四、结果展示

现在你可以通过 http://114.115.169.233/ 访问我的这个项目了

整个项目.png

注:为了简单,需要nickname为书店老板才能开启热卖 网页展示.png

分类:
后端
标签:
收藏成功!
已添加到「」, 点击更改