前言
想写个项目,于是就搞个k8s
原理等网上文档比较全,我就省略掉了
头铁都默认选了最新版,有一些小坑,例如
Docker Engine 没有实现 CRI, 而这是容器运行时在 Kubernetes 中工作所需要的。 为此,必须安装一个额外的服务 cri-dockerd。 cri-dockerd 是一个基于传统的内置 Docker 引擎支持的项目, 它在 1.24 版本从 kubelet 中移除
环境
服务器
esxi 4台 ubuntu20.04
网络
IP | hostname |
---|---|
10.2.7.100 | k8s-master |
10.2.7.101 | k8s-node1 |
10.2.7.102 | k8s-node2 |
10.2.7.103 | k8s-node3 |
软件及版本号
安装时间 2022年8月20日 均为当前时间的最新版本
System Info:
Kernel Version: 5.15.0-46-generic
OS Image: Ubuntu 20.04.4 LTS
Operating System: linux
Architecture: amd64
Container Runtime Version: containerd://1.5.9
Kubelet Version: v1.24.4
Kube-Proxy Version: v1.24.4
命令
# 设置hostname
sudo hostnamectl set-hostname k8s-master-node
# 换源
sudo sed -i "s@http://.*archive.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list
sudo sed -i "s@http://.*security.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list
# 更新软件
apt update
apt install openssh-server vim ntpdate
ntpdate ntp.aliyun.com
# 配公私钥 更改密码等
sudo ssh-keygen -t ecdsa
sudo cp /root/.ssh/id_ecdsa.pub /root/.ssh/authorized_keys
sudo cat /root/.ssh/id_ecdsa
# 禁止密码登录 x11forwarding 密码登录
vi /etc/ssh/sshd_config
X11Forwarding no
PasswordAuthentication no
AllowUsers root
DenyUsers user3 user4
# 图形化有这个打印机驱动 看着不爽
sudo systemctl stop cups.service
sudo systemctl disable cups.service
update-rc.d -f cupsys remove
apt remove cups-common cups-client cups
apt autoremove
# 关闭虚拟内存
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久
# 设置hostname
cat >> /etc/hosts << EOF
10.2.7.100 k8s-master
10.2.7.101 k8s-node1
10.2.7.102 k8s-node2
10.2.7.103 k8s-node3
EOF
cat <<EOF | tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables=1
net.bridge.bridge-nf-call-iptables=1
net.ipv4.ip_forward=1
net.ipv4.tcp_tw_recycle=0
fs.inotify.max_user_instances=8192
fs.inotify.max_user_watches=1048576
fs.file-max=52706963
fs.nr_open=52706963
EOF
# 使配置生效
sysctl -p /etc/sysctl.d/k8s.conf
# 默认进入命令行模式
systemctl set-default multi-user.target
# 习惯性重启
reboot
# 正式安装kubernetes 从阿里云复制来的
apt-get install -y apt-transport-https curl
curl 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-get update
apt-get install -y kubelet kubeadm kubectl containerd
sudo mkdir -p /etc/containerd
containerd config default | sudo tee /etc/containerd/config.toml
vi /etc/containerd/config.toml
修改sandbox为 registry.aliyuncs.com/google_containers/
修改镜像源
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
endpoint = ["https://------.mirror.aliyuncs.com", "https://registry-1.docker.io"]
修改为systemd
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
...
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
SystemdCgroup = true
#网上抄的 没试过
#mkdir -p /etc/containerd
#containerd config default > /etc/containerd/config.toml
## 替换配置文件
#sed -i "s#k8s.gcr.io#registry.cn-hangzhou.aliyuncs.com/google_containers#g" /etc/containerd/config.toml
#sed -i '/containerd.runtimes.runc.options/a\ \ \ \ \ \ \ \ \ \ \ \ SystemdCgroup = true' /etc/containerd/config.toml
#sed -i "s#https://registry-1.docker.io#https://registry.cn-hangzhou.aliyuncs.com#g" /etc/containerd/config.toml
# 设置默认 1.2.4版本好像默认CRI后端默认是Containerd,就不要装docker了,但是因为新版本,网上资料
crictl config --set runtime-endpoint=unix:///run/containerd/containerd.sock
sudo systemctl restart containerd
# 配置代码自动补全
echo "source <(kubectl completion bash)" >> ~/.profile
所有主机都按上列命令运行一遍,然后基础环境就装好了
之后写个配置文件
例子
apiVersion: kubeadm.k8s.io/v1beta3
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: "10.2.7.100"
bindPort: 6443
nodeRegistration:
criSocket: "unix:///run/containerd/containerd.sock"
imagePullPolicy: IfNotPresent
name: k8s-master
taints: null
---
apiVersion: kubeadm.k8s.io/v1beta3
kind: ClusterConfiguration
kubernetesVersion: "v1.24.4"
imageRepository: "registry.aliyuncs.com/google_containers"
apiServer:
timeoutForControlPlane: 4m0s
controlPlaneEndpoint: "10.2.7.100:6443"
certificatesDir: "/etc/kubernetes/pki"
clusterName: "kubernetes"
etcd:
local:
dataDir: "/var/lib/etcd"
networking:
serviceSubnet: "10.96.0.0/16"
podSubnet: "10.244.0.0/16"
dnsDomain: "zzzz"
# controllerManager: {}
# scheduler: {}
---
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
cgroupDriver: systemd
执行命令
kubeadm init --config kubernetes-init.yaml
之后各个节点加一下就行了