雷达智富

首页 > 内容 > 程序笔记 > 正文

程序笔记

如何在 Ubuntu 24.04 上安装 Kubernetes(完整步骤)

2024-07-01 99

您对如何在 Ubuntu 24.04 (Noble Numbat) Linux 系统上安装 Kubernetes 感兴趣吗? Kubernetes 是一个开源容器编排系统,它彻底改变了服务的部署、管理和扩展方式。它提供了一个标准化平台,开发人员和系统管理员可以在其中管理容器。开发人员可以利用 Kubernetes API 构建通过 CI/CD、无服务器计算和服务网格实现现代化的创新应用程序。

在 Kubernetes 中,集群是一组协同工作的虚拟机或物理服务器,提供一个完整的容器平台,您可以在其中运行工作负载。它实现容器化应用程序的部署自动化、自动扩展和管理。

1.Kubernetes组件

以下是 Kubernetes 关键组件的细分。

  • 控制平面:Kubernetes集群的核心是控制平面。这可以管理整个集群状态和所有工作节点。它包含 API 服务器(集群的入口点)、将工作负载分配给集群中节点的调度程序、管理整个集群状态的控制器以及 etcd 键值数据存储。
  • 工作节点:这些是将运行容器化应用程序的操作系统(计划在其中运行 Pod)。在每个节点上运行的 Kubelet 服务是 Pod 生命周期管理的大脑。

为了让您的应用程序在 kubernetes 中运行,必须将其打包在容器中。容器是一个轻量级应用程序,捆绑了所有依赖项和库。与虚拟机相比,它们是可移植的,因为它们包含运行应用程序所需的一切。容器共享主机系统的底层操作系统,提供更好的效率。

2. 环境准备

本指南中使用的实验室环境将具有以下设置。

OS Server IP Server Hostname Server Role
Ubuntu 24.04 192.168.1.201 k8smaster.computingforgeeks.com Control Plane
Ubuntu 24.04 192.168.1.205 k8snode01.computingforgeeks.com Worker node
Ubuntu 24.04 192.168.1.206 k8snode02.computingforgeeks.com Worker node

登录到您环境中的每台服务器并设置正确的主机名。您还可以将记录添加到 DNS 服务器或使用 /etc/hosts 文件。

# Master Node
sudo hostnamectl set-hostname k8smaster.computingforgeeks.com

# Worker Node
sudo hostnamectl set-hostname k8snode01.computingforgeeks.com

将此处的值替换为您正确的环境设置。完成后注销以使新主机名生效。

logout

3.安装Kubernetes组件

我们在公司 Github 帐户 CloudSpinx 下创建了 Ansible Playbook。该剧本将在节点上自动执行以下操作:

  • 更新系统上的软件包并安装依赖项
  • 配置时区
  • 配置NTP时间同步
  • 禁用交换
  • 加载所需的内核模块
  • 配置sysctl参数
  • 在每个节点上配置 /etc/hosts 文件。但您必须手动设置正确的主机名(FQDN)(首选)。
  • 使用 Mirantis cri-dockerd 安装和配置容器运行时 – CRI-O、Containerd 或 Docker

在您的工作站中安装 Ansible

您需要选择将执行 ansible playbook 的本地计算机。这可以是 macOS、Linux 或 Windows 操作系统。您可以使用集群节点之一作为工作站来运行 ansible 执行。

在您的工作站中安装 Ansible。

### On a system with Python3 ###
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py --user
python3 -m pip install ansible --user

### On a system with Python2 ###
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py --user
python -m pip install ansible --user

macOS 用户可以使用 brew 安装 ansible

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install ansible

通过检查 ansible 的版本来验证安装。

$ ansible --version
ansible --version
ansible [core 2.16.0]
  config file = None
  configured module search path = ['/Users/jkmutai/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/Cellar/ansible/9.0.1/libexec/lib/python3.12/site-packages/ansible
  ansible collection location = /Users/jkmutai/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.12.0 (main, Oct  2 2023, 12:03:24) [Clang 15.0.0 (clang-1500.0.40.1)] (/usr/local/Cellar/ansible/9.0.1/libexec/bin/python)
  jinja version = 3.1.2
  libyaml = True

更新工作站计算机中的 /etc/hosts 文件,并将主机名映射到集群节点的 IP 地址。

$ sudo vim /etc/hosts
192.168.1.201  k8smaster
192.168.1.205  k8snode01
192.168.1.206  k8snode02

如果您还没有 SSH 密钥,请生成该密钥。

ssh-keygen -t rsa -b 4096 -N ''

将 SSH 密钥复制到所有 Kubernetes 集群节点。

ssh-copy-id  root@k8smaster
ssh-copy-id  root@k8snode01
ssh-copy-id  root@k8snode02

准备安装k8s的集群节点

将 git 存储库克隆到您的工作站计算机:

git clone https://github.com/cloudspinx/k8s-bootstrap.git

将当前工作目录更改为 k8s-pre-bootstrap 文件夹。

cd k8s-bootstrap

使用 Kubernetes 集群节点更新清单文件。例子;

$ vim hosts
# We are using mappings we did on the /etc/hosts file
[k8snodes]
k8smaster
k8snode01
k8snode02

接下来我们更新 playbook 文件中的一些关键变量;

  • Kubernetes 版本:k8s_version
  • 您所在位置的时区:时区
  • 要使用的 Kubernetes CNI:k8s_cni
  • 容器运行时:container_runtime
  • 吊舱网络
$ vim  k8s-prep.yml
---
- name: Prepare Kubernetes Nodes for Cluster bootstrapping
  hosts: k8snodes
  become: yes
  become_method: sudo
  #gather_facts: no
  vars:
    k8s_version: "1.29"                                  # Kubernetes version to be installed
    timezone: "Africa/Nairobi"                           # Timezone to set on all nodes
    k8s_cni: calico                                      # calico, flannel
    container_runtime: cri-o                             # docker, cri-o, containerd
    pod_network_cidr: "172.18.0.0/16"                    # pod subnet if using cri-o runtime
    configure_firewalld: false                           # true / false (keep it false, k8s>1.19 have issues with firewalld)
    # Docker proxy support
    setup_proxy: false                                   # Set to true to configure proxy
    proxy_server: "proxy.example.com:8080"               # Proxy server address and port
    docker_proxy_exclude: "localhost,127.0.0.1"          # Adresses to exclude from proxy
  roles:
    - kubernetes-bootstrap

将您的私钥身份添加到 OpenSSH 身份验证代理

eval `ssh-agent -s` && ssh-add

更新所有集群节点的清单列表。

$ vim hosts
[k8snodes]
k8smaster
k8snode01
k8snode02

准备好所有集群节点(控制平面和工作节点)时执行剧本。

ansible-playbook -i hosts k8s-prep.yml

某节点执行测试截图:

4.创建Kubernetes集群

现在所有软件包都已安装并完成配置,我们应该设置在主节点上创建 kubernetes 控制平面。用于此目的的工具是 kubeadm。 Kubeadm 将自动执行具有功能性控制平面组件所需的集群设置任务。

我们将运行的命令是 kubeadm init。这会配置控制平面组件,例如 API 服务器、控制器管理器和调度程序。

登录到您的第一个控制平面节点。

ssh user@k8smaster

检查可与 kubeadm init 命令一起使用的命令选项。

kubeadm init --help

单节点控制平面

对于单个控制平面服务器,使用以下命令初始化集群。

sudo kubeadm init --pod-network-cidr=172.18.0.0/16

如果主机上有多个 CRI,则需要在 kubeadm 配置文件中设置“criSocket”字段:

  • 容器:unix:///var/run/containerd/containerd.sock
  • CRI-O:unix:///var/run/crio/crio.sock
  • Docker:unix:///var/run/cri-dockerd.sock

CRI-O 示例:

sudo kubeadm init --pod-network-cidr=172.18.0.0/16 --cri-socket=unix:///var/run/crio/crio.sock

多个控制平面节点

如果您打算添加多个控制平面服务器,则需要 ControlPlaneEndpoint 的负载均衡器 IP。

kubeadm init \
  --pod-network-cidr=172.18.0.0/16 \
  --cri-socket=unix://var/run/crio/crio.sock \
  --upload-certs \
  --control-plane-endpoint=k8sapi.example.com \

其中k8sapi.example.com有一个A记录到负载均衡器IP,所有控制平面节点作为后端。

等待安装成功消息。

...
[mark-control-plane] Marking the node ubuntu-2404-server as control-plane by adding the taints [node-role.kubernetes.io/control-plane:NoSchedule]
[bootstrap-token] Using token: qgvgrr.u8w9e5lrf294kmvi
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] Configured RBAC rules to allow Node Bootstrap tokens to get nodes
[bootstrap-token] Configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] Configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] Configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 192.168.1.201:6443 --token qgvgrr.u8w9e5lrf294kmvi \
	--discovery-token-ca-cert-hash sha256:4c3a99418e3cd40cafe8858306657205f97b1d56ed1e57d729be10a0d2d0d431

从控制平面节点配置集群访问。

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

通过列出集群中的节点来检查安装是否成功。

$ kubectl get nodes
NAME                              STATUS   ROLES           AGE     VERSION
k8smaster.computingforgeeks.com   Ready    control-plane   3m1s    v1.29.4

5. 将Worker节点添加到集群中

如果您没有在第 3 步中执行此操作,则必须针对节点运行 playbook 来准备它。确保在清单文件 hosts 中添加工作节点。

ansible-playbook -i hosts k8s-prep.yml

集群测试正常后,是时候添加运行容器化应用程序的工作节点了。我们可以使用 kubeadm token create --print-join-command 命令生成要在工作节点上运行的集群加入命令。

如果您有多个控制平面节点,请登录到其中一个控制平面节点并运行以下命令:

kubeadm token create --print-join-command

以下是您刚刚执行的命令的解释:

  • token create:该子命令用于创建引导令牌,用于加入 kubernetes 集群中的新工作节点。
  • --print-join-command:通过使用此标志,kubeadm 将打印将工作节点加入集群的完整命令

请注意,生成的引导命令是短暂的(默认持续24小时)。如果过期了,您需要生成新的。

我的控制平面机器的示例连接命令生成输出。

$ kubeadm token create --print-join-command
kubeadm join 192.168.1.201:6443 --token 72n5yk.7khhkaav311irike --discovery-token-ca-cert-hash sha256:4c3a99418e3cd40cafe8858306657205f97b1d56ed1e57d729be10a0d2d0d431

复制打印的整个命令,然后登录到工作节点之一,然后

kubeadm join 192.168.1.201:6443 --token 72n5yk.7khhkaav311irike --discovery-token-ca-cert-hash sha256:4c3a99418e3cd40cafe8858306657205f97b1d56ed1e57d729be10a0d2d0d431

在哪里;

  • kubeadm join:将工作节点加入集群的命令。
  • 192.168.1.100:6443:控制平面节点上API服务器的IP地址和端口号。
  • –token 72n5yk.7khhkaav311irike:在控制平面上生成引导令牌,供工作节点加入集群。
  • –discovery-token-ca-cert-hash 是在集群节点之间提供安全通信的证书颁发机构 (CA) 证书的哈希值。

这是在我的工作节点中运行命令的输出。

从控制平面机器列出集群中的节点。

$ kubectl get nodes
NAME                              STATUS   ROLES           AGE     VERSION
k8snode01.computingforgeeks.com   Ready    <none>          24s     v1.29.4
k8smaster.computingforgeeks.com   Ready    control-plane   6h49m   v1.29.4

您可以考虑下一个集群配置。

  • 安装 Kubernetes 仪表板
  • 在 Kubernetes 上配置 Prometheus 和 Grafana
  • 在 Kubernetes 上安装 Nginx 入口控制器
  • 安装和配置 Traefik 入口控制器
  • 将 NFS 配置为 Kubernetes 持久卷存储
  • 在 Kubernetes 集群上设置 Rook Ceph 存储
  • 在 Kubernetes 集群上安装 MetalLB 负载均衡器
  • 购买 Kubernetes 学习书籍

更新于:2个月前
赞一波!2

文章评论

全部评论