Rancher 2.x 环境服务器环境配置(一)

  

1.1. 配置要求

参考节点需求

1.2. 主机名配置

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

  1. Hosts

    • Linux系统安装完成后,在hosts(/etc/hosts)中应该有localhost指向127.0.0.1,如果没有则手动添加上。
    • 配置每台主机的hosts(/etc/hosts),添加host_ip $hostname/etc/hosts文件中。
  2. CentOS关闭selinux

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

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

    对于刚刚接触Rancher的用户,建议在关闭防火墙的测试环境或桌面虚拟机来运行rancher,以避免出现网络通信问题。

    1. 关闭防火墙

      • CentOS

      systemctl stop firewalld.service && systemctl disable firewalld.service

      • Ubuntu

      ufw disable

    2. 端口放行

      端口放行请查看端口需求

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

    1. 查看时区

      date -R或者timedatectl

    2. 修改时区

      ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
      

       

    3. 修改系统语言环境

      sudo echo 'LANG="en_US.UTF-8"' >> /etc/profile;source /etc/profile
      

       

    4. 配置主机NTP时间同步

  5. 配置主机DNS

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

    操作方法:

    1. 禁用systemd-resolved.service

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

       

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

    3. 重启docker服务

      systemctl daemon-reload ; systemctl restart docker
      

      1

2. Kernel性能调优

cat >> /etc/sysctl.conf<<EOF
net.ipv4.ip_forward=1
watchdog_thresh=30
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

 

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

2.1. 模块列表

如果要使用ceph存储相关功能,需保证worker节点加载RBD模块

以下模块需要在主机上加载

模块名称
br_netfilter
ip6_udp_tunnel
ip_set
ip_set_hash_ip
ip_set_hash_net
iptable_filter
iptable_nat
iptable_mangle
iptable_raw
nf_conntrack_netlink
nf_conntrack
nf_conntrack_ipv4
nf_defrag_ipv4
nf_nat
nf_nat_ipv4
nf_nat_masquerade_ipv4
nfnetlink
udp_tunnel
veth
vxlan
x_tables
xt_addrtype
xt_conntrack
xt_comment
xt_mark
xt_multiport
xt_nat
xt_recent
xt_set
xt_statistic
xt_tcpudp

提示

模块查询: lsmod | grep <模块名>
模块加载: modprobe <模块名>

3. ETCD集群容错表

建议在ETCD集群中使用奇数个成员,通过添加额外成员可以获得更高的失败容错。具体详情可以查阅optimal-cluster-size

集群大小MAJORITY失败容错
110
220
321
431
532
642
743
853
954

4. Docker安装与配置

4.1. Docker安装

  • 修改系统源
  1. Ubuntu 16.04.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
    

     

  2. Ubuntu 18.04.x

    sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
    cat > /etc/apt/sources.list << EOF
    deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
    EOF
    

     

  3. Centos7.x

    yum install ca-certificates ;
    update-ca-trust;
    
    cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo-bak
    cat << 'EOF' > /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安装
  1. Ubuntu

    # 定义用户名
    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.09.9;
    # 删除旧的docker组件
    sudo apt-get remove docker docker-engine docker.io containerd runc -y;
    # 更新 apt 源
    sudo apt-get update;
    # 对系统进行全面的更新升级,推荐升级一下(可选)
    sudo apt-get -y upgrade;
    # 安装必要的一些系统工具
    sudo apt-get -y install apt-transport-https ca-certificates \
        curl software-properties-common bash-completion  gnupg-agent;
    # 安装GPG证书
    sudo curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | \
        sudo apt-key add -;
    # 添加Docker APT 源
    sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable";
    # 更新并安装 Docker-CE
    sudo apt-get -y update;
    install_version=$( apt-cache madison docker-ce | grep ${docker_version} | awk '{print $3}' );
    # --allow-downgrades 允许降级安装
    sudo apt-get -y install docker-ce=${install_version} docker-ce-cli=${install_version} --allow-downgrades;
    # 把当前用户加入docker组
    sudo usermod -aG docker $NEW_USER;
    # 设置开机启动
    sudo systemctl enable docker;
    # 清理不需要的依赖
    apt-get autoremove  -y
    

     

    Docker-engine

    Docker-Engine Docker官方已经不推荐使用,请安装Docker-CE。

  2. Centos

    因为CentOS的安全限制,通过RKE安装K8S集群时候无法使用root账户。所以,建议CentOS用户使用非root用户来运docker,不管是RKE还是custom安装k8s,详情查看无法为主机配置SSH隧道

    # 定义用户名
    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
    # 对系统进行全面的更新升级,推荐升级一下(可选)
    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;
    

     

    Docker-engine

    Docker-Engine Docker官方已经不推荐使用,请安装Docker-CE。

4.2. 锁定Docker版本

可能因为某些原因无意间执行了yum update或者apt-get -y upgrade;导致Docker版本升级。为了避免此类问题发生,建议在安装好Docker后对Docker软件进行锁定,防止Docker意外更新。

centos

  1. 安装yum-plugin-versionlock插件

    yum install yum-plugin-versionlock
    

     

  2. 锁定软件包

    yum versionlock add docker-ce docker-ce-cli
    

     

    [root@izwz969o7lu6t9lh4ta6m5z ~]# yum versionlock add docker-ce docker-ce-cli
    已加载插件:fastestmirror, versionlock
    Adding versionlock on: 0:docker-ce-17.06.2.ce-3.el7.centos
    versionlock added: 1
    [root@izwz969o7lu6t9lh4ta6m5z ~]#
    

     

  3. 查看已锁定的软件包

    yum versionlock list
    

     

  4. 解锁定的软件包

    yum versionlock delete <软件包名称>
    

     

  5. 解锁所有的软件包

    yum versionlock clear
    

     

Ubuntu

  1. 锁定软件:

    sudo apt-mark hold docker-ce docker-ce-cli
    

     

  2. 查看已锁定的软件

    sudo apt-mark showhold
    

     

    软件包被锁定后,再次执行apt-get upgrade可以看到以下日志提示

    root@alihost-03:/home/docker# apt-get  upgrade
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    Calculating upgrade... Done
    The following packages have been kept back:
      docker-ce docker-ce-cli linux-generic linux-headers-generic linux-image-generic ubuntu-minimal
    0 upgraded, 0 newly installed, 0 to remove and 6 not upgraded.
    root@alihost-03:/home/docker#
    
    

     

    被锁定的软件不会再升级。

  3. 解除锁定:

    sudo apt-mark unhold docker-ce docker-ce-cli
    

     

4.3. Docker配置

对于通过systemd来管理服务的系统(比如CentOS7.X、Ubuntu16.X), Docker有两处可以配置参数: 一个是docker.service服务配置文件,一个是Docker daemon配置文件daemon.json。

  1. docker.service

    对于CentOS系统,docker.service默认位于/usr/lib/systemd/system/docker.service;对于Ubuntu系统,docker.service默认位于/lib/systemd/system/docker.service

  2. daemon.json

    daemon.json默认位于/etc/docker/daemon.json,如果没有可手动创建,基于systemd管理的系统都是相同的路径。通过修改daemon.json来改过Docker配置,也是Docker官方推荐的方法。

以下说明均基于systemd,并通过/etc/docker/daemon.json来修改配置。

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

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

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

 

4.3.2. 配置镜像加速地址

Rancher从v1.6.15开始到v2.x.x,Rancher系统相关的所有镜像(包括1.6.x上的K8S镜像)都托管在Dockerhub仓库。Dockerhub节点在国外,国内直接拉取镜像会有些缓慢。为了加速镜像的下载,可以给Docker配置国内的镜像地址。

编辑/etc/docker/daemon.json加入以下内容

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

 

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

4.3.3. 配置insecure-registries私有仓库

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

编辑/etc/docker/daemon.json加入以下内容:

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

 

4.3.4. 配置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"]
}

4.3.5. 配置日志驱动

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

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

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

 

4.4. 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;


 

注意 通过以上命令可自动配置参数,如果/etc/default/grub非默认配置,需根据实际参数做调整。 提示 以上配置完成后,建议重启一次主机。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值