k8s安装教程

1 检查 centos / hostname(在 master 节点和 worker 节点都要执行)

# 在 master 节点和 worker 节点都要执行
cat /etc/redhat-release

# 此处 hostname 的输出将会是该机器在 Kubernetes 集群中的节点名字
# 不能使用 localhost 作为节点的名字
hostname

# 请使用 lscpu 命令,核对 CPU 信息
# Architecture: x86_64   本安装文档不支持 arm 架构
# CPU(s):       2         CPU 内核数量不能低于 2
lscpu

2 修改 hostname (在 master 节点和 worker 节点都要执行)

如果您需要修改 hostname,可执行如下指令:

# 修改 hostname
hostnamectl set-hostname your-new-host-name
# 查看修改结果
hostnamectl status
# 设置 hostname 解析
echo "127.0.0.1   $(hostname)" >> /etc/hosts

3 安装containerd/kubelet/kubeadm/kubectl(在 master 节点和 worker 节点都要执行)

# 最后一个参数 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 节点(只在master节点执行)

关于初始化时用到的环境变量

  • APISERVER_NAME 不能是 master 的 hostname

  • APISERVER_NAME 必须全为小写字母、数字、小数点,不能包含减号

  • POD_SUBNET 所使用的网段不能与 master节点/worker节点 所在的网段重叠。该字段的取值为一个 CIDR 值,如果您对 CIDR 这个概念还不熟悉,请仍然执行 export POD_SUBNET=10.100.0.0/16 命令,不做修改

# 替换 192.168.x.x 为 master 节点实际 IP(请使用内网 IP)
export MASTER_IP=192.168.x.x

# 替换 apiserver.demo 为 您想要的 dnsName
export APISERVER_NAME=apiserver.demo

# Kubernetes 容器组所在的网段,该网段安装完成后,由 kubernetes 创建,事先并不存在于您的物理网络中
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

如果出现如下错误:

[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

请执行如下命令:

在原命令的最后增加参数 /coredns

curl -sSL https://kuboard.cn/install-script/v1.22.x/init_master.sh | sh -s 1.22.5 /coredns

4.1 检查 master 初始化结果(只在 master 节点执行)

coredns 将处于启动失败的状态,请继续下一步,完成 安装网络插件 这个步骤后,coredns 将正常启动

# 执行如下命令,等待 3-10 分钟,直到所有的容器组处于 Running 状态
watch kubectl get pod -n kube-system -o wide

# 查看 master 节点初始化结果
kubectl get nodes -o wide

4.2 安装网络插件(只在 master 节点执行)

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节点

5.1 在 master 节点上执行(只在 master 节点执行)

kubeadm token create --print-join-command

可获取kubeadm join 命令及参数,如下所示,有效时间2小时

# kubeadm token create 命令的输出
kubeadm join apiserver.demo:6443 --token 7pk9ym.cedespgigk1mnqej --discovery-token-ca-cert-hash sha256:e7cbdf2463b98f324bda2348af602513a1477ca63677367e26313563f080fc37

5.2 针对所有的 worker 节点执行 (只在 worker 节点执行)

# 替换 x.x.x.x 为 master 节点的内网 IP
export MASTER_IP=x.x.x.x

# 替换 apiserver.demo 为初始化 master 节点时所使用的 APISERVER_NAME
export APISERVER_NAME=apiserver.demo
echo "${MASTER_IP}    ${APISERVER_NAME}" >> /etc/hosts

# 替换为 master 节点上 kubeadm token create 命令的输出
kubeadm join apiserver.demo:6443 --token 7pk9ym.cedespgigk1mnqej --discovery-token-ca-cert-hash sha256:e7cbdf2463b98f324bda2348af602513a1477ca63677367e26313563f080fc37

5.3 检查初始化结果(只在 master 节点执行)

# 只在 master 节点执行
kubectl get nodes -o wide

 

原文地址:https://www.cnblogs.com/marshu/p/15748474.html