Tsim koj tus kheej duab nrog ntshiab CentOS 5.9 hauv Amazon huab

Raws li koj paub, hauv Amazon huab virtual piv txwv tau pib ua raws li cov duab (lub npe hu ua AMI). Amazon muab ntau tus ntawm lawv; koj tuaj yeem siv pej xeem cov duab npaj los ntawm peb tog, uas cov neeg muab kev pabcuam huab, tau kawg, tsis muaj lub luag haujlwm. Tab sis qee zaum koj xav tau ib daim duab huv si nrog rau qhov tsim nyog tsis, uas tsis nyob hauv daim ntawv teev cov duab. Tom qab ntawd tib txoj kev tawm yog ua kom koj tus kheej AMI.

Cov ntaub ntawv raug piav qhia txoj kev tsim ib qho "piv txwv li khw-rov qab AMI". Qhov tsis zoo ntawm txoj hauv kev no yog tias cov duab tiav yuav tsum tau hloov mus rau hauv "EBS-backed AMI"

Yuav ua li cas los tsim koj tus kheej EBS-rov qab AMI hauv Amazon huab yam tsis muaj cov kauj ruam nruab nrab yuav tau tham hauv tsab xov xwm no.

Action Plan:

  • Npaj ib puag ncig
  • Nruab ib lub kaw lus huv thiab ua qhov tsim nyog
  • Siv ib daim duab ntawm lub disk
  • Sau npe AMI

Npaj ib puag ncig

Rau peb lub hom phiaj, ib qho piv txwv ntawm cov duab yog tsim nyog, txawm tias t1.micro. Koj tuaj yeem khiav nws ntawm CLI:

aws ec2 run-instances --image-id ami-1624987f --max-count 1 --min-count 1 --key-name mel --instance-type t1.micro

Cia peb tsim ebs-volume, qhov twg peb yuav nruab peb qhov system tom qab:

aws ec2 create-volume --availability-zone us-east-1a --size 10

Cov lus txib no yuav ua rau 10 Gb disk rau peb. Tseem ceeb: lub disk yuav tsum nyob rau hauv tib cheeb tsam li qhov piv txwv (nyob rau hauv peb cov ntaub ntawv nws yog peb-sab hnub tuaj-1a).
Tom ntej no, koj yuav tsum txuas lub disk rau qhov piv txwv:

aws ec2 attach-volume --instance-id i-2bc0925b --volume-id vol-08ab3079 --device /dev/xvdf

Tam sim no cia peb nkag mus rau hauv qhov piv txwv ntawm ssh, format lub disk thiab mount nws hauv cov npe:

mkfs.ext3 /dev/xvdf
mkdir /mnt/centos-image
mount /dev/xvdf /mnt/centos-image
cd !$

Txhim kho huv Centos 5.9

Ua ntej txhim kho lub system, koj yuav tsum tsim ib daim ntawv teev cov ntoo, mount proc thiab sysfs, thiab tsim ib qho tsawg kawg nkaus ntawm cov khoom siv:

mkdir centos-image/{boot,tmp,dev,sys,proc,etc,var}
mount -t proc none /mnt/centos-image/proc/
mount -t sysfs none /mnt/centos-image/sys/
for i in console null zero ; do /sbin/MAKEDEV -d /mnt/centos-image/dev -x $i ; done

Peb yuav nruab qhov system siv yum thiab cov ntaub ntawv teeb tsa hauv qab no:
yum-centos.conf

[main]
cachedir=/var/cache/yum
debuglevel=2
logfile=/var/log/yum.log
exclude=*-debuginfo
gpgcheck=0
obsoletes=1
reposdir=/dev/null

[base]
name=CentOS-5.9 - Base
mirrorlist=http://mirrorlist.centos.org/?release=5.9&arch=x86_64&repo=os
#baseurl=http://mirror.centos.org/centos/5.9/os/x86_64/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5

[updates]
name=CentOS-5.9 - Updates
mirrorlist=http://mirrorlist.centos.org/?release=5.9&arch=x86_64&repo=updates
#baseurl=http://mirror.centos.org/centos/5.9/updates/x86_64/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5

[extras]
name=CentOS-5.9 - Extras
mirrorlist=http://mirrorlist.centos.org/?release=5.9&arch=x86_64&repo=extras
#baseurl=http://mirror.centos.org/centos/5.9/extras/x86_64/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-5

[centosplus]
name=CentOS-5.9 - Plus
mirrorlist=http://mirrorlist.centos.org/?release=5.9&arch=x86_64&repo=centosplus
#baseurl=http://mirror.centos.org/centos/5.9/centosplus/x86_64/
gpgcheck=1
enabled=0
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-5

[contrib]
name=CentOS-5.9 - Contrib
mirrorlist=http://mirrorlist.centos.org/?release=5.9&arch=x86_64&repo=contrib
#baseurl=http://mirror.centos.org/centos/5.9/contrib/x86_64/
gpgcheck=1
enabled=0
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-5

yum -c ~/yum-centos.conf --installroot=/mnt/centos-image/ -y groupinstall Base

Tom qab ua tiav cov txheej txheem installation, koj tuaj yeem nruab ib lub pob tsim nyog rau tib txoj kev:

yum -c ~/yum-centos.conf --installroot=/mnt/centos-image/ install $packet_name

Cia peb hloov fstab:

vi /mnt/centos-image

/dev/xvda1  /      ext3    defaults        0 0
none       /dev/pts  devpts  gid=5,mode=620  0 0
none       /dev/shm  tmpfs   defaults        0 0
none       /proc     proc    defaults        0 0
none       /sys      sysfs   defaults        0 0

Hauv CentOS 5.9 koj tseem yuav tsum tau nruab ib lub ntsiav nrog xen yug:

yum -c ~/yum-centos.conf --installroot=/mnt/centos-image/ -y install kernel-xen

Nruab Grub:

chroot /mnt/centos-image/ grub-install /dev/xvdf

thiab tsim ib qho tshiab initrd:

chroot /mnt/centos-image/
cd boot/
mkinitrd --omit-scsi-modules --with=xennet --with=xenblk --fstab=/etc/fstab --preload=xenblk initrd-2.6.18-348.1.1.el5xen.img 2.6.18-348.1.1.el5xen

Nws yog ib qho tseem ceeb heev los qhia tag nrho cov kev txwv no thiab fstab tshiab, txwv tsis pub lub kaw lus yuav tsis khau raj.
Tom ntej no koj yuav tsum tsim ib cov ntaub ntawv menu.lst rau grub:

default=0
timeout=5
hiddenmenu
title CentOS_5.9_(x86_64)
        root (hd0)
        kernel /boot/vmlinuz-2.6.18-348.1.1.el5xen ro root=/dev/xvda1
        initrd /boot/initrd-2.6.18-348.1.1.el5xen.img

Cia peb teeb tsa lub network thiab sshd:

vi etc/sysconfig/network-scripts/ifcfg-eth0
ONBOOT=yes
DEVICE=eth0
BOOTPROTO=dhcp
TYPE=Ethernet
USERCTL=yes
PEERDNS=yes
IPV6INIT=no

vi etc/sysconfig/network
NETWORKING=yes

chroot /mnt/centos5img/ chkconfig --level 2345 network on

vi /mnt/centos5img/etc/ssh/sshd_config
...
UseDNS no
PermitRootLogin without-password

Yog li, peb yuav tau txais lub network ua haujlwm thiab muaj peev xwm nkag mus rau hauv qhov piv txwv uas siv cov yuam sij. Tab sis tus yuam sij nws tus kheej yuav tsum tau pauv mus rau qhov piv txwv li cas. Qhov no tuaj yeem ua tiav siv tsab ntawv uas yuav coj tus yuam sij thiab txuag nws ntawm qhov piv txwv:

vi /mnt/centos5img/etc/init.d/ec2-get-ssh

ec2-get-ssh#! / rau hauv / bash
#chkconfig: 2345 95 20
# txheej txheem npe: ec2-get-ssh
# piav qhia: ntes AWS cov ntawv pov thawj tseem ceeb rau cov neeg siv EC2

# Lub tsev qiv ntawv muaj nuj nqi
. /etc/rc.d/init.d/functions

# Qhov chaw network teeb tsa
[ -r /etc/sysconfig/network ] &&. /etc/sysconfig/network

# Hloov qhov hloov pauv ib puag ncig hauv qab no rau koj lub cev
export PATH =:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin

# Txheeb xyuas tias kev sib txuas lus tau teeb tsa
yog [ "${NETWORKING}" = "tsis muaj" ]; ces
ncha "Networking is not configured."
tawm ntawm 1
fi

pib () {
yog tias [! -d /root/.ssh]; ces
mkdir -p /root/.ssh
chmod 700 /root/.ssh
fi
# Khaws cov yuam sij pej xeem los ntawm metadata server siv HTTP
curl -f 169.254.169.254/latest/meta-data/public-keys/0/openssh-key > /tmp/my-public-key
yog tias [$? -eq 0]; ces
ncha "EC2: Retrieve public key from metadata server using HTTP."
miv /tmp/my-public-key >> /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
rm /tmp/my-public-key
fi
}

nres() {
echo "Tsis muaj dab tsi ua ntawm no"
}

restart() {
nres
pib
}

#Saib seb peb hu li cas.
rooj plaub "$ 1" hauv
pib)
pib
;;
nres)
nres
;;
rov pib dua)
pib dua
;;
*)
ncha $"Siv: $0 {start|stop|restart}"
tawm ntawm 1
ntawd C

tawm $?
Cia peb ua nws executable thiab ntxiv rau kev pib:

chmod +x /mnt/centos-image/etc/init.d/ec2-get-ssh
/usr/sbin/chroot /mnt/centos-image/ /sbin/chkconfig --level 34 ec2-get-ssh on

Nws kuj tseem pom zoo kom lov tes taw Selinux, lossis teeb tsa nws kom raug. Txwv tsis pub, piv txwv li, tus yuam sij yuav tsis tau txais kev cawmdim ntawm qhov piv txwv.
Ntawm no koj tuaj yeem tso tseg qhov system. Peb twb muaj cov ntshiab CentOS npaj khiav hauv huab. Txhua yam uas tseem tshuav yog kom unmount ebs disk nrog peb lub cev thiab sau npe ami.

umount /mnt/centos-image/proc/
umount /mnt/centos-image/sys/
umount /mnt/centos-image/

AMI sau npe

Yuav kom tau txais ami los ntawm ebs disk, koj thawj zaug yuav tsum tau thaij duab ntawm lub disk:

aws ec2 create-snapshot --volume-id vol-0b4bd07a --description centos-snap

Txoj hauv kev yooj yim tshaj rau kev sau npe ami yog los ntawm AWS Management Console. Txhawm rau ua qhov no, koj tsuas yog yuav tsum mus rau ntu "Snapshots" hauv EC2 qhov kev pabcuam, xaiv qhov koj xav tau (hauv peb rooj plaub nws yog centos-snap), nyem rau ntawm nws thiab xaiv "Tsim Duab los ntawm Snapshot"
Tom qab ntawd, hauv lub qhov rais uas qhib, koj yuav tsum xaiv kwv yees li cov hauv qab no tsis:

Tsim koj tus kheej duab nrog ntshiab CentOS 5.9 hauv Amazon huab

Koj tuaj yeem nrhiav tau Kernel ID xaiv raws li hauv qab no:

aws ec2 describe-images --owner amazon --region us-east-1 --output text | grep "/pv-grub-hd0.*-x86_64" | awk '{print $7}' | grep aki
aki-88aa75e1
aki-b4aa75dd

Yog tag nrho. Tam sim no koj tuaj yeem tso cov piv txwv.
Ua li no, koj tuaj yeem tsim cov duab, feem ntau yuav, nrog rau Linux faib. Tsawg kawg, twv yuav raug hu Debian (siv debootstrap rau nruab ib lub kaw lus huv) thiab Rhel-tsev neeg.

Tau qhov twg los: www.hab.com

Ntxiv ib saib