文章

k8s - 安装

k8s - 安装

k8s 安装

提前准备

  • 主机信息-系统、硬件

    内核6.1.0-35-amd64 //uname -r

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
    NAME="Debian GNU/Linux"
    VERSION_ID="12"
    VERSION="12 (bookworm)"
    VERSION_CODENAME=bookworm
    ID=debian
    HOME_URL="https://www.debian.org/"
    SUPPORT_URL="https://www.debian.org/support"
    BUG_REPORT_URL="https://bugs.debian.org/"
    
    1
    2
    3
    4
    5
    
    Tasks:  90 total,   1 running,  89 sleeping,   0 stopped,   0 zombie
    %Cpu0  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st 
    %Cpu1  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st 
    MiB Mem :   3722.8 total,   2740.4 free,    453.2 used,    758.3 buff/cache     
    MiB Swap:      0.0 total,      0.0 free,      0.0 used.   3269.6 avail Mem 
    
  • docker (debian) https://docs.docker.com/engine/install/debian/

    配置国内镜像源

    1
    2
    3
    4
    5
    6
    7
    8
    
    wangfuyu]# vim /etc/docker/daemon.json
      
    {   
        "data-root":"/data/docker",
        "registry-mirrors": [
                    "https://71t1c6hh.mirror.aliyuncs.com"
                            ] 
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    # 启动项重新加载
    systemctl daemon-reload
      
    # 重启服务,配置生效
    service docker restart
    # Redirecting to /bin/systemctl restart docker.service
      
    # 查看状态
    systemctl status docker
      
    # 开机启动
    systemctl start docker
      
    # 查看跟进问题
    journalctl -xe
      
    
  • 计划安装Kubernetes版本

    V1.35

    文档:https://kubernetes.io/zh-cn/docs/home/supported-doc-versions/

    1
    2
    3
    4
    
    #你需要在每台机器上安装以下的软件包:
    #https://kubernetes.io/zh-cn/docs/tasks/tools/install-kubectl-linux/
    # kubeadm:用来初始化集群的指令。
    #kubeadm \ kubelet \ kubectl
    

参考本级目录的install.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cat <<EOF | sudo tee /etc/systemd/system/kubelet.service
[Unit]
Description=kubelet: The Kubernetes Node Agent
Documentation=https://kubernetes.io
Wants=network-online.target
After=network-online.target

[Service]
ExecStart=/usr/local/bin/kubelet
Restart=always
StartLimitInterval=0
RestartSec=10

[Install]
WantedBy=multi-user.target
EOF
1
2
3
systemctl daemon-reload
systemctl start kubelet
systemctl enable kubelet

安装容器运行时

https://kubernetes.p2hp.com/docs/setup/production-environment/container-runtimes.html

  • 前置条件(转发 IPv4 并让 iptables 看到桥接流量)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF

sudo modprobe overlay
sudo modprobe br_netfilter

# 设置所需的 sysctl 参数,参数在重新启动后保持不变
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

# 应用 sysctl 参数而不重新启动
sudo sysctl --system

你需要启用 CRI 支持才能在 Kubernetes 集群中使用 containerd。 要确保 cri 没有出现在 /etc/containerd/config.toml 文件中 disabled_plugins 列表内。如果你更改了这个文件,也请记得要重启 containerd

1
systemctl restart containerd
  • Cri-o
1
https://github.com/cri-o/packaging/blob/main/README.md#usage
1
2
KUBERNETES_VERSION=v1.34
CRIO_VERSION=v1.34
1
2
apt-get update
apt-get install -y software-properties-common curl
1
2
3
4
5
curl -fsSL https://pkgs.k8s.io/core:/stable:/$KUBERNETES_VERSION/deb/Release.key |
    gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/$KUBERNETES_VERSION/deb/ /" |
    tee /etc/apt/sources.list.d/kubernetes.list
1
2
3
4
5
curl -fsSL https://download.opensuse.org/repositories/isv:/cri-o:/stable:/$CRIO_VERSION/deb/Release.key |
    gpg --dearmor -o /etc/apt/keyrings/cri-o-apt-keyring.gpg

echo "deb [signed-by=/etc/apt/keyrings/cri-o-apt-keyring.gpg] https://download.opensuse.org/repositories/isv:/cri-o:/stable:/$CRIO_VERSION/deb/ /" |
    tee /etc/apt/sources.list.d/cri-o.list
1
2
3
4
5
6
7
8
9
apt-get update
apt-get install -y cri-o kubelet kubeadm kubectl

 apt-get install -y cri-tools
 # cri-o 与containerd 重复 保留一个
 sudo systemctl stop containerd
sudo systemctl disable containerd
 sudo apt-get purge -y containerd.io containerd
sudo apt-get autoremove -y
  • 配置systemCgroup

    https://kubernetes.io/zh-cn/docs/setup/production-environment/container-runtimes/#containerd-systemd

1
2
3
 #containerd config default > /etc/containerd/config.toml 重置 containerd 配置,然后相应地设置上述配置参数。
 # modify toml 
SystemdCgroup = true
1
2
3
4
# /etc/crio/crio.conf.d/10-crio.conf
[crio.runtime]
conmon_cgroup = "pod"
cgroup_manager = "cgroupfs"
1
2
systemctl restart crio

1
关闭 Swap:sudo swapoff -a,并从 /etc/fstab 中删除 swap 行。

/etc/default/kubelet

1
#KUBELET_EXTRA_ARGS="--container-runtime-endpoint=unix:///var/run/containerd/containerd.sock"

sudo systemctl daemon-reload && sudo systemctl restart kubelet

1
2
3
4
5
6
# For AMD64 / x86_64
[ $(uname -m) = x86_64 ] && curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.31.0/kind-linux-amd64
# For ARM64
[ $(uname -m) = aarch64 ] && curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.31.0/kind-linux-arm64
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind

集群部署

既然你已经在两台服务器(001 和 002)上安装好了基础组件,接下来的核心流程是:初始化 Master 节点 -> 配置网络插件 -> 加入 Worker 节点 -> 部署应用

以下是针对你的环境(001 为 Master,002 为 Worker)的详细部署步骤:

1
kubeadm config images pull  --kubernetes-version=v1.35.0 --cri-socket=unix:///var/run/containerd/containerd.sock
1
2
3
4
5
6
7
ctr image pull registry.k8s.io/kube-apiserver:v1.35.0
ctr image pull registry.k8s.io/kube-controller-manager:v1.35.0
ctr image pull registry.k8s.io/kube-scheduler:v1.35.0
ctr image pull registry.k8s.io/kube-proxy:v1.35.0
ctr image pull registry.k8s.io/coredns/coredns:v1.13.1
ctr image pull registry.k8s.io/pause:3.10.1
ctr image pull registry.k8s.io/etcd:3.6.6-0
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
61
62
63
64
65
66
67
68
69
70
71
72
# 初始化 Master 节点 (在 001 上执行)
# 注意:--pod-network-cidr 是为后续网络插件 Flannel 预留的网段,建议不要修改
#sudo kubeadm init \
#  --apiserver-advertise-address=192.168.0.58 \
#  --image-repository ://registry.aliyuncs.com \
#  --kubernetes-version v1.35.0 \
#  --pod-network-cidr=10.244.0.0/16
   kubeadm reset
 kubeadm init \
  --apiserver-advertise-address=192.168.0.58 \
  --kubernetes-version v1.35.0 \
  --pod-network-cidr=10.244.0.0/16 \
  --node-name=master-node 
=
  --v=5
  
   kubeadm init \
  --apiserver-advertise-address=192.168.0.151 \
  --kubernetes-version v1.35.1 \
  --pod-network-cidr=10.244.0.0/16 \
  --node-name=master-node 
  
  kubeadm init --pod-network-cidr=10.244.0.0/16 --kubernetes-version   v1.35.0 
  
  journalctl -u kubelet -f --no-pager
  journalctl -u kubelet -f
#out
[init] Using Kubernetes version: v1.35.0
[preflight] Running pre-flight checks
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action beforehand using 'kubeadm config images pull'


[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local raha-0001] and IPs [10.96.0.1 192.168.0.58]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [localhost raha-0001] and IPs [192.168.0.58 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [localhost raha-0001] and IPs [192.168.0.58 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "super-admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/instance-config.yaml"
[patches] Applied patch of type "application/strategic-merge-patch+json" to target "kubeletconfiguration"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Starting the kubelet
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests"
[kubelet-check] Waiting for a healthy kubelet at http://127.0.0.1:10248/healthz. This can take up to 4m0s
[kubelet-check] The kubelet is healthy after 502.010382ms
[control-plane-check] Waiting for healthy control plane components. This can take up to 4m0s
[control-plane-check] Checking kube-apiserver at https://192.168.0.58:6443/livez
[control-plane-check] Checking kube-controller-manager at https://127.0.0.1:10257/healthz
[control-plane-check] Checking kube-scheduler at https://127.0.0.1:10259/livez
1
2
3
4
5
6
7
8
##错误时每次 init 失败都会残留文件,必须清理:

sudo kubeadm reset -f --cri-socket unix:///var/run/containerd/containerd.sock
sudo rm -rf /etc/cni/net.d
rm -rf /var/lib/kubelet/*
rm -rf /var/lib/dockershim
rm -rf /var/lib/rkt
rm -rf /etc/kubernetes/*
1
2
kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml
本文由作者按照 CC BY 4.0 进行授权