ubuntu 安装kubenetes

一、准备三台虚拟机

ubuntu6:(master)192.168.0.114

ubuntu7:(node)192.168.0.115

ubuntu8:(node)192.168.0.116

二、操作系统初始化

1、master配置host (master)

编辑 /etc/hosts ,添加如下host配置

192.168.0.114 ubuntu6
192.168.0.115 ubuntu7
192.168.0.116 ubuntu8

2、其他初始化(master,node)

(1)关闭防火墙

# 关闭防火墙
ufw disable
# 查看防火墙状态
ufw status

(2)关闭swap

# 临时关闭
swapoff -a
# 永久关闭
vim /etc/fstab
`注释掉该行:
  #/swap.img      none    swap    sw      0       0
`

(3)注意:在 Ubuntu 上安装 Kubernetes 时,通常不需要关闭 SELinux。因为 Ubuntu 默认使用的是 AppArmor 而不是 SELinux。

(4)开启ip转发

cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF

sudo modprobe overlay
sudo modprobe br_netfilter

# sysctl params required by setup, params persist across reboots
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

# Apply sysctl params without reboot
sudo sysctl --system

3、安装docker(master,node)

docker安装,参见docker 官网:https://docs.docker.com/engine/install/ubuntu/

docker安装完成,设置docker镜像仓库为阿里云,参见:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

4、安装容器运行时(Kubernetes 1.24版本中不再支持Docker容器运行时)(master,node)

在 Docker 1.20 版本中,Docker 引入了 containerd 作为其默认容器运行时管理器,取代了之前直接使用的 docker-runc。因此,io.containerd.runc.v2 成为了 Docker 1.20 版本及以后版本中的默认运行时

Kubernetes 默认的容器运行时是 Docker,但从 Kubernetes 1.24 版本开始,默认的容器运行时将变为 containerd。为了使用 containerd 作为容器运行时,你需要在 Kubernetes 节点上安装和配置 containerd。(安装教程自行百度)

查看docker的容器运行时:

docker info | grep "Runtime"

该docker的docker版本为:26.1.2

 我们来查看早期的docker容器运行时(docker版本为:1.13.1)

 查看容器运行时:

如果想继续使用dockered作为容器运行时,则需要安装cri-dockerd容器运行时

CRI-Dockerd 是一个 Kubernetes 的容器运行时,它是 Docker 容器运行时(Dockerd)通过 Container Runtime Interface(CRI)进行扩展,以便与 Kubernetes 集成。CRI 是 Kubernetes 用于与容器运行时(container runtime)通信的标准接口,它允许 Kubernetes 与各种容器运行时(如 Docker、containerd、CRI-O 等)进行交互。

CRI-Dockerd 使得 Kubernetes 可以直接与 Dockerd 通信,而无需通过 Docker shim。在 Kubernetes 1.20 之前,Docker 是 Kubernetes 的默认容器运行时,并且 Kubernetes 的 kubelet 直接与 Dockerd 通信。但自 Kubernetes 1.20 版本之后,Docker 不再是 Kubernetes 的默认容器运行时,而是建议使用 CRI 标准兼容的容器运行时。

因此,CRI-Dockerd 的作用主要是充当 Kubernetes 和 Dockerd 之间的桥梁,使得 Kubernetes 集群能够通过标准化的 CRI 接口与 Dockerd 进行交互,管理容器的生命周期、调度等操作。

安装教程,详见:https://mirantis.github.io/cri-dockerd/usage/install/ ,如何配置kubernetes使用cri-dockerd容器运行时,请自行百度

注意:安装完成docker后,通常默认安装并启动 containerd 容器运行时

初始化 默认配置文件(安装docker后/etc/containerd/config.toml 配置文件并不完整)

config default > /etc/containerd/config.toml

三、搭建kubernetes集群

1、安装kubeadm、kubelet、kubectl (master,node)

(1)、配置阿里云的镜像,并安装kubeadm、kubelet、kubectl,如下: (也可使用官方文档方式安装:https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/

打开阿里云开源镜像网址:https://developer.aliyun.com/mirror/  找到kubernetes。

一下我们选择使用kubernetes v1.25

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

2、 kubenet init (master)

kubeadm init --apiserver-advertise-address=192.168.0.114 \
--image-repository registry.aliyuncs.com/google_containers \
--pod-network-cidr=10.244.0.0/16

--apiserver-advertise-address :集群通告地址

--image-repository :由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里指定阿里云镜像仓库地址

--pod-network :Pod网络
初始化如果报如下错误:
[ERROR CRI]: container runtime is not running: output: time="2024-02-29T07:49:23Z" 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"

则编辑:/etc/containerd/config.toml文件(如下图),或者直接重新生成默认配置文件(原来的配置文件并不完整,不用参考下图)

config default > /etc/containerd/config.toml

注释掉以下行:参考地址:https://blog.51cto.com/u_1264026/7552228

 初始化如果报如下错误:

[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[kubelet-check] Initial timeout of 40s passed.

Unfortunately, an error has occurred:
        timed out waiting for the condition

This error is likely caused by:
        - The kubelet is not running
        - The kubelet is unhealthy due to a misconfiguration of the node in some way (required cgroups disabled)

If you are on a systemd-powered system, you can try to troubleshoot the error with the following commands:
        - 'systemctl status kubelet'
        - 'journalctl -xeu kubelet'

Additionally, a control plane component may have crashed or exited when started by the container runtime.
To troubleshoot, list all containers using your preferred container runtimes CLI.

 排查错误:

参考地址:https://blog.csdn.net/master_boy/article/details/127148422

https://blog.csdn.net/d198902/article/details/136443863

 

journalctl -xeu kubelet | grep Failed

 错误日志里面说这个registry.k8s.io/pause:3.9镜像无法拉取

查看kubernetes需要哪些镜像:

kubeadm config images lis
或指定版本
kubeadm config images list --kubernetes-version=v1.30.0

 

 



posted @ 2024-02-29 13:22  远洪  阅读(38)  评论(0)    收藏  举报