使用 Kubeadm 部署 Kubernetes
2024-01-05 05:34:55
准备环境
首先,需要准备好要部署 Kubernetes 集群的机器。这些机器可以是物理服务器或虚拟机,但必须满足以下最低要求:
- 至少 2GB 内存
- 至少 20GB 磁盘空间
- 至少 1 个网络接口
- 64 位 Linux 操作系统(推荐使用 Ubuntu 16.04 或 CentOS 7)
安装 Kubeadm
在所有机器上安装 Kubeadm。对于 Ubuntu 16.04,可以使用以下命令:
sudo apt-get update
sudo apt-get install kubeadm
对于 CentOS 7,可以使用以下命令:
sudo yum update
sudo yum install kubeadm
初始化集群
在其中一台机器上初始化集群。这将创建第一个 Kubernetes 控制节点。可以使用以下命令:
kubeadm init --pod-network-cidr=10.244.0.0/16
加入集群
在其他机器上加入集群。可以使用以下命令:
kubeadm join 10.0.0.1:6443 --token 44382e.344822fb7b2c07f1 --discovery-token-ca-cert-hash sha256:50675c88cc7e05375d09b1352288f521c20a206359408e3620d044d1491d90d0
其中,10.0.0.1 是控制节点的 IP 地址,44382e.344822fb7b2c07f1 是令牌,50675c88cc7e05375d09b1352288f521c20a206359408e3620d044d1491d90d0 是发现令牌 CA 证书的哈希值。
验证集群
使用以下命令验证集群是否正常运行:
kubectl get nodes
如果看到以下输出,则表示集群正常运行:
NAME STATUS ROLES AGE VERSION
10.0.0.2 Ready control-plane 6m v1.19.1
10.0.0.3 Ready worker 6m v1.19.1
10.0.0.4 Ready worker 6m v1.19.1
安装网络插件
Kubernetes 集群需要网络插件来实现容器之间的网络通信。可以使用以下命令安装 Flannel 网络插件:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
安装存储插件
Kubernetes 集群需要存储插件来管理持久性存储。可以使用以下命令安装 NFS 存储插件:
kubectl apply -f https://raw.githubusercontent.com/kubernetes/kubernetes/master/cluster/addons/storage/nfs/nfs-server-pv.yaml
kubectl apply -f https://raw.githubusercontent.com/kubernetes/kubernetes/master/cluster/addons/storage/nfs/nfs-client-pv.yaml
安装应用
现在,可以开始在集群上安装应用了。可以使用以下命令安装 Nginx:
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/provider/cloud/deploy.yaml
监控和日志
可以使用以下命令安装 Prometheus 和 Grafana 来监控集群:
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install prometheus prometheus-community/prometheus
helm install grafana grafana/grafana
可以使用以下命令安装 Fluentd 和 Kibana 来收集和分析日志:
helm repo add fluent https://fluent.github.io/helm-charts
helm repo update
helm install fluentd fluent/fluentd
helm install kibana kibana/kibana
安全
可以使用以下命令安装 Calico 来保护集群:
kubectl apply -f https://docs.projectcalico.org/manifests/tigera-operator.yaml
kubectl apply -f https://docs.projectcalico.org/manifests/custom-resources.yaml
自动化
可以使用以下命令安装 Argo CD 来实现持续交付:
helm repo add argocd https://argoproj.github.io/argo-cd
helm repo update
helm install argocd argocd/argo-cd
总结
本文介绍了如何使用 Kubeadm 部署 Kubernetes 集群,从准备环境到安装和配置 Kubernetes,再到后续的管理和维护。通过学习本文,您将掌握 Kubeadm 的使用,以便为您的云原生项目奠定基础。