rancher2基礎環境配置


一、主機配置

1、配置要求

參考節點要求

2、主機名配置

因為K8S的規定,主機名只支持包含 - 和 .(中橫線和點)兩種特殊符號,並且主機名不能出現重復。

3、Hosts

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

4、CentOS關閉selinux

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

5、關閉防火牆(可選)或者放行相應端口

對於剛剛接觸Rancher的用戶,建議在關閉防火牆的測試環境或桌面虛擬機來運行rancher,以避免出現網絡通信問題。

  1. 關閉防火牆

    1、CentOS

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

    2、Ubuntu

    ufw disable

  2. 端口放行

    端口放行請查看端口需求

6、配置主機時間、時區、系統語言

  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
  • 配置主機NTP時間同步

7、配置主機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

8、Kernel性能調優

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

數值根據實際環境自行配置,最后執行sysctl -p保存配置。

9、內核模塊

警告如果要使用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 <模塊名>

10、ETCD集群容錯表

建議在ETCD集群中使用奇數個成員,通過添加額外成員可以獲得更高的失敗容錯。具體詳情可以查閱optimal-cluster-size

集群大小 MAJORITY 失敗容錯
1 1 0
2 2 0
3 2 1
4 3 1
5 3 2
6 4 2
7 4 3
8 5 3
9 5 4

二、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;

    Docker-engine

    Docker-Engine Docker官方已經不推薦使用,請安裝Docker-CE。

CentOS 7.x

  • Docker-ce安裝

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

    Docker-engine

    Docker-Engine Docker官方已經不推薦使用,請安裝Docker-CE。

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

配置鏡像下載和上傳並發數

從Docker1.12開始,支持自定義下載和上傳鏡像的並發數,默認值上傳為3個並發,下載為5個並發。通過添加”max-concurrent-downloads”和”max-concurrent-uploads”參數對其修改:

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

配置鏡像加速地址

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)。

配置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存儲驅動

OverlayFS是一個新一代的聯合文件系統,類似於AUFS,但速度更快,實現更簡單。Docker為OverlayFS提供了兩個存儲驅動程序:舊版的overlay,新版的overlay2(更穩定)。

先決條件:

  • overlay2: Linux內核版本4.0或更高版本,或使用內核版本3.10.0-514+的RHEL或CentOS。
  • overlay: 主機Linux內核版本3.18+
  • 支持的磁盤文件系統

編輯/etc/docker/daemon.json加入以下內容

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

配置日志驅動

容器在運行時會產生大量日志文件,很容易占滿磁盤空間。通過配置日志驅動來限制文件大小與文件的數量。 >限制單個日志文件為100M,最多產生3個日志文件

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

3、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非默認配置,需根據實際參數做調整。提示以上配置完成后,建議重啟一次主機。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM