Docker安装经验分享

本文分享了Docker的安装步骤,包括环境准备、添加yum源、安装与启动配置,以及基础配置如镜像加速、存储驱动、日志驱动和数据存储路径的设定。还涉及了系统优化和内核升级的相关内容。

    Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。关于Docker的更多信息请查看Docker官网

安装

    当前docker分为社区版和企业版,linux系统自带的源一般都是企业版,在安装社区版时需要添加yum源。

安装环境

CentOS 7.5 64bit

安装必要的系统工具

yum install -y yum-utils device-mapper-persistent-data \
    lvm2 bash-completion;

添加yum源

yum-config-manager --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo;

安装

sudo yum -y install --setopt=obsoletes=0 docker-ce;

–setopt=obsoletes=0 此参数为取消安装包取代关系,有时候有些旧的安装包被新的安装包取代了,安装的时候就会自动安装新的。

设置开机启动

sudo systemctl enable docker;

基础配置

Docker有两处可以配置参数: 一个是docker.service服务配置文件,一个是Docker daemon配置文件daemon.json。建议所有修改都在daemon.json中进行。

在未启动Docker前,没有文件daemon.json,请自行创建。
以下说明均基于CentOS 7.5,并通过/etc/docker/daemon.json来修改配置。要查看所有配置请参考官方配置指导

配置镜像下载和上传并发数

从Docker1.12开始,支持自定义下载和上传镜像的并发数,默认值上传为3个并发,下载为5个并发。通过添加”max-concurrent-downloads”和”max-concurrent-uploads”参数对其修改:

{
    "max-concurrent-downloads": 3,
    "max-concurrent-uploads": 5
}

配置镜像加速地址

国内直接拉取镜像会有些缓慢,为了加速镜像的下载,可以给Docker配置国内的镜像地址。

{
    "registry-mirrors": ["https://7bezldxe.mirror.aliyuncs.com/"]
}

可以设置多个registry-mirrors地址,以数组形式书写,地址需要添加协议头(https或者http)。

配置insecure-registries私有仓库

Docker默认只信任TLS加密的仓库地址(https),所有非https仓库默认无法登陆也无法拉取镜像。insecure-registries字面意思为不安全的仓库,通过添加这个参数对非https仓库进行授信。可以设置多个insecure-registries地址,以数组形式书写,地址不能添加协议头(http)。

{
    "insecure-registries": ["IP:PORT"]
}

配置Docker存储驱动

OverlayFS是一个新一代的联合文件系统,类似于AUFS,但速度更快,实现更简单。Docker为OverlayFS提供了两个存储驱动程序:旧版的overlay,新版的overlay2(更稳定)。

启用overlay2条件: Linux内核版本4.0或更高版本,或使用内核版本3.10.0-514+的RHEL或CentOS。

{
    "storage-driver": "overlay2",
    "storage-opts": ["overlay2.override_kernel_check=true"]
}

配置日志驱动

容器在运行时会产生大量日志文件,很容易占满磁盘空间。通过配置日志驱动来限制文件大小与文件的数量。

限制单个日志文件为100M,最多产生3个日志文件

{
    "log-driver": "json-file",
    "log-opts": {
        "max-size": "100m",
        "max-file": "3"
    }
}

配置数据存储路径

{
    "data-root": "/home/docker-data"
}

其他

该部分内容为优化系统配置

  • Hosts
配置每台主机的hosts(/etc/hosts),添加host_ip $hostname到/etc/hosts文件中。
  • CentOS关闭selinux
sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
  • OS内核调优
echo "
net.bridge.bridge-nf-call-ip6tables=1
net.bridge.bridge-nf-call-iptables=1
net.ipv4.ip_forward=1
net.ipv4.conf.all.forwarding=1
net.ipv4.neigh.default.gc_thresh1=4096
net.ipv4.neigh.default.gc_thresh2=6144
net.ipv4.neigh.default.gc_thresh3=8192
net.ipv4.neigh.default.gc_interval=60
net.ipv4.neigh.default.gc_stale_time=120

# 参考 https://github.com/prometheus/node_exporter#disabled-by-default
kernel.perf_event_paranoid=-1

#sysctls for k8s node config
net.ipv4.tcp_slow_start_after_idle=0
net.core.rmem_max=16777216
fs.inotify.max_user_watches=524288
kernel.softlockup_all_cpu_backtrace=1
kernel.softlockup_panic=1
fs.file-max=2097152
fs.inotify.max_user_instances=8192
fs.inotify.max_queued_events=16384
vm.max_map_count=262144
fs.may_detach_mounts=1
net.core.netdev_max_backlog=16384
net.ipv4.tcp_wmem=4096 12582912 16777216
net.core.wmem_max=16777216
net.core.somaxconn=32768
net.ipv4.ip_forward=1
net.ipv4.tcp_max_syn_backlog=8096
net.ipv4.tcp_rmem=4096 12582912 16777216

net.ipv6.conf.all.disable_ipv6=1
net.ipv6.conf.default.disable_ipv6=1
net.ipv6.conf.lo.disable_ipv6=1

kernel.yama.ptrace_scope=0
vm.swappiness=0

# 可以控制core文件的文件名中是否添加pid作为扩展。
kernel.core_uses_pid=1

# Do not accept source routing
net.ipv4.conf.default.accept_source_route=0
net.ipv4.conf.all.accept_source_route=0

# Promote secondary addresses when the primary address is removed
net.ipv4.conf.default.promote_secondaries=1
net.ipv4.conf.all.promote_secondaries=1

# Enable hard and soft link protection
fs.protected_hardlinks=1
fs.protected_symlinks=1

# 源路由验证
# see details in https://help.aliyun.com/knowledge_detail/39428.html
net.ipv4.conf.all.rp_filter=0
net.ipv4.conf.default.rp_filter=0
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_announce=2
net.ipv4.conf.all.arp_announce=2

# see details in https://help.aliyun.com/knowledge_detail/41334.html
net.ipv4.tcp_max_tw_buckets=5000
net.ipv4.tcp_syncookies=1
net.ipv4.tcp_fin_timeout=30
net.ipv4.tcp_synack_retries=2
kernel.sysrq=1
" >> /etc/sysctl.conf
  • nofile
cat >> /etc/security/limits.conf <<EOF
* soft nofile 65535
* hard nofile 65536
EOF
  • 修改Docker默认IP

1.停止docker运行

systemctl stop docker.service

2.删除已有的docker0接口

sudo ip link del docker0

3.修改docker配置文件

在/etc/docker/daemon.json中添加"bip": "169.254.123.1/24"
  • Docker.service配置

对于CentOS系统,docker.service默认位于/usr/lib/systemd/system/docker.service;对于Ubuntu系统,docker.service默认位于/lib/systemd/system/docker.service。编辑docker.service,添加以下参数。

1.防止docker服务OOM

OOMScoreAdjust=-1000

2.开启iptables转发链:

ExecStartPost=/usr/sbin/iptables -P FORWARD AC
  • centos升级内核

1、导入key,如果已经修改了repo的gpgcheck=0也可以不导入key

rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org

2、安装elrepo的yum源

rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm

3.载入元数据

yum --disablerepo=\* --enablerepo=elrepo-kernel repolist

4.查看可用安装包

yum --disablerepo=\* --enablerepo=elrepo-kernel list kernel*

5.安装内核

yum --disablerepo=\* --enablerepo=elrepo-kernel install  kernel-lt.x86_64  -y

6.设置启动内核

grub2-set-default 'CentOS Linux (4.20.12-1.el7.elrepo.x86_64) 7 (Core)'

参考文献

1、Rancher提供的Docker安装与配置

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值