Kubernetes使用集群联邦实现多集群管理 £神魔★判官ぃ 2022-06-06 04:15 373阅读 0赞 > 作者介绍:有容云系统工程师 吕龙。 [Kubernetes使用集群联邦实现多集群管理][Kubernetes] Kubernetes在1.3版本之后,增加了“集群联邦”Federation的功能。这个功能使企业能够快速有效的、低成本的跨区跨域、甚至在不同的云平台上运行集群。这个功能可以按照地理位置创建一个复制机制,将多个kubernetes集群进行复制,即使遇到某个区域连接中断或某个数据中心故障,也会保持最关键的服务运行。在1.7版以后支持本地多个集群联邦管理,不需要使用依赖云平台。 ## 一、Kubernetes集群联邦介绍 ## ### 1、管理多個kubernetes集群 ### “集群联邦”在架构上同kubernetes集群很相似。有一个“集群联邦”的API server提供一个标准的Kubernetes API,并且通过etcd来存储状态。不同的是,一个通常的Kubernetes只是管理节点计算,而“集群联邦”管理所有的kubernetes集群。 ![20171025100201.jpg][] ### 2、跨集群服务发现 ### Kubernetes有一个标准的插件:kube-dns,这个插件可以在集群内部提供DNS服务,通过DNS解析service名字来访问kubernetes服务。Kubernetes服务是由一组kubernetesPOD组成的,这些POD是一些已经容器化了的应用,这些POD前面使用到了负载均衡器。假如我们有一个kubernetes集群,这个集群裡面有一个服务叫做mysql,这个服务是由一组mysql POD组成的。在这个kubernetes集群中,其他应用可以通过DNS来访问这个mysql服务。 集群联邦federation/v1beta1 API扩展基于DNS服务发现的功能。利用DNS,让POD可以跨集群、透明的解析服务。 ![20171025100218.jpg][] ### 3、跨集群调度 ### 为了追求高可用性和更高的性能,集群联邦能够把不同POD指定给不同的Kubernetes集群中。集群联邦调度器将决定如何在不同kubernetes集群中分配工作负载。 通过跨集群调度,我们可以: * 跨kubernetes集群均匀的调度任务负载 * 将各个kubernetes集群的工作负载进行最大化,如果当前kubernetes集群超出了承受能力,那麽将额外的工作负载路由到另一个比较空闲的kubernetes集群中 * 根据应用地理区域需求,调度工作负载到不同的kubernetes集群中,对于不同的终端用户,提供更高的带宽和更低的延迟。 ### 4、集群高可用,故障自动迁移 ### 集群联邦可以跨集群冗馀部署,当某个集群所在区域出现故障时,并不影响整个服务。集群联邦还可以检测集群是否为不可用状态,如果发现某个集群为不可用状态时,可以将失败的任务重新分配给集群联邦中其他可用状态的集群上。 ## 二、Kubernetes使用集群联邦实现多集群管理 ## ### 1、系统环境及初始化 ### 1.1 系统环境 <table style="border-spacing:0px; border-collapse:collapse; font-size:12px; border-top:1px solid rgb(221,221,221); border-left:1px solid rgb(221,221,221); width:780px; margin-bottom:18px; color:rgb(85,85,85); font-family:"Microsoft Yahei""> <tbody style=""> <tr style=""> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 功能组件</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 系统组件</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 系统版本</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 推荐配置</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 设备数量</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 备注</td> </tr> <tr style=""> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 联邦集群控制平面</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> K8S+Federation</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> CentOS 7.2</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 4核/8G/80G</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 2台</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 联邦集群控制平面</td> </tr> <tr style=""> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> K8s集群01</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> Kubernetes master+node</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> CentOS 7.2</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 4核/8G/80G</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 2台</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 联邦集群节点</td> </tr> <tr style=""> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> K8s集群02</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> Kubernetes master+node</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> CentOS 7.2</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 4核/8G/80G</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 2台</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 联邦集群节点</td> </tr> </tbody> </table> 1.2 安装前准备 1.2.1 准备kubernetes组件镜像 Kubernetes1.7版本的集群与联邦集群功能的安装方式均为镜像安装,所有组件功能都使用官方提供的镜像来实现,由于官方镜像国内无法正常下载,需要提前准备好镜像或科学上。 网,具体镜像列表如下 <table style="border-spacing:0px; border-collapse:collapse; font-size:12px; border-top:1px solid rgb(221,221,221); border-left:1px solid rgb(221,221,221); width:780px; margin-bottom:18px; color:rgb(85,85,85); font-family:"Microsoft Yahei""> <tbody style=""> <tr style=""> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 序号</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> Kubernetes镜像</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 版本</td> </tr> <tr style=""> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 1</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> gcr.io/google_containers/kube-proxy-amd64</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> v1.7.0</td> </tr> <tr style=""> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 2</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> gcr.io/google_containers/kube-apiserver-amd64</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> v1.7.0</td> </tr> <tr style=""> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 3</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> gcr.io/google_containers/kube-controller-manager-amd64</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> v1.7.0</td> </tr> <tr style=""> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 4</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> gcr.io/google_containers/kube-scheduler-amd64</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> v1.7.0</td> </tr> <tr style=""> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 5</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> gcr.io/google_containers/kube-aggregator</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> v1.7.0</td> </tr> <tr style=""> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 6</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> gcr.io/google_containers/k8s-dns-sidecar-amd64</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 1.14.4</td> </tr> <tr style=""> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 7</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> gcr.io/google_containers/k8s-dns-kube-dns-amd64</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 1.14.4</td> </tr> <tr style=""> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 8</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> gcr.io/google_containers/k8s-dns-dnsmasq-nanny-amd64</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 1.14.4</td> </tr> <tr style=""> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 9</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> gcr.io/google_containers/etcd-amd64</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 3.0.17</td> </tr> <tr style=""> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 10</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> quay.io/coreos/flannel</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> v0.7.1-amd64</td> </tr> <tr style=""> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 11</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> gcr.io/google_containers/pause-amd64</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 3.0</td> </tr> <tr style=""> <td colspan="3" style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 联邦集群镜像</td> </tr> <tr style=""> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 1</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> gcr.io/google_containers/hyperkube-amd64</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> v1.7.0</td> </tr> <tr style=""> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 2</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> docker.io/coredns/coredns</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 006</td> </tr> <tr style=""> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> 3</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> gcr.io/kubernetes-helm/tiller</td> <td style="padding:5px 10px; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(221,221,221)"> v2.1.3</td> </tr> </tbody> </table> 1.2.2 安装Docker 1.12 Kubernetes 1.7还没有针对docker 1.13上做测试和验证,所以这里安装Kubernetes官方推荐的Docker 1.12版本。 1.2.3 修改系统配置 创建/etc/sysctl.d/k8s.conf文件,添加如下内容: net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 执行sysctl -p /etc/sysctl.d/k8s.conf使修改生效。 在/etc/hostname中修改各节点的hostname,在/etc/hosts设置hostname对应ip: 192.168.5.13 test01.example.com 192.168.5.14 test02.example.com 关闭防火墙服务与禁用开机启动项 systemctl stop firewalld systemctl disable firewalld 关闭SELINUX选项 setenforce 0 sed -i 's/SELINUX=enforcing/SELINUX=disabled/g'/etc/sysconfig/selinux 1.2.4 安装Kuberadm与Kubelet 注意:该yum源需要科学才能正常安装 在每个节点安装kubeadm和kubelet: cat <<EOF > /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=http://yum.kubernetes.io/repos/kubernetes-el7-x86_64 enabled=1 gpgcheck=1 repo_gpgcheck=1 gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg EOF 安装kubeadm, kubelet, kubectl, kubernets-cni yum install -y kubelet kubeadm kubectl kubernetes-cni kubelet服务设置开机启动 systemctl enable kubelet.service 安装完成后初始化环境准备完成。 ### 2、安装kubernetes集群 ### 首先在一个节点上安装kubernetes作为master节点,将master节点参与集群调度.然后在该节点上安装联邦集群功能,作为联邦集群的控制平面。 2.1 初始化集群 使用kubeadm初始化集群,选择node1作为Master,在node1上执行下面的命令: > 注:因为我们选择flannel作为Pod网络插件,所以命令指定–pod-network-cidr=10.240.0.0/16 kubeadm init --kubernetes-version=v1.7.0 --pod-network-cidr=10.240.0.0/16 --apiserver-advertise-address=192.168.5.13 kubeadm init执行成功后输出下面的信息: > 注:请保存好该信息,后续节点加入集群需要使用该命令. You can now join any number of machines by running the following on each node as root: kubeadm join --token e7986d.e440de5882342711 192.168.5.13:6443 为了使用kubectl访问apiserver,在~/.bash\_profile中追加下面的环境变量: echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> /etc/profile source /etc/profile 此时kubectl命令在master node上就好用了,查看一下当前机器中的Node: kubectl get nodes NAME STATUS AGE VERSION node0 Ready 3m v1.7.0 2.2 安装Pod网络组件 接下来安装flannel network add-on: wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel-rbac.yml wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml 目前需要在kube-flannel.yml中使用–iface参数指定集群主机内网网卡的名称,否则可能会出现dns无法解析。需要将kube-flannel.yml下载到本地,flanneld启动参数加上–iface=<iface-name> vi kube-flannel.yml command: [ "/opt/bin/flanneld", "--ip-masq", "--kube-subnet-mgr", "--iface=eth1" ] //改为本机网卡名称 部署网络组件: kubectl create -f kube-flannel-rbac.yml kubectl apply -f kube-flannel.yml 确保所有的Pod都处于Running状态。 kubectl get pod --all-namespaces -o wide ![20171025100309.jpg][] 使用下面的命令使Master Node参与工作负载: kubectl taint nodes --all node-role.kubernetes.io/master- ### 三、安装联邦集群组件 ### 3.1安装前配置修改 修改前先停止kubelet服务 vi /etc/kubernetes/manifests/etcd.yaml - --listen-client-urls=http://0.0.0.0:2379 - --advertise-client-urls=http://0.0.0.0:2379 vi /etc/kubernetes/manifests/kube-apiserver.yaml - --insecure-port=8080 - --insecure-bind-address=0.0.0.0 重启kubelet服务让etcd与apiserver监听端口生效 systemctl restart kubelet 3.2安装helm工具部署Coredns curl https://raw.githubusercontent.com/kubernetes/helm/master/scripts/get > get_helm.sh chmod 700 get_helm.sh ./get_helm.sh 修改RBAC临时访问权限 kubectl create clusterrolebinding add-on-cluster-admin --clusterrole=cluster-admin --serviceaccount=kube-system:default 创建coredns键值文件 cat <<EOF > /opt/Values.yaml isClusterService: false serviceType: "NodePort" middleware: kubernetes: enabled: false etcd: enabled: true zones: - "example.com." endpoint: "http://192.168.5.13:2379" EOF 初始化helm,并使用helm安装coredns组件 helm init helm install --namespace default --name coredns -f /opt/Values.yaml stable/coredns 新建coredns配置文件 vi /root/coredns-provider.conf [Global] etcd-endpoints = http://192.168.0.51:2379 zones = example.com. 3.3添加kubernetes从节点 在kubernetes工作节点上初始安装后执行以下命令 kubeadm join --token e7986d.e440de5882342711 192.168.5.13:6443 在master节点查看节点状态 kubectl get nodes 重复上述步骤创建多个kubernetes集群,为联邦集群添加集群节点做准备。 3.4初始化联邦集群 kubefed init fellowship \ --host-cluster-context=kubernetes-admin@kubernetes \ --dns-provider="coredns" \ --dns-zone-name="example.com." \ --dns-provider-config="/root/coredns-provider.conf" \ --apiserver-enable-basic-auth=true \ --apiserver-enable-token-auth=true \ --apiserver-arg-overrides="--anonymous-auth=false,--v=4" \ --api-server-service-type="NodePort" \ --api-server-advertise-address="192.168.5.13" \ --etcd-persistent-storage=false 初始化完成后集群联邦会在federation-system命名空间中创建两个POD ![20171025100352.jpg][] 3.5其他kubernetes集群加入联邦集群 选择集群的context kubectl config use-context fellowship //fellowship为联邦初始化时创建 添加联邦集群文件 cat /home/tmp/c1.yaml apiVersion: federation/v1beta1 kind: Cluster metadata: name: c1 spec: serverAddressByClientCIDRs: - clientCIDR: 0.0.0.0/0 serverAddress: http://192.168.15.53:8080 cat /home/tmp/c2.yaml apiVersion: federation/v1beta1 kind: Cluster metadata: name: c2 spec: serverAddressByClientCIDRs: - clientCIDR: 0.0.0.0/0 serverAddress: http://192.168.8.12:8080 添加集群到联邦 kubectl create -f c1.yaml kubectl create -f c2.yaml 查看联邦集群状态 [root@test01 ~]# kubectl get cluster NAME STATUS AGE c1 Ready 2d c2 Ready 2d 3.6通过联邦集群部署应用 通过联邦集群部署应用,需要在联邦集群控制平面的节点上,切换到联邦的context kubectl config use-context fellowship //fellowship为联邦初始化时创建 配置应用在分布在2个集群中 vi tomcat.yaml apiVersion: exensions/v1beta1 kind: ReplicaSet metadata: name: tomcat lables: app: tomcat annotaions: federation.kubernetes.io/replica-set-preferences: { "rebalance": true, "clusters": { "c1": { "weight": 1 }, "c2": { "weight": 1 } } } spec: replicas: 4 template: metadata: labels: #region: tomcat app: tomcat spec: containers: - name: fed-tomcat image: 192.168.18.250:5002/admin/tomcat:v0.0.5 resourcces: requests: cpu: 200M memory: 500Mi ports: - containerPort: 80 通过联邦创建应用,应用的4个实例按比例分布到2个集群中 [root@test01 ~]# kubectl create namespace default namespace "default" created [root@test01 ~]# kubectl create -f tomcat.yaml replicaset "tomcat" created [root@test01 ~]# kubectl get rs NAME DESIRED CURRENT AGE tomcat 4 4 <invalid> [root@test01 ~]# kubectl -s 192.168.15.53:8080 get pod NAME READY STATUS RESTARTS AGE tomcat-juywx 1/1 Running 0 33s tomcat-siidb 1/1 Running 0 33s [root@test01 ~]# kubectl -s 192.168.8.12:8080 get pod NAME READY STATUS RESTARTS AGE tomcat-sw437 1/1 Running 0 33s tomcat-n7z74 1/1 Running 0 33s 集群联邦只支持以下几种类型资源的创建: > Cluster > > ConfigMap > > DaemonSets > > Deployment > > Events > > Ingress > > Namespaces > > ReplicaSets > > Secrets > > Services [Kubernetes]: https://www.kubernetes.org.cn/2987.html [20171025100201.jpg]: /images/20220606/1d54a1ec4e8c4819aa6fa4dc91139fb8.png [20171025100218.jpg]: /images/20220606/5a5e7b0b58984de9b8ed7115ef178ebc.png [20171025100309.jpg]: /images/20220606/25affeff9a60498abebd1f6a266c90b6.png [20171025100352.jpg]: /images/20220606/a8a2e93d9d1f4890b84986d93413c26e.png
还没有评论,来说两句吧...