k8s安裝教程
1 檢查 centos / hostname(在 master 節(jié)點(diǎn)和 worker 節(jié)點(diǎn)都要執(zhí)行)
# 在 master 節(jié)點(diǎn)和 worker 節(jié)點(diǎn)都要執(zhí)行
cat /etc/redhat-release
?
# 此處 hostname 的輸出將會(huì)是該機(jī)器在 Kubernetes 集群中的節(jié)點(diǎn)名字
# 不能使用 localhost 作為節(jié)點(diǎn)的名字
hostname
?
# 請(qǐng)使用 lscpu 命令,核對(duì) CPU 信息
# Architecture: x86_64 本安裝文檔不支持 arm 架構(gòu)
# CPU(s): 2 CPU 內(nèi)核數(shù)量不能低于 2
lscpu
2 修改 hostname (在 master 節(jié)點(diǎn)和 worker 節(jié)點(diǎn)都要執(zhí)行)
如果您需要修改 hostname,可執(zhí)行如下指令:
# 修改 hostname
hostnamectl set-hostname your-new-host-name
# 查看修改結(jié)果
hostnamectl status
# 設(shè)置 hostname 解析
echo "127.0.0.1 $(hostname)" >> /etc/hosts
3 安裝containerd/kubelet/kubeadm/kubectl(在 master 節(jié)點(diǎn)和 worker 節(jié)點(diǎn)都要執(zhí)行)
# 最后一個(gè)參數(shù) 1.22.3 用于指定 kubenetes 版本,支持所有 1.22.x 版本的安裝
# 騰訊云 docker hub 鏡像
# export REGISTRY_MIRROR="https://mirror.ccs.tencentyun.com"
# DaoCloud 鏡像
# export REGISTRY_MIRROR="http://f1361db2.m.daocloud.io"
# 華為云鏡像
# export REGISTRY_MIRROR="https://05f073ad3c0010ea0f4bc00b7105ec20.mirror.swr.myhuaweicloud.com"
# 阿里云 docker hub 鏡像
export REGISTRY_MIRROR=https://registry.cn-hangzhou.aliyuncs.com
curl -sSL https://kuboard.cn/install-script/v1.22.x/install_kubelet.sh | sh -s 1.22.5
4 初始化 master 節(jié)點(diǎn)(只在master節(jié)點(diǎn)執(zhí)行)
關(guān)于初始化時(shí)用到的環(huán)境變量
APISERVER_NAME 不能是 master 的 hostname
APISERVER_NAME
POD_SUBNET 所使用的網(wǎng)段不能與 master節(jié)點(diǎn)/worker節(jié)點(diǎn) 所在的網(wǎng)段重疊。該字段的取值為一個(gè)
# 替換 192.168.x.x 為 master 節(jié)點(diǎn)實(shí)際 IP(請(qǐng)使用內(nèi)網(wǎng) IP)
export MASTER_IP=192.168.x.x
?
# 替換 apiserver.demo 為 您想要的 dnsName
export APISERVER_NAME=apiserver.demo
?
# Kubernetes 容器組所在的網(wǎng)段,該網(wǎng)段安裝完成后,由 kubernetes 創(chuàng)建,事先并不存在于您的物理網(wǎng)絡(luò)中
export POD_SUBNET=10.100.0.0/16
echo "${MASTER_IP} ${APISERVER_NAME}" >> /etc/hosts
curl -sSL https://kuboard.cn/install-script/v1.22.x/init_master.sh | sh -s 1.22.5
如果出現(xiàn)如下錯(cuò)誤:
[config/images] Pulled registry.aliyuncs.com/k8sxio/pause:3.2
[config/images] Pulled registry.aliyuncs.com/k8sxio/etcd:3.4.13-0
failed to pull image "swr.cn-east-2.myhuaweicloud.com/coredns:1.8.0": output: time="2021-04-30T13:26:14+08:00" level=fatal
msg="pulling image failed: rpc error: code = NotFound desc = failed to pull and unpack image \"swr.cn-east-2.myhuaweicloud.com/coredns:1.8.0\":
failed to resolve reference \"swr.cn-east-2.myhuaweicloud.com/coredns:1.8.0\":
swr.cn-east-2.myhuaweicloud.com/coredns:1.8.0: not found", error: exit status 1
To see the stack trace of this error execute with --v=5 or higher
請(qǐng)執(zhí)行如下命令:
在原命令的最后增加參數(shù)
/coredns
curl -sSL https://kuboard.cn/install-script/v1.22.x/init_master.sh | sh -s 1.22.5 /coredns
4.1 檢查 master 初始化結(jié)果(只在 master 節(jié)點(diǎn)執(zhí)行)
coredns 將處于啟動(dòng)失敗的狀態(tài),請(qǐng)繼續(xù)下一步,完成 安裝網(wǎng)絡(luò)插件 這個(gè)步驟后,coredns 將正常啟動(dòng)
# 執(zhí)行如下命令,等待 3-10 分鐘,直到所有的容器組處于 Running 狀態(tài)
watch kubectl get pod -n kube-system -o wide
?
# 查看 master 節(jié)點(diǎn)初始化結(jié)果
kubectl get nodes -o wide
4.2 安裝網(wǎng)絡(luò)插件(只在 master 節(jié)點(diǎn)執(zhí)行)
export POD_SUBNET=10.100.0.0/16
wget https://kuboard.cn/install-script/flannel/flannel-v0.14.0.yaml
sed -i "s#10.244.0.0/16#${POD_SUBNET}#" flannel-v0.14.0.yaml
kubectl apply -f ./flannel-v0.14.0.yaml
5 初始化 worker節(jié)點(diǎn)
5.1 在 master 節(jié)點(diǎn)上執(zhí)行(只在 master 節(jié)點(diǎn)執(zhí)行)
kubeadm token create --print-join-command
可獲取kubeadm join 命令及參數(shù),如下所示,有效時(shí)間2小時(shí)
# kubeadm token create 命令的輸出
kubeadm join apiserver.demo:6443 --token 7pk9ym.cedespgigk1mnqej --discovery-token-ca-cert-hash sha256:e7cbdf2463b98f324bda2348af602513a1477ca63677367e26313563f080fc37
5.2 針對(duì)所有的 worker 節(jié)點(diǎn)執(zhí)行 (只在 worker 節(jié)點(diǎn)執(zhí)行)
# 替換 x.x.x.x 為 master 節(jié)點(diǎn)的內(nèi)網(wǎng) IP
export MASTER_IP=x.x.x.x
?
# 替換 apiserver.demo 為初始化 master 節(jié)點(diǎn)時(shí)所使用的 APISERVER_NAME
export APISERVER_NAME=apiserver.demo
echo "${MASTER_IP} ${APISERVER_NAME}" >> /etc/hosts
?
# 替換為 master 節(jié)點(diǎn)上 kubeadm token create 命令的輸出
kubeadm join apiserver.demo:6443 --token 7pk9ym.cedespgigk1mnqej --discovery-token-ca-cert-hash sha256:e7cbdf2463b98f324bda2348af602513a1477ca63677367e26313563f080fc37
5.3 檢查初始化結(jié)果(只在 master 節(jié)點(diǎn)執(zhí)行)
# 只在 master 節(jié)點(diǎn)執(zhí)行
kubectl get nodes -o wide

浙公網(wǎng)安備 33010602011771號(hào)