1. 程式人生 > >使用salt-cloud創建openstack虛擬機

使用salt-cloud創建openstack虛擬機

align aws 依賴包 font lin url engine set pcl

salt-cloud也是基於openstack來做的,它可以支持多種雲的使用。比如:Aliyun、Azure、DigitalOcean、EC2、Google Compute Engine、HP Cloud、OpenStack、等等

安裝相關組件和依賴包

[root@linux-node1 ~]# yum -y install salt-master salt-minion salt-cloud python-libcloud

修改配置文件

[root@linux-node1 ~]# cd /etc/salt/cloud.providers.d/
[root@linux-node1 cloud.providers.d]#
cat openstack.conf my-openstack-config: # Set the location of the salt-master # minion: master: 192.168.56.11 # Configure the OpenStack driver # identity_url: http://192.168.56.11:5000/v2.0/tokens compute_name: nova protocol: ipv4 compute_region: RegionOne # 註意這裏的類型可以通過openstack endpoint list 查看到
# Configure Openstack authentication credentials # user: demo password: demo # tenant is the project name tenant: demo # 項目名稱 provider: openstack # skip SSL certificate validation (default false) insecure: false

配置saltstack

[root@linux-node1 cloud.providers.d]#
systemctl start salt-master [root@linux-node1 cloud.providers.d]# vim /etc/salt/minion master: 192.168.56.12 [root@linux-node1 cloud.providers.d]# systemctl start salt-minion [root@linux-node1 cloud.providers.d]# salt-key Accepted Keys: Denied Keys: Unaccepted Keys: linux-node1.oldboyedu.com Rejected Keys: [root@linux-node1 cloud.providers.d]# salt-key -A The following keys are going to be accepted: Unaccepted Keys: linux-node1.oldboyedu.com Proceed? [n/Y] y Key for minion linux-node1.oldboyedu.com accepted.

[root@linux-node1 cloud.providers.d]# salt ‘*‘ test.ping
linux-node1.oldboyedu.com:
True

測試salt-cloud能否連接上openstack

###### 列出所有雲主機的鏡像
[root@linux-node1 cloud.providers.d]# salt-cloud --list-images openstack -l debug
...........................
............................
...........................
my-openstack-config:
    ----------
    openstack:
        ----------
        cirros:
            ----------
            driver:
            extra:
                ----------
                created:
                    2015-12-23T16:47:33Z
                metadata:
                    ----------
                minDisk:
                    0
                minRam:
                    0
                progress:
                    100
                serverId:
                    None
                status:
                    ACTIVE
                updated:
                    2015-12-23T16:47:33Z
            get_uuid:
            id:
                90f1b52f-25dd-43f1-9f78-4d4de67aabfe
            name:
                cirros
            uuid:
                b22ad8d988b5925dc328cdc21a84a7670fbda0c4

##### 列出所有雲主機的類型
[root@linux-node1 cloud.providers.d]# salt-cloud --list-size openstack -l debug

編寫虛擬機的創建文件

[root@linux-node1 cloud.profiles.d]# pwd
/etc/salt/cloud.profiles.d

[root@linux-node1 cloud.profiles.d]# cat web.conf 
web-node:
  provider: my-openstack-config                 # /etc/salt/cloud.providers.d/openstack.conf的ID
  size: m1.tiny                                 # 雲主機的類型
  image: cirros                                 # 使用的鏡像名稱
  ssh_key_file: /root/.ssh/id_rsa               # 密鑰
  ssh_key_name: mykey                           # openstack頁面中,訪問安全裏面找密鑰對
  ssh_interface: private_ips
  networks:                                     # 設置使用的網絡,可以從openstack頁面的網絡板塊中找
    - fixed:
      - 8fb1d37e-414f-4f5d-89f7-8d20fa8fabed
  minion:
    master: 192.168.56.11
    grains:
      role: webserver
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[root@linux-node1 cloud.profiles.d]# salt-cloud -p web-node node1 -l debug

########################################################################################################

使用salt-cloud創建openstack虛擬機

眾所周知,saltstack有三大功能:遠程執行、配置管理、雲管理。其中的雲管理就是通過salt-cloud完成的。salt-cloud能夠管理很多雲環境(公有雲、私有雲),連大名鼎鼎的阿裏雲也可以通過salt-cloud來創建虛擬機哦。

本次主要講解使用salt-cloud管理openstack(L版)私有雲來創建虛擬機。(前提openstack環境運行正常,能正常創建虛擬機)

1、 安裝saltstack

yum install salt-master salt-minion salt-cloud -y

安裝依賴包:

yum install python-libcloud -y

2、 創建provider配置文件,告訴salt-cloud如何連接openstack(yaml語法,註意空格

[root@linux-node1 cloud.providers.d]# pwd
/etc/salt/cloud.providers.d
[root@linux-node1 cloud.providers.d]# vim openstack.conf 
myopenstack:
  # Set the location of the salt-master
  #
  minion:
    master: 192.168.56.11
  # Configure the OpenStack driver
  #
  identity_url: http://192.168.56.11:5000/v2.0/tokens
  compute_name: nova
  protocol: ipv4
  compute_region: RegionOne
  # Configure Openstack authentication credentials
  #
  user: demo
  password: demo
  # tenant is the project name
  tenant: demo
 
  provider: openstack
  # skip SSL certificate validation (default false)
  insecure: false
 

3 、檢查配置:

語法:

salt-cloud --list-locations <provider_name> 查看可用的區域

salt-cloud --list-p_w_picpaths <provider_name> 查看可用的鏡像

salt-cloud --list-sizes <provider_name> 查看可用的虛擬機大小

例:查看可用的鏡像

salt-cloud --list-p_w_picpaths myopenstack -l debug

myopenstack:provider配置文件中定義的ID

-l debug :開啟debug模式,方便定位和調試故障。

[root@linux-node1 ~]# salt-cloud --list-p_w_picpaths myopenstack -l debug 
[DEBUG   ] Reading configuration from /etc/salt/cloud
[DEBUG   ] Reading configuration from /etc/salt/master
[DEBUG   ] Using cached minion ID from /etc/salt/minion_id: linux-node1.example.com
[DEBUG   ] Missing configuration file: /etc/salt/cloud.providers
[DEBUG   ] Including configuration from /etc/salt/cloud.providers.d/openstack.conf
[DEBUG   ] Reading configuration from /etc/salt/cloud.providers.d/openstack.conf
[DEBUG   ] Missing configuration file: /etc/salt/cloud.profiles
[DEBUG   ] Configuration file path: /etc/salt/cloud
[WARNING ] Insecure logging configuration detected! Sensitive data may be logged.
[INFO    ] salt-cloud starting
[WARNING ] /usr/lib/python2.7/site-packages/novaclient/v1_1/__init__.py:30: UserWarning: Module novaclient.v1_1 is deprecated (taken as a basis for novaclient.v2). The preferable way to get client class or object you can find in novaclient.client module.
  warnings.warn("Module novaclient.v1_1 is deprecated (taken as a basis for "
[DEBUG   ] Could not LazyLoad parallels.avail_sizes
 
[DEBUG   ] LazyLoaded parallels.avail_locations
[DEBUG   ] LazyLoaded proxmox.avail_sizes
[DEBUG   ] Could not LazyLoad saltify.destroy
[DEBUG   ] Could not LazyLoad saltify.avail_sizes
[DEBUG   ] Could not LazyLoad saltify.avail_p_w_picpaths
[DEBUG   ] Could not LazyLoad saltify.avail_locations
[DEBUG   ] LazyLoaded rackspace.reboot
[DEBUG   ] Could not LazyLoad openstack.list_locations
[DEBUG   ] LazyLoaded rackspace.list_locations
[DEBUG   ] OpenStack authenticating using password
[DEBUG   ] LazyLoaded nested.output
myopenstack:
    ----------
    openstack:
        ----------
        cirros:
            ----------
            driver:
            extra:
                ----------
                created:
                    2015-12-13T14:24:05Z
                metadata:
                    ----------
                minDisk:
                    0
 
                minRam:
                    0
                progress:
                    100
                serverId:
                    None
                status:
                    ACTIVE
                updated:
                    2015-12-13T14:24:05Z
            get_uuid:
            id:
                c503fb83-7d52-40e9-b1cc-4773e650986e
            name:
                cirros
            uuid:
                2986f30365f55599f838f656a51acdaec1fa81a2
        new:
            ----------
            driver:
            extra:
                ----------
                created:
                    2015-12-25T09:17:50Z
                metadata:
                    ----------
                minDisk:
 
                    0
                minRam:
                    0
                progress:
                    100
                serverId:
                    None
                status:
                    ACTIVE
                updated:
                    2015-12-25T09:17:57Z
            get_uuid:
            id:
                fdd664b3-ebf2-4904-815f-0d1cdf9955d4
            name:
                new
            uuid:
                9f4f8a437ebc8123c1a8725156ee91566928d9f0
 

可以看到,當前有兩個可用的鏡像,cirros 和new ,並且可以看到更過鏡像的信息。

4、 編寫profile配置文件,告訴salt-cloud創建虛擬機的細節,後期創建虛擬機時只需要調用該名字即可。

[root@linux-node1 cloud.profiles.d]# vim openstack_web.conf
web-node:
  provider: myopenstack
  size: m1.tiny
  p_w_picpath: new
  ssh_key_file: /root/.ssh/id_rsa
  ssh_key_name: mykey
  ssh_interface: private_ips
  networks:
    - fixed:
      - 2c4c9edc-dbb8-467c-bc67-a2c6ff23d885
  minion:
    master: 192.168.56.11
    grains: 
      role: webserver

5 、創建虛擬機

[root@linux-node1 cloud.profiles.d]# salt-cloud -p web-node test -l debug

-p 指定profile中虛擬機ID名稱

6 、查看驗證:

[root@linux-node1 ~]# nova list     
--------------------+------+--------+------------+-------------+---------------------+
| ID                              | Name | Status | Task State | Power State | Networks            |
+-------------------+------+--------+------------+-------------+---------------------+
| b4035529-6cd7-4b9b-a6e9-c63d5bfb2d4b | test | ACTIVE | -          | Running     | flat=192.168.56.118 |
+-------------------+------+--------+------------+-------------+---------------------+

通過上面的配置文件,salt-cloud不僅能夠創建虛擬機,還能夠在虛擬機裏安裝salt-minion,

並自動添加到salt-master認證裏面去(還設置了一個grains哦,很方便吧)

使用salt-cloud創建openstack虛擬機