1.Magnum簡介
Magnum項目通過Openstack API能夠在Openstack中創建基於容器的服務,但它本身並不直接對容器進行操作,而是通過Kubernetes, Swarm或者Mesos后端進行管理。Openstack正式項目Zun具有直接管理docker的功能。
Magnum特征:
(1)Clusters(node對象的集合, 以前版本稱為Bay)抽象化。
(2)與Kubernetes, Swarm以及Mesos進行集成,管理容器。
(3)與Keystone集成,提供多租戶安全。
(4)與Neutron集成,為Kubernetes提供多租戶安全。
(5)與Cinder集成,為容器提供volume服務。

3.Magnum命令
command | parameters | function | example |
cluster-create | --cluster-template \ The ID or name of the ClusterTemplate to use. --name \ Name of the cluster to create. --keypair \ The name or UUID of the SSH keypair to configure in the cluster servers for ssh access. --node-count \ The number of servers that will serve as node in the cluster. --master-count \ The number of servers that will serve as master for the cluster. --discovery-url \ The custom discovery url for node discovery. --timeout \ The timeout for cluster creation in minutes. |
創建一個cluster,以異步的方式執行。 | magnum cluster-create --name mycluster \ --cluster-template mytemplate \ --node-count 8 \ --master-count 3 說明:使用名為mytemplate的模板, 創建了一個擁有8個node, 3個master node, 名為mycluster的cluster |
cluster-delete | 通過name或ID刪除一個cluster | magnum cluster-delete mycluster | |
cluster-list | 列出所以得cluster | magnum cluster-list | |
cluster-show | 顯示某個cluster的詳細信息 | magnum cluster-show mycluster | |
cluster-update | <cluster> \ This is the first parameter, specifying the UUID or name of the cluster to update. <op> \ This is the second parameter, specifying the desired change to be made to the cluster attributes. The allowed changes are 'add', 'replace' and 'remove'. <attribute=value> \ This is the third parameter, specifying the targeted attributes in the cluster as a list separated by blank space. |
更新某個cluster | magnum cluster-update mycluster replace node_count=8 說明:把mycluster中的node節點設置為8 |
cluster-template-create | --coe \ Specify the Container Orchestration Engine to use. Supported COE's include 'kubernetes', 'swarm', 'mesos'. --image \ The name or UUID of the base image in Glance to boot the servers for the cluster. --keypair \ The name or UUID of the SSH keypair to configure in the cluster servers for ssh access. --external-network \ The name or network ID of a Neutron network to provide connectivity to the external internet for the cluster. --name \ Name of the ClusterTemplate to create. --public \ Access to a ClusterTemplate is normally limited to the admin, owner or users within the same tenant as the owners. --server-type \ The servers in the cluster can be VM or baremetal. --volume-driver \ The name of a volume driver for managing the persistent storage for the containers. --dns-nameserver \ The DNS nameserver for the servers and containers in the cluster to use. --flavor \ The nova flavor id for booting the node servers. --master-flavor \ The nova flavor id for booting the master or manager servers. --http-proxy /--https-proxy \ The IP address for a proxy to use when direct http/https access from the servers to sites on the external internet is blocked. --no-proxy \ When a proxy server is used, some sites should not go through the proxy and should be accessed normally. --docker-volume-size \ If specified, container images will be stored in a cinder volume of the specified size in GB. --docker-storage-driver \ The name of a driver to manage the storage for the images and the container's writable layer. --labels \ Arbitrary labels in the form of key=value pairs. --tls-disabled \ Transport Layer Security (TLS) is normally enabled to secure the cluster. --registry-enabled \ Docker images by default are pulled from the public Docker registry, but in some cases, users may want to use a private registry. --master-lb-enabled \ Since multiple masters may exist in a bay, a load balancer is created to provide the API endpoint for the bay and to direct requests to the masters. |
創建cluster template | magnum cluster-template-create --name mesos-cluster-template \ --image ubuntu-mesos \ --keypair testkey \ --external-network public \ --dns-nameserver 8.8.8.8 \ --master-flavor m1.magnum \ --docker-volume-size 4 \ --tls-disabled \ --flavor m1.magnum \ --coe mesos \ --volume-driver rexray \ --labels rexray-preempt=true |
cluster-template-delete | 刪除某個模板 | magnum cluster-template-delete mytemplate | |
cluster-template-list | 列出所有的模板 | magnum cluster-template-delete | |
cluster-template-show | 顯示某個模板的詳細信息 | magnum cluster-template-show mytemplate | |
cluster-template-update | 更新某個模板 | magnum cluster-template-update mytemplate [ ...] | |
service-list | 列出所以得magnum service | magnum service-list | |
ca-show | 顯示某個cluster CA certificate 的詳細信息 | magnum ca-show mycluster | |
ca-sign | --csr File path of the csr file to send to Magnum to get signed. --cluster ID or name of the cluster. |
為某個cluster產生一個CA certificate | |
cluster-config | Configure native client to access cluster. | ||
stats-list | Show stats for the given project_id | magnum stats-list [--project-id ] |
說明:
(1)當前版本中支持的image如下。
COE | os-distro |
Kubernetes | Fedora-atomic, CoreOS |
Swarm | Fedora-atomic |
Mesos | Ubuntu |
(2)當前版本支持的容器network-driver如下。
COE | Network-Driver | Default |
Kubernetes | Flannel | Flannel |
Swarm | Docker, Flannel | Flannel |
Mesos | Docker | Docker |
(3)當前版本支持的容器volume-driver如下。
COE | Volume-Driver | Default |
Kubernetes | Cinder | No Driver |
Swarm | Rexray | No Driver |
Mesos | Rexray | No Driver |
4.Magnum源碼結構
說明:
api:以Pecan框架(http://www.pecanpy.org/)基礎的API請求處理代碼,接收到客戶端請求后,進行解析轉發或者直接處理。
cmd: 整個Magnum組件對外提供操作的入口。
common: 公共代碼。
condutor: magnum-conductor接收到magnum-api消息后,在此處選擇相應的driver進行下一步處理。
conf: 配置相關代碼。
db: sqlalchemy相關代碼,包括數據庫migrating功能。
drviers: 后端各種driver(Kubernetes, Swarm以及Mesos)相關代碼。
hacking: 供magnum單元測試使用的檢測對象。
objects: 數據庫model抽象,提供操作數據庫功能。
service:周期性更新clusters狀態至數據庫。
servicegroup: 周期性更新magnum服務組狀態至數據庫。
tests: 單元測試相關代碼。
5.Magnum代碼流程分析
(1)啟動腳本
安裝完Magnum組件,會在系統/usr/bin目錄下面生產4個可執行腳本,與源碼文件setup.cfg中設置相對應,如下圖所示。系統默認啟動magnum-api和magnum-conductor腳本,magnum系統即可正常工作,magnum-db-manage和magnum-driver-manage可用於手動操作,完成相關任務。
magnum-api: 負責接收處理以及轉發client的請求消息。
magnum-conductor: 負責接收magnum-api所發出的消息,進行cluster等具體操作。
magnum-db-manage: 負責數據庫的stamp,upgrade,version相關操作。
magnum-driver-manage: 負責管理后端各種driver(Kubernetes, Swarm以及Mesos)相關配置。
(2)代碼流程
如上圖所示,Magnum邏輯功能主要三個方面.
(1)通過magnum-db-manage控制數據庫的migrating,目前只支持sqlalchemy的backend, 位於源碼文件setup.py, 如下圖所示。
執行過程:調用源碼文件magnum/cmd/db_manage.py:main, 之后調用magnum/db/migration.py直接操作,如下圖所示。此過程較簡單,不再深入。
(2)通過magnum-driver-manage腳本,調用源碼文件magnum/cmd/driver_manage.py:main,之后調用magnum/drivers/driver.py, 管理drivers/目錄下各種driver(Kubernetes, Swarm以及Mesos)的相關配置。此過程較簡單,不再深入。
(3)magnum-api消息處理流程:通過magnum-api腳本,調用源碼文件magnum/cmd/api.py,之后系統進入api目錄完成消息的解析以及處理。整個api目錄下的代碼以Pecan為框架組成,如下圖所示。
說明:
(1)magnum-api接收到消息后,經Middleware進行過濾。
(2)之后由Pecan Route轉發消息至相應的分支進行處理。
(3)Cluster和Certificate分支相關的消息,被發送到消息隊列服務端,由magnum-conductor進程處理。其它分支消息只需跟數據庫進行交互。
magnum-conductor消息處理大致流程:此進程啟動RPC service接受magnum-api發出的消息,之后轉發到相應的endpoint進行處理,如下圖所示,位於源碼文件cmd/conductor.py。
之后跳轉到與endpoint對應的conductor/handlers/目錄下進行處理,如下圖所示。
其中,ca_conductor.py:具體負責certificate相關消息處理,包括生成,存儲,刪除以及讀取certificate操作。支持不同的backend來進行相關的certificate操作,位於源碼文件setup.py, 如下圖所示。
cluster_conductor.py:負責處理cluster create,update,delete操作,根據cluster的屬性選擇相應的后端driver(kuberntes, mesos或者swarm, 位於源碼文件setup.py, 如下圖所示),最后通過heat組件以template形式來執行cluster相關的操作。
conductor_listener.py:代表magnum-conductor alive狀態,即相當於keepalive概念。
indirection_api.py:主要用於處理與數據庫相關操作。
參考文章:
1.http://docs.openstack.org/developer/magnum/
2.https://wiki.openstack.org/wiki/Magnum
3.https://github.com/openstack/magnum