kubeadm安裝k8s

无敌小鲁班發表於2024-08-05

目錄
  • 1.環境準備
  • 2.所有節點安裝docker
  • 3.所有節點安裝kubeadm
  • 4.Nginx負載均衡部署
  • 5.部署K8S叢集
  • 6.所有節點部署網路外掛flannel

1.環境準備

(1)在所有節點上安裝Docker和kubeadm
(2)部署Kubernetes Master
(3)部署容器網路外掛
(4)部署 Kubernetes Node,將節點加入Kubernetes叢集中
(5)部署 Dashboard Web 頁面,視覺化檢視Kubernetes資源
(6)部署 Harbor 私有倉庫,存放映象資源

#關閉防火牆
systemctl stop firewalld
systemctl disable firewalld
iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X 

#關閉selinux
setenforce 0
sed -i 's/enforcing/disabled/' /etc/selinux/config
vim /etc/selinux/config

#關閉swap
swapoff -a  #交換分割槽必須要關閉

#永久關閉swap分割槽,&符號在sed命令中代表上次匹配的結果
sed -ri 's/.*swap.*/#&/' /etc/fstab 

#載入 ip_vs 模組
for i in $(ls /usr/lib/modules/$(uname -r)/kernel/net/netfilter/ipvs|grep -o "^[^.]*");do echo $i; /sbin/modinfo -F filename $i >/dev/null 2>&1 && /sbin/modprobe $i;done

#修改主機名
hostnamectl set-hostname master01
hostnamectl set-hostname master02
hostnamectl set-hostname master03
hostnamectl set-hostname node01
hostnamectl set-hostname node02

bash

#在master新增hosts
vim /etc/hosts
20.0.0.160 master01
20.0.0.170 master02
20.0.0.180 master03
20.0.0.130 node01
20.0.0.140 node02

#調整核心引數
cat > /etc/sysctl.d/kubernetes.conf << EOF
#開啟網橋模式,可將網橋的流量傳遞給iptables鏈
net.bridge.bridge-nf-call-ip6tables=1
net.bridge.bridge-nf-call-iptables=1
#關閉ipv6協議
net.ipv6.conf.all.disable_ipv6=1
net.ipv4.ip_forward=1
EOF

ls
sysctl --system   #載入

#核心限制最佳化配置
vim /etc/security/limits.conf
* soft nofile 65536
* hard nofile 65536
* soft nproc 65536
* hard nproc 65536
* soft memlock unlimited
* hard memlock unlimited

#升級核心
uname -r  #檢視核心
hostnamectl
yum clean all && yum makecache
cd /etc/yum.repos.d/
ls
mv local.repo repo.bak/
ls
vim /etc/yum.repos.d/elrepo.repo
[elrepo]
name=elrepo
baseurl=https://mirrors.aliyun.com/elrepo/archive/kernel/el7/x86_64
gpgcheck=0
enabled=1

rz -E   #上傳aliyun的線上源
CentOS-Base-aliyun.repo

yum clean all   #清空快取
yum install -y kernel-lt kernet-lt-devel   #叢集升級核心
#awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg  #檢視核心序號
grub2-set-default 0   #設定預設啟動核心
reboot   #重啟作業系統
uname -r 
hostnamectl  #檢視生效版本




















2.所有節點安裝docker

#yum install -y yum-utils device-mapper-persistent-data lvm2 
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 
yum install -y docker-ce docker-ce-cli containerd.io

cat > /etc/docker/daemon.json <<EOF
{
  "registry-mirrors": ["https://6ijb8ubo.mirror.aliyuncs.com", "https://docker.m.daocloud.io", "https://docker.1panel.live"],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "500m", "max-file": "3"
  }
}
EOF

systemctl enable --now docker
docker info



3.所有節點安裝kubeadm

//定義kubernetes源
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

yum install -y kubelet-1.20.15 kubeadm-1.20.15 kubectl-1.20.15

//開機自啟kubelet
systemctl enable kubelet.service


4.Nginx負載均衡部署

20.0.0.100 lb01
20.0.0.110 lb02

#### 在lb01、lb02節點上操作####
systemctl disable --now firewalld
setenforce 0
vim /etc/selinux/config
    disabled
    
cd /etc/yum.repos.d/
ls
rz -E 上傳nginx.repo
yum install -y nginx
cd /etc/nginx/
ls
nginx -V
//修改nginx配置檔案,配置四層反向代理負載均衡,指定k8s群集2臺master的節點ip和6443埠
vim nginx.conf

//檢查配置檔案語法
nginx -t   

//啟動nginx服務,檢視已監聽6443埠
systemctl enable --now nginx
netstat -lntp | grep nginx 

//部署keepalived服務
yum install -y keepalived 

#### 在lb01節點上操作####
//修改keepalived配置檔案
cd /etc/keepalived/
ls
cp keepalived.conf keepalived.conf.bak
ls
touch nginx_check.sh
vim nginx_check.sh

#!/bin/bash

if ! killall -0 nginx &> /dev/null
   then
   systemctl stop keepalived
fi

chmod +x nginx_check.sh
vim  keepalived.conf

scp keepalived.conf nginx_check.sh 20.0.0.110:`pwd`


#### 20.0.0.110 lb02 #####
vim /etc/keepalived/keepalived.conf

router_id NGINX_01
state BACKUP
priority 90

systemctl enable --now keepalived.service
ip a

#驗證
#####20.0.0.100 lb01#####
systemctl stop nginx
ip a

#####20.0.0.110 lb01#####
ip a

#####20.0.0.110 lb01#####
systemctl start nginx keepalived.service

















驗證


5.部署K8S叢集

//在 master01 節點上操作
cd /opt/
mkdir k8s
cd k8s/
ls

//初始化kubeadm
kubeadm config print init-defaults > kubeadm-config.yaml
ls
vim kubeadm-config.yaml


//線上拉取映象
cd /opt/k8s/
kubeadm config images list --kubernetes-version 1.20.15  #檢視需要哪些映象
kubeadm config images pull --config kubeadm-config.yaml

//在 master01 節點上操作
scp kubeadm-config.yaml master 02:/opt/k8s/
scp kubeadm-config.yaml master 03:/opt/k8s/

//在 master02 、master03節點上操作
cd /opt/
mkdir k8s
ls
kubeadm config images pull --config kubeadm-config.yaml





注:首行縮排兩空格,冒號後空格


  • 線上拉取映象





//初始化 master01 
#kubeadm reset ##重置

kubeadm init --config=kubeadm-config.yaml --upload-certs | tee kubeadm-init.log
#--upload-certs 引數可以在後續執行加入節點時自動分發證書檔案
#tee kubeadm-init.log 用以輸出日誌

mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config  #在master節點檢視節點狀態

kubectl get nodes


//在 master02 、master03節點上操作
kubeadm join 20.0.0.88:6443 --token abcdef.0123456789abcdef \
    --discovery-token-ca-cert-hash sha256:33e1edc63043eeab570588be06de5ac5711e3c9a60f7e3047948d3a96f1ac6bc \
    --control-plane --certificate-key 6a31310b6e019ebb74d2134b5f279263aebe3897217650a60c7f27920bf3d34e


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


kubectl get nodes


//在 node 節點上執行 kubeadm join 命令加入群集
kubeadm join 20.0.0.88:6443 --token abcdef.0123456789abcdef \
    --discovery-token-ca-cert-hash sha256:33e1edc63043eeab570588be06de5ac5711e3c9a60f7e3047948d3a96f1ac6bc







6.所有節點部署網路外掛flannel

20.0.0.160 master01
20.0.0.170 master02
20.0.0.180 master03
20.0.0.130 node01
20.0.0.140 node02

//在 master01 節點上操作
cd /opt/k8s/
rz -E 上傳flannel-v0.21.5.zip
unzip flannel-v0.21.5.zip
ls
docker load -i flannel.tar
docker load -i flannel-cni-plugin.tar


//在node 01 02 節點上操作
cd /opt/
mkdir k8s
cd k8s/


//在 master01 節點上操作
scp cni-plugins-linux-amd64-v1.3.0.tgz flannel-cni-plugin.tar flannel.tar master02:/opt/k8s/

scp cni-plugins-linux-amd64-v1.3.0.tgz flannel-cni-plugin.tar flannel.tar master03:/opt/k8s/

scp cni-plugins-linux-amd64-v1.3.0.tgz flannel-cni-plugin.tar flannel.tar node01:/opt/k8s/

scp cni-plugins-linux-amd64-v1.3.0.tgz flannel-cni-plugin.tar flannel.tar node02:/opt/k8s/


//在 master02 03,node 01 02節點上操作
docker load -i flannel.tar
docker load -i flannel-cni-plugin.tar




//在 所有 節點上操作
20.0.0.160 master01
20.0.0.170 master02
20.0.0.180 master03
20.0.0.130 node01
20.0.0.140 node02

cd /opt/
ls 
mv cni/ cni_bak
ls
mkdir -p /opt/cni/bin
cd /opt/k8s/
tar xf cni-plugins-linux-amd64-v1.3.0.tgz -C /opt/cni/bin 
ls !$

//在 master 節點建立 flannel 資源
kubectl apply -f kube-flannel.yml 
kubectl get pods -A
kubectl get nodes  #在master節點檢視節點狀態


#建立service account並繫結預設cluster-admin管理員叢集角色
kubectl create serviceaccount dashboard-admin -n kube-system
kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')


//測試訪問
https://20.0.0.130:30001



相關文章