文章目录
前言
写给自己的入门篇。后续会在原理方面持续更新
一、创建 k8s 集群
k8s 集群的创建有多种方法,可以按照官方文档的说明来操作 https://kubernetes.io/docs/setup/production-environment/tools/
对于新手(比如我)来说,我认为需要结合 k8s 架构来理解集群创建的过程。
(图片来源于 https://www.redhat.com/en/topics/containers/kubernetes-architecture)
需要安装:
- container runtime: 用于运行 container 的服务,每个 node 都需要安装并启动
- kubectl: 用户 CLI,是集群资源管理、容器部署、调试时的主要工具
- kubelet: 运行在 node 上的服务,确保 pod 与 container 启动并运行 (需要关闭 swap),每个 node 都需要安装并启动
- kubeadm: 创建与管理集群
初始化 control-plane (master) 节点:
# kubeadm init
这个过程一般会遇到很多报错,Google 是最好的寻求解决方案的地方。初始化成功后,将会有如下输出:
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 10.7.157.30:6443 --token 2rg4l1.n0rhvdp0uvxdrxjv \
--discovery-token-ca-cert-hash sha256:fd7d661ec35868d036761e844597807a3d076daf3c8b71de6e1b55ee01e66a32
此时会发现,如下 Pods 已创建,除了 coredns 处于 Pending 状态,其余都处于 Running 状态:
# export KUBECONFIG=/etc/kubernetes/admin.conf
# kubectl get node -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
node1 Ready control-plane 2m50s v1.24.0 10.7.157.30 <none> Red Hat Enterprise Linux Server 7.7 (Maipo) 3.10.0-1062.el7.x86_64 containerd://1.6.4
# kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-6d4b75cb6d-752q4 0/1 Pending 0 35s
kube-system coredns-6d4b75cb6d-7h2g5 0/1 Pending 0 35s
kube-system etcd-node1 1/1 Running 5 47s
kube-system kube-apiserver-node1 1/1 Running 4 48s
kube-system kube-controller-manager-node1 1/1 Running 1 47s
kube-system kube-proxy-px447 1/1 Running 0 35s
kube-system kube-scheduler-node1 1/1 Running 4 48s
二、启用 primary network
先说 k8s 的网络模型。
对于 k8s 网络,核心理念是 - 每个 pod 都有唯一的 IP。 Pod 中所有 container 共享该 IP,并可以与其他 Pod 通信。
通常会在 kubeadm.config.yaml 中设置 pod subnet 作为 CIDR 块,即一系列 IP 地址,在此范围内分配 IP 给 pod:
#### in kubeadm-config.yaml ####
kind: ClusterConfiguration
apiVersion: kubeadm.k8s.io/v1beta3
kubernetesVersion: v1.24.0
networking:
podSubnet: 10.244.0.0/16
Pod 之间的通信,通常会结合管道对与以太网桥来实现:
cni0 本质上是 Linux 网桥,可以发送 ARP request 与解析 ARP response
eno1 作为 node 之间通信的网络接口,启用了 IP 转发,可以依据 Route Table 将收到的数据包转发给 cni0
为了启用 k8s primary network,需要安装 primary network CNI
有多种选择,如 flannel, Calico, WeaveNet 等。 此例选取 flannel,需要设置 flannel 使用的网络接口:
# yum install -y flannel
# vi /etc/sysconfig/flanneld ## add additional options:
FLANNEL_OPTIONS="-iface=eno1"
# cp /usr/bin/flanneld /opt/bin
# kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
Warning: policy/v1beta1 PodSecurityPolicy is deprecated in v1.21+, unavailable in v1.25+
podsecuritypolicy.policy/psp.flannel.unprivileged created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.apps/kube-flannel-ds created
此时 coredns 将会变为 Running 状态
三、启用 secondary network
Primary network 常用于 Pod 之间的基本通信。通常需要为 Pod 提供 secondary network,作为高性能网络供应用程序使用:
需要部署:
- k8s-rdma-shared-dev-plugin
- Multus CNI
- Secondary CNI
- Multi-Network CRD
其中 Multus CNI 可以看作一个 meta plugin,与其他 CNI plugin 配合使用,以实现多网络接口的功能:
k8s-rdma-shared-dev-plugin
创建 configmap
# cat k8s-rdma-shared-dev-plugin-config-map.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: rdma-devices
namespace: kube-system
data:
config.json: |
{
"periodicUpdateInterval": 300,
"configList": [