这篇文章主要介绍“Kubernetes集群联邦怎么创建”,在日常操作中,相信很多人在Kubernetes集群联邦怎么创建问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Kubernetes集群联邦怎么创建”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
Federation使用域名将不同子集群的同一个服务暴露出来,因此需要用户提供一个可配置的域名。您可以通过万网在阿里云上购买域名然后配置到您的Federation集群上。
以域名spacexnice.xyz
为例,请按照购买域名文档购买好域名。
由于Federation在添加域名解析记录的时候设置域名的TTL为3分钟,域名层级为5级,因此需要到阿里云上对您刚购买的域名进行设置,使得该域名允许的最小TTL值小于3分钟,同时域名层级大于5.
进入云解析控制台选择您刚刚购买的域名,更多里面选[升级]或者[升级VIP]
在弹出的页面中更改最低TTL值为60s,子域名级别为7级,然后保存。
本示例创建的Federation集群包含两个子集群,分别在杭州和北京region。我们首先需要在这两个Region创建出两个可用的集群,集群创建步骤参考文档
杭州集群
[root@iZbp1bpj58bdzdosdxxf82Z ~]# kubectl get no NAME STATUS ROLES AGE VERSION cn-hangzhou.i-bp11ajet2v9o3tp0o2uw Ready master 3d v1.8.4 cn-hangzhou.i-bp15pt2475mgw64k72ma Ready master 3d v1.8.4 cn-hangzhou.i-bp18fggko6p3vyr0p0ew Ready <none> 3d v1.8.4 cn-hangzhou.i-bp18fggko6p3vyr0p0ex Ready <none> 3d v1.8.4 cn-hangzhou.i-bp1bpj58bdzdosdxxf82 Ready master 3d v1.8.4
北京集群
[root@iZze7z98ssc50pfzt6be9 ~]# kubectl get no NAME STATUS ROLES AGE VERSION cn-beijing.i-2ze7z98ssc50pfzt6be9 Ready master 3d v1.8.4 cn-beijing.i-2ze8hr6k03ccroopicd2 Ready master 3d v1.8.4 cn-beijing.i-2zeipodwz2junslmihqz Ready master 3d v1.8.4 cn-beijing.i-2zeipodwz2junyipunrf Ready <none> 3d v1.8.4
选择杭州的子集群作为Federation的宿主集群,安装Federation的控制平面。并配置好本地的kubeconfig。通过运行kubefed init命令来初始化主集群。具体步骤如下:
在hangzhou集群的master节点安装kubefed
[root@iZze7z98ssc50pfzt6be9 ~]# curl -L http://aliacs-k8s-cn-hangzhou.oss.aliyuncs.com/bin/kubefed-1.8.4 -o kubefed [root@iZze7z98ssc50pfzt6be9 ~]# sudo cp kubefed /usr/bin [root@iZze7z98ssc50pfzt6be9 ~]# sudo chmod +x /usr/bin/kubefed [root@iZze7z98ssc50pfzt6be9 ~]# kubefed version
在杭州集群master节点中配置hangzhou,beijing两个集群的context,让改节点能通过切换context连接不同的子集群。要修改的内容如下:
修改hangzhou集群的kubeconfig的cluster,user,context的名字为hangzhou
拷贝beijing集群kubeconfig中的cluster,user,context内容到a)步骤中kubeconfig,并修改相应的cluster,user,context名字为beijing
为了方便初学者,我们提供了一个自动化的脚本,可以自动通过ssh从您指定的集群中下载kubeconfig配置文件,并合并成一个整体。
通过执行脚本来自动生成~/.kube/config
文件。脚本需要指明从哪些集群上下载kubeconfig文件并合并,格式如下python context.py -H hangzhou:1.1.1.1,beijing:2.2.2.2
注意参数替换成你自己的集群Region和IP,脚本可能会要求您输入ssh的登录密码。
[root@iZbp1bpj58bdzdosdxxf82Z ~]# curl http://aliacs-k8s-cn-hangzhou.oss.aliyuncs.com/tools/context.py > context.py [root@iZbp1bpj58bdzdosdxxf82Z ~]# sudo pip install pyyaml [root@iZbp1bpj58bdzdosdxxf82Z ~]# unset KUBECONFIG [root@iZbp1bpj58bdzdosdxxf82Z ~]# python context.py -H ${HOST} Info: host, hangzhou:118.31.177.21,beijing:47.95.96.234 . Info: file /root/.kube/config successfully generated.
配置结果如下:
[root@iZbp1bpj58bdzdosdxxf82Z ~]# kubectl config get-contexts CURRENT NAME CLUSTER AUTHINFO NAMESPACE hangzhou kubernetes-hangzhou kubernetes-admin-hangzhou * beijing kubernetes-beijing kubernetes-admin-beijing
zones填写您前面申请的域名,注意以点号结尾。并且替换您的accesskeyid和accesskeysecret.
cat >alidns.yaml <<EOF [Global] access-key-id = <Your-key-id> access-key-secret = <Your-key-secret> zones = spacexnice.xyz. EOF
通过本条命令初始化federation控制平面,参数如下: kubefed init federation \ # 联邦的名字 --host-cluster-context=hangzhou \ # 主集群的context名字 --dns-provider="alidns" \ # DNS服务提供商 --dns-zone-name="spacexnice.xyz." \ # 前面注册好的域名,必须以.结束 --dns-provider-config="alidns.yaml" \ # alidns配置文件 --image="registry.cn-hangzhou.aliyuncs.com/google-containers/hyperkube-amd64:v1.8.4-4_99c084ce" \ # hyperkube镜像 --etcd-image='registry.cn-hangzhou.aliyuncs.com/google-containers/etcd-amd64:3.1.11' \ #etcd 镜像 --etcd-persistent-storage=false # 关闭etcd持久存储
[root@iZbp1bpj58bdzdosdxxf82Z ~]# kubefed init federation \ --host-cluster-context=hangzhou \ --dns-provider="alidns" \ --dns-zone-name="spacexnice.xyz." \ --dns-provider-config="alidns.yaml" \ --image="registry.cn-hangzhou.aliyuncs.com/google-containers/hyperkube-amd64:v1.8.4-4_99c084ce" \ --etcd-image='registry.cn-hangzhou.aliyuncs.com/google-containers/etcd-amd64:3.1.11' \ --etcd-persistent-storage=false Creating a namespace federation-system for federation system components... done Creating federation control plane service..... done Creating federation control plane objects (credentials, persistent volume claim)... done Creating federation component deployments... done Updating kubeconfig... done Waiting for federation control plane to come up..................................................................................................................................................... done Federation API server is running at: 120.55.196.1
目前为止您已经成功的初始化好了Federation的控制平面。接下来需要将各个子集群加入到Federation集群中。
添加hangzhou集群
命令行参数解析 kubefed join hangzhou \ #加入联邦的集群命名名字 --context=federation \ #联邦的context --cluster-context=hangzhou \ #要添加集群的context --host-cluster-context=hangzhou #主集群的context
[root@iZbp1bpj58bdzdosdxxf82Z ~]# kubefed join hangzhou \ --context=federation \ --cluster-context=hangzhou \ --host-cluster-context=hangzhou [root@iZbp1bpj58bdzdosdxxf82Z ~]# kubectl get cluster --context=federation NAME STATUS AGE hangzhou Ready 28s
添加beijing集群
[root@iZbp1bpj58bdzdosdxxf82Z ~]# kubefed join beijing \ --context=federation \ --cluster-context=beijing \ --host-cluster-context=hangzhou [root@iZbp1bpj58bdzdosdxxf82Z ~]# kubectl get cluster --context=federation NAME STATUS AGE hangzhou Ready 4m beijing Ready 59s
后面介绍下集群查询,移除集群,删除联邦等命令
查询注册到Federation的kubernetes集群列表
[root@iZbp1bpj58bdzdosdxxf82Z ~]# kubectl get cluster --context=federation NAME STATUS AGE hangzhou Ready 4m beijing Ready 59s
移除beijing集群
[root@iZbp1bpj58bdzdosdxxf82Z ~]# kubefed unjoin beijing --host-cluster-context=hangzhou --context=federation [root@iZbp1bpj58bdzdosdxxf82Z ~]# kubectl get clusters --context=federation NAME STATUS AGE hangzhou Ready 1d
集群联邦控制平面的删除功能还在开发中,目前可以通过删除namespace federation-system的方法来清理(注意pv不会删除)。命令在host-cluster-context上执行。
[root@iZbp1bpj58bdzdosdxxf82Z ~]# kubectl delete ns federation-system
集群联邦支持以下联邦资源,这些资源会自动在所有注册的kubernetes集群中创建.
Federated ConfigMap
Federated Service
Federated DaemonSet
Federated Deployment
Federated Ingress
Federated Namespaces
Federated ReplicaSets
Federated Secrets
Federated Events(仅存在federation控制平面)
[root@iZbp1bpj58bdzdosdxxf82Z ~]# kubectl run nginx --image nginx --context=federation [root@iZbp1bpj58bdzdosdxxf82Z ~]# kubectl expose deploy nginx --port 80 --target-port 80 --type LoadBalancer --context=federation [root@iZbp1bpj58bdzdosdxxf82Z ~]# kubectl get svc --context=federation
您还可以通过kubectl scale deploy nginx --replicas=3 --context=federation
来扩展nginx副本,然后观察nginx应用在各个子集群中的分布情况。
[root@iZbp1bpj58bdzdosdxxf82Z ~]# kubectl get deploy --context=beijing [root@iZbp1bpj58bdzdosdxxf82Z ~]# kubectl get deploy --context=hangzhou
[root@iZbp1bpj58bdzdosdxxf82Z ~]# cat deployment-nginx.yaml apiVersion: extensions/v1beta1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.7.9 ports: - containerPort: 80 [root@iZbp1bpj58bdzdosdxxf82Z ~]# kubectl create -f deployment-nginx.yaml --context=federation
[root@iZbp1bpj58bdzdosdxxf82Z ~]# kubectl describe svc nginx --context=federation Name: nginx Namespace: default Labels: app=nginx Annotations: federation.kubernetes.io/service-ingresses={"items":[{"cluster":"hangzhou","items":[{"ip":"120.55.149.69"}]},{"cluster":"shanghai","items":[{"ip":"139.196.70.146"}]}]} Selector: app=nginx Type: LoadBalancer IP: LoadBalancer Ingress: 120.55.149.69, 139.196.70.146 Port: http 80/TCP Endpoints: <none> Session Affinity: None Events: <none>
通过域名访问服务:
[root@iZbp1bpj58bdzdosdxxf82Z ~]# curl nginx.default.fed.svc.spacexnice.xyz <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h2>Welcome to nginx!</h2> <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>
我们可以在阿里云云解析控制台看见刚刚添加的nginx的dns记录。
到此,关于“Kubernetes集群联邦怎么创建”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。