手把手教你部署Docker(手撸官网)

Docker安装(centos)

1、改阿里云yum源(配置方法参考:centos镜像_centos下载地址_centos安装教程-阿里巴巴开源镜像站)

  • 备份原有yum源

     mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
  • 下载新的 CentOS-Base.repo 到 /etc/yum.repos.d/

    CentOS 7

     wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

    或者

     curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
  • 添加软件信息源

     yum -y install yum-utils
     yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
     sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
  • 运行 yum makecache 生成缓存

     yum makecache fast
  • 更新系统(可选)

     yum update

2、安装gcc环境

 yum install -y gcc gcc-c++ yum-utils device-mapper-persistent-data lvm2 vim

3、卸载旧版本docker(可选)

 yum remove docker \
                   docker-ce-cli \
                   docker-client \
                   docker-client-latest \
                   docker-common \
                   docker-latest \
                   docker-latest-logrotate \
                   docker-logrotate \
                   docker-selinux \
                   docker-engine-selinux \
                   docker-engine
  
 rm -rf /etc/systemd/system/docker.service.d
 rm -rf /var/lib/docker
 rm -rf /var/run/docker  

4、安装docker

  • 下载docker

     yum -y install docker-ce
  • 安装后检查

     docker version
  • 设置开机启动

     systemctl enable docker && systemctl daemon-reload

Ubuntu安装方法

 # step 1: 安装必要的一些系统工具
 sudo apt-get update
 sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
 # step 2: 安装GPG证书
 curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
 # Step 3: 写入软件源信息
 sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
 # Step 4: 更新并安装Docker-CE
 sudo apt-get -y update
 sudo apt-get -y install docker-ce
 ​
 # 安装指定版本的Docker-CE:
 # Step 1: 查找Docker-CE的版本:
 # apt-cache madison docker-ce
 #   docker-ce | 17.03.1~ce-0~ubuntu-xenial | https://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 #   docker-ce | 17.03.0~ce-0~ubuntu-xenial | https://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 # Step 2: 安装指定版本的Docker-CE: (VERSION例如上面的17.03.1~ce-0~ubuntu-xenial)
 # sudo apt-get -y install docker-ce=[VERSION]

4、设置docker加速器--CentOS(阿里云登录 - 欢迎登录阿里云,安全稳定的云计算服务平台)

 sudo mkdir -p /etc/docker
 # "exec-opts"为了kubernetes部署,默认情况下Kubernetes cgroup为system,我们需要更改Docker cgroup驱动
 sudo tee /etc/docker/daemon.json <<-'EOF'
 {
   "registry-mirrors": ["https://hy55ajmk.mirror.aliyuncs.com"],
   "exec-opts": ["native.cgroupdriver=systemd"]
 }
 EOF
 ​
 sudo systemctl daemon-reload
 sudo systemctl restart docker

Docker进阶

学习视频参考狂神: 1、Docker基础回顾_哔哩哔哩_bilibili

Docker Compose

官方文档

Using Compose is basically a three-step process:

  1. Define your app’s environment with a Dockerfile so it can be reproduced anywhere.

  2. Define the servicesthat make up your app in docker-compose.yml so they can be run together in an isolated environment.

  3. Run docker compose up and the Docker compose command starts and runs your entire app. You can alternatively run docker-compose up using the docker-compose binary.

A docker-compose.yml looks like this:

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

Compose:重要概念

  • 服务service:容器、应用(web、redis、mysql...)

  • 项目project:一组关联的容器。博客

安装

Install Compose on Linux systems

On Linux, you can download the Docker Compose binary from the Compose repository release page on GitHub. Follow the instructions from the link, which involve running the curl command in your terminal to download the binaries. These step-by-step instructions are also included below.

For alpine, the following dependency packages are needed: py-pip, python3-dev, libffi-dev, openssl-dev, gcc, libc-dev, rust, cargo and make.

  1. Run this command to download the current stable release of Docker Compose:

     $ sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

    To install a different version of Compose, substitute 1.29.2 with the version of Compose you want to use. For instructions on how to install Compose 2.2.3 on Linux, see Install Compose 2.0.0 on Linux

Github最新版本查看:Releases · docker/compose · GitHub

国内下载源(按照所需版本下载):

 curl -L https://get.daocloud.io/docker/compose/releases/download/v2.12.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
  1. Apply executable permissions to the binary:

 $ sudo chmod +x /usr/local/bin/docker-compose

Note:

If the command docker-compose fails after installation, check your path. You can also create a symbolic link to /usr/bin or any other directory in your path.

For example:

 $ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
  1. Test the installation.

 $ docker-compose --version

安装完成!

体验

官网例程:Get started with Docker Compose | Docker Documentation

  1. 应用app.py

  2. Dockerfile 应用打包为镜像

  3. Docker-compose yaml 文件(定义整个服务,需要的环境。web、redis),完整的上线服务

  4. 启动compose项目(docker-compose up)

手把手教你部署Docker(手撸官网)_第1张图片

默认的服务名 文件名_服务名_num

多个服务(集群)时_num 为副本数量

Docker小结

1、Docker镜像。run => 容器

2、Dockerfile构建镜像(服务打包)

3、docker-compose启动项目(编排、多个微服务/环境)

4、Docker网络

yaml规则

docker-compose.yaml 核心

例子参考:Compose file version 3 reference | Docker Documentation

 #3层结构
 version:'' #版本
 service:  #服务
     web:  #服务1
       images
       build
       network
       ......
     redis: #服务2
       ......
     mysql: #服务3
 ​
 #其他配置,网络、卷、全局规则
 volumes:
 nerwork:
 configs:

学习强化:多写、多看

  • 官网文档

    Compose file version 3 reference | Docker Documentation

  • 开源项目

    官方个人博客实例:Quickstart: Compose and WordPress | Docker Documentation

Docker Swarm

集群的方式部署

购买网址:阿里云登录 - 欢迎登录阿里云,安全稳定的云计算服务平台

测试环境要求:4台服务器(每台至少1核2G)

每台机的安装参考:Docker安装

工作原理

官网地址:How nodes work | Docker Documentation

手把手教你部署Docker(手撸官网)_第2张图片

Raft--一致性算法

管理节点--manager

工作节点--worker

操作

  • 查看swarm的相关命令

[root@localhost ~]# docker swarm --help

Usage:  docker swarm COMMAND

Manage Swarm

Commands:
  ca          Display and rotate the root CA
  init        Initialize a swarm
  join        Join a swarm as a node and/or manager
  join-token  Manage join tokens
  leave       Leave the swarm
  unlock      Unlock swarm
  unlock-key  Manage the unlock key
  update      Update the swarm

Run 'docker swarm COMMAND --help' for more information on a command.

  • 初始化节点

docker swarm init --advertier-add [IP]

手把手教你部署Docker(手撸官网)_第3张图片

  • 加入一个节点docker swarm join

#获取令牌(主节点运行)
docker swarm join-token [manager]
docker swarm join-token [worker]

手把手教你部署Docker(手撸官网)_第4张图片

  

主节点运行docker node ls

手把手教你部署Docker(手撸官网)_第5张图片

Raft协议

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

手把手教你部署Docker(手撸官网)_第6张图片

Raft协议(针对Manager):保证大多数节点存活才可用。只要>1,至少要3台!!

双主双从实验:

1、将docker-1机器停止,双主另外一个主节点也不能使用(Raft协议)

2、重启docker-1,此时状态为reachable,且Leader转换到docker-4

手把手教你部署Docker(手撸官网)_第7张图片

3、worker只是工作,不能执行管理命令

疑问:Manager至少3台的要求,会不会浪费服务器资源

概念总结


swarm

集群的管理和编号,docker可以初始化一个swarm集群,其他节点可以加入。(Manager、Worker)

Node

就是一个docker节点,多个节点就组成了一个网络集群

service

任务,可以在管理节点或者工作节点来运行,核心!用户访问!

task

容器内的命令,细节任务!

Docker Stack

Docker-Compose 单机部署项目!

Docker-Stack 集群部署项目

#单机部署
docker-compose up -d my_nginx.yaml
#集群部署
docker stack deploy my_nginx.yaml

[root@localhost ~]# docker stack

Usage:  docker stack [OPTIONS] COMMAND

Manage Docker stacks

Options:
      --orchestrator string   Orchestrator to use (swarm|kubernetes|all)

Commands:
  deploy      Deploy a new stack or update an existing stack
  ls          List stacks
  ps          List the tasks in the stack
  rm          Remove one or more stacks
  services    List the services in the stack

Run 'docker stack COMMAND --help' for more information on a command.

Docker Seccret

安全!配置密码等!

Docker Config

配置

你可能感兴趣的:(服务器,linux,docker,centos)