Docker 宿主机安装配置(K8S前置)

不论是单纯的使用 Docker 还是作为 Rancher 、Kubernetes 的前置安装,我们一般都需要做一些配置和处理。此处本文记录共通的一些配置操作,防止遇到问题反复排查。也包括国内加速。基本按顺序处理之后没什么问题。

文中的内容主要是 Centos7 下的操作,其他系统如果有不共通地方,请自行调整。

前置

配置 hsotName 和 host 解析

hostname自己命名即可,但集群中的hostname主机名必须不同!HostName是为了区分不同的主机,防止冲突。云服务商的ECS默认Host的名称一般不会冲突,但是我们最好自己设置下,便于管理。

Centos7 参考Centos 7 Hostname 配置,其他系统自行Google。

关闭防火墙

在每台主机上执行以下命令关闭防火墙

1
2
systemctl stop firewalld
systemctl disable firewalld

禁用 SELinux

如果开启SELinux,很多情况要进行相关的配置,我们这里直接关闭SELinux,减少配置。
在每台主机上执行以下命令关闭SELinux。

1
setenforce 0

上面的是临时设置,重启后就无效了。如果需要永久生效,编辑 /etc/selinux/config 文件,修改 SELINUX=disabled

1
sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config

配置网络

如通不配置,安装K8S会提示:

1
[ERROR FileContent--proc-sys-net-bridge-bridge-nf-call-iptables]: /proc/sys/net/bridge/bridge-nf-call-iptables contents are not set to 1

先执行:

1
2
3
4
cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

然后执行:

1
sysctl -p

关闭 Swap

使用如下命令关闭 Swap:

1
swapoff -a && sed -i '/swap/d' /etc/fstab

前面关闭当前,后面永久关闭。
否则在当前机器上安装Kubernetes会提示:

1
[ERROR Swap]: running with swap on is not supported. Please disable swap

删除老版本

如果机器已经存在老版本Docker,用如下命令删除(不确定也可直接执行):

1
2
3
4
5
6
7
8
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine

通过仓库安装新版

配置仓库

  1. 安装依赖包
1
2
3
sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
  1. 配置阿里加速仓库
1
2
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo && \
sudo yum makecache fast
  1. 配置官方 stable 仓库(和上面二选一)
1
2
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo  && \
sudo yum makecache fast

安装 DOCKER CE

安装最新版

1
sudo yum install docker-ce docker-ce-cli containerd.io

安装指定版本

1
2
3
4
5
# 查看版本
yum list docker-ce --showduplicates | sort -r
# 选择安装
sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
#Install a specific version by its fully qualified package name, which is the package name (docker-ce) plus the version string (2nd column) starting at the first colon (:), up to the first hyphen, separated by a hyphen (-). For example, docker-ce-18.09.1

启动 docker

1
sudo systemctl start docker

安装后基本配置

配置代理

1
2
3
4
5
mkdir -p /etc/systemd/system/docker.service.d
vim /etc/systemd/system/docker.service.d/http-proxy.conf
## 添加如下配置
[Service]
Environment="HTTP_PROXY=http://user:password@192.168.x.x:port" "HTTPS_PROXY=http://user:password@192.168.x.x:port" "NO_PROXY=localhost,127.0.0.1"

私库 –insecure-registry

1
2
3
vim /usr/lib/systemd/system/docker.service
# 将 ExecStart=/usr/bin/dockerd 参数后添加 --insecure-registry 配置,示例如下
ExecStart=/usr/bin/dockerd -H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375 -H fd:// --containerd=/run/containerd/containerd.sock --insecure-registry=docker.foo.com --insecure-registry=docker.bar.com

国内加速

关于镜像加速包括 gcr.io、quay.io 建议看文章:Docker 国内镜像加速

1
2
3
4
5
6
7
8
sudo mkdir -p /etc/docker
vim /etc/docker/daemon.json
# 添加如下内容
{
"registry-mirrors": ["https://dockerhub.azk8s.cn"]
}
#
sudo systemctl daemon-reload && sudo systemctl restart docker

修改Docker Cgroup Driver

1
2
3
4
5
vim /etc/docker/daemon.json
# 添加如下内容
{
"exec-opts": ["native.cgroupdriver=systemd"]
}

Docker常用命令

重启

1
2
systemctl daemon-reload
systemctl restart docker

清除机器上容器

1
2
3
4
5
6
7
docker stop $(docker ps -aq)

# docker container prune # Remove all stopped containers
# docker volume prune # Remove all unused volumes
# docker image prune # Remove unused images
# docker system prune # All of the above, in this order: containers, volumes, images
echo y | docker system prune
谢谢鼓励