k8s叢集搭建
阿新 • • 發佈:2020-07-19
1 環境說明
1.1 k8s-master 節點
#系統環境: 記憶體:4G 處理器:4核 #主機環境(待安裝): docker、kubelet、kubeadm、kubectl、flannel
1.2 k8s-node1 節點
#系統環境: 記憶體:2G 處理器:2核 #主機環境(待安裝): docker、kubelet、kubeadm、kubectl、flannel
1.3 k8s-node2 節點
#系統環境: 記憶體:2G 處理器:2核 #主機環境(待安裝): docker、kubelet、kubeadm、kubectl、flannel
2 系統初始化(三個節點)
2.1 修改主機名,配置 hosts 解析
hostnamectl set-hostname {k8s-master | k8s-node1 | k8s-node2} exit #退出重新登入,使主機名生效 cat >> /etc/hosts <<EOF 192.168.81.131 k8s-master 192.168.81.132 k8s-node1 192.168.81.133 k8s-node2
EOF
2.2 關閉防火牆、禁用 selinux
#檢視防火牆狀態 firewall-cmd --state #停止firewall;禁止firewall開機啟動systemctl stop firewalld.service systemctl disable firewalld.service #防火牆設定iptables,並清空 yum install iptables-services -y systemctl start iptables systemctl enable iptables iptables -F service iptables save -y #關閉selinux getenforce setenforce 0 sed -i 's/^ *SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
2.3 安裝依賴包
yum install -y conntrack ntpdate ntp ipvsadm ipset jp iptables curl sysstat libseccomp wget vim net-tools git
2.4 調整 k8s 核心引數
#載入模組 modprobe br_netfilter #調整引數 cat > kubernetes.conf <<EOF net.bridge.bridge-nf-call-iptables=1 net.bridge.bridge-nf-call-ip6tables=1 net.ipv4.ip_forward=1 net.ipv4.tcp_tw_recycle=0 vm.swappiness=0 vm.overcommit_memory=1 vm.panic_on_oom=0 fs.inotify.max_user_instances=8192 fs.inotify.max_user_watches=2310720 EOF cp kubernetes.conf /etc/sysctl.d/kubernetes.conf sysctl -p /etc/sysctl.d/kubernetes.conf
2.5 調整系統時區
#設定時區為中國/上海 timedatectl set-timezone Asia/Shanghai #將當前的UTC時間寫入硬體時間 timedatectl set-local-rtc 0 #重啟依賴於時間的服務 systemctl restart rsyslog systemctl restart crond
2.6 關閉系統不需要的服務
systemctl stop postfix && systemctl disable postfix
2.7 設定 rsyslogd 和 systemd journald
mkdir /var/log/journal #持久化儲存日誌目錄 mkdir /etc/systemd/journald.conf.d cat > /etc/systemd/journald.conf.d/99-prophet.conf <<EOF [Journal] #持久化儲存在磁碟 Storage=persistent #壓縮歷史日誌 Compress=yes SyncIntervalSec=5m RateLimitInterval=30s RateLimitBurst=1000 #最大佔用空間10G SystemMaxUse=10G #單日誌檔案最大200M SystemMaxFileSize=200M #日誌儲存時間2周 MaxRetentionSec=2week #不將日誌轉發到syslog ForwardToSyslog=no EOF systemctl restart systemd-journald
2.8 升級核心版本至4.4
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org yum install https://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
#新增 repository 後, 列出可以使用的kernel包版本 yum --disablerepo="*" --enablerepo="elrepo-kernel" list available
#安裝需要的kernel版本,這裡安裝 kernel-lt(長期維護版) yum --enablerepo=elrepo-kernel install kernel-lt
#安裝完成檢查/boot/grub2/grub.cfg中對應的核心menuentry中是否包含initrd16配置,如果沒有再安裝一次 cat /boot/grub2/grub.cfg vi /etc/default/grub GRUB_DEFAULT=0
#重新建立核心配置 grub2-mkconfig -o /boot/grub2/grub.cfg
#重啟 reboot
3 kubernetes 叢集部署
3.1 kube-proxy 開啟 ipvs 的前置條件(三個節點)
舊版本使用 iptables 做轉發,新版本預設使用 lvs 做轉發,所以進行如下設定:
modprobe br_netfilter 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
3.2 安裝 docker(三個節點)
#1、配置yum源 wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo yum clean all yum makecache #2、docker安裝 yum install -y yum-utils device-mapper-persistent-data lvm2 yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo yum update -ycat /boot/grub2/grub.cfg | grep "CentOS Linux" grub2-set-default 'CentOS Linux (4.4.230-1.el7.elrepo.x86_64) 7 (Core)' grub2-mkconfig -o /boot/grub2/grub.cfg reboot
yum install -y docker-ce
#3、建立/etc/docker目錄
mkdir /etc/docker
#配置daemon
cat > /etc/docker/daemon.json <<EOF
{
"registry-mirrors": ["https://162ncvra.mirror.aliyuncs.com"]
}
EOF
mkdir -p /etc/systemd/system/docker.service.d
#4、啟動服務
systemctl daemon-reload && systemctl restart docker && systemctl enable docker
#5、命令補全:安裝/載入bash-completion
yum install bash-completion -y
source /etc/profile.d/bash_completion.sh
#6、驗證
docker --version
docker run hello-world
3.3 設定 kubernetes 源(三個節點)
#1、驗證mac和uuid,保證各節點mac和uuid唯一 cat /sys/class/net/ens32/address cat /sys/class/dmi/id/product_uuid #2、禁用swap #臨時生效 swapoff -a #永久生效 sed -i.bak '/swap/s/^/#/' /etc/fstab #3、核心引數修改 cat /proc/sys/net/bridge/bridge-nf-call-iptables cat /proc/sys/net/bridge/bridge-nf-call-ip6tables (1)臨時修改 sysctl net.bridge.bridge-nf-call-iptables=1 sysctl net.bridge.bridge-nf-call-ip6tables=1 (2)永久修改 cat <<EOF > /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-iptables=1 net.bridge.bridge-nf-call-ip6tables=1 EOF sysctl -p /etc/sysctl.d/k8s.conf #4、修改cgroup driver #修改daemon.json,新增 "exec-opts": ["native.cgroupdriver=systemd"] cat /etc/docker/daemon.json { "registry-mirrors": ["https://162ncvra.mirror.aliyuncs.com"], "exec-opts": ["native.cgroupdriver=systemd"] } #重新載入docker systemctl daemon-reload systemctl restart docker #修改cgroup driver,是為了消除告警 #5、設定kubernetes源 cat <<EOF > /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/ enabled=1 gpgcheck=1 repo_gpgcheck=1 gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg EOF yum clean all yum -y makecache
3.4 安裝 kubernetes(三個節點)
yum list kubelet --showduplicates | sort -r yum install -y kubelet-1.15.1 kubeadm-1.15.1 kubectl-1.15.1 #建議指定版本安裝,保持環境一致。不指定版本安裝為最新版。 #kubelet:執行在叢集所有節點上,用於啟動pod和容器等物件的工具 #kubeadm:用於初始化叢集,啟動叢集的命令工具 #kubectl:用於和叢集通訊的命令列,通過kubectl可以部署和管理應用,檢視各種資源,建立刪除和更新各種元件 #啟動kubelet systemctl enable kubelet && systemctl start kubelet #kubelet命令補全 echo "source <(kubectl completion bash)" >> ~/.bash_profile
3.5 下載映象(三個節點)
將下載好的包上傳到伺服器並解壓
連結:https://pan.baidu.com/s/1H8TtCc1VfPTw_iCMgODsdw
提取碼:u7sx
tar xvf kubeadm-basic.images.tar.gz #編寫安裝指令碼 vim image.sh #!/bin/bash ls /root/kubeadm-basic.images > /tmp/image-list.txt cd /root/kubeadm-basic.images for i in $( cat /tmp/image-list.txt ) do docker load -i $i done
rm -rf /tmp/image-list.txt
#執行匯入 sh image.sh
3.6 初始化配置 master 節點
初始化完成記錄 node 節點加入叢集的命令:
#初始化配置Master [root@k8s-master ~]# kubeadm config print init-defaults > kubeadm-config.yaml
#開啟該配置檔案,修改如下配置:
advertiseAddress:192.168.81.131
kubernetesVersion:v1.15.1
新增:podSubnet:10.244.0.0/16
並開通ipvs認證方式,文末新增如下: --- apiVersion: kubeproxy.config.k8s.io/v1alpha1 kind: kubeProxyConfiguration featureGates: SupportIPVSProxyMode: true mode: ipvs
[root@k8s-master ~]# vim kubeadm-config.yaml [root@k8s-master ~]# cat kubeadm-config.yaml apiVersion: kubeadm.k8s.io/v1beta2 bootstrapTokens: - groups: - system:bootstrappers:kubeadm:default-node-token token: abcdef.0123456789abcdef ttl: 24h0m0s usages: - signing - authentication kind: InitConfiguration localAPIEndpoint: advertiseAddress: 192.168.81.131 bindPort: 6443 nodeRegistration: criSocket: /var/run/dockershim.sock name: k8s-master taints: - effect: NoSchedule key: node-role.kubernetes.io/master --- apiServer: timeoutForControlPlane: 4m0s apiVersion: kubeadm.k8s.io/v1beta2 certificatesDir: /etc/kubernetes/pki clusterName: kubernetes controllerManager: {} dns: type: CoreDNS etcd: local: dataDir: /var/lib/etcd imageRepository: k8s.gcr.io kind: ClusterConfiguration kubernetesVersion: v1.15.1 networking: dnsDomain: cluster.local podSubnet: 10.244.0.0/16 serviceSubnet: 10.96.0.0/12 scheduler: {} --- apiVersion: kubeproxy.config.k8s.io/v1alpha1 kind: kubeProxyConfiguration featureGates: SupportIPVSProxyMode: true mode: ipvs #初始化Master [root@k8s-master ~]# kubeadm init --config=kubeadm-config.yaml --experimental-upload-certs|tee kubeadim-init.log
[root@k8s-master ~]# mkdir -p $HOME/.kube
[root@k8s-master ~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@k8s-master ~]# sudo chown $(id -u):$(id -g) $HOME/.kube/config
3.7 master 節點,安裝 pod 網路
[root@k8s-master ~]# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
3.8 node 節點加入叢集
node1 節點:
[root@k8s-node1 ~]# kubeadm join 192.168.81.131:6443 --token abcdef.0123456789abcdef \ --discovery-token-ca-cert-hash sha256:071999240d2fd37b38cffde6918ad00afc0adb6ff5385547f3cf0f633289ed85
node2 節點:
[root@k8s-node2 ~]# kubeadm join 192.168.81.131:6443 --token abcdef.0123456789abcdef \ --discovery-token-ca-cert-hash sha256:071999240d2fd37b38cffde6918ad00afc0adb6ff5385547f3cf0f633289ed85
4 kubernetes 叢集驗證
[root@k8s-master ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-master NotReady master 11m v1.15.1 k8s-node1 NotReady <none> 6m57s v1.15.1 k8s-node2 NotReady <none> 2m40s v1.15.1