码迷,mamicode.com
首页 > 其他好文 > 详细

Openstack之路(八)创建云主机镜像

时间:2018-02-01 17:15:40      阅读:391      评论:0      收藏:0      [点我收藏+]

标签:fail   mail   ted   failure   ffffff   pack   count   listen   tree   

  • 准备CentOS-7.x镜像,可以从网上直接下载,我这里准备的镜像是CentOS-7.2-x86_64
  • [root@linux-node1 ~]# ls -l /tmp/CentOS-7-x86_64-DVD-1511.iso
    -rw-r--r-- 1 root root 4329570304 Jan  7  2016 /tmp/CentOS-7-x86_64-DVD-1511.iso
    • 安装相关软件
    [root@linux-node1 ~]# yum -y install qemu-kvm libvirt virt-install
    [root@linux-node1 ~]# rpm -qa qemu-kvm libvirt virt-install
    libvirt-3.2.0-14.el7_4.5.x86_64
    qemu-kvm-1.5.3-141.el7_4.4.x86_64
    virt-install-1.4.1-7.el7.noarch
    • 启动libvirtd,会自动创建虚拟网卡virbr0,默认地址为192.168.122.1
    [root@linux-node1 ~]# systemctl start libvirtd
    [root@linux-node1 ~]# systemctl status libvirtd
    [root@linux-node1 ~]# ifconfig virbr0
    virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
            inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
            ether 00:00:00:00:00:00  txqueuelen 0  (Ethernet)
            RX packets 0  bytes 0 (0.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 0  bytes 0 (0.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    • 创建虚拟磁盘,”-f”指定磁盘格式qcow2,存放路径/opt/CentOS-7.2_x86_64.qcow2,磁盘大小10G
    [root@linux-node1 ~]# qemu-img create -f qcow2 /opt/CentOS-7.2-x86_64.qcow2 10G
    Formatting ‘/opt/CentOS-7.2-x86_64.qcow2‘, fmt=qcow2 size=10737418240 encryption=off cluster_size=65536 lazy_refcounts=off refcount_bits=16
    [root@linux-node1 ~]# ls -l /opt/CentOS-7.2-x86_64.qcow2
    -rw-r--r-- 1 root root 196768 Jan 23 14:39 /opt/CentOS-7.2-x86_64.qcow2
    • 创建云主机
    [root@linux-node1 ~]# virt-install --virt-type kvm --name CentOS-7.2 --ram 1024 --disk /opt/CentOS-7.2-x86_64.qcow2,format=qcow2 --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole --os-type=linux --os-variant=centos7.0 --location=/tmp/CentOS-7-x86_64-DVD-1511.iso
    • 通过TightVNS工具连接192.168.56.11:5900(默认端口是5900),接下来的步骤和我们平时安装系统没有什么区别,注意,只分根分区,不需要分交换分区

    技术分享图片

    • 启动云主机,并查看状态
    [root@linux-node1 ~]# virsh start CentOS-7.2
    Domain CentOS-7.2 started
    [root@linux-node1 ~]# virsh list --all
     Id    Name                           State
    ----------------------------------------------------
     2     CentOS-7.2                     running
    • 通过TightVNS工具连接192.168.56.11:5900,配置云主机网络

    DNS配置

    [root@localhost ~]# echo -e "nameserver 114.114.114.114\nnameserver 202.96.128.86" > /etc/resolv.conf
    [root@localhost ~]# cat /etc/resolv.conf
    nameserver 114.114.114.114
    nameserver 202.96.128.86

    网卡配置

    [root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
    TYPE=Ethernet
    BOOTPROTO=dhcp
    IPV4_FAILURE_FATAL=no
    NAME=eth0
    DEVICE=eth0
    ONBOOT=yes
    
    [root@localhost ~]# systemctl stop NetworkManager
    [root@localhost ~]# systemctl status NetworkManager
    [root@localhost ~]# systemctl disable NetworkManager
    
    [root@localhost ~]# systemctl restart netwrok
    [root@localhost ~]# systemctl status netwrok
    • 关闭firewalld,selinux,postfix等服务
    [root@localhost ~]# systemctl stop firewalld
    [root@localhost ~]# systemctl status firewalld
    [root@localhost ~]# systemctl disable firewalld
    
    [root@localhost ~]# setenforce 0
    [root@localhost ~]# getenforce
    Permissive
    [root@localhost ~]# sed -i ‘s#SELINUX=enforcing#SELINUX=disabled#g‘ /etc/selinux/config
    
    [root@localhost ~]# systemctl stop postfix
    [root@localhost ~]# systemctl status postfix
    [root@localhost ~]# systemctl disable postfix
    • 设置系统文件描述符数
    [root@localhost ~]# ulimit -SHn 65535
    [root@localhost ~]# ulimit -n
    65535
    [root@localhost ~]# echo "*               -       nofile          65535" >> /etc/security/limits.conf
    • 设置系统字符集
    [root@localhost ~]# vi /etc/locale.conf
    LANG="en_US.UTF-8"
    • 更新国内yum源

    aliyun

    [root@localhost ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

    epel

    [root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    • 安装必要的软件包
    [root@localhost ~]# yum -y install lrzsz vim dos2unix telnet nmap nc net-tools ntpdate wget screen tree
    • 安装ACPI服务,支持关闭和重启云主机实例
    [root@localhost ~]# yum -y install acpid
    [root@localhost ~]# systemctl enable acpid
    • 同步网络时间服务器
    [root@localhost ~]# ntpdate 0.pool.ntp.org
    [root@localhost ~]# hwclock
    [root@localhost ~]# crontab -e
    ####Synchronization Network Time Server####
    */5 * * * * /usr/sbin/ntpdate 0.pool.ntp.org &>/dev/null
    • 编写镜像初始化脚本
    [root@localhost ~]# vim /tmp/system_init.sh
    #!/bin/bash
    # Name:system_init.sh
    # Version:V1.0
    # Type:system_init
    # Language:Bash Shell
    # Date:2018-01-25
    # Author:LinBin
    # Email:linbin@keywa.com
    
    set_key() {
    # Determine whether the file exists
    if [ ! -d /root/.ssh ]
      then
        mkdir -p /root/.ssh
        chmod 700 /root/.ssh
    fi
    
    # Fetch public key using HTTP
    for ((i=1;i<6;i++))
    do
      if [ ! -f /root/.ssh/authorized_keys ]
        then
          curl -f http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key > /tmp/metadata-key 2>/dev/null
          if [ $? -eq 0 ]
            then
              cat /tmp/metadata-key >> /root/.ssh/authorized_keys
              chmod 0600 /root/.ssh/authorized_keys
              restorecon /root/.ssh/authorized_keys
              rm -f /tmp/metadata-key
              echo "Successfully retrieved public key from instance metadata"
              echo "*****************"
              echo "AUTHORIZED KEYS"
              echo "*****************"
              cat /root/.ssh/authorized_keys
              echo "*****************"
         fi
      else
        break;
      fi
    done
    }
    
    # Set the system host name
    set_hostname() {
        Hostname=$(curl -s http://169.254.169.254/latest/meta-data/hostname)
        echo "$Hostname" > /etc/hostname
        hostnamectl set-hostname $(echo "$Hostname")
    }
    
    # Set static ip address
    set_static_ip() {
        IP=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)
        cat > /etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF
    TYPE=Ethernet
    BOOTPROTO=static
    NAME=eth0
    DEVICE=eth0
    ONBOOT=yes
    IPADDR=$IP
    PREFIX=24
    GATEWAY=192.168.56.2
    DNS1=114.114.114.114
    DNS2=202.96.128.86
    EOF
    }
    
    # Reboot the instance
    reboot_instance() {
        rm -f /tmp/system_init.sh
        sed -i ‘$d‘ /etc/rc.local
        reboot
    }
    
    # Main function
    main() {
        set_key;
        set_hostname;
        set_static_ip;
        reboot_instance;
    }
    
    # Executive main function
    main
    • 设置/etc/rc.local文件的执行权限,并添加镜像初始化脚本
    [root@localhost ~]# ls -l /etc/rc.local
    lrwxrwxrwx 1 root root 13 Jan 20 13:24 /etc/rc.local -> rc.d/rc.local
    [root@localhost ~]# ls -l /etc/rc.d/rc.local
    -rw-r--r-- 1 root root 473 Oct 20 11:07 /etc/rc.d/rc.local
    [root@localhost ~]# chmod +x /etc/rc.d/rc.local
    [root@localhost ~]# ls -l /etc/rc.d/rc.local
    -rwxr-xr-x 1 root root 473 Oct 20 11:07 /etc/rc.d/rc.local
    
    [root@localhost ~]# echo "/bin/bash /tmp/system_init.sh" >> /etc/rc.local
    [root@localhost ~]# tail -1 /etc/rc.local
    /bin/bash /tmp/system_init.sh
    • 关闭云主机,并查看状态
    [root@linux-node1 ~]# virsh shutdown CentOS-7.2
    Domain CentOS-7.2 is being shutdown
    
    [root@linux-node1 ~]# virsh list --all
     Id    Name                           State
    ----------------------------------------------------
     -     CentOS-7.2                     shut off
    • 获得admin凭证来获取只有管理员能执行的命令的访问权限
    [root@linux-node1 ~]# source admin-openrc
    • 上传镜像到镜像服务并设置公共可见,这样所有的项目都可以访问它
    [root@linux-node1 ~]# openstack image create "CentOS-7.2-x86_64" --file /opt/CentOS-7.2-x86_64.qcow2 --disk-format qcow2 --container-format bare --public
    +------------------+------------------------------------------------------+
    | Field            | Value                                                |
    +------------------+------------------------------------------------------+
    | checksum         | 9a6d3efdd6874d4aa8ad555e9752b012                     |
    | container_format | bare                                                 |
    | created_at       | 2018-01-25T11:44:35Z                                 |
    | disk_format      | qcow2                                                |
    | file             | /v2/images/18587404-efaa-4c9d-bd88-682a835933db/file |
    | id               | 18587404-efaa-4c9d-bd88-682a835933db                 |
    | min_disk         | 0                                                    |
    | min_ram          | 0                                                    |
    | name             | CentOS-7.2-x86_64                                    |
    | owner            | 14055178975d417987c5a94f030c7acf                     |
    | protected        | False                                                |
    | schema           | /v2/schemas/image                                    |
    | size             | 1196359680                                           |
    | status           | active                                               |
    | tags             |                                                      |
    | updated_at       | 2018-01-25T11:44:57Z                                 |
    | virtual_size     | None                                                 |
    | visibility       | public                                               |
    +------------------+------------------------------------------------------+
    • 确认镜像的上传并验证属性
    [root@linux-node1 ~]# openstack image list
    +--------------------------------------+-------------------+--------+
    | ID                                   | Name              | Status |
    +--------------------------------------+-------------------+--------+
    | 18587404-efaa-4c9d-bd88-682a835933db | CentOS-7.2-x86_64 | active |
    | cd96090c-87ca-4eb3-b964-a7457639bc1e | cirros            | active |
    +--------------------------------------+-------------------+--------+

    Openstack之路(八)创建云主机镜像

    标签:fail   mail   ted   failure   ffffff   pack   count   listen   tree   

    原文地址:http://blog.51cto.com/11097612/2067808

    (0)
    (0)
       
    举报
    评论 一句话评论(0
    登录后才能评论!
    © 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
    迷上了代码!