一、集群规划
ip |
hostname |
role |
version |
10.4.7.201 |
debian201 |
master |
v1.28.2 |
10.4.7.202 |
debian202 |
node |
v1.28.2 |
10.4.7.203 |
debian203 |
node |
v1.28.2 |
二、前期准备
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| $ ip link $ sudo cat /sys/class/dmi/id/product_uuid
$ sudo vim /etc/hosts 10.4.7.201 debian201 10.4.7.202 debian202 10.4.7.203 debian203
$ sudo modprobe overlay $ sudo modprobe br_netfilter
$ cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf overlay br_netfilter EOF
$ lsmod | grep br_netfilter
$ cat <<EOF | sudo 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 $ sudo sysctl --system
$ sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab $ sudo swapoff -a $ free -m
|
基于安全性(如在官方文档中承诺的 Secret 只会在内存中读写,不会落盘)、利于保证节点同步一致性等原因,从 1.8 版开始,Kubernetes 就在它的文档中明确声明了它默认不支持 Swap 分区,在未关闭 Swap 分区的机器中,集群将直接无法启动
三、容器运行时
1. Containerd
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| $ wget -c https://github.com/containerd/containerd/releases/download/v1.7.6/containerd-1.7.6-linux-amd64.tar.gz $ sudo tar Cxzvf /usr/local containerd-1.7.6-linux-amd64.tar.gz
$ wget -c https://raw.githubusercontent.com/containerd/containerd/main/containerd.service $ sudo mkdir -p /usr/local/lib/systemd/system/ $ sudo cp containerd.service /usr/local/lib/systemd/system/ $ sudo chmod 755 /usr/local/lib/systemd/system/containerd.service
$ sudo mkdir -p /etc/containerd/ $ containerd config default | sudo tee /etc/containerd/config.toml $ sudo vi /etc/containerd/config.toml [plugins."io.containerd.grpc.v1.cri"] sandbox_image = "registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.9" [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options] SystemdCgroup = true [plugins."io.containerd.grpc.v1.cri".registry] [plugins."io.containerd.grpc.v1.cri".registry.mirrors] [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"] endpoint = ["https://ung2thfc.mirror.aliyuncs.com","https://docker.mirrors.ustc.edu.cn"] [plugins."io.containerd.grpc.v1.cri".registry.mirrors."gcr.io"] endpoint = ["https://gcr.mirrors.ustc.edu.cn"] [plugins."io.containerd.grpc.v1.cri".registry.mirrors."k8s.gcr.io"] endpoint = ["https://gcr.mirrors.ustc.edu.cn/google-containers/"] [plugins."io.containerd.grpc.v1.cri".registry.mirrors."quay.io"] endpoint = ["https://quay.mirrors.ustc.edu.cn"] [plugins."io.containerd.grpc.v1.cri".registry.mirrors."registry.k8s.io"] endpoint = ["k8s.m.daocloud.io"] [plugins."io.containerd.grpc.v1.cri".registry.mirrors."10.4.7.200:88"] endpoint = ["http://10.4.7.200:88"] [plugins."io.containerd.grpc.v1.cri".registry.configs] [plugins."io.containerd.grpc.v1.cri".registry.configs."10.4.7.200:88".tls] insecure_skip_verify = true [plugins."io.containerd.grpc.v1.cri".registry.configs."10.4.7.200:88".auth] username = "admin" password = "Harbor12345"
$ sudo systemctl daemon-reload $ sudo systemctl enable --now containerd
$ wget -c https://github.com/opencontainers/runc/releases/download/v1.1.9/runc.amd64 $ sudo install -m 755 runc.amd64 /usr/local/sbin/runc
$ wget -c https://github.com/containernetworking/plugins/releases/download/v1.3.0/cni-plugins-linux-amd64-v1.3.0.tgz $ sudo mkdir -p /opt/cni/bin $ sudo tar Cxzvf /opt/cni/bin cni-plugins-linux-amd64-v1.3.0.tgz
$ wget -c https://github.com/kubernetes-sigs/cri-tools/releases/download/v1.28.0/crictl-v1.28.0-linux-amd64.tar.gz $ sudo tar Czxvf /usr/local/bin crictl-v1.28.0-linux-amd64.tar.gz $ cat << EOF | sudo tee /etc/crictl.yaml runtime-endpoint: unix:///run/containerd/containerd.sock image-endpoint: unix:///run/containerd/containerd.sock timeout: 2 debug: false pull-image-on-create: false EOF
|
pause 版本可通过 sudo kubeadm config images list --kubernetes-version v1.28.2
查看
2. Docker
- K8s 1.24 版本移除了 Dockershim,因此需要注意容器运行时的选择
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| $ for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done
$ sudo apt-get update $ sudo apt-get install ca-certificates curl gnupg $ sudo install -m 0755 -d /etc/apt/keyrings $ curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg $ sudo chmod a+r /etc/apt/keyrings/docker.gpg
$ echo \ "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \ "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null $ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
$ cat << EOF | sudo tee /etc/docker/daemon.json { "exec-opts": ["native.cgroupdriver=systemd"], "storage-driver": "overlay2", "log-driver": "json-file", "log-opts": { "max-size": "100m" }, "insecure-registries": ["10.4.7.200:88"], "registry-mirrors": ["https://ung2thfc.mirror.aliyuncs.com","https://registry.docker-cn.com"] } EOF $ sudo systemctl restart docker
$ sudo usermod -aG docker $USER
$ sudo curl -L "https://github.com/docker/compose/releases/download/v2.22.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
|
3. CRI-O
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| $ sudo apt install gnupg2 apt-transport-https -y
$ export OS=Debian_11 $ export VERSION=1.24 $ curl -L https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/$OS/Release.key | sudo gpg --dearmor -o /usr/share/keyrings/libcontainers-archive-keyring.gpg $ curl -L https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable:/cri-o:/$VERSION/$OS/Release.key | sudo gpg --dearmor -o /usr/share/keyrings/libcontainers-crio-archive-keyring.gpg $ echo "deb [signed-by=/usr/share/keyrings/libcontainers-archive-keyring.gpg] https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/$OS/ /" | sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list $ echo "deb [signed-by=/usr/share/keyrings/libcontainers-crio-archive-keyring.gpg] https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable:/cri-o:/$VERSION/$OS/ /" | sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:stable:cri-o:$VERSION.list
$ sudo apt update
$ sudo apt install cri-o cri-o-runc cri-tools -y
$ sudo vim /etc/crio/crio.conf [crio.network] network_dir = "/etc/cni/net.d/" plugin_dirs = [ "/opt/cni/bin/", ] $ sudo vim /etc/cni/net.d/100-crio-bridge.conf "ranges": [ [{ "subnet": "10.10.0.0/24" }], [{ "subnet": "1100:200::/24" }] ] $ sudo systemctl restart crio $ sudo systemctl enable crio
|
三、K8s 集群
- kubeadm:用于引导、安装和启动 Kubernetes 集群的命令行工具,只需要简单的几个命令工具(如 init、join、upgrade、reset 等),便可以快速完成集群的维护管理工作
- kubelet:每个节点上的代理程序,负责节点上的大部分操作,比如由它来启停容器等,节点上只有它能与 apiserver 进行通信
- kubectl:Kubernetes 客户端工具,用来操控 Kubernetes 集群,比如查看集群中的节点信息等
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| $ sudo apt install -y apt-transport-https ca-certificates curl gnupg2 $ curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add - $ cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main EOF $ sudo apt update
$ sudo apt install -y kubeadm=1.28.2-00 kubelet=1.28.2-00 kubectl=1.28.2-00 $ sudo apt-mark hold kubeadm kubelet kubectl $ sudo systemctl enable kubelet --now
$ sudo kubeadm init \ --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers \ --pod-network-cidr=10.10.0.0/16 \ --apiserver-advertise-address=10.4.7.201 \ --kubernetes-version=v1.28.2 \ --v=5 $ mkdir -p $HOME/.kube $ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config $ sudo chown $(id -u):$(id -g) $HOME/.kube/config
$ sudo kubeadm join 10.4.7.201:6443 --token o1szra.s2i6veja96njcdq5 \ --discovery-token-ca-cert-hash sha256:bf61c81dbacc0a4909a373c9b388e243ea37ce07142c5c79c60c28c7e39496b4
|
四、CNI 网络插件
1. calico
1 2 3 4 5
| $ curl https://raw.githubusercontent.com/projectcalico/calico/v3.26.1/manifests/calico.yaml -O
$ kubectl apply -f calico.yaml
|
2. flannel
1 2 3 4 5 6 7 8 9 10
| $ curl https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml -O
$ kubectl apply -f kube-flannel.yml
|
五、Helm
1 2 3
| $ wget -c https://get.helm.sh/helm-v3.13.0-linux-amd64.tar.gz $ tar -zxvf helm-v3.13.0-linux-amd64.tar.gz $ sudo mv linux-amd64/helm /usr/local/bin/helm
|
六、其他命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| $ echo "source <(kubectl completion bash)" >> ~/.bashrc
$ kubectl taint nodes debian201 node-role.kubernetes.io/control-plane:NoSchedule-
$ sudo vi /etc/kubernetes/manifests/kube-apiserver.yaml --service-node-port-range=5000-32767 $ export apiserver_pods=$(kubectl get pods --selector=component=kube-apiserver -n kube-system --output=jsonpath={.items..metadata.name}) $ kubectl delete pod $apiserver_pods -n kube-system $ kubectl describe pod $apiserver_pods -n kube-system | grep 5000
$ kubeadm token create --print-join-command
|
参考