k8s的安装与启动

k8s是一个非常庞大的组件,对新手不太友好,由于版本升级和环境问题常常安装失败。这里记录一下2024年3月29日使用最新的k8s的安装和启动流程。

我是在一台虚拟机中进行实验的,当前的虚拟机比较干净。先装一个docker,这个比较容易,在docker官网可以找到安装最新docker的教程。

k8s依赖一些kernel module

modprobe ip_vs
modprobe  ip_vs_rr
modprobe ip_vs_wrr
modprobe  ip_vs_sh
modprobe br_netfilter

可以将这些module放到/etc/modules-load.d/下面的文件中,如果没有文件可以新建一个,这样就可以实现开机启动时自动加载。

关闭swap,k8s不喜欢swap。

swapoff -a

安装kubeadm,kubelet,kubectl

apt-get update && apt-get install -y apt-transport-https
curl -fsSL https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.28/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://mirrors.aliyun.com/kubernetes-new/core/stable/v1.28/deb/ /" |
    tee /etc/apt/sources.list.d/kubernetes.list
apt-get update
apt-get install -y kubelet kubeadm kubectl

修改containerd config。最新的k8s已经默认runtime为containerd了,使用containerd default config覆盖之前的config,另外由于config设置了sandbox image为谷歌的镜像源,改成aliyun的。

containerd config default > /etc/containerd/config.toml
#change sandbox_image to registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.9
systemctl restart containerd

设置cgroup driver为systemd

echo "KUBELET_EXTRA_ARGS="--cgroup-driver=systemd" >> /etc/sysconfig/kubelet

 

至此前期的工作就结束了,尝试启动试试。

kubeadm init \
        --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers \
        --kubernetes-version=v1.28.8 \
        --pod-network-cidr=10.244.0.0/16 \
        --service-cidr=10.96.0.0/12   \--apiserver-advertise-address=192.168.108.100 \

这里是出错高发区,apiserver那一项要改成本机的ip地址。

如果上述命令执行成功且你是root用户执行下面的命令,如果不是那就按照提示执行设置。

export KUBECONFIG=/etc/kubernetes/admin.conf

看看node的状态

kubectl get nodes
kubectl describe nodes
NAME          STATUS     ROLES           AGE     VERSION
kube-master   NotReady   control-plane   4m42s   v1.28.0

查看kubelet log

"Container runtime network not ready" networkReady="NetworkReady=false reason:NetworkPluginNotReady message:Network plugin returns error: cni plugin not initialized"

cni没有装好。但是在/opt/cni/bin下所有cni插件都已经就位,通过搜索发现是没有cni的配置文件。

cat << EOF | tee /etc/cni/net.d/10-containerd-net.conflist
{
 "cniVersion": "1.0.0",
 "name": "containerd-net",
 "plugins": [
   {
     "type": "bridge",
     "bridge": "cni0",
     "isGateway": true,
     "ipMasq": true,
     "promiscMode": true,
     "ipam": {
       "type": "host-local",
       "ranges": [
         [{
           "subnet": "10.88.0.0/16"
         }],
         [{
           "subnet": "2001:db8:4860::/64"
         }]
       ],
       "routes": [
         { "dst": "0.0.0.0/0" },
         { "dst": "::/0" }
       ]
     }
   },
   {
     "type": "portmap",
     "capabilities": {"portMappings": true},
     "externalSetMarkChain": "KUBE-MARK-MASQ"
   }
 ]
}
EOF

重启containerd,再次查看node状态。

kubectl get nodes
NAME          STATUS   ROLES           AGE   VERSION
kube-master   Ready    control-plane   15m   v1.28.0

很好。

如果一切OK可以尝试去启动一个pod。

#
# Copyright (c) 2018 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
#
apiVersion: v1
kind: Pod
metadata:
  name: hello-pod
spec:
  containers:
  - name: hello-container
    image: docker.io/library/ubuntu:latest
    command:
      - sleep
      - "100s"

可以通过kubectl查看pod。

kubectl get pods

发现:

NAME        READY   STATUS    RESTARTS   AGE
hello-pod   0/1     Pending   0          5s

describe pods发现

Warning  FailedScheduling  2m21s  default-scheduler  0/1 nodes are available: 1 node(s) had untolerated taint {node-role.kubernetes.io/control-plane: }. preemption: 0/1 nodes are available: 1 Preemption is not helpful for scheduling..

也就node有NoSchedule污点,去掉污点试试。

kubectl taint nodes kube-master node-role.kubernetes.io/control-plane:NoSchedule-

再看看pods状态。

kubectl get pods
NAME        READY   STATUS    RESTARTS   AGE
hello-pod   1/1     Running   0          2m45s

成功!

注意:

如果你是非root用户,使用kubectl都不需要sudo,否则会有认证问题。

centos的安装可以参考:【保姆级教程-无需外网】年轻人的第一篇通过Kubeadm 部署 Kubernetes(K8S)全程无需外网的部署教程(含UI--Dashboard)_kubernetes 部署需要联网吗-CSDN博客,结合上述碰到的问题的解决方案。

issue list and solusion

1. centos上碰到的问题:

Failed to remove containers: output: time="2024-04-03T01:39:43Z" 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"

有两种原因,第一可能是containerd的config里面disable 了cri;第二,当前的根文件系统是xfs,在未设置d_type时对overlayfs支持有问题,详细解释见踩坑:Linux 上修改系统盘 xfs ftype=0 以支持 overlay 的方法 - 掘金 (juejin.cn),解决办法是重新挂载一块盘,格式化为ext4,将此盘挂载到/var/lib/containerd,重启containerd。

2. 初始化kubeadm时碰到node not found issue

"Error getting node" err="node \"k8s-master\" not found"

开始以为是hostname的问题,在改了/etc/hostname,/etc/hosts文件,确认hostname可用之后还是报错。上翻journalctl log,看到下面这条:

failed to pull image \"registry.k8s.io/pause:3.6\"

应该是拉取镜像失败。在containerd的config文件中设置sandbox_image为

sandbox_image = "registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.5"

当然pause的版本可以改为自己机器上推荐的版本。然后systemctl daemon_reload && systemctl restart containerd再次尝试初始化kubeadm,成功。

3 修改kubelet 容器运行时

在/var/lib/kubelet/kubeadm-flags.env中修改

posted @ 2024-03-29 19:20  半山随笔  阅读(115)  评论(0编辑  收藏  举报