Docker 进阶,Docker 集群部署

Docker Compose

DockerFile build run 手动操作,单个容器!

如果是微服务,100个微服务!各种依赖关系

  • Docker Compose 可以轻松高效的管理容器,定义运行多个容器 == 批量容器编排
步骤: 1、DockerFile 保证我们的项目在任何地方可以运行 2、service 什么是服务; docker-compose.yml 文件怎么写? 3、启动项目 up / down 

Compose 是 Docker 官方的开源项目,需要安装!

Dockerfile 让程序运行在任何地方,web 服务,redis,mysql,nginx…多个容器

docker-compose.yml

version: "3.9"  services: web:  build: . ports: - "8000:5000" volumes: - .:/code - logvolume01:/var/log links: - redis redis: image: redis  volumes: logvolume01: {} 

docker-compose up 100 个微服务,一建上线

Compose 重要概念

  • 服务 service (web、redis、mysql …)

  • 项目 project = 一组关联的容器 = 对外的业务单元

DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker} mkdir -p $DOCKER_CONFIG/cli-plugins curl -SL https://github.com/docker/compose/releases/download/v2.4.1/docker-compose-linux-x86_64 -o $DOCKER_CONFIG/cli-plugins/docker-compose  chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose 
import time import redis from flask import Flask app = Flask(__name__) cache = redis.Redis(host='redis', port=6379) def get_hit_count(): retries = 5 while True: try: return cache.incr('hits') except redis.exceptions.ConnectionError as exc: if retries == 0: raise exc retries -= 1 time.sleep(0.5) @app.route('/') def hello(): count = get_hit_count() return 'Hello World! I have been seen {} times.\n'.format(count)  flask redis 
# syntax=docker/dockerfile:1 FROM python:3.7-alpine WORKDIR /code ENV FLASK_APP=app.py ENV FLASK_RUN_HOST=0.0.0.0 RUN apk add --no-cache gcc musl-dev linux-headers COPY requirements.txt requirements.txt RUN pip install -r requirements.txt EXPOSE 5000 COPY . . CMD ["flask", "run"] 
  • 定义整个服务,需要的环境 web redis) , 完整的上线服务
version: "3.9" services: web: build: . ports: - "8000:5000" redis: image: "redis:alpine" 
docker compose up .  docker compose down   1、创建网络 2、执行 docker-compose.yml 3、启动服务 ⠿ Network composetest_default Created ⠿ Container composetest-redis-1 Created ⠿ Container composetest-web-1 Created 
  • docker-compose.yaml 是核心
 version: ''  services:  服务1: web  images: build: network: ... 服务2: redis ... 服务3: mysql ... 服务4: ...  volumes: networks: configs: 
version: "3.9" services: db:  image: mysql:5.7 volumes: - db_data:/var/lib/mysql  restart: always environment: MYSQL_ROOT_PASSWORD: somewordpress MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress wordpress:  depends_on:  - db image: wordpress:latest volumes: - wordpress_data:/var/www/html  ports: - "8000:80" restart: always environment: WORDPRESS_DB_HOST: db WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress WORDPRESS_DB_NAME: wordpress volumes:  db_data: {} wordpress_data: {} 

Docker Swarm

集群的方式部署,环境需要 4 台 2v4g 的云服务器

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CeoNfhcI-1651712847192)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\image-20220428215748278.png)]

  • 四台服务器搭建好 docker 环境后启动 docker !
docker swarm --help  docker swarm init --advertise-addr 172.16.0.4  docker swarm join --token SWMTKN-1-6a8k9vzo6vlnlq6wrzeolrcznkiwfxz151g4bjo2t8g8dla27u-by93wkc6oka6nxbic09rxz6yx 172.16.0.4:2377  docker swarm join-token manager docker swarm join-token worker  docker node ls  docker swarm leave  

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zXr7rpIf-1651712847193)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\image-20220428221303675.png)]

  • 双主双从:假设一个节点挂了,其他节点是否可用?

Raft 协议:保证大多数节点存活才可以用,至少 > 一台,集群模式至少 > 三台,高可用

告别 docker run

docker-compose up 启动一个项目,单机版!

集群: swarm docker service 容器==》服务==》副本

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DttHvuAr-1651712847193)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\image-20220428223126272.png)]

service 启动,具有扩缩容,滚动更新

docker service --help  docker service create -p 8888:80 --name mynginx nginx  docker service ps mynginx  docker service ls  docker service rm mynginx  
docker service update --relipcas 3 mynginx   
docker service create --replicas 2 --network host -p 8081:8081 --name nginx nginx  docker service create --mode  

Docker Stack

  • Docker Stack 部署,集群部署
docker compose up -d wordpress.yaml  docker stack deploy wordpress.yaml  

Docker Secret

  • 安全,配置密码,证书
docker secret create 

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RsoHm8ph-1651712847194)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\image-20220428231054415.png)]

Dcoekr Config

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mylfTato-1651712847194)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\image-20220428231218415.png)]

原文链接:https://blog.csdn.net/weixin_56301040/article/details/124581952?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168623078216800185866810%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=168623078216800185866810&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-10-124581952-null-null.268%5Ev1%5Ekoosearch&utm_term=docker%E3%80%81wordpress%E3%80%81wordpress%E5%BB%BA%E7%AB%99%E3%80%81wordpress%E4%B8%BB%E9%A2%98%E3%80%81%E5%AE%B9%E5%99%A8%E9%95%9C%E5%83%8F%E3%80%81

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享