目录
- 步骤一:centos系统设置
- 步骤二:docker-ce安装
- 说明:docker配置
- 防止 docker 服务 OOM
- 配置镜像下载和上传并发数
- 配置镜像加速地址
- 配置insecure-registries私有仓库
- 配置docker数据存储位置
- 配置Docker存储驱动
- 配置日志驱动
步骤一:centos系统设置
##关闭selinux
#setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
##关闭防火墙
systemctl stop firewalld.service && systemctl disable firewalld.service
##修改时区
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
##修改系统语言环境
echo 'LANG="en_US.UTF-8"' >> /etc/profile;source /etc/profile
##kernel性能调优
cat >> /etc/sysctl.conf<> /etc/security/limits.conf < /etc/yum.repos.d/CentOS-Base.repo
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client. You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#
[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#released updates
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
EOF
步骤二:docker-ce安装
因为CentOS的安全限制,通过RKE安装K8S集群时候无法使用root账户。所以,建议CentOS用户使用非root用户来运行docker,不管是RKE还是custom安装k8s,详情查看无法为主机配置SSH隧道。
##安装docker
# 定义用户名
NEW_USER=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=18.06.3
#export docker_version=19.03.15
# step 1: 安装必要的一些系统工具
#sudo yum update -y;
sudo yum install -y yum-utils device-mapper-persistent-data \
lvm2 bash-completion;
# 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;
mkdir -p /etc/docker
#配置docker
cat << 'EOF' >/etc/docker/daemon.json
{
"oom-score-adjust": -1000,
"max-concurrent-downloads": 3,
"max-concurrent-uploads": 5,
"registry-mirrors": [ "https://8j2m7k20.mirror.aliyuncs.com" ],
"graph": "/data/docker",
"storage-driver": "overlay2",
"storage-opts": ["overlay2.override_kernel_check=true"],
"log-driver": "json-file",
"log-opts": { "max-size":"10m","max-file":"3" }
}
EOF
systemctl restart docker
说明:docker配置
防止 docker 服务 OOM
"oom-score-adjust": -1000,
配置镜像下载和上传并发数
从Docker1.12开始,支持自定义下载和上传镜像的并发数,默认值上传为3个并发,下载为5个并发。通过添加”max-concurrent-downloads”和”max-concurrent-uploads”参数对其修改:
"max-concurrent-downloads": 3,
"max-concurrent-uploads": 5,
配置镜像加速地址
编辑/etc/docker/daemon.json加入以下内容
"registry-mirrors": [ "https://8j2m7k20.mirror.aliyuncs.com"],
配置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数据存储位置
"graph": "/data/docker",
配置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"],
配置日志驱动
容器在运行时会产生大量日志文件,很容易占满磁盘空间。通过配置日志驱动来限制文件大小与文件的数量。 >限制单个日志文件为100M,最多产生3个日志文件
"log-driver": "json-file",
"log-opts": { "max-size":"10m","max-file":"3" }