使用kubeadm安裝Kubernetes 1.16 集羣

kubeadm是Kubernetes官方提供的用於快速安裝Kubernetes集羣的工具,伴隨Kubernetes每一個版本的發佈都會同步更新,kubeadm會對集羣配置方面的一些實踐作調整,經過實驗kubeadm能夠學習到Kubernetes官方在集羣配置上一些新的最佳實踐。node

操做系統
Ubuntu 16.04+、Debian 九、CentOS 七、RHEL 七、Fedora 25/26 (best-effort)、其餘
內存2GB + ,2核CPU +
集羣節點之間能夠通訊
每一個節點惟一主機名,MAC地址和product_uuid
檢查MAC地址:使用ip link或者ifconfig -a
檢查product_uuid:cat /sys/class/dmi/id/product_uuid
禁止swap分區。這樣才能使kubelet正常工做linux

準備

1.1系統配置

主機名與IP對應關係:docker

[root@k8s-master ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.201 k8s-master
192.168.1.202 k8s-node1
192.168.1.203 k8s-node2

若是各個主機啓用了防火牆,須要開放Kubernetes各個組件所須要的端口,能夠查看Installing kubeadm中的」Check required ports」一節。 這裏簡單起見在各節點禁用防火牆:json

systemctl stop firewalld
systemctl disable firewalld

禁用SELINUX:
sed -i 's/enforcing/disabled/' /etc/selinux/config <br/>setenforce 0
vim

vi /etc/selinux/config
SELINUX=disabled

關閉swap:centos

swapoff -a  # 臨時
vim /etc/fstab  # 永久

同步時間:api

yum install ntpdate -y
ntpdate  ntp.api.bz

建立/etc/sysctl.d/k8s.conf文件,添加以下內容:bash

net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1

執行命令使修改生效:服務器

modprobe br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf

1.2kube-proxy開啓ipvs的前置條件

因爲ipvs已經加入到了內核的主幹,因此爲kube-proxy開啓ipvs的前提須要加載如下的內核模塊,在全部的Kubernetes節點node1和node2上執行如下腳本:app

cat > /etc/sysconfig/modules/ipvs.modules <<EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF

chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4

上面腳本建立了的/etc/sysconfig/modules/ipvs.modules文件,保證在節點重啓後能自動加載所需模塊。 使用lsmod | grep -e ip_vs -e nf_conntrack_ipv4命令查看是否已經正確加載所需的內核模塊。

接下來還須要確保各個節點上已經安裝了ipset軟件包
yum install ipset
爲了便於查看ipvs的代理規則,最好安裝一下管理工具 ipvsadm
yum install ipvsadm
若是以上前提條件若是不知足,則即便kube-proxy的配置開啓了ipvs模式,也會退回到iptables模式。

1.3安裝Docker

Kubernetes從1.6開始使用CRI(Container Runtime Interface)容器運行時接口。默認的容器運行時仍然是Docker,使用的是kubelet中內置dockershim CRI實現。

安裝docker的yum源:

yum install -y yum-utils device-mapper-persistent-data lvm2

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

查看最新的Docker版本:

yum list docker-ce.x86_64  --showduplicates |sort -r

[root@go-docker ~]# yum list docker-ce.x86_64  --showduplicates |sort -r
 * updates: mirrors.aliyun.com
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror, langpacks
 * extras: mirrors.aliyun.com
docker-ce.x86_64            3:19.03.5-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.4-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.3-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.2-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.1-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.0-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.9-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.8-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.7-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.6-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.5-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.4-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.3-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.2-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.1-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.0-3.el7                     docker-ce-stable
docker-ce.x86_64            18.06.3.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.06.2.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.06.1.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.06.0.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.03.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            18.03.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.12.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.12.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.09.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.09.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.2.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.3.ce-1.el7                    docker-ce-stable
docker-ce.x86_64            17.03.2.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.0.ce-1.el7.centos             docker-ce-stable
 * base: mirrors.aliyun.com
Available Packages

Kubernetes 1.16當前支持的docker版本列表是1.13.1, 17.03, 17.06, 17.09, 18.06, 18.09。 這裏在各節點安裝docker的18.09.7版本。

yum makecache fast

yum install -y --setopt=obsoletes=0 \  docker-ce-18.09.7-3.el7 

systemctl start docker

systemctl enable docker

確認一下iptables filter表中FOWARD鏈的默認策略(pllicy)爲ACCEPT。

iptables -nvL

[root@k8s-master ~]# iptables -nvL
Chain INPUT (policy ACCEPT 20 packets, 2866 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DOCKER-USER  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 DOCKER-ISOLATION-STAGE-1  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  *      docker0  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
    0     0 DOCKER     all  --  *      docker0  0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  docker0 !docker0  0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  docker0 docker0  0.0.0.0/0            0.0.0.0/0           

Chain OUTPUT (policy ACCEPT 19 packets, 2789 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain DOCKER (1 references)
 pkts bytes target     prot opt in     out     source               destination         

Chain DOCKER-ISOLATION-STAGE-1 (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DOCKER-ISOLATION-STAGE-2  all  --  docker0 !docker0  0.0.0.0/0            0.0.0.0/0           
    0     0 RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain DOCKER-ISOLATION-STAGE-2 (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       all  --  *      docker0  0.0.0.0/0            0.0.0.0/0           
    0     0 RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain DOCKER-USER (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0

1.4 修改docker cgroup driver爲systemd

根據文檔CRI installation中的內容,對於使用systemd做爲init system的Linux的發行版,使用systemd做爲docker的cgroup driver能夠確保服務器節點在資源緊張的狀況更加穩定,所以這裏修改各個節點上docker的cgroup driver爲systemd。
建立或修改/etc/docker/daemon.json:

vim   /etc/docker/daemon.json

{
  "exec-opts": ["native.cgroupdriver=systemd"]
}

重啓docker:

systemctl restart docker

docker info | grep Cgroup
Cgroup Driver: systemd
相關文章
相關標籤/搜索