rancher部署Kubernetes集群之基础环境docker-ce部署

通过rancher快速部署Kubernetes集群请看这里

基础环境配置:

1、操作系统和软件版本要求:

Rancher在以下操作系统及其后续的非主要发行版上受支持:

Ubuntu 16.04.x (64-bit)
Docker 18.06.x, 18.09.x
Ubuntu 18.04.x (64-bit)
Docker 18.06.x, 18.09.x
RancherOS 1.3.x+ (64-bit)
Docker 18.06.x, 18.09.x
Windows Server version 1803 (64-bit)
Docker 17.06

1、Ubuntu、Centos操作系统有Desktop和Server版本,选择请安装server版本,别自己坑自己!
2、如果您正在使用RancherOS,请确保切换到受支持的Docker版本:
sudo ros engine switch docker-18.09.2

2、硬件配置要求:

  • 单节点安装需求

rancher

  • HA安装需求(标准3节点)

在这里插入图片描述

3、主机名配置

因为K8S的规定,主机名只支持包含 - 和 .(中横线和点)两种特殊符号,并且主机名不能出现重复。

4、Hosts配置

配置每台主机的hosts(/etc/hosts),添加host_ip $hostname到/etc/hosts文件中

5、CentOS关闭selinux

sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

6、关闭防火墙(可选)或者放行相应端口

  • 关闭防火墙
    CentOS
systemctl stop firewalld.service && systemctl disable firewalld.service

Ubuntu

ufw disable
  • 端口放行

通过iptables或者firewall-cmd配置端口

7、配置主机时间、时区、系统语言

  • 查看时区
date -R或者timedatectl
  • 修改时区
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  • 修改系统语言环境
sudo echo 'LANG="en_US.UTF-8"' >> /etc/profile;source /etc/profile
  • 配置主机NTP时间同步

8、配置主机NTP时间同步

对于类似Ubuntu 18这类默认使用systemd-resolve管理DNS的系统,建议禁用systemd-resolved服务,然后手动配置DNS
操作方法:

禁用systemd-resolved.service

systemctl disable systemd-resolved.service
systemctl stop systemd-resolved.service
rm -rf /etc/resolv.conf ; touch /etc/resolv.conf

接着编辑/etc/resolv.conf添加DNS服务器

重启docker服务

systemctl daemon-reload ; systemctl restart docker

9、Kernel性能调优

数值根据实际环境自行配置,最后执行sysctl -p保存配置。

cat >> /etc/sysctl.conf<<EOF
net.ipv4.ip_forward=1
net.bridge.bridge-nf-call-iptables=1
net.ipv4.neigh.default.gc_thresh1=4096
net.ipv4.neigh.default.gc_thresh2=6144
net.ipv4.neigh.default.gc_thresh3=8192
EOF

Docker安装与配置

1、Docker安装

  • Ubuntu 16.x
    -修改系统源
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
cat > /etc/apt/sources.list << EOF

deb http://mirrors.aliyun.com/ubuntu/ xenial main
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security universe

EOF

-Docker-ce安装

# 定义用户名
NEW_USER=rancher
# 添加用户(可选)
sudo adduser $NEW_USER
# 为新用户设置密码
sudo passwd $NEW_USER
# 为新用户添加sudo权限
sudo echo "$NEW_USER ALL=(ALL) ALL" >> /etc/sudoers
# 定义安装版本
export docker_version=18.06.3;

# step 1: 安装必要的一些系统工具
sudo apt-get remove docker docker-engine docker.io containerd runc -y;
sudo apt-get update;
sudo apt-get -y install apt-transport-https ca-certificates \
    curl software-properties-common bash-completion  gnupg-agent;
    
# 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;
# 把当前用户加入docker组
sudo usermod -aG docker $NEW_USER;
# 设置开机启动
sudo systemctl enable docker;
  • CentOS 7.x
    -Docker-ce安装
    因为CentOS的安全限制,通过RKE安装K8S集群时候无法使用root账户。所以,建议CentOS用户使用非root用户来运行docker,不管是RKE还是custom安装k8s
# 定义用户名
NEW_USER=rancher
# 添加用户(可选)
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

# 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;

2、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来修改配置。

  1. 配置镜像下载和上传并发数
    从Docker1.12开始,支持自定义下载和上传镜像的并发数,默认值上传为3个并发,下载为5个并发。通过添加”max-concurrent-downloads”和”max-concurrent-uploads”参数对其修改:
"max-concurrent-downloads": 3,
"max-concurrent-uploads": 5
  1. 配置镜像加速地址
    Rancher从v1.6.15开始到v2.x.x,Rancher系统相关的所有镜像(包括1.6.x上的K8S镜像)都托管在Dockerhub仓库。Dockerhub节点在国外,国内直接拉取镜像会有些缓慢。为了加速镜像的下载,可以给Docker配置国内的镜像地址。可以设置多个registry-mirrors地址,以数组形式书写,地址需要添加协议头(https或者http)。
    编辑 /etc/docker/daemon.json
{
"registry-mirrors": ["https://7bezldxe.mirror.aliyuncs.com/","https://IP:PORT/"]
}
  1. 配置insecure-registries 私有仓库
    Docker默认只信任TLS加密的仓库地址(https),所有非https仓库默认无法登陆也无法拉取镜像。insecure-registries字面意思为不安全的仓库,通过添加这个参数对非https仓库进行授信。可以设置多个insecure-registries地址,以数组形式书写,地址不能添加协议头(http)。
    编辑 /etc/docker/daemon.json
{
"insecure-registries": ["192.168.1.100","IP:PORT"]
}
  1. 配置Docker存储驱动
    OverlayFS是一个新一代的联合文件系统,类似于AUFS,但速度更快,实现更简单。Docker为OverlayFS提供了两个存储驱动程序:旧版的overlay,新版的overlay2(更稳定)。
    编辑 /etc/docker/daemon.json

先决条件:
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

{
"storage-driver": "overlay2",
"storage-opts": ["overlay2.override_kernel_check=true"]
}
  1. 配置日志驱动
    容器在运行时会产生大量日志文件,很容易占满磁盘空间。通过配置日志驱动来限制文件大小与文件的数量。 >限制单个日志文件为100M,最多产生3个日志文件。
    编辑 /etc/docker/daemon.json
{
"log-driver": "json-file",
"log-opts": {
    "max-size": "100m",
    "max-file": "3"
    }
}
  1. Ubuntu\Debian系统 ,docker info提示WARNING: No swap limit support
    Ubuntu\Debian系统下,默认cgroups未开启swap account功能,这样会导致设置容器内存或者swap资源限制不生效。可以通过以下命令解决:
# 统一网卡名称为ethx
sudo sed -i 's/en[[:alnum:]]*/eth0/g' /etc/network/interfaces;
sudo sed -i 's/GRUB_CMDLINE_LINUX="\(.*\)"/GRUB_CMDLINE_LINUX="net.ifnames=0 cgroup_enable=memory swapaccount=1 biosdevname=0 \1"/g' /etc/default/grub;
sudo update-grub;
  1. 修改docker工作目录

docker安装时如果不指定家目录(也就是工作目录),一般默认工作目录是 /var/lib/docker ,很多时候需要修改到大容量磁盘上进行存储,这里记录一下修改默认路径为 /data/docker

查看当前docker运行的工作目录

[root@server-1 ~]# docker info |grep "Docker Root Dir"
  1. 方法一
    编辑 /etc/docker/daemon.json
{
    "data-root": "/data/docker"
}

重启docker

systemctl restart docker 
  1. 方法二
    编辑文件 /usr/lib/systemd/system/docker.service,在ExecStart后添加–data-root=/data/docker
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock  --data-root=/data/docker
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2……

重启docker

systemctl daemon-reload 
systemctl restart docker

通过rancher快速部署Kubernetes集群请看这里

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章