K8S安裝

程序菜小子發表於2024-04-23

1、基礎設定

1.1 關閉防火牆

systemctl stop firewalld
systemctl disable firewalld

1.2 關閉selinux

setenforce 0 # 這個是臨時關閉

1.3 關閉swap

swapoff -a # 這個是臨時關閉

2、安裝docker

2.1 解除安裝舊版本

sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine

2.2 安裝yum-utils

sudo yum install -y yum-utils

2.3 設定阿里雲映象倉庫

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

2.4 安裝docker engine-community

sudo yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin

2.5 啟動docker

systemctl start docker

2.6 將docker設定為自啟動

systemctl enable docker

3、配置 containerd

3.1 配置並啟用containerd.conf

cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF
modprobe -- overlay
modprobe -- br_netfilter

3.2 設定網路引數,開啟iptable橋接模式與ip_forward

cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF

3.3 系統配置生效

sysctl --system

3.4 生成containerd 的預設配置檔案路徑

mkdir -p /etc/containerd
containerd config default | tee /etc/containerd/config.toml

3.5 修改config.xml檔案

vim /etc/containerd/config.toml
# 1、使用 / 搜尋 containerd.runtimes.runc.options 將該內容下發的 SystemdCgroup 改為 true
# 2、使用 / 搜尋 sandbox_image 將值改為 registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.6

3.6 重新載入配置檔案,並設定containerd 自啟動

systemctl daemon-reload
systemctl enable --now containerd

3.7 新增對外暴露的sock埠

cat > /etc/crictl.yaml <<EOF
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
timeout: 10
debug: false
EOF

4、安裝k8s

4.1 配置k8s映象倉庫

vi /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg

4.2 安裝k8s 1.26版本

yum install kubeadm-1.26* kubelet-1.26* kubectl-1.26* -y

4.3 配置LOCAL_IP和kubeadm-config.yaml檔案

export LOCAL_IP=192.168.110.30  # 我的ip地址 192.168.110.30
echo $LOCAL_IP
mkdir /etc/k8s
cd /etc/k8s
rm -f kubeadm-config.yaml
wget http://manongbiji.oss-cn-beijing.aliyuncs.com/ittailkshow/k8s/downloa
d/kubeadm-config.yaml
sed -i 's/{LOCAL_IP}/'$LOCAL_IP'/' kubeadm-config.yaml
kubeadm config migrate --old-config kubeadm-config.yaml --new-config new.yaml

4.4 啟動並配置自啟動

systemctl start kubelet
systemctl enable kubelet

4.5 啟動 containerd

systemctl restart containerd

4.6 配置daemon.json

vim /etc/docker/daemon.json
{
  "registry-mirrors": ["https://82m9ar63.mirror.aliyuncs.com"],
  "exec-opts": ["native.cgroupdriver=systemd"]
}
systemctl daemon-reload
systemctl restart docker

4.7 初始化kubeadm

kubeadm init --config /etc/k8s/new.yaml --upload-certs

4.8 根據k8s的提示,執行如下指令碼

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
export KUBECONFIG=/etc/kubernetes/admin.conf
cat >> /etc/profile <<-'EOF' export KUBECONFIG=/etc/kubernetes/admin.conf EOF

4.9 在從節點上執行如下指令碼( k8s 日誌中的配置)->從節點只需要執行到4.6就可以執行下方命令

kubeadm join 192.168.110.30:6443 --token 7t2weq.bjbawausm0jaxury \
        --discovery-token-ca-cert-hash sha256:0c656039a96c0aea3d88289649f5eda98cbc8c4cceaa3b1be38e059fae5565bb \
        --control-plane --certificate-key 71a99f892cc1c1b6658e8476dd0238eb109077d8773b2d90be4269aabf91a54b

相關文章