四、安装 Node 节点

四、安装 Node 节点 #

接下来我们安装 Node 节点,主要是安装 kubelet,容器的 Runtime,还有准备网络工具(也就是所谓的 CNI Plugins)。

这部分的操作要在两个 Node 服务器上分别执行。

Node 的多数二进制的文件,我们也都都放在 /usr/local/bin 里面,并且可以在 ~/.bash_profile 里面把 /usr/local/bin 加到 PATH 里。

# 两个节点的命令一样

mkdir /usr/local/bin
echo "export PATH=$PATH:/usr/local/bin" >> ~/.bash_profile
source ~/.bash_profile
# 两个节点的命令一样

mkdir /usr/local/bin
echo "export PATH=$PATH:/usr/local/bin" >> ~/.bash_profile
source ~/.bash_profile

安装容器 Runtime #

在 Node 节点上,调用的链条主要是下面这样:kubelet 指挥 containerd,containerd 指挥 runc,其中 containerd 和 runc 共同构成容器的运行环境。


block-beta
block
a["kubelet"]
b["containerd"]
c["runc"]
end

我们第一步安装 containerd。首先下载 containerd 的文件包,把里面东西都解压到 /usr/local/bin

# 两个节点的命令一样

wget https://github.com/containerd/containerd/releases/download/v1.7.22/containerd-1.7.22-linux-amd64.tar.gz
tar Cxzvf /usr/local containerd-1.7.22-linux-amd64.tar.gz
# 两个节点的命令一样

wget https://github.com/containerd/containerd/releases/download/v1.7.22/containerd-1.7.22-linux-amd64.tar.gz
tar Cxzvf /usr/local containerd-1.7.22-linux-amd64.tar.gz

然后可以下载 containerd.service 文件,官方帮忙准备好了。

# 两个节点的命令一样

cd /etc/systemd/system/
wget https://raw.githubusercontent.com/containerd/containerd/main/containerd.service
# 两个节点的命令一样

cd /etc/systemd/system/
wget https://raw.githubusercontent.com/containerd/containerd/main/containerd.service

创建 containerd 的配置文件 /etc/containerd/config.toml

# 两个节点的内容一样
version = 2

[plugins."io.containerd.grpc.v1.cri"]
  [plugins."io.containerd.grpc.v1.cri".containerd]
    snapshotter = "overlayfs"
    default_runtime_name = "runc"
  [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
    runtime_type = "io.containerd.runc.v2"
  [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
    SystemdCgroup = true
[plugins."io.containerd.grpc.v1.cri".cni]
  bin_dir = "/opt/cni/bin"
  conf_dir = "/etc/cni/net.d"
# 两个节点的内容一样
version = 2

[plugins."io.containerd.grpc.v1.cri"]
  [plugins."io.containerd.grpc.v1.cri".containerd]
    snapshotter = "overlayfs"
    default_runtime_name = "runc"
  [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
    runtime_type = "io.containerd.runc.v2"
  [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
    SystemdCgroup = true
[plugins."io.containerd.grpc.v1.cri".cni]
  bin_dir = "/opt/cni/bin"
  conf_dir = "/etc/cni/net.d"

启动 containerd,设置开机启动。

# 两个节点命令一样
systemctl daemon-reload
systemctl start containerd
systemctl enable containerd
# 两个节点命令一样
systemctl daemon-reload
systemctl start containerd
systemctl enable containerd

然后下载 runc,放在 /usr/local/bin

# 两个节点命令一样
cd /usr/local/bin
wget https://github.com/opencontainers/runc/releases/download/v1.1.14/runc.amd64
mv runc.amd64 runc
chmod +x runc
# 两个节点命令一样
cd /usr/local/bin
wget https://github.com/opencontainers/runc/releases/download/v1.1.14/runc.amd64
mv runc.amd64 runc
chmod +x runc

runc 不用启动,放在这里就行。

然后下载网络插件 CNI Plugins,放在 /opt/cni/bin,注意这个路径和我们前面用的都不一样,是约定俗成放在这里。

网络的配置在后面的章节。现在做到这些就可以。

# 两个节点的命令一样
mkdir -p /opt/cni/bin
wget https://github.com/containernetworking/plugins/releases/download/v1.5.1/cni-plugins-linux-amd64-v1.5.1.tgz
tar Cxzvf /opt/cni/bin cni-plugins-linux-amd64-v1.5.1.tgz
# 两个节点的命令一样
mkdir -p /opt/cni/bin
wget https://github.com/containernetworking/plugins/releases/download/v1.5.1/cni-plugins-linux-amd64-v1.5.1.tgz
tar Cxzvf /opt/cni/bin cni-plugins-linux-amd64-v1.5.1.tgz

到这里,容器 Runtime 的部分就算安装完成了。

安装 Kubelet #

下载 kubelet,放在 /usr/local/bin

# 两个节点的命令一样
cd /usr/local/bin
wget https://dl.k8s.io/v1.31.1/bin/linux/amd64/kubelet
chmod +x kubelet
# 两个节点的命令一样
cd /usr/local/bin
wget https://dl.k8s.io/v1.31.1/bin/linux/amd64/kubelet
chmod +x kubelet

把 Master 节点的 kubeconfig、CA 证书和 Key,以及 client 的证书和 Key 都复制到 Node 节点。

下面这个动作在 Master 节点执行。

# 从 master 复制到 node01
scp /etc/kubernetes/admin.kubeconfig root@node01:/etc/kubernetes
scp /etc/kubernetes/pki/ca* root@node01:/etc/kubernetes/pki
scp /etc/kubernetes/pki/client* root@node01:/etc/kubernetes/pki

# 从 master 复制到 node02
scp /etc/kubernetes/admin.kubeconfig root@node02:/etc/kubernetes
scp /etc/kubernetes/pki/ca* root@node02:/etc/kubernetes/pki
scp /etc/kubernetes/pki/client* root@node02:/etc/kubernetes/pki

准备 kubelet 的配置文件 /etc/kubernetes/kubelet.yaml

# 两个节点的内容**不一样**

kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
authentication:
  anonymous:
    enabled: false
  webhook:
    enabled: true
  x509:
    clientCAFile: "/etc/kubernetes/pki/ca.pem"
serverTLSBootstrap: true
port: 10250

clusterDomain: "cluster.local"
clusterDNS:
  - "10.96.0.10"

authorization:
  mode: Webhook

cgroupDriver: systemd
containerRuntimeEndpoint: "unix:///var/run/containerd/containerd.sock"

resolvConf: "/etc/resolv.conf"
runtimeRequestTimeout: "15m"

# 两个节点的这个配置不一样,node01 是 10.244.1.0/24,node02 是 10.244.2.0/24
podCIDR: "10.244.1.0/24"
# 两个节点的内容**不一样**

kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
authentication:
  anonymous:
    enabled: false
  webhook:
    enabled: true
  x509:
    clientCAFile: "/etc/kubernetes/pki/ca.pem"
serverTLSBootstrap: true
port: 10250

clusterDomain: "cluster.local"
clusterDNS:
  - "10.96.0.10"

authorization:
  mode: Webhook

cgroupDriver: systemd
containerRuntimeEndpoint: "unix:///var/run/containerd/containerd.sock"

resolvConf: "/etc/resolv.conf"
runtimeRequestTimeout: "15m"

# 两个节点的这个配置不一样,node01 是 10.244.1.0/24,node02 是 10.244.2.0/24
podCIDR: "10.244.2.0/24"

准备 kubelet 的 Systemd service 文件 /etc/systemd/system/kubelet.service,请注意 --node-ip 的参数,这个地方要分别改为 Node 的主 IP,我们这个实验当中分别是 192.168.56.11192.168.56.12

# 两个节点的内容不一样

[Unit]
Description=Kubernetes Kubelet
Documentation=https://github.com/kubernetes/kubernetes

[Service]
ExecStart=/usr/local/bin/kubelet --kubeconfig=/etc/kubernetes/admin.kubeconfig \
--node-ip=192.168.56.11 \
--config=/etc/kubernetes/kubelet.yaml \
--v=1
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
# 两个节点的内容不一样

[Unit]
Description=Kubernetes Kubelet
Documentation=https://github.com/kubernetes/kubernetes

[Service]
ExecStart=/usr/local/bin/kubelet --kubeconfig=/etc/kubernetes/admin.kubeconfig \
--node-ip=192.168.56.12 \
--config=/etc/kubernetes/kubelet.yaml \
--v=1
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

启动 kubelet,设置开机启动。

# 两个节点的命令一样

systemctl daemon-reload
systemctl start kubelet
systemctl enable kubelet
# 两个节点的命令一样

systemctl daemon-reload
systemctl start kubelet
systemctl enable kubelet

到这里,Worker 的安装工作就结束了。

接下来我们为集群设置网络

comments powered by Disqus