如何在 Ubuntu 22.04 上设置 Kubernetes 集群如何在 Ubuntu 22.04 上设置 Kubernetes 集群如何在 Ubuntu 22.04 上设置 Kubernetes 集群如何在 Ubuntu 22.04 上设置 Kubernetes 集群
  • 业务
  • 目标
  • 支持
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容

如何在 Ubuntu 22.04 上设置 Kubernetes 集群

发表 admin at 2025年2月28日
类别
  • 未分类
标签

Kubernetes 是一款免费的容器编排工具,可帮助您实现容器化应用程序的自动化部署、扩展和管理。 Kubernetes集群由Master节点和Worker节点组成。主节点负责管理集群中的节点和 Pod。工作节点用于部署应用程序工作负载。借助 Kubernetes,您可以使用本地基础设施或公共云平台来部署和管理云原生应用程序。

本教程将向您展示如何在 Ubuntu 22.04 服务器上设置 Kubernetes 集群。

先决条件

  • 运行 Ubuntu 22.04 的服务器。
  • 服务器上配置了 root 密码。

入门

首先,您需要将系统软件包更新并升级到最新版本。您可以使用以下命令来完成此操作:

apt update -y
apt upgrade -y

更新所有软件包后,您还需要禁用系统上的交换。您可以使用以下命令禁用它:

swapoff -a

接下来,您需要在所有节点上加载一些所需的内核模块。为此,请编辑 /etc/modules-load.d/containerd.conf 文件:

nano /etc/modules-load.d/containerd.conf

添加以下行:

overlay
br_netfilter

保存并关闭文件,然后使用以下命令加载模块:

modprobe overlay
modprobe br_netfilter

接下来,您还需要创建一个细粒度并定义一些所需的内核参数:

nano /etc/sysctl.d/kubernetes.conf

添加以下行:

net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1

保存并关闭文件,然后运行以下命令以应用更改:

sysctl --system

安装容器

接下来,您需要为 Kubernetes 集群安装 Containerd。首先,使用以下命令安装所有必需的依赖项:

apt install curl gnupg2 software-properties-common apt-transport-https ca-certificates -y

安装所有依赖项后,使用以下命令添加 Docker 存储库:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmour -o /etc/apt/trusted.gpg.d/docker.gpg
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

接下来,更新存储库并使用以下命令安装 Containerd:

apt update -y
apt install containerd.io -y

接下来,您需要配置容器,以便它由 systemd 启动。您可以使用以下命令来完成此操作:

containerd config default | tee /etc/containerd/config.toml >/dev/null 2>&1
sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml

接下来,重新启动 Containerd 服务以应用更改:

systemctl restart containerd

安装 Kubernetes 组件

默认情况下,Kubernetes 组件不包含在 Ubuntu 默认存储库中。因此,您需要将 Kubernetes 存储库添加到您的系统中。您可以使用以下命令添加它:

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"

接下来,使用以下命令更新存储库并安装所有 Kubernetes 组件:

apt update -y
apt install kubelet kubeadm kubectl -y

安装所有软件包后,您可以继续下一步。

初始化 Kubernetes 集群

至此,所有 Kubernetes 组件均已安装完毕。现在,在主节点上运行以下命令来初始化集群:

kubeadm init --control-plane-endpoint=kubernetes-master-ip

您将得到以下输出:

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/

You can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:

  kubeadm join kubernetes-master-ip:6443 --token chmz7m.fbjgdcqne1q0ff4t \
	--discovery-token-ca-cert-hash sha256:c614bf14af27472e470546539a9a2ff63e5d558dbbb3cc06d6f7a030fcb55426 \
	--control-plane 

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

kubeadm join kubernetes-master-ip:6443 --token chmz7m.fbjgdcqne1q0ff4t \
	--discovery-token-ca-cert-hash sha256:c614bf14af27472e470546539a9a2ff63e5d558dbbb3cc06d6f7a030fcb55426 

注意:从上面的输出中复制 kubeadm join 命令。您需要在工作节点上运行此命令才能加入集群。接下来,您需要运行以下命令来与 Kubernetes 集群交互:

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

将工作节点加入集群

接下来,登录Worker节点,执行以下命令将Worker节点加入Kubernetes集群:

kubeadm join kubernetes-master-ip:6443 --token chmz7m.fbjgdcqne1q0ff4t --discovery-token-ca-cert-hash sha256:c614bf14af27472e470546539a9a2ff63e5d558dbbb3cc06d6f7a030fcb55426

您将得到以下输出:

[preflight] Running pre-flight checks
	[WARNING SystemVerification]: missing optional cgroups: blkio
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Starting the kubelet
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

安装 Calico Pod 网络附加组件

接下来,您需要在 Kubernetes Master 节点上安装 Calico Pod Network 来管理网络。

您可以使用以下命令下载并安装它:

curl https://projectcalico.docs.tigera.io/manifests/calico.yaml -O calico.yaml
kubectl apply -f calico.yaml

您将得到以下输出:

poddisruptionbudget.policy/calico-kube-controllers created
serviceaccount/calico-kube-controllers created
serviceaccount/calico-node created
configmap/calico-config created
customresourcedefinition.apiextensions.k8s.io/bgpconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/bgppeers.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/blockaffinities.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/caliconodestatuses.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/clusterinformations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/felixconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/globalnetworkpolicies.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/globalnetworksets.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/hostendpoints.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamblocks.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamconfigs.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamhandles.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ippools.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipreservations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/kubecontrollersconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/networkpolicies.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/networksets.crd.projectcalico.org created
clusterrole.rbac.authorization.k8s.io/calico-kube-controllers created
clusterrole.rbac.authorization.k8s.io/calico-node created
clusterrolebinding.rbac.authorization.k8s.io/calico-kube-controllers created
clusterrolebinding.rbac.authorization.k8s.io/calico-node created

接下来,使用以下命令验证 pod 的状态:

kubectl get pods -n kube-system

您将得到以下输出:

NAME                                            READY   STATUS    RESTARTS   AGE
calico-kube-controllers-58dbc876ff-nh2st        1/1     Running   0          5m58s
calico-node-7cfz7                               1/1     Running   0          5m58s
calico-node-lt5cv                               1/1     Running   0          5m58s
coredns-565d847f94-dm6qc                        1/1     Running   0          21m
coredns-565d847f94-zhng9                        1/1     Running   0          21m
etcd-k8smaster.example.net                      1/1     Running   0          22m
kube-apiserver-k8smaster.example.net            1/1     Running   0          22m
kube-controller-manager-k8smaster.example.net   1/1     Running   0          22m
kube-proxy-9w2xp                                1/1     Running   0          14m
kube-proxy-gdb97                                1/1     Running   0          21m
kube-scheduler-k8smaster.example.net            1/1     Running   0          22m

您现在可以使用以下命令检查 Kubernetes 集群的状态:

kubectl get nodes

您应该看到 Master 和 Worker 节点都处于就绪状态:

NAME                    STATUS   ROLES           AGE   VERSION
k8smaster.example.net   Ready    control-plane   22m   v1.25.0
kubernetes              Ready              14m   v1.25.0

在 Kubernetes 上部署 Nginx 应用程序

为了测试 Kubernetes,我们将在集群上部署 Nginx 应用程序。

运行以下命令部署 Nginx 应用程序:

kubectl create deployment nginx-app --image=nginx --replicas=2

您可以使用以下命令验证您的应用程序:

kubectl get deployment nginx-app

您将得到以下输出:

NAME        READY   UP-TO-DATE   AVAILABLE   AGE
nginx-app   2/2     2            2           13s

接下来,使用以下命令在端口 80 上公开您的应用程序:

kubectl expose deployment nginx-app --type=NodePort --port=80

接下来,使用以下命令验证 Nginx 服务状态:

kubectl get svc nginx-app

您将得到以下输出:

NAME        TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
nginx-app   NodePort   10.109.89.196           80:30921/TCP   14s

您还可以使用以下命令查看 Nginx 应用程序的详细信息:

kubectl describe svc nginx-app

您应该看到以下输出:

Name:                     nginx-app
Namespace:                default
Labels:                   app=nginx-app
Annotations:              
Selector:                 app=nginx-app
Type:                     NodePort
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.109.89.196
IPs:                      10.109.89.196
Port:                       80/TCP
TargetPort:               80/TCP
NodePort:                   30921/TCP
Endpoints:                192.168.192.129:80,192.168.192.130:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   

现在,记下上面输出中的 Nginx 应用程序 IP 地址,并使用curl 命令验证您的 Nginx 应用程序:

curl http://10.109.89.196

如果一切正常,您将得到以下输出:

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

恭喜!您已在 Ubuntu 22.04 服务器上成功部署 Kubernetes 集群。

©2015-2025 Norria support@norria.com