共計 6151 個字符,預計需要花費 16 分鐘才能閱讀完成。
今天就跟大家聊聊有關 kubeadm 如何部署單 Master 節點 K8S 集群,可能很多人都不太了解,為了讓大家更加了解,丸趣 TV 小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
1 環境
Host Name Role IP master1 master1 10.10.25.149 node1 node1 10.10.25.150 node2 node2 10.10.25.151
2 內核調優
vim /etc/sysctl.conf
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
vm.swappiness = 0
net.ipv4.neigh.default.gc_stale_time=120
net.ipv4.ip_forward = 1
# see details in https://help.aliyun.com/knowledge_detail/39428.html
net.ipv4.conf.all.rp_filter=0
net.ipv4.conf.default.rp_filter=0
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_announce=2
net.ipv4.conf.all.arp_announce=2
# see details in https://help.aliyun.com/knowledge_detail/41334.html
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 1024
net.ipv4.tcp_synack_retries = 2
kernel.sysrq = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-arptables = 1
modprobe br_netfilter
sysctl -p
3 設置文件最大描述符
echo * soft nofile 65536 /etc/security/limits.conf
echo * hard nofile 65536 /etc/security/limits.conf
echo * soft nproc 65536 /etc/security/limits.conf
echo * hard nproc 65536 /etc/security/limits.conf
echo * soft memlock unlimited /etc/security/limits.conf
echo * hard memlock unlimited /etc/security/limits.conf
4 配置 yum 源
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
cd /etc/yum.repos.d
wget https://download.docker.com/linux/centos/docker-ce.repo
5 安裝依賴和常用軟件
yum install -y epel-release
yum install -y yum-utils device-mapper-persistent-data lvm2 net-tools conntrack-tools wget vim ntpdate libseccomp libtool-ltdl lrzsz wget
6 時間同步
一個集群內的時間同步必不可少
systemctl enable ntpdate.service
echo */30 * * * * /usr/sbin/ntpdate time7.aliyun.com /dev/null 2 1 /tmp/crontab2.tmp
crontab /tmp/crontab2.tmp
systemctl start ntpdate.service
ntpdate -u ntp.api.bz
7 關閉 SELinux、防火墻
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i s/SELINUX=enforcing/SELINUX=disabled/g /etc/selinux/config
8 關閉系統的 Swap
swapoff -a
yes | cp /etc/fstab /etc/fstab_bak
cat /etc/fstab_bak |grep -v swap /etc/fstab
9 安裝 docker
yum list docker-ce --showduplicates | sort -r
yum install docker-ce- VERSION_STRING
systemctl daemon-reload
systemctl enable docker
systemctl start docker
10 配置 hosts 解析
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.10.25.151 node2
10.10.25.149 master-1
10.10.25.150 node1
11 配置節點免密登錄
ssh-keygen
ssh-copy-id -i ~/.ssh/id_rsa.pub 用戶名字 @192.168.x.xxx
12 配置 ipvs 模塊
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
chmod 755 /etc/sysconfig/modules/ipvs.modules bash /etc/sysconfig/modules/ipvs.modules lsmod | grep -e ip_vs -e nf_conntrack_ipv4
yum install ipset ipvsadm
13 master 和 node 節點安裝 kubelet kubeadm kubectl
yum install -y kubelet kubeadm kubectl master
systemctl enable kubelet
暫不啟動 kubelet
14 master 節點進行集群初始化
kubeadm init --kubernetes-version=v1.14.1 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12
保存這段內容
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
You should now deploy a pod network to the cluster.
Run kubectl apply -f [podnetwork].yaml with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 10.10.25.149:6443 --token r03k6k.rhc8lh0bhjzuz7vx \
--discovery-token-ca-cert-hash sha256:b6b354ce28904600e9e38b4803ca5834061f1ffce0cde08ab9fd002756fcfc14
15 創建相關文件夾
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
16 初始化 flannel 網絡
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
flannel 網絡默認使用 vxlan 模式此外 flannel 還有其他兩種模式分別為 udp 模式和 host-gw 模式
flannel 模式介紹:
vxlan 模式: 屬于隧道封裝, 豹紋會封裝很多層增加了額外開銷,vxlan 里面還有一種 Directrouting : ture 模式效率相對很高
host-gw 模式: 屬于三層網絡, 采用的是將宿主機作為網關不進行封裝傳輸, 其效果還優于 calico
udp 模式: 是由于 flannel 網絡出現的時候 linux 內核還不支持, 所以采用了 udp 方式, 此種方式的效率比 vxlan 的但是還要更加的底所以無需考慮, 這種模式也是造成業界任務 flannel 網絡效率底愿意之一
設計網絡模式需要在部署 k8s 集群前規劃好, 以免中途改變費時費力
cd /etc/kubernetes/manifests
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
vim kube-flannel.yml
修改網絡模式 為 host-gw 模式
data:
cni-conf.json: |
{
name : cbr0 ,
plugins : [
{
type : flannel ,
delegate : {
hairpinMode : true,
isDefaultGateway : true
}
},
{
type : portmap ,
capabilities : {
portMappings : true
}
}
]
}
net-conf.json: |
{
Network : 10.244.0.0/16 ,
Backend : {
Type : host-gw
}
}
default via 10.10.25.254 dev ens192 proto static metric 100
10.10.25.0/24 dev ens192 proto kernel scope link src 10.10.25.149 metric 100
10.244.0.0/24 dev cni0 proto kernel scope link src 10.244.0.1
10.244.1.0/24 via 10.10.25.151 dev ens192
10.244.2.0/24 via 10.10.25.150 dev ens192
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1
17 拷貝 kubelet 配置文件到 node 節點
scp /etc/sysconfig/kubelet 10.10.25.150:/etc/sysconfig/kubelet
scp /etc/sysconfig/kubelet 10.10.25.150:/etc/sysconfig/kubelet
18 將 node 節點加入到集群
在 node 節點運行以下命令
kubeadm join 10.10.25.149:6443 --token r03k6k.rhc8lh0bhjzuz7vx --discovery-token-ca-cert-hash sha256:b6b354ce28904600e9e38b4803ca5834061f1ffce0cde08ab9fd002756fcfc14
19 查看集群狀態
kubectl get node
NAME STATUS ROLES AGE VERSION
master-1 Ready master 109m v1.14.1
node1 Ready none 54m v1.14.1
node2 Ready none 54m v1.14.1
20 查看 kube-system 下的 pod
kubectl get pod -n kube-system -o wide
因為使用 kubeadm 部署 k8s 集群默認 pod 組件 pod 運行在 kebe-system 命名空間內
21 啟動 ipvs
kube-proxy 開啟 ipvs
kubectl edit cm kube-proxy -n kube-system
kubectl get pod -n kube-system | grep kube-proxy | awk {system( kubectl delete pod $1 -n kube-system)}
22 創建一個測試 pod 驗證集群
kubectl run net-test --image=alpine --replicas=2 sleep 3600
23 查看網卡信息
ifconfig
看完上述內容,你們對 kubeadm 如何部署單 Master 節點 K8S 集群有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注丸趣 TV 行業資訊頻道,感謝大家的支持。