本页内容
- 准备工作
- 开始之前
- 安装 Containerd
- 安装 Kubernetes 组件
- 初始化 Kubernetes 集群
- 将 Worker 节点加入集群
- 安装 Calico Pod 网络插件
- 在 Kubernetes 上部署 Nginx 应用
Kubernetes 是一款免费的容器编排工具,可帮助您实现容器化应用的自动化部署、扩缩容和管理。一个 Kubernetes 集群由一个 Master 节点和一个 Worker 节点组成。Master 节点负责管理集群中的节点和 Pod。Worker 节点用于部署应用负载。借助 Kubernetes,您可以使用本地基础设施或公有云平台来部署和管理云原生应用。
本教程将向您展示如何在 Ubuntu 22.04 服务器上搭建 Kubernetes 集群。
准备工作
- 一台运行 Ubuntu 22.04 的服务器。
- 服务器上已配置 root 密码。
开始之前
首先,您需要将系统软件包更新并升级到最新版本。可以使用以下命令完成:
apt update -y
apt upgrade -y
所有软件包更新完成后,您还需要禁用系统上的 Swap(交换分区)。可以使用以下命令将其禁用:
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
安装 Containerd
接下来,您需要为 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
接下来,您需要配置 Containerd,使其由 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 组件均已安装完成。现在,在 Master 节点上运行以下命令来初始化集群:
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 命令。您需要在 Worker 节点上运行该命令以加入集群。接下来,您需要运行以下命令来与 Kubernetes 集群进行交互:
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
将 Worker 节点加入集群
接下来,登录到 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 网络以管理网络。
可以使用以下命令下载并安装它:
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 节点均处于就绪(ready)状态:
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 集群。