一、单集群下安装
1、安装nginx
# 1、拉取镜像
docker pull nginx
# 2、查看镜像是否下载成功
docker images
# 3、启动
#-d 后台运行
#--name 别名
#-p 服务器外网端口对应容器内的端口号
docker run -d --name nginx01 -p 3344:80 nginx
# 4、本机查看状态
curl localhost:3344
# 5、进入容器
docker exec -it nginx01 /bin/bash
# 6、查看配置文件
whereis nginx
cd /etc/nginx/
2、端口暴露的概念
3、安装Tomcat
官方使用:docker run -it --rm tomcat:9.0
该写法用完就删,测试时可以这么用。
推荐使用:
# 下载Tomcat
docker pull tomcat:9.0
# 运行Tomcat
docker run -d -p 3355:8080 --name tomcat01 tomcat
问题:linux命令少了,没有webapp。镜像原因,默认是最小镜像,不必要的都剔除掉。保证最小可运行的环境。我们以后要部署项目,如果每次都要进入容器十分麻烦,可以在容器外部提供一个映射路径,webapps,我们在外部放置项目,就自动同步到内部就好了。
4、安装es
问题:暴露的端口很多!十分的耗内存,数据一般需要防止在安全目录!挂载!
#下载镜像
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2
#查看cpu状态
docker stats
#增加内存限制:修改配置文件 -e 环境配置修改
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.6.2
注意:启动之后服务器直接卡了,需要增加内存限制
5、安装portainer图形化管理面板
docker 图形化界面管理工具,提供后台面板供我们操作。
docker run -d -p 8088:9000 --name portainer01 --restart=always -v "/var/run/docker.sock:/var/run/docker.sock" --privileged=true portainer/portainer
浏览器访问公网ip+端口号:
选择本地docker链接:
6、安装MySQL(使用数据卷)
#下载MySQL5.7
docker search mysql
docker pull mysql:5.7
#运行容器+容器挂载
#官方:$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
#-v:数据挂载;-e:配置环境
docker run -d -p 3310:3306 -v /test/mysql01/conf:/etc/mysql/conf.d -v /test/mysql01/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root123 --name mysq01 mysql:5.7
可以使用数据库链接工具进行链接。主机为服务器外网Ip,用户名默认root,密码为刚才设置的密码。
7、springboot微服务打包docker镜像
- 构建springboot项目
- 打包应用
maven 中 clean packge - 编写dockerfile
idea软件下载docker插件
FROM java:8
COPY *.jar /xiaoyu.jar
CMD ["----SERVER.PORT=8081"]
EXPOSE 8081
ENTRYPOINT ["java","-jar","/xiaoyu.jar"]
- 构建镜像
docker build -f Dockerfile -t xiaoyushoping .
- 发布运行
docker run -d -P --name xiaoyushopping xiaoyushoping
- 测试访问
二、集群部署
1、redis
#创建网卡
docker network create redis --subnet 172.38.0.0/16
#通过脚本创建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
#启动redis 模板
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
#启动 redis-1
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
#启动 redis-2
docker run -p 6372:6379 -p 16372:16379 --name redis-2 \
-v /mydata/redis/node-2/data:/data \
-v /mydata/redis/node-2/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.12 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
#启动 redis-3
docker run -p 6373:6379 -p 16373:16379 --name redis-3 \
-v /mydata/redis/node-3/data:/data \
-v /mydata/redis/node-3/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.13 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
#启动 redis-4
docker run -p 6374:6379 -p 16374:16379 --name redis-4 \
-v /mydata/redis/node-4/data:/data \
-v /mydata/redis/node-4/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.14 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
#启动 redis-5
docker run -p 6375:6379 -p 16375:16379 --name redis-5 \
-v /mydata/redis/node-5/data:/data \
-v /mydata/redis/node-5/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.15 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
#启动 redis-6
docker run -p 6376:6379 -p 16376:16379 --name redis-6 \
-v /mydata/redis/node-6/data:/data \
-v /mydata/redis/node-6/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.16 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
#进入集群
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
创建集群:
/data # 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
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 172.38.0.15:6379 to 172.38.0.11:6379
Adding replica 172.38.0.16:6379 to 172.38.0.12:6379
Adding replica 172.38.0.14:6379 to 172.38.0.13:6379
M: 36807176c8267a45c50fbee9c0c87f7d63333c1d 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
M: 05773135c4572b7cee2927863212fff34558ed17 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
M: 68f105a58861cfe2ac2ecc02af7db84f8cc4f626 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
S: f9fab9e9136467374866f40a81df1aeab5d8d24a 172.38.0.14:6379
replicates 68f105a58861cfe2ac2ecc02af7db84f8cc4f626
S: debd289edf9d94d86fe14d18c66af3fc46143145 172.38.0.15:6379
replicates 36807176c8267a45c50fbee9c0c87f7d63333c1d
S: 316c6f897da28f5438b05d817f23ca9a57f13c35 172.38.0.16:6379
replicates 05773135c4572b7cee2927863212fff34558ed17
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
...
>>> Performing Cluster Check (using node 172.38.0.11:6379)
M: 36807176c8267a45c50fbee9c0c87f7d63333c1d 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 05773135c4572b7cee2927863212fff34558ed17 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: f9fab9e9136467374866f40a81df1aeab5d8d24a 172.38.0.14:6379
slots: (0 slots) slave
replicates 68f105a58861cfe2ac2ecc02af7db84f8cc4f626
S: debd289edf9d94d86fe14d18c66af3fc46143145 172.38.0.15:6379
slots: (0 slots) slave
replicates 36807176c8267a45c50fbee9c0c87f7d63333c1d
M: 68f105a58861cfe2ac2ecc02af7db84f8cc4f626 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 316c6f897da28f5438b05d817f23ca9a57f13c35 172.38.0.16:6379
slots: (0 slots) slave
replicates 05773135c4572b7cee2927863212fff34558ed17
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
测试集群:
/data # redis-cli -c
127.0.0.1:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:299
cluster_stats_messages_pong_sent:293
cluster_stats_messages_sent:592
cluster_stats_messages_ping_received:288
cluster_stats_messages_pong_received:299
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:592
127.0.0.1:6379> cluster nodes
36807176c8267a45c50fbee9c0c87f7d63333c1d 172.38.0.11:6379@16379 myself,master - 0 1657607669000 1 connected 0-5460
05773135c4572b7cee2927863212fff34558ed17 172.38.0.12:6379@16379 master - 0 1657607670354 2 connected 5461-10922
f9fab9e9136467374866f40a81df1aeab5d8d24a 172.38.0.14:6379@16379 slave 68f105a58861cfe2ac2ecc02af7db84f8cc4f626 0 1657607671557 4 connected
debd289edf9d94d86fe14d18c66af3fc46143145 172.38.0.15:6379@16379 slave 36807176c8267a45c50fbee9c0c87f7d63333c1d 0 1657607671357 5 connected
68f105a58861cfe2ac2ecc02af7db84f8cc4f626 172.38.0.13:6379@16379 master - 0 1657607670554 3 connected 10923-16383
316c6f897da28f5438b05d817f23ca9a57f13c35 172.38.0.16:6379@16379 slave 05773135c4572b7cee2927863212fff34558ed17 0 1657607670000 6 connected
127.0.0.1:6379> set 1 3
-> Redirected to slot [9842] located at 172.38.0.12:6379
OK
#测试停掉redis-2,是否有从节点接替主节点
docker stop redis-2
#测试成功!此时redis-6已接替挂掉的redis-2
/data # redis-cli -c
127.0.0.1:6379> get 1
-> Redirected to slot [9842] located at 172.38.0.16:6379
"3"