虽然本文标题是针对Ubuntu的,但对于CentOS也是可以变通的,无非是几个命令不完全相同,有经验的人可以自行修改,部分不容易找的包或者命令已经在文档中作出了注释,可以作为参考。
目录导航:
-
安装Docker引擎服务
-
安装docker-enter工具,便于使用docker
-
安装docker-registry服务
-
支持HTTPS的docker-registry服务
-
编译安装nginx
-
免费制作https所需的可被权威CA认证的SSL证书(DV Certificates)
-
配置nginx https服务
-
-
验证最终结果
-
CentOS运行python2.7的方法
(1)系统初始化完成后开始安装Docker引擎服务
# for service docker
# Refer: https://www.docker.com/
# Refer: https://docs.docker.com/linux/step_one/
静默更新系统,-qq参数需要用在apt-get子命令之前,-y随意
1
|
apt-get -qq update -y
|
安装wget工具用于下载文件
1
|
which
wget >
/dev/null
|| apt-get -qq
install
-y wget
|
添加docker的软件仓库key
1
|
wget -qO- https:
//get
.docker.com
/gpg
| apt-key add -
|
执行docker安装程序
1
|
wget -qO- https:
//get
.docker.com/ | sh
|
执行docker的测试命令,通过此命令看一看到docker引擎的版本等信息
1
|
docker version
|
# If you would like to use Docker as a non-root user, you should now consider adding your user to the "docker" group with something like:
# sudo usermod -aG docker your-user
(2)安装docker-enter工具,便于使用docker
# for program docker-enter
# Refer: http://dockerpool.com/static/books/docker_practice/container/enter.html
安装curl用于下载文件,此步骤可以省略,但建议安装
1
|
which
curl >
/dev/null
|| apt-get -qq
install
-y curl
|
下载并解压util工具,编译并安装nsenter,nsenter是docker-enter的主要组件
#cd /tmp; curl https://www.kernel.org/pub/linux/utils/util-linux/v2.24/util-linux-2.24.tar.gz | tar -zxf-; cd util-linux-2.24;
1
2
3
4
5
6
|
cd
/tmp
; wget -q https:
//www
.kernel.org
/pub/linux/utils/util-linux/v2
.24
/util-linux-2
.24.
tar
.gz;
tar
xzvf util-linux-2.24.
tar
.gz
cd
util-linux-2.24
.
/configure
--without-ncurses &&
make
nsenter
cp
nsenter
'/usr/local/bin'
which
nsenter
cd
|
下载.bashrc_docker文件,并将此文件添加到当前用户的bash初始化文件中
1
2
|
wget -P ~ https:
//github
.com
/yeasy/docker_practice/raw/master/_local/
.bashrc_docker;
echo
"[ -f ~/.bashrc_docker ] && . ~/.bashrc_docker"
>> ~/.bashrc;
source
~/.bashrc
|
(3)安装docker-registry服务
# for service docker-registry
静默安装依赖软件docker-registry的软件包,执行安装程序
1
|
apt-get -qq -y
install
build-essential python-dev libevent-dev python-pip libssl-dev liblzma-dev libffi-dev swig
|
从github获取docker-registry,安装方式还有其他几种,可以参考官方文档https://docs.docker.com/registry/deploying/
1
2
3
|
git clone https:
//github
.com
/docker/docker-registry
.git
cd
docker-registry
python setup.py
install
|
如果目录存在则进入目录
1
|
test
-d
'/usr/local/lib/python2.7/dist-packages/docker_registry-1.0.0_dev-py2.7.egg/config/'
&&
cd
/usr/local/lib/python2
.7
/dist-packages/docker_registry-1
.0.0_dev-py2.7.egg
/config/
|
以config_sample.yml模板创建config.yml文件
1
|
cp
config_sample.yml config.yml
|
以后台程序的方式运行gunicorn,以启动docker-registry
1
|
nohup
gunicorn --access-logfile - --error-logfile - -k gevent -b 0.0.0.0:5000 -w 4 --max-requests 100 docker_registry.wsgi:application >>
/tmp/docker-registry
.log &
|
如果要结束docker-registry则执行killall gunicorn
1
|
killall gunicorn
|
配置docker可以使用刚刚配置好的docker-registry
把docker.domain.com改成你的域名,后面在配置支持HTTPS的docker-registry服务时会继续使用。
1
2
3
|
# Insecure Registry, Deploying a plain HTTP registry,Using self-signed certificates
grep
insecure
/etc/default/docker
||
echo
'DOCKER_OPTS="--insecure-registry docker.domain.com:5000"'
>>
/etc/default/docker
service docker stop && service docker start
|
(4)配置支持HTTPS的docker-registry服务
编译安装nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
wget http:
//nginx
.org
/download/nginx-1
.8.0.
tar
.gz
tar
zxf nginx-1.8.0.
tar
.gz
wget
ftp
:
//ftp
.csx.cam.ac.uk
/pub/software/programming/pcre/pcre-8
.37.
tar
.gz
tar
zxf pcre-8.37.
tar
.gz
wget https:
//www
.openssl.org
/source/openssl-1
.0.2e.
tar
.gz
tar
zxf openssl-1.0.2e.
tar
.gz
wget http:
//zlib
.net
/zlib-1
.2.8.
tar
.gz
tar
zxf zlib-1.2.8.
tar
.gz
groupadd -r www
useradd
-r -g www www -c
"Web user"
-d
/dev/null
-s
/sbin/nologin
cd
nginx-1.8.0
.
/configure
--prefix=
/usr/local/nginx
--with-http_ssl_module --user=www --group=www --with-pcre=
/root/pcre-8
.37 --with-zlib=
/root/zlib-1
.2.8 --with-openssl=
/root/openssl-1
.0.2e
make
&&
make
install
/usr/local/nginx/sbin/nginx
-V
|
免费制作https所需的可被权威CA认证的SSL证书(DV Certificates)
制作证书可以自己制作自签名证书,既可以使用Windows自带的证书服务,也可以使用OpenSSL工具,但弊端是用户必须安装CA证书,浏览器才能承认此证书。如果想申请互联网公认的证书必须由公认的权威CA签发,这样的证书通常需要购买或者试用获得。如果不想花钱或者试用,可以借助Let’s Encrypt CA项目,自己给自己发放管理证书。
Let’s Encrypt CA项目是由Mozilla、思科、Akamai、IdenTrust、EFF和密歇根大学研究人员等共同创建的 ,项目官方网站是https://letsencrypt.org/,Github项目网站是https://github.com/letsencrypt/letsencrypt。此项目为网站(个人网站、非个人网站)提供免费SSL证书,向任何需要加密证书的网站自动发行和管理免费证书。
系统要求(先决条件):
-
python2.6以上(不含python2.6),CentOS系统默认安装的是python2.6,CentOS升级python的方法参照下文。
-
要申请的证书的common name也就是证书使用者(持有人)的域名需要指向当前客户端所在的主机,例如你要给自己的网站域名为www.domian.com申请证书,则www.domain.com必须与你执行下列步骤时所在的主机是同一个主机(域名解析到此主机,不区分A记录还是CNAME记录),原因是在生成证书的过程中letsencrypt服务器会与当前客户端进行通信,如果不是同一个主机则将导致无法通过验证或者通信失败,进而导致证书生成失败。 如果,域名不是DNS运营商来得到的,则可以尝试修改hosts文件。例如在Linux下,修改/etc/hosts文件,添加your_ip_address www.yourdomainname.com条目即可。
1
2
3
|
git clone https:
//github
.com
/letsencrypt/letsencrypt
cd
letsencrypt
.
/letsencrypt-auto
certonly --standalone --email dgdenterprise@gmail.com -d www.domain.com
|
命令会自动根据当前的操作系统下载合适的依赖包(主要是python),并且判断当前系统环境是否满足生成证书的要求,如果不满足系统要求,则该程序会显示出警告或者错误信息。
在命令成功完成后会有较为明显的提示,生成的证书将存放在/etc/letsencrypt/live/www.domain.com/目录下,这个目录下的文件是/etc/letsencrypt/archive/www.domain.com/下的符号链接文件
配置nginx https服务
有了证书和key就可以开始配置nginx https服务了。
可选步骤,(尚未验证,读者可忽略此段斜体文字)为docker-registry设置身份验证,需要用到Apache项目中的htpasswd,在CentOS中htpasswd位于httpd-tools包中
1
2
|
# for htpasswd
apt-get -qq -y
install
apache2-utils
|
配置nginx https服务,有两种选择,即可以像配置nginx http服务那样一步一步新建虚拟主机,也可以直接用https代理http。此处选择后者,简单易行。
1
|
mkdir
/usr/local/nginx/conf/vhost
|
# sed在文件最后一行的上一行插入新行,如果是最后一行插入新行,则可以将i换成a
1
|
sed
-i
'$i include vhost/*.conf;'
/usr/local/nginx/conf/nginx
.conf
|
# 注意:把下文中的www.yourdomainname.com换成想设置的域名
#注意:把下文中的docker-registry-server换成上文监听5000端口的某个IP地址,建议将此IP地址使用内网IP地址
1
|
vim
/usr/local/nginx/conf/vhost/https_www
.yourdomainname.com.conf
|
server {
listen 443 ssl;
server_name www.yourdomainname.com;
index index.html index.htm;
ssl on;
ssl_certificate /etc/letsencrypt/live/www.yourdomainname.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.yourdomainname.com/privkey.pem;
#ssl_password_file /usr/local/;
location / {
client_max_body_size 0;
proxy_connect_timeout 300s;
proxy_send_timeout 900;
proxy_read_timeout 900;
proxy_buffer_size 32k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_redirect off;
proxy_hide_header Vary;
proxy_set_header Accept-Encoding '';
proxy_set_header Host host;proxysetheaderRefererhttp_referer;
proxy_set_header Cookie httpcookie;proxysetheaderX−Real−IPremote_addr;
proxy_set_header X-Forwarded-For proxy_add_x_forwarded_for; proxy_set_header Hosthost;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
proxy_pass http://docker-registry-server/;
}
}
1
|
vim
/usr/local/nginx/conf/vhost/http_www
.yourdomainname.com.conf
|
server {
listen 80;
server_name www.yourdomainname.com;
index index.html index.htm;
location / {
client_max_body_size 0;
proxy_connect_timeout 300s;
proxy_send_timeout 900;
proxy_read_timeout 900;
proxy_buffer_size 32k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_redirect off;
proxy_hide_header Vary;
proxy_set_header Accept-Encoding '';
proxy_set_header Host host;proxysetheaderRefererhttp_referer;
proxy_set_header Cookie httpcookie;proxysetheaderX−Real−IPremote_addr;
proxy_set_header X-Forwarded-For proxy_add_x_forwarded_for; proxy_set_header Hosthost;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
proxy_pass http://docker-registry-server/;
}
}
1
2
|
/usr/local/nginx/sbin/nginx
-t
/usr/local/nginx/sbin/nginx
-s reload
|
(5)验证最终结果
此时可以使用docker命令,将从其他地方导入的images或从hub.docker.com网站pull的镜像,commit或tag成www.yourdomainname.com/imagesname ,然后push到自己建的docker-registry上。
附加:CentOS将python2.6切换到python2.7的方法
文章可以参考《How to install Python 2.7 and Python 3.3 on CentOS 6》或《Installing python 2.7 on centos 6.3. Follow this sequence exactly for centos machine only》。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
yum groupinstall -y
"Development tools"
yum
install
-y zlib-devel
bzip2
-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
echo
'/usr/local/lib'
>>
/etc/ld
.so.conf
ldconfig
# Python 2.7.6:
wget -c http:
//python
.org
/ftp/python/2
.7.6
/Python-2
.7.6.
tar
.xz
tar
xf Python-2.7.6.
tar
.xz
cd
Python-2.7.6
.
/configure
--prefix=
/usr/local
--
enable
-unicode=ucs4 --
enable
-shared LDFLAGS=
"-Wl,-rpath /usr/local/lib"
make
&&
make
altinstall
cd
# First get the setup script for Setuptools:
wget -c https:
//bitbucket
.org
/pypa/setuptools/raw/bootstrap/ez_setup
.py
python2.7 ez_setup.py
easy_install-2.7 pip
# Install virtualenv for Python 2.7 and create a sandbox called my27project:
pip2.7
install
virtualenv
virtualenv-2.7 my27project
# Check the system Python interpreter version:
python --version
# This will show Python 2.6.6
# Activate the my27project sandbox and check the version of the default Python interpreter in it:
source
my27project
/bin/activate
python --version
# This will show Python 2.7.6
# 在这里运行生成证书的程序
# 生成证书完毕后执行deactivate,退出virtualenv
deactivate
|
tag:Ubuntu安装docker,Ubuntu安装docker-registry,Ubuntu安装docker-enter,Ubuntu安装docker引擎,Ubuntu 容器虚拟化
--end--
本文转自 urey_pp 51CTO博客,原文链接:http://blog.51cto.com/dgd2010/1729858,如需转载请自行联系原作者