准备
apt-get 修改国内源
mv /etc/apt/sources.list /etc/apt/sources.list.bk
cat > /etc/apt/sources.list <<EOF
deb https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
EOF
更改机器名
hostnamectl set-hostname master # master 节点
hostnamectl set-hostname node1 # node1 节点
关闭swap和防火墙
swapoff -a
sed -i '/swap/s/^/#/' /etc/fstab
开启IPv4转发
每台机器都要设置
cat <<EOF | tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
sysctl --system
sudo modprobe br_netfilter
echo '1' | tee /proc/sys/net/bridge/bridge-nf-call-iptables
cat >> /etc/sysctl.conf <<EOF
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sysctl -p
echo "br_netfilter" > /etc/modules-load.d/br_netfilter.conf
安装K8S相关
安装containerd
apt install containerd
配置crictl客户端
cat > /etc/crictl.yaml <<EOF
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
timeout: 10
debug: true
EOF
安装kubuadm、kubelet、kubectl
apt install -y apt-transport-https curl
curl -fsSL 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 update
apt install -y kubelet kubeadm kubectl
systemctl enable kubelet
初始化master节点
拉取下载镜像
kubeadm config images pull --image-repository=registry.aliyuncs.com/google_containers --kubernetes-version="v1.28.2"
master 初始化(node 节点不需要)
kubeadm init --kubernetes-version=v1.28.2 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --image-repository=registry.aliyuncs.com/google_containers --upload-certs
master 查看 token 和 discovery-token-ca-cert-hash
kubeadm token list # 查看 token 列表,node 节点加入 master 时 token 的值
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //' # node 节点连接 master 时 --discovery-token-ca-cert-hash 参数的值
node节点加入master
kubeadm join $master_ip:6443 --token $token --discovery-token-ca-cert-hash sha256:$hash_value # $master_ip: master节点的IP;$token:master节点上“kubeadm token list”出来的token值;$hash_value:openssl x509....出来的值
部署应用
kubectl get deployment
kubectl get service
kubectl get pod -A
遇到问题:
执行 kubeadm init 时报错: [ERROR CRI]: container runtime is not running:
报错内容:
error execution phase preflight: [preflight] Some fatal errors occurred:
[ERROR CRI]: container runtime is not running: output: time="2023-02-20T08:33:48Z" level=fatal msg="validate service connection: CRI v1 runtime API is not implemented for endpoint "unix:///var/run/containerd/containerd.sock": rpc error: code = Unimplemented desc = unknown service runtime.v1.RuntimeService"
, error: exit status 1
[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`
To see the stack trace of this error execute with --v=5 or higher
service containerd status # 显示running
解决:
mv /etc/containerd/config.toml /etc/containerd/config.toml.bk
service containerd restart
执行 kubeadm init 时报错: error execution phase preflight: [preflight] Some fatal errors occurred:
报错内容:
[init] Using Kubernetes version: v1.28.2
[preflight] Running pre-flight checks
error execution phase preflight: [preflight] Some fatal errors occurred:
[ERROR FileAvailable--etc-kubernetes-manifests-kube-apiserver.yaml]: /etc/kubernetes/manifests/kube-apiserver.yaml already exists
[ERROR FileAvailable--etc-kubernetes-manifests-kube-controller-manager.yaml]: /etc/kubernetes/manifests/kube-controller-manager.yaml already exists
[ERROR FileAvailable--etc-kubernetes-manifests-kube-scheduler.yaml]: /etc/kubernetes/manifests/kube-scheduler.yaml already exists
[ERROR FileAvailable--etc-kubernetes-manifests-etcd.yaml]: /etc/kubernetes/manifests/etcd.yaml already exists
[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`
To see the stack trace of this error execute with --v=5 or higher
解决:删除 /etc/kubernetes/manifests
mv /etc/kubernetes/manifests /etc/kubernetes/manifests-bk
标签:K8S,kubernetes,--,apt,etc,token,master,Ubuntu20,安装
From: https://www.cnblogs.com/aaron-agu/p/18214175