前言
CloudStack 和OpenStack 一样都是IaaS层 开源框架,可以管理XenServer、ESXI、KVM、OVM等主流虚拟机,相对OpenStack比较简单、稳定;
二、Cloud Stack架构
Zone:相当于现实中的1个数据中心,它是CloudStack中最大的一个单元
Pod(机柜):1个Zone包含N个Pod
Pod(集群):1个Pod包含N个Cluster
Host(主机):1个Cluster包含N个Host
三、基于centos7安装Cloud Stack4.8.0
1.下载cloudstack4.8.0
2.修改hostname 和 hosts文件
agent1
vim /etc/hostname
192.168.226.159 master1
vim /etc/hosts
检查主机名是否修改成功?
[root@master1 zhanggen]# hostname --fqdn
master1
hostname --fqdn
3.关闭Selinux和Firewalld
setenforce 0
systemctl stop firewalld.service
setenforce 0 && systemctl stop firewalld.service
4.安装epel yum源
yum -y install epel*
yum -y install epel*
5.安装 ntp服务 保证主从间时间同步
yum -y install ntp
yum -y install ntp
启动ntpd服务
[root@master1 zhanggen]# chkconfig ntpd on
注意:正在将请求转发到“systemctl enable ntpd.service”。
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
[root@master1 zhanggen]# systemctl enable ntpd.service
[root@master1 zhanggen]#
systemctl enable ntpd.service
6.安装nfs服务
yum -y install nfs-utils
yum -y install nfs-utils
配置
/export/secondary *(rw,async,no_root_squash,no_subtree_check)
vim /etc/exports
LOCKD_TCPPORT=32803
LOCKD_UDPPORT=32769
MOUNTD_PORT=892
STATD_PORT=662
RQUOTAD_PORT=875
STATD_OUTGOING_PORT=2020
vim /etc/sysconfig/nfs
启动nfs、rpcbind 服务
systemctl start nfs
systemctl start nfs
[root@master1 /]# systemctl start rpcbind
systemctl start rpcbind
7.master1主节点和 agent从节点挂载NFS
master1主节点
[root@master1 /]# echo "/dev/sdb /export/secondary ext4 defaults 0 0 " >> /etc/fstab
[root@master1 /]#
echo "/dev/sdb /export/secondary ext4 defaults 0 0 " >> /etc/fstab
agent1从节点
[root@agent1 export]# echo "/dev/sdb /export/primary ext4 defaults 0 0 " >> /etc/fstab
[root@agent1 export]# mount -a
[root@agent1 export]# df
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/sda3 18555904 5128464 13427440 28% /
devtmpfs 1435012 0 1435012 0% /dev
tmpfs 1449728 156 1449572 1% /dev/shm
tmpfs 1449728 9156 1440572 1% /run
tmpfs 1449728 0 1449728 0% /sys/fs/cgroup
/dev/sda1 303780 154916 148864 51% /boot
tmpfs 289948 8 289940 1% /run/user/1000
/dev/sdb 51474912 53272 48783816 1% /export/primary
[root@agent1 export]#
echo "/dev/sdb /export/primary ext4 defaults 0 0 " >> /etc/fstab
检查NFS是否挂载成功
[root@agent1 export]# showmount -e master1
Export list for master1:
/export/secondary *
[root@agent1 export]#
showmount -e master1
8.master节点
CloudStack管理端安装
cloudstack-management-4.8.0-1.el7.centos.x86_64.rpm 和 cloudstack-common-4.8.0-1.el7.centos.x86_64.rpm
[root@master1 Desktop]# ls
cloudstack-common-4.8.0-1.el7.centos.x86_64.rpm
cloudstack-management-4.8.0-1.el7.centos.x86_64.rpm
[root@master1 Desktop]# yum install cloudstack-management-4.8.0-1.el7.centos.x86_64.rpm cloudstack-common-4.8.0-1.el7.centos.x86_64.rpm
yum install cloudstack-management-4.8.0-1.el7.centos.x86_64.rpm cloudstack-common-4.8.0-1.el7.centos.x86_64.rpm
yum install mariadb-server mariadb
systemctl enable mariadb
mariadb 安装
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
vim /etc/my.cnf
[root@master1 /]# mysqladmin -uroot password 123.com
[root@master1 /]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> delete from mysql.user where user='';
Query OK, 2 rows affected (0.00 sec)
MariaDB [(none)]>
设置mysql密码删除空用户
[root@master1 /]# mysql -uroot -p123.com -e "GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY '123.com'";
[root@master1 /]#
给root用户授权
9.Agent 节点 安装KVM代理
[root@agent1 Desktop]# yum -y install cloudstack-agent-4.8.0-1.el7.centos.x86_64.rpm cloudstack-common-4.8.0-1.el7.centos.x86_64.rpm
yum -y install cloudstack-agent-4.8.0-1.el7.centos.x86_64.rpm cloudstack-common-4.8.0-1.el7.centos.x86_64.rpm
10.Master节点初始化数据库
[root@master1 Desktop]# rpm -qa | grep cloud
cloudstack-management-4.8.0-1.el7.centos.x86_64
cloudstack-common-4.8.0-1.el7.centos.x86_64
[root@master1 Desktop]# cloudstack-setup-databases cloud:123.com@localhost --deploy-as=root:123.com
Mysql user name:cloud [ OK ]
Mysql user password:****** [ OK ]
Mysql server ip:localhost [ OK ]
Mysql server port:3306 [ OK ]
Mysql root user name:root [ OK ]
Mysql root user password:****** [ OK ]
Checking Cloud database files ... [ OK ]
Checking local machine hostname ... [ OK ]
Checking SELinux setup ... [ OK ]
Detected local IP address as 192.168.226.159, will use as cluster management server node IP[ OK ]
Preparing /etc/cloudstack/management/db.properties [ OK ]
Applying /usr/share/cloudstack-management/setup/create-database.sql [ OK ]
Applying /usr/share/cloudstack-management/setup/create-schema.sql [ OK ]
Applying /usr/share/cloudstack-management/setup/create-database-premium.sql [ OK ]
Applying /usr/share/cloudstack-management/setup/create-schema-premium.sql [ OK ]
Applying /usr/share/cloudstack-management/setup/server-setup.sql [ OK ]
Applying /usr/share/cloudstack-management/setup/templates.sql [ OK ]
Processing encryption ... [ OK ]
Finalizing setup ... [ OK ]
CloudStack has successfully initialized database, you can check your database configuration in /etc/cloudstack/management/db.properties
[root@master1 Desktop]# mysql -uroot -p123.com
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 14
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| cloud |
| cloud_usage |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.04 sec)
MariaDB [(none)]>
cloudstack-setup-databases cloud:123.com@localhost --deploy-as=root:123.com Mysql user name:cloud
11.启动Master
[root@master1 management]# cloudstack-setup-management --tomcat7
Starting to configure CloudStack Management Server:
Configure Firewall ... [OK]
Configure CloudStack Management Server ...[OK]
CloudStack Management Server setup is Done!
[root@master1 management]# systemctl status cloudstack-management.service
● cloudstack-management.service - CloudStack Management Server
Loaded: loaded (/usr/lib/systemd/system/cloudstack-management.service; enabled; vendor preset: disabled)
Active: active (running) since 三 2019-04-03 16:16:38 CST; 5s ago
Main PID: 18268 (java)
CGroup: /system.slice/cloudstack-management.service
└─18268 /usr/lib/jvm/jre/bin/java -Djava.awt.headless=true -Dcom.sun.management.jmxremote=false -Xmx2g -XX:+HeapDumpOnOutOfMem...
4月 03 16:16:40 master1 server[18268]: Apr 03, 2019 4:16:40 PM org.apache.catalina.core.StandardEngine startInternal
4月 03 16:16:40 master1 server[18268]: INFO: Starting Servlet Engine: Apache Tomcat/7.0.76
4月 03 16:16:40 master1 server[18268]: Apr 03, 2019 4:16:40 PM org.apache.catalina.startup.HostConfig deployDirectory
4月 03 16:16:40 master1 server[18268]: INFO: Deploying web application directory /usr/share/cloudstack-management/webapps/client
4月 03 16:16:41 master1 server[18268]: Apr 03, 2019 4:16:41 PM org.apache.catalina.loader.WebappClassLoaderBase validateJarFile
4月 03 16:16:41 master1 server[18268]: INFO: validateJarFile(/usr/share/cloudstack-management/webapps/client/WEB-INF/lib/servlet-...t.class
4月 03 16:16:41 master1 server[18268]: Apr 03, 2019 4:16:41 PM org.apache.catalina.loader.WebappClassLoaderBase validateJarFile
4月 03 16:16:41 master1 server[18268]: INFO: validateJarFile(/usr/share/cloudstack-management/webapps/client/WEB-INF/lib/servlet-...t.class
4月 03 16:16:41 master1 server[18268]: Apr 03, 2019 4:16:41 PM org.apache.catalina.loader.WebappClassLoaderBase validateJarFile
4月 03 16:16:41 master1 server[18268]: INFO: validateJarFile(/usr/share/cloudstack-management/webapps/client/WEB-INF/lib/tomcat-e...t.class
Hint: Some lines were ellipsized, use -l to show in full.
[root@master1 management]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 *:3306 *:*
LISTEN 0 128 *:111 *:*
LISTEN 0 5 192.168.122.1:53 *:*
LISTEN 0 128 *:662 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:892 *:*
LISTEN 0 64 *:2049 *:*
LISTEN 0 64 *:32803 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 100 :::20400 :::*
LISTEN 0 128 :::8080 :::*
LISTEN 0 128 :::662 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 50 :::42743 :::*
LISTEN 0 128 ::1:631 :::*
LISTEN 0 128 :::892 :::*
LISTEN 0 64 :::2049 :::*
LISTEN 0 64 :::32803
cloudstack-setup-management --tomcat7
12.访问
http://192.168.226.159:8080/client/
默认user :admin 默认密码:password
[root@master1 Desktop]# /usr/share/cloudstack-common/scripts/storage/secondary/cloud-install-sys-tmplt -m /export/secondary/ -f /home/zhanggen/Desktop/systemvm64template-4.6.0-ovm.raw.bz2 -h kvm -F
Uncompressing to /usr/share/cloudstack-common/scripts/storage/secondary/3a2c5a43-2dd0-4130-b225-535221756540.qcow2.tmp (type bz2)...could take a long time
Moving to /export/secondary/template/tmpl/1/3///3a2c5a43-2dd0-4130-b225-535221756540.qcow2...could take a while
Successfully installed system VM template /home/zhanggen/Desktop/systemvm64template-4.6.0-ovm.raw.bz2 to /export/secondary/template/tmpl/1/3/
[root@master1 Desktop]# cd /export/secondary/
lost+found/ template/
[root@master1 Desktop]# cd /export/secondary/template/tmpl/1/3/
[root@master1 3]# ls
3a2c5a43-2dd0-4130-b225-535221756540.qcow2 template.properties
[root@master1 3]#
加载kvm模板
14.Agent libvert配置文件
CloudStack也是通过libverd来管理kvm的所有要修改 agent节点配置文件
vnc_listen = "0.0.0.0"
vim /etc/libvirt/qemu.conf
listen_tls = 0
listen_tcp = 1
tcp_port = "16509"
auth_tcp = "none"
mdns_adv =0
vim /etc/libvirt/libvirtd.conf
四、配置CloudStack
查看管理节点日志
tail -f /var/log/cloudstack/management/management-server.log
查看Agent节点日志
tail -f /var/log/cloudstack/agent/agent.log
1.添加资源
2.基本资源
3.设置DNS
4.设置网卡名称
4.设置网络
注意网段不要包含master 和agent的IP,避免造成IP冲突
5.设置管理网络
就是管理虚拟机用得网络,注意不要和上面的IP冲突
6.设置集群名称
7.添加agent节点
设置agent节点
8. 设置主存储
主存储 就是Agent上的存储路径,通过网络挂载到 Master 节点的NFS路径
9.设置二级存储
二级存储就是 Master节点中 NFS路径
10.添加资源成功
五、搭建网络镜像仓库
搭建http服务器,用于管理ISO镜像和虚拟机模板,通过url下载镜像、模板
1.安装Nginx
yum -y install nginx
systemctl start nginx
安装Nginx
2.修改Nginx配置文件成为文件目录服务器
access_log /var/log/nginx/access.log main;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
sendfile on;
/etc/nginx/nginx.conf
3.开放secstrage.allow权限
4.重启cloudstack-management.service
[root@master1 html]# systemctl restart cloudstack-management.service
[root@master1 html]#
六、注册ISO镜像
1.组册ISO
七、添加实例
资源和镜像全有了,也该添加虚拟机实例了
1.添加实例
2.选择通过ISO镜像的方式创建虚拟机
八、python3调用CloudStack的API
import requests
import urllib,hmac,base64,hashlib
api_url='http://192.168.226.159:8080/client/api'
api_key='Y3-zVsLYyBjf1sJr73Eb_17BitmsErXrvc6oIJBQBVjaIn2SbOvapI7gTs5Lycw8DqbzWu3NyVu8urR6ldcMyQ'
security_key='JqNOR8WtSvSS7wdDRhmwQn1VU8HBc5LbgWIImdzpRsvojYLvS1ClIGgBe_t40j7BaOtSHU7cMEe803QvN6FcdQ'
def get_url(cmd): #获取加密后的签名+拼接URL
arges={
"apikey":api_key.lower(),
"command":cmd.lower(),
"response":"json"
}
sig_url=urllib.parse.urlencode(arges)
hmac_byte=hmac.new(bytes(security_key,'utf-8'),bytes(sig_url,'utf-8'),hashlib.sha1 ).digest()
base64_str=base64.b64encode(hmac_byte).strip()
sig = urllib.parse.quote_plus(base64_str)
full_url = api_url + '?' + 'command=' + cmd + '&apikey=' + api_key + '&response=json' + '&signature=' + sig
return full_url
urI=get_url(cmd='listHosts')
print(requests.get(url=urI).json())
获取主机