本文是从imscc.io迁移过来的
准备工作
CentOS关闭selinux
sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
关闭防火墙(可选)
- CentOS
systemctl stop firewalld.service && systemctl disable firewalld.service
- Ubuntu
ufw disable
Docker安装与配置
Ubuntu
Docker-ce
# 定义安装版本
export docker_version=17.03.2
# step 1: 安装必要的一些系统工具
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
# step 2: 安装GPG证书
sudo curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# Step 3: 写入软件源信息
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# Step 4: 更新并安装 Docker-CE
sudo apt-get -y update
version=$(apt-cache madison docker-ce|grep ${docker_version}|awk '{print $3}')
# --allow-downgrades 允许降级安装
sudo apt-get -y install docker-ce=${version} --allow-downgrades
# 设置开机启动
sudo systemctl enable docker
CentOS
Docker-ce
因为CentOS的安全限制,通过RKE安装K8S集群时候无法使用root账户。所以,建议CentOS用户使用非root用户来运行docker。
# 添加用户(可选)
sudo adduser `<new_user>`
# 为新用户设置密码
sudo passwd `<new_user>`
# 为新用户添加sudo权限
sudo echo '<new_user> ALL=(ALL) ALL' >> /etc/sudoers
# 卸载旧版本Docker软件
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine \
container*
# 定义安装版本
export docker_version=17.03.2
# step 1: 安装必要的一些系统工具
sudo yum update -y
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3: 更新并安装 Docker-CE
sudo yum makecache all
version=$(yum list docker-ce.x86_64 --showduplicates | sort -r|grep ${docker_version}|awk '{print $2}')
sudo yum -y install --setopt=obsoletes=0 docker-ce-${version} docker-ce-selinux-${version}
# 如果已经安装高版本Docker,可进行降级安装(可选)
yum downgrade --setopt=obsoletes=0 -y docker-ce-${version} docker-ce-selinux-${version}
# 把当前用户加入docker组
sudo usermod -aG docker `<new_user>`
# 设置开机启动
sudo systemctl enable docker
Docker配置
对于通过systemd来管理服务的系统(比如CentOS7.X、Ubuntu16.X), Docker有两处可以配置参数: 一个是docker.service服务配置文件,一个是Docker daemon配置文件daemon.json。
docker.service
对于CentOS系统,docker.service默认位于/usr/lib/systemd/system/docker.service;对于Ubuntu系统,docker.service默认位于/lib/systemd/system/docker.service
daemon.json
daemon.json默认位于/etc/docker/daemon.json,如果没有可手动创建,基于systemd管理的系统都是相同的路径。通过修改daemon.json来改过Docker配置,也是Docker官方推荐的方法。
以下说明均基于systemd,并通过/etc/docker/daemon.json来修改配置。
配置镜像下载和上传并发数
从Docker1.12开始,支持自定义下载和上传镜像的并发数,默认值上传为3个并发,下载为5个并发。通过添加”max-concurrent-downloads”和”max-concurrent-uploads”参数对其修改:
"max-concurrent-downloads": 3,
"max-concurrent-uploads": 5
配置镜像加速地址
为了加速镜像的下载,可以给Docker配置国内的镜像地址。
编辑 /etc/docker/daemon.json 加入以下内容
{
"registry-mirrors": ["https://bo6y8t8g.mirror.aliyuncs.com","https://IP:PORT/"]
}
可以设置多个registry-mirrors地址,以数组形式书写,地址需要添加协议头(https或者http)。
配置insecure-registries私有仓库
Docker默认只信任TLS加密的仓库地址(https),所有非https仓库默认无法登陆也无法拉取镜像。insecure-registries字面意思为不安全的仓库,通过添加这个参数对非https仓库进行授信。可以设置多个insecure-registries地址,以数组形式书写,地址不能添加协议头(http)。
编辑/etc/docker/daemon.json加入以下内容
{
"insecure-registries": ["192.168.1.100","IP:PORT"]
}
配置Docker存储驱动
OverlayFS是一个新一代的联合文件系统,类似于AUFS,但速度更快,实现更简单。Docker为OverlayFS提供了两个存储驱动程序:旧版的overlay,新版的overlay2(更稳定)。
先决条件:
overlay2: Linux内核版本4.0或更高版本,或使用内核版本3.10.0-514+的RHEL或CentOS。
overlay: 主机Linux内核版本3.18+
支持的磁盘文件系统
ext4(仅限RHEL 7.1)
xfs(RHEL7.2及更高版本),需要启用d_type=true。
具体详情参考Docker Use the OverlayFS storage driver
编辑/etc/docker/daemon.json加入以下内容
{
"storage-driver": "overlay2",
"storage-opts": ["overlay2.override_kernel_check=true"]
}
Ubuntu系统 ,docker info提示WARNING: No swap limit support
Ubuntu系统下,默认cgroups未开启swap account功能,将会导致需要swap的容器出错。通过修改grub启动参数来开启swap account功能:
sudo sed -i 's/GRUB_CMDLINE_LINUX=".*"/GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1 net.ifnames=0"/g' /etc/default/grub
sudo update-grub
通过以上命令可自动配置参数,如果/etc/default/grub非默认配置,需根据实际参数做调整。