Kubeadm 搭建 Kubernetes 集群

主機(jī)規(guī)劃

使用 kubeadm 初始化 kubernetes 集群燕偶,操作系統(tǒng)為 CentOS 7.6.1810 x86_64唤崭,用到的各相關(guān)程序版本如下:

  • ubuntu: v18.04.6 LTS
  • kubeadm: v1.22.6
  • docker: v20.10.6
  • flannel: v1.0.1
主機(jī)名 IP 地址 配置
kubeadm-master-01 192.168.124.200 8GB Ram, 2vcpus
kubeadm-node-01 192.168.124.211 8GB Ram, 2vcpus
kubeadm-node-02 192.168.124.212 8GB Ram, 2vcpus
kubeadm-node-03 192.168.124.212 8GB Ram, 2vcpus

準(zhǔn)備

  • 升級(jí)軟件包
apt update
apt -y upgrade
  • 主機(jī)名解析
cat >> /etc/hosts <<EOF

192.168.124.200 kubeadm-master-01
192.168.124.211 kubeadm-node-01
192.168.124.212 kubeadm-node-02
192.168.124.213 kubeadm-node-03

# 用來(lái)擴(kuò)展集群為高可用集群
192.168.124.200 kubeadm-vip.linux.io
EOF
  • 時(shí)間同步
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

apt install ntpdate -y
ntpdate time1.aliyun.com
echo "*/3 * * * * /usr/sbin/ntpdate time1.aliyun.com &> /dev/null" >> /var/spool/cron/crontabs/root
  • 關(guān)閉防火墻和 swap 分區(qū)
ufw disable
sed -i 's@^/swap@# &1@g' /etc/fstab  && swapoff  -a
  • 調(diào)整內(nèi)核參數(shù)
modprobe overlay && modprobe br_netfilter

cat >> /etc/sysctl.d/99-kubernetes-cri.conf << EOF
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
user.max_user_namespaces=28633
vm.swappiness=0
EOF

sysctl  --system
  • 配置 ipvs 功能
apt install ipvsadm ipset -y

modprobe ip_vs && modprobe ip_vs_rr && modprobe ip_vs_wrr && modprobe ip_vs_sh
cat <<EOF >> /etc/modules
ip_vs_rr
ip_vs_wrr
ip_vs_sh
ip_vs
EOF

reboot

lsmod | grep -e ip_vs -e nf_conntrack_ipv4
ip_vs_sh               16384  0
ip_vs_wrr              16384  0
ip_vs_rr               16384  0
ip_vs                 151552  6 ip_vs_rr,ip_vs_sh,ip_vs_wrr
nf_defrag_ipv6         20480  1 ip_vs
nf_conntrack          135168  1 ip_vs
libcrc32c              16384  4 nf_conntrack,xfs,raid456,ip_vs

安裝容器運(yùn)行時(shí)

apt-get remove docker docker-engine docker.io
apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common

curl -fsSL https://repo.huaweicloud.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

add-apt-repository "deb [arch=amd64] https://repo.huaweicloud.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

apt update
apt-cache madison docker-ce
apt install docker-ce=5:20.10.6~3-0~ubuntu-bionic -y
mkdir /etc/docker
cat >> /etc/docker/daemon.json << EOF
{
    "registry-mirrors": ["https://o4uba187.mirror.aliyuncs.com"],
    "exec-opts": ["native.cgroupdriver=systemd"],
    "log-driver": "json-file",
    "log-opts": {
    "max-size": "100m"
    },
    "storage-driver": "overlay2",
    "graph":  "/var/lib/docker"
}
EOF

systemctl  enable docker --now

安裝 Kubeadm

apt-get update && apt-get install -y apt-transport-https
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
apt-get update
apt-get install kubeadm=1.22.5-00 kubelet=1.22.5-00 kubectl=1.22.5-00
systemctl enable kubelet.service

初始化 Master 節(jié)點(diǎn)

kubeadm init --kubernetes-version=v1.22.5 \
    --control-plane-endpoint=kubeadm-vip.linux.io \
    --apiserver-advertise-address=0.0.0.0 \
    --pod-network-cidr=10.244.0.0/16   \
    --service-cidr=10.96.0.0/12 \
    --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers \
    --ignore-preflight-errors=Swap | tee kubeadm-init.log

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf

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/

You can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:

  kubeadm join kubeadm-vip.linux.io:6443 --token pa5fyb.mcoy474qbznp8cvd \
    --discovery-token-ca-cert-hash sha256:5c1deff287d32725b8c3eaab49a32afc4f046ff1514056b15f11535c1fb45df5 \
    --control-plane

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join kubeadm-vip.linux.io:6443 --token pa5fyb.mcoy474qbznp8cvd \
    --discovery-token-ca-cert-hash sha256:5c1deff287d32725b8c3eaab49a32afc4f046ff1514056b15f11535c1fb45df5
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

部署 Flannel

kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml

Join worker 節(jié)點(diǎn)

kubeadm join kubeadm-vip.linux.io:6443 --token pa5fyb.mcoy474qbznp8cvd \
    --discovery-token-ca-cert-hash sha256:5c1deff287d32725b8c3eaab49a32afc4f046ff1514056b15f11535c1fb45df5

啟用 IPVS 模式

kubectl  edit cm kube-proxy -n kube-system
    mode: "ipvs"

kubectl  delete pod -n kube-system -l k8s-app=kube-proxy
ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.96.0.1:443 rr
  -> 192.168.124.200:6443         Masq    1      0          0
TCP  10.96.0.10:53 rr
  -> 10.244.0.2:53                Masq    1      0          0
  -> 10.244.0.3:53                Masq    1      0          0
TCP  10.96.0.10:9153 rr
  -> 10.244.0.2:9153              Masq    1      0          0
  -> 10.244.0.3:9153              Masq    1      0          0
UDP  10.96.0.10:53 rr
  -> 10.244.0.2:53                Masq    1      0          0
  -> 10.244.0.3:53                Masq    1      0          0

驗(yàn)證集群

kubectl  create deployment myapp --image=ikubernetes/myapp:v1 --replicas=3
kubectl  expose deployment  myapp  --port=80 --target-port=80
kubectl  get pod -o wide && kubectl get svc
NAME                    READY   STATUS    RESTARTS   AGE   IP           NODE              NOMINATED NODE   READINESS GATES
myapp-7d4b7b84b-98cx7   1/1     Running   0          83s   10.244.3.3   kubeadm-node-03   <none>           <none>
myapp-7d4b7b84b-pfkqq   1/1     Running   0          83s   10.244.2.3   kubeadm-node-02   <none>           <none>
myapp-7d4b7b84b-zg5dc   1/1     Running   0          83s   10.244.1.4   kubeadm-node-01   <none>           <none>
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP   60m
myapp        ClusterIP   10.101.62.58   <none>        80/TCP    2m40s
kubectl  exec -it myapp-7d4b7b84b-98cx7 -- wget -O  - 10.244.1.4
Connecting to 10.244.1.4 (10.244.1.4:80)
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>

kubectl  exec -it myapp-7d4b7b84b-98cx7 -- ping 192.168.124.212

 kubectl  exec -it myapp-7d4b7b84b-98cx7 -- nslookup myapp
nslookup: can't resolve '(null)': Name does not resolve

Name:      myapp
Address 1: 10.101.62.58 myapp.default.svc.cluster.local
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末撑刺,一起剝皮案震驚了整個(gè)濱河市嫁蛇,隨后出現(xiàn)的幾起案子齿坷,更是在濱河造成了極大的恐慌聪蘸,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,718評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件疯搅,死亡現(xiàn)場(chǎng)離奇詭異濒生,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)幔欧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,683評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門(mén)罪治,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)丽声,“玉大人,你說(shuō)我怎么就攤上這事觉义⊙闵纾” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 158,207評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵晒骇,是天一觀的道長(zhǎng)霉撵。 經(jīng)常有香客問(wèn)我,道長(zhǎng)厉碟,這世上最難降的妖魔是什么喊巍? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,755評(píng)論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮箍鼓,結(jié)果婚禮上崭参,老公的妹妹穿的比我還像新娘。我一直安慰自己款咖,他們只是感情好何暮,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,862評(píng)論 6 386
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著铐殃,像睡著了一般海洼。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上富腊,一...
    開(kāi)封第一講書(shū)人閱讀 50,050評(píng)論 1 291
  • 那天坏逢,我揣著相機(jī)與錄音,去河邊找鬼赘被。 笑死是整,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的民假。 我是一名探鬼主播浮入,決...
    沈念sama閱讀 39,136評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼羊异!你這毒婦竟也來(lái)了事秀?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 37,882評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤野舶,失蹤者是張志新(化名)和其女友劉穎易迹,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體平道,經(jīng)...
    沈念sama閱讀 44,330評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡赴蝇,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,651評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了巢掺。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片句伶。...
    茶點(diǎn)故事閱讀 38,789評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖陆淀,靈堂內(nèi)的尸體忽然破棺而出考余,到底是詐尸還是另有隱情,我是刑警寧澤轧苫,帶...
    沈念sama閱讀 34,477評(píng)論 4 333
  • 正文 年R本政府宣布楚堤,位于F島的核電站,受9級(jí)特大地震影響含懊,放射性物質(zhì)發(fā)生泄漏身冬。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,135評(píng)論 3 317
  • 文/蒙蒙 一岔乔、第九天 我趴在偏房一處隱蔽的房頂上張望酥筝。 院中可真熱鬧,春花似錦雏门、人聲如沸嘿歌。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,864評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)宙帝。三九已至,卻和暖如春募闲,著一層夾襖步出監(jiān)牢的瞬間步脓,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,099評(píng)論 1 267
  • 我被黑心中介騙來(lái)泰國(guó)打工浩螺, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留靴患,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,598評(píng)論 2 362
  • 正文 我出身青樓年扩,卻偏偏與公主長(zhǎng)得像蚁廓,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子厨幻,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,697評(píng)論 2 351

推薦閱讀更多精彩內(nèi)容