解析kubernetes架构
一. 简介:
kubernetes是一个开源的容器管理工具,是基于GO语言开实现的,轻量级和便携式的应用,可以把kubernetes cluster在linux主机上部署、管理和扩容docker容器的应用在多个主机上。
二. 架构:
1. kubernetes由以下组件组成:
- kubernetes master
- kubernetes nodes
- etcd
- kubernetes network
2. 组件是通过网络连接的,如下图所示:
3. 上面的图可以汇总如下信息:
- kubernetes master: 通过HTTP或者HTTPS连接etcd去存储数据。
- kubernetes nodes: 通过HTTP或者HTTPS连接kubernetes master去获取命令和报告状态。
- kubernetes network: 通过L2,L3或overlay和容器之间建立连接。
(1) kubernetes master:
kubernetes主要有以下几个功能:
- 认证和授权
- RESTful API entry point
- 对kubernetes nodes容器部署的调度
- 扩容和复制容器
- 读取配置去创建群集
下图显示了master daemon 是如何工作实现上述功能的:
API server (kube-apiserver)
API server提供基于HTTP或者HTTPS的RESTful API,它是kubernetes 组件中心,比如kubectl, the scheduler, the replication controller, 和etcd 数据存储,及运行在kubernetes nodes上的kubelet 和kube-proxy.
Scheduler (kube-scheduler)
调度器帮助选择哪个容器运行在哪个节点上,针对派送和绑定容器到节点,它只是使用一个简单的算法定义优先级。
比如:
- CPU
- 内存
- 多少容器运行
Controller manager( kube-controller-manager)
该控制管理执行群集的操作。比如:
- 管理kubernetes nodes
- 创建和更新kubernetes内部信息
- 尝试改变当前的状态到满意的状态。
Command-line interface (kubectl)
安装完kubernetes master后,你可以使用kubernetes 命令行接口,kuberctl,去管理kubernetes群集,比如使用kubectl get cs返回每个组件的状态,kubectl get nodes返回kubernetes节点的列表。
//see the Component Statuses # kubectl get cs NAME STATUS MESSAGE ERROR controller-manager Healthy ok nil scheduler Healthy ok nil etcd-0 Healthy {"health": "true"} nil //see the nodes # kubectl get nodes NAME LABELS STATUS AGE kub-node1 kubernetes.io/hostname=kub-node1 Ready 26d kub-node2 kubernetes.io/hostname=kub-node2 Ready 26d
(2) kubernetes节点
kubernetes node在kubernetes群集中是slave node, 它是由kubernetes master控制的,使用docker支行应用。
下图描述了节点中的任务和角色:
从上图可以看出该了该节点有两个守护进程,kubelet和kube-proxy。
kubelet在kubernetes节点中是主要的进程,它负责和kubernetes master通信,完成以下操作。
- 周期性的访问 API controller去检查和报告
- 执行容器的操作
- 支行HTTP服务器提供简单的APIs.
proxy(kube-proxy)
proxy处理网络代理和每个容器的负载均衡,它通过改变linux iptables rules来控制在容器上的TCP和UDP包。
在配置完kube-proxy守护进程后,它配置Iptables规则,可以使用iptables –t nat –L 或者iptables –t nat –S 去检查nat 表的规则,如下所示:
//the result will be vary and dynamically changed by kube-proxy
# sudo iptables -t nat -S
-P PREROUTING ACCEPT
-P INPUT ACCEPT
-P OUTPUT ACCEPT
-P POSTROUTING ACCEPT
-N DOCKER
-N FLANNEL
-N KUBE-NODEPORT-CONTAINER
-N KUBE-NODEPORT-HOST
-N KUBE-PORTALS-CONTAINER
-N KUBE-PORTALS-HOST
-A PREROUTING -m comment --comment "handle ClusterIPs; NOTE: this must be before the NodePort rules" -j KUBE-PORTALS-CONTAINER
-A PREROUTING -m addrtype --dst-type LOCAL -m comment --comment "handle service NodePorts; NOTE: this must be the last rule in the chain" -j KUBE-NODEPORT-CONTAINER
-A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER
-A OUTPUT -m comment --comment "handle ClusterIPs; NOTE: this must be before the NodePort rules" -j KUBE-PORTALS-HOST
-A OUTPUT -m addrtype --dst-type LOCAL -m comment --comment "handle service NodePorts; NOTE: this must be the last rule in the chain" -j KUBE-NODEPORT-HOST
-A OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER
-A POSTROUTING -s 192.168.90.0/24 ! -o docker0 -j MASQUERADE
-A POSTROUTING -s 192.168.0.0/16 -j FLANNEL
-A FLANNEL -d 192.168.0.0/16 -j ACCEPT
-A FLANNEL ! -d 224.0.0.0/4 -j MASQUERADE
(3) etcd
etcd是分布式的键值数据存储,可以通过RESTful API去执行CRUD的操作,kubernetes使用etcd作为主要数据存储。
可以通过curl命令去获取:
//example: etcd server is localhost and default port is 4001
# curl -L http://127.0.0.1:4001/v2/keys/registry
{"action":"get","node":{"key":"/registry","dir":true,"nodes":[{"key":"/registry/namespaces","dir":true,"modifiedIndex":6,"createdIndex":6},{"key":"/registry/pods","dir":true,"modifiedIndex":187,"createdIndex":187},{"key":"/registry/clusterroles","dir":true,"modifiedIndex":196,"createdIndex":196},{"key":"/registry/replicasets","dir":true,"modifiedIndex":178,"createdIndex":178},{"key":"/registry/limitranges","dir":true,"modifiedIndex":202,"createdIndex":202},{"key":"/registry/storageclasses","dir":true,"modifiedIndex":215,"createdIndex":215},{"key":"/registry/apiregistration.k8s.io","dir":true,"modifiedIndex":7,"createdIndex":7},{"key":"/registry/serviceaccounts","dir":true,"modifiedIndex":70,"createdIndex":70},{"key":"/registry/secrets","dir":true,"modifiedIndex":71,"createdIndex":71},{"key":"/registry/deployments","dir":true,"modifiedIndex":177,"createdIndex":177},{"key":"/registry/services","dir":true,"modifiedIndex":13,"createdIndex":13},{"key":"/registry/configmaps","dir":true,"modifiedIndex":52,"createdIndex":52},{"key":"/registry/ranges","dir":true,"modifiedIndex":4,"createdIndex":4},{"key":"/registry/minions","dir":true,"modifiedIndex":58,"createdIndex":58},{"key":"/registry/clusterrolebindings","dir":true,"modifiedIndex":171,"createdIndex":171}],"modifiedIndex":4,"createdIndex":4}}
(4) kubernetes network:
容器如果在单一的节点之间进行通信 ,可以使用Docker network或者Docker compose去发现彼此。假如在多个节点之间进行通信,Kubernetes使用overlay network或者 container network interface(CNI) 去完成多个容器间的通信 。