Docker--实战部署

  1. Redis集群部署实战:
    1. #1.创建redis网卡
      [root@localhost ~]# docker network create redis --subnet 172.38.0.0/16
      0f4c6f7177af264f852d832b273057fc0b5a97a5e4c951ea369aeaa2c49274cf
      [root@localhost ~]# docker network ls
      NETWORK ID     NAME      DRIVER    SCOPE
      64c5491595d9   bridge    bridge    local
      731c3469a28b   host      host      local
      05487626ee18   mynet     bridge    local
      d6d43b6796b4   none      null      local
      0f4c6f7177af   redis     bridge    local
      
      #2.创建6个redis的配置

      for port in $(seq 1 6); \
      do \
      mkdir -p /mydata/redis/node-${port}/conf
      touch /mydata/redis/node-${port}/conf/redis.conf
      cat << EOF >/mydata/redis/node-${port}/conf/redis.conf
      port 6379
      bind 0.0.0.0
      cluster-enabled yes
      cluster-config-file nodes.conf
      cluster-node-timeout 5000
      cluster-announce-ip 172.38.0.1${port}
      cluster-announce-port 6379
      cluster-announce-bus-port 16379
      appendonly yes
      EOF
      done

       
       

       #3.运行redis:

      for port in $(seq 1 6); \
      do \
      docker run -p 637${port}:6379 -p 1637${port}:16379 --name redis-${port} \
      -v /mydata/redis/node-${port}/data:/data \
      -v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
      -d --net redis --ip 172.38.0.1${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf; \
      done

      docker run -p 6371:6379 -p 16371:16379 --name redis-1 \
      -v /mydata/redis/node-1/data:/data \
      -v /mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf \
      -d --net redis --ip 172.38.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

      验证:docker ps 是否全部启动

      #4.进入集群交互模式,创建redis集群

      进入node-1容器内部:docker exec -it redis-1 /bin/sh

      创建集群:redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38.0.15:6379 172.38.0.16:6379 --cluster-replicas 1

       

       

      #5.停掉node-1容器,仍然可以获得我们在node-1创建的值,且会有从机代替node-1主机的位置

  2.  SpringBoot微服务打包成Docker镜像:

    1.  

       

       

       

    2. [root@localhost idea]# ls
      Dockerfile  springboot14-0.0.1-SNAPSHOT.jar
      [root@localhost idea]# docker build -f Dockerfile -t springboot001.jar .
      Sending build context to Docker daemon  17.49MB
      Step 1/5 : FROM java:8
      8: Pulling from library/java
      5040bd298390: Pull complete 
      fce5728aad85: Pull complete 
      76610ec20bf5: Pull complete 
      60170fec2151: Pull complete 
      e98f73de8f0d: Pull complete 
      11f7af24ed9c: Pull complete 
      49e2d6393f32: Pull complete 
      bb9cdec9c7f3: Pull complete 
      Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
      Status: Downloaded newer image for java:8
       ---> d23bdf5b1b1b
      Step 2/5 : COPY *.jar /app.jar
       ---> 2675b815e101
      Step 3/5 : CMD ["--server.port=8080"]
       ---> Running in 5e89601e49f9
      Removing intermediate container 5e89601e49f9
       ---> 07db2ad65be6
      Step 4/5 : EXPOSE 8080
       ---> Running in 1e48bc4fc6a9
      Removing intermediate container 1e48bc4fc6a9
       ---> 5d00b902d35d
      Step 5/5 : ENTRYPOINT ["java","-jar","/app.jar"]
       ---> Running in bfc9a8ed0a79
      Removing intermediate container bfc9a8ed0a79
       ---> e0e19ba62db0
      Successfully built e0e19ba62db0
      Successfully tagged springboot001.jar:latest
      [root@localhost idea]# docker images
      REPOSITORY                                     TAG                IMAGE ID       CREATED         SIZE
      springboot001.jar                              latest             e0e19ba62db0   4 seconds ago   661MB
      18348950824/tomcat                             latest             3f018185ca85   19 hours ago    701MB
      mytomcat                                       latest             3f018185ca85   19 hours ago    701MB
      registry.cn-beijing.aliyuncs.com/zzh_ylm/zzh   1.0                3f018185ca85   19 hours ago    701MB
      centos-cmd-test                                1.0                f27678c32a28   26 hours ago    231MB
      centos-entrypoint-test                         1.0                81b9631193e0   26 hours ago    231MB
      mycentos                                       1.0                b307dcdb775f   37 hours ago    318MB
      zzh/centos                                     1.0                43a9cd4aedda   43 hours ago    231MB
      tomcat01                                       1.0                bed16098b340   47 hours ago    684MB
      tomcat                                         latest             c662ee449a7e   5 days ago      680MB
      nginx                                          latest             f8f4ffc8092c   6 days ago      133MB
      mysql                                          5.7                9f35042c6a98   6 days ago      448MB
      centos                                         latest             5d0da3dc9764   2 weeks ago     231MB
      portainer/portainer                            latest             580c0e4e98b0   6 months ago    79.1MB
      redis                                          5.0.9-alpine3.11   3661c84ee9d0   17 months ago   29.8MB
      elasticsearch                                  latest             5acf0e8da90b   3 years ago     486MB
      java                                           8                  d23bdf5b1b1b   4 years ago     643MB
      [root@localhost idea]# docker run -d -p 8080:8080 --name springboot springboot001.jar
      38d9b41bab2bb8903541d0d3f7658a142fec5fb888244d5cb6d3b2563f467832
      [root@localhost idea]# docker ps
      CONTAINER ID   IMAGE               COMMAND                  CREATED         STATUS         PORTS                                       NAMES
      38d9b41bab2b   springboot001.jar   "java -jar /app.jar …"   5 seconds ago   Up 4 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   springboot
      [root@localhost idea]# 

       

        

posted @ 2021-10-05 11:26  张紫韩  阅读(66)  评论(0编辑  收藏  举报