1. 程式人生 > >轉 叫板OpenStack:用Docker實現私有雲

轉 叫板OpenStack:用Docker實現私有雲

ahp pup tag aam 倉庫 wps pcs clas dht

http://www.cnblogs.com/alexkn/p/4239457.html

看到各大廠商的雲主機,會不會覺得高大上?目前大公司的主流方案是OpenStack,比如某個公司的私有雲

技術分享

其實,我們可以通過Docker可以很容易實現自己的私有雲。

下面進入正題。

假 設你所在的公司擁有一臺功能強悍的服務器,假設是centos,但開發人員,測試人員如果都在這一臺主機上進行開發,測試,後果是災難性的,但由於只有一 臺服務器,你們老板甚至還想把這剩余的性能榨取出來搭建一些服務賣給大眾...以往的話,開發,測試人員肯定要摔鍵盤了,這不是坑爹麽?但如果你會 Docker,就能很容易實現這一目標,升職加薪,當上總經理,出任CEO,贏取白富美,迎來人生的巔峰呢!

是不是有點小激動了?來吧,哥教你怎麽做。

一.升級Centos內核

官方推薦內核使用3.8以上,那我們升到長期穩定版的3.10.

[[email protected] ~]# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
[[email protected] ~]# rpm -ivh http://www.elrepo.org/elrepo-release-6-5.el6.elrepo.noarch.rpm
[[email protected] ~]# yum --enablerepo=elrepo-kernel install kernel-lt -y 
[[email protected]
/* */ ~]# vim /etc/grub.conf default=1 改為default=0
[[email protected] ~]# reboot
[[email protected] ~]# uname -a
Linux iZ2893wjzgyZ 3.10.65-1.el6.elrepo.x86_64 #1 SMP Sat Jan 17 10:36:35 EST 2015 x86_64 x86_64 x86_64 GNU/Linux

二.安裝docker

[[email protected] ~]# yum install http://mirrors.yun-idc.com/epel/6/x86_64/epel-release-6-8.noarch.rpm
[[email protected]
/* */ ~]# yum install docker-io

三.啟動docker

[[email protected] ~]# service docker start 
Starting cgconfig service: [ OK ]
Starting docker: [ OK ]

四.獲取鏡像

由於鏡像倉庫在國內,國內慢的令人發指,推薦有import方式使用鏡像(此例采用的此種鏡像方案可以很容易安裝ssh服務),在http://openvz.org/Download/templates/precreated中有很多壓縮的鏡像文件,可以將這些文件下載後采用import方式使用鏡像

# wget http://download.openvz.org/template/precreated/ubuntu-14.04-x86_64-minimal.tar.gz
# cat ubuntu-14.04-x86_64-minimal.tar.gz  |docker import - ubuntu:14.04 
# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu 14.04 05ac7c0b9383 17 seconds ago 215.5 MB

這樣我們就可以使用這個鏡像作為自己的Base鏡像

五.實現sshd,在Base鏡像基礎上生成一個新鏡像

#docker run -t -i ubuntu:base /bin/bash

[email protected]:/# vim /etc/apt/sources.list

deb http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ trusty-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ trusty-backports main restricted universe multiverse

[email protected]:/# apt-get update

安裝supervisor服務

[email protected]:/# apt-get supervisor

[email protected]:/# cp supervisord.conf conf.d/

[email protected]:/# cd conf.d/

[email protected]:/# vi supervisord.conf

  1. ; supervisor config file
  2. [unix_http_server]
  3. file=/var/run/supervisor.sock ; (the path to the socket file)
  4. chmod=0700 ; sockef file mode (default 0700)
  5. [supervisord]
  6. logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
  7. pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
  8. childlogdir=/var/log/supervisor ; (‘AUTO‘ child log dir, default $TEMP)
  9. nodaemon=true ;(修改該軟件的啟動模式為非daemon,否則docker 在執行的時候會直接退出)
  10. [include]
  11. files = /etc/supervisor/conf.d/*.conf
  12. [program:sshd]
  13. command = /usr/sbin/sshd -D ;

[email protected]:/# mkdir /var/run/sshd

[email protected]:/# passwd root

[email protected]:/# vi /etc/ssh/sshd_config

[email protected]:/# exit

退出之後自動生成一個容器,接下來把容器commit生成封裝了sshd的鏡像

# docker commit f3c8 ubuntu

5c21b6cf7ab3f60693f9b6746a5ec0d173fd484462b2eb0b23ecd2692b1aff6b

[[email protected] tmp]# docker images

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE

ubuntu sshd 02c4391d40a0 47 minutes ago 661.4 MB

六.開始分配容器

[[email protected] tmp]# docker run -p 301:22 -d --name test ubuntu /usr/bin/supervisord

[[email protected] tmp]# docker run -p 302:22 -d --name dev ubuntu /usr/bin/supervisord

[[email protected] tmp]# docker run -p 303:22 -d --name client1 ubuntu /usr/bin/supervisord

.......

[[email protected] tmp]# docker run -p xxxxx:22 -d --name clientN ubuntu /usr/bin/supervisord

讓我們進入容器看一看,瞧一瞧(114.215.86.228是宿主機的IP)

技術分享

通過Xshell即可進入。

這樣就順利隔離了N個容器,且每一個都是以黨中央centos領導下的純凈的ubuntu系統,按這種分配方式,所有容器性能和宿主機一樣,讓我們看一看:

Centos:

技術分享

容器:test

技術分享

七.搭建自己的私有倉庫

老板現在用這臺閑置的主機賺了很多錢,於是公司快速發展,老板嘗到甜頭,又買來了幾十臺服務器,這時候,摳門老板想了想,每臺主機這麽搞一次,我豈不是要多給幾天工錢?

服務的封裝才是Docker的殺手鐧,怎麽可能讓這種工作重復數十次?我們可以搭建自己的私有倉庫。有點類似github的方式,將封裝好的鏡像push到倉庫,其他主機裝好docker後,pull下來即可,在這裏不做說明。

八.擴展

不同人群需要的主機性能不同,總不能所有的人都分配一樣的主機吧?這就涉及到容器的管理了,老板意識到這個問題,有一天對開發說,你看看人家openstack管理界面那麽高大上,還能將不同主機切割不同的性能,我們為什麽不可以?然後老板很快得到滿意的方案:Kubernetes(有很多其他方案可以實現)

Kubernetes是Google開源的容器集群管理系統。它構建於docker技術之上,為容器化的應用提供資源調度、部署運行、服務發現、擴 容縮容等整一套功能,本質上可看作是基於容器技術的mini-PaaS平臺.

總結:經過這八個步驟,大概就已經實現了私有雲的基本功能,其實Docker能做的事情遠不如此,本人才疏學淺,使用不久,這裏只闡述想到的這一種方案。

轉 叫板OpenStack:用Docker實現私有雲