3.3. 场景示例
3.3.1. 交互式构建yum镜像
有了操作系统基础镜像后可以交互式构建其它镜像。大致过程为用基础镜像创建启动容器,在容器内交互式安装和部署,然后停止容器,最后转换容器为新镜像。
# cocker -a create -m "calvin=rhel-7.4-x86_64" --host yum --volume "/mnt/cdrom:/mnt/cdrom" -c "calvin=yum"
OK
# cocker -a boot -c "calvin=yum" -t
[root@yum /root]
在容器内配置好yum,在我的环境里这样配置
[root@yum /root] mkdir -p /etc/yum.repos.d
[root@yum /root] vi /etc/yum.repos.d/cdrom.repo
[cdrom]
name=cdrom
baseurl=file:///mnt/cdrom
gpgcheck=0
enable=1
[root@yum /root] yum search sshd
cdrom | 4.1 kB 00:00:00
(1/2): cdrom/group_gz | 137 kB 00:00:00
(2/2): cdrom/primary_db | 4.0 MB 00:00:00
=============================================================================================================== Matched: sshd ===============================================================================================================
openssh-server.x86_64 : An open source SSH server daemon
转换容器为yum镜像
[root@yum /etc/yum.repos.d] exit
logout
# cocker -a shutdown -c "calvin=yum"
OK
# cocker -s containers
container_id image hostname net netns size status
-----------------------------------------------------------------------------------------------------------
calvin=yum calvin=rhel-7.4-x86_64 yum HOST 24 MB STOPED
# cocker -a to_image --from-container "calvin=yum" --version "1.0.0" --to-image "calvin=yum"
OK
# cocker -s containers
# cocker -s images
image_id version modify_datetime size
--------------------------------------------------------------------
calvin=rhel-7.4-x86_64 1.0.0 2018-11-25T09:55:25 271 MB
calvin=yum 1.0.0 2018-11-25T10:16:59 24 MB
3.3.2. 交互式构建sshd镜像
注意:交互式构建sshd依赖yum。
# cocker -a create -m "calvin=rhel-7.4-x86_64,calvin=yum" --host sshd --volume "/mnt/cdrom:/mnt/cdrom" --net BRIDGE --vip 166.88.0.2 --port-mapping "2222:22" -c "calvin=sshd"
OK
# cocker -a boot -c "calvin=sshd" -t
[root@sshd /root]
在容器内配置好sshd,在我的环境里这样配置
[root@sshd /root] yum install -y openssh-server
...
[root@sshd /root] ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
...(一般全直接按回车)
[root@sshd /root] ssh-keygen -t rsa -f /etc/ssh/ssh_host_ecdsa_key
...(一般全直接按回车)
[root@sshd /root] echo "root:root" | chpasswd
...(如有卡住,按Ctrl+C结束)
[root@sshd /root] nohup /usr/sbin/sshd -D &
另外开一屏连接sshd容器
# ssh root@166.88.0.2 -p 2222
The authenticity of host '[166.88.0.2]:2222 ([166.88.0.2]:2222)' can't be established.
RSA key fingerprint is SHA256:kSX5DU3MiwEy8dArBoAk00kbB7hBtRXl/Pm4n9jWjBY.
RSA key fingerprint is MD5:27:5d:b6:5a:5a:b1:bc:eb:b9:82:98:58:40:7e:eb:45.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[166.88.0.2]:2222' (RSA) to the list of known hosts.
root@166.88.0.2's password: (root密码前面被重置成root了)
Last login: Mon Nov 26 13:28:07 2018 from 192.168.6.7
-bash-4.2#
转换容器为sshd镜像
[root@sshd /root] exit
logout
# cocker -a shutdown -c "calvin=sshd"
OK
# cocker -s containers
container_id image hostname net netns size status
-----------------------------------------------------------------------------------------------------------
calvin=yum calvin=rhel-7.4-x86_64 yum HOST 24 MB STOPED
# cocker -a to_image --from-container "calvin=sshd" --version "1.0.0" --to-image "calvin=sshd"
OK
# cocker -s containers
# cocker -s images
image_id version modify_datetime size
--------------------------------------------------------------------
calvin=rhel-7.4-x86_64 1.0.0 2018-11-25T09:55:25 271 MB
calvin=yum 1.0.0 2018-11-25T10:16:59 24 MB
calvin=sshd 1.0.0 2018-11-26T09:16:59 335 MB
3.3.3. 交互式构建G6镜像
G6
是本人以前独立自研的负载均衡器(源码托管地址 : 开源中国、github),不少公司在使用,下面介绍交互式构建G6镜像。
# cocker -a create -m "calvin=rhel-7.4-x86_64,calvin=yum,calvin=sshd" --host G6 --volume "/mnt/cdrom:/mnt/cdrom" --net BRIDGE --vip 166.88.0.2 --port-mapping "8600:8600,2222:222" -c "calvin=G6"
OK
# cocker -a boot -c "calvin=G6" -t
[root@G6 /root]
在容器内配置好G6,在我的环境里这样配置
[root@G6 /root] yum install -y git
[root@G6 /root] yum install -y make
[root@G6 /root] yum install -y gcc
...(这里仅作演示,所以把git、gcc都安装在一起了)
[root@G6 /root] mkdir src && cd src
[root@G6 /root/src] git clone https://gitee.com/calvinwillisms/G6 && cd G6
[root@G6 /root/src/G6] cd src
[root@G6 /root/src/G6/src] make -f makefile.Linux install
...
[root@G6 /root/src/G6/src] mkdir ~/etc/ && vi ~/etc/G6.conf
admin_rule_id G 127.0.0.1:* - 127.0.0.1:8600 ;
my_rule RR *:* - 0.0.0.0:222 > 166.88.0.2:22 ;
[root@G6 /root/src/G6/src] cd ~
[root@G6 /root] nohup /usr/sbin/sshd -D &
[root@G6 /root] G6
G6 v1.0.6 build Nov 26 2018 14:28:18
TCP Bridge && Load-Balance Dispenser
Copyright by calvin 2016
USAGE : G6 -f (config_pathfilename) [ -t (forward_thread_size) ] [ -s (forward_session_size) ] [ --log-level (DEBUG|INFO|WARN|ERROR|FATAL) ] [ --log-filename (logfilename) ] [ --close-log ] [ --no-daemon ] [ --set-cpu-affinity ]
[root@G6 /root] G6 -f ~/etc/G6.conf
另外开一屏连接G6容器
# ssh root@166.88.0.2 -p 2222
root@166.88.0.2's password: (root密码前面被重置成root了)
Last login: Mon Nov 26 13:28:07 2018 from 192.168.6.7
-bash-4.2#
转换容器为G6镜像
[root@G6 /root] ps -ef
...
[root@G6 /root] ps -ef | grep -v grep | grep "G6 -f" | awk '{if($3==1)print $2}' | xargs kill
[root@G6 /root] ps -ef | grep -v grep | grep -w sshd | awk '{print $2}' | xargs kill
[root@G6 /root] exit
logout
# cocker -a shutdown -c "calvin=G6"
OK
# cocker -s containers
container_id image hostname net netns size status
-----------------------------------------------------------------------------------------------------------
calvin=G6 calvin=rhel-7.4-x86_64,calvin=yum,calvin=sshd G6 BRIDGE nns2513F44178 373 MB STOPED
# cocker -a to_image --from-container "calvin=G6" --version "1.0.0" --to-image "calvin=G6"
OK
# cocker -s containers
# cocker -s images
image_id version modify_datetime size
--------------------------------------------------------------------
calvin=rhel-7.4-x86_64 1.0.0 2018-11-25T09:55:25 271 MB
calvin=yum 1.0.0 2018-11-25T10:16:59 24 MB
calvin=sshd 1.0.0 2018-11-26T09:16:59 335 MB
calvin=G6 1.0.0 2018-11-26T10:01:48 373 MB
3.3.4. 镜像配置实例化容器并启动
还是拿本人的G6
做例子,把镜像calvin=G6
转化为容器,把配置文件改成模板后再转化回镜像
# cocker -a to_container --from-image "calvin=G6" -m "calvin=rhel-7.4-x86_64" --host G6 --to-container "calvin=G6"
OK
# cocker -a boot -c "calvin=G6" -t
[root@G6 /root] cd etc
[root@G6 /root/etc] mv G6.conf G6.conf.tpl
[root@G6 /root/etc] vi G6.conf.tpl
admin_rule G 127.0.0.1:* - 127.0.0.1:${ADMIN_PORT} ;
my_rule RR *:* - 0.0.0.0:${FORWARD_PORT} > ${DEST_IP}:${DEST_PORT} ;
[root@G6 /root/etc] vi ../bin/sshd.start
nohup /usr/sbin/sshd -D &
[root@G6 /root/etc] chmod +x ../bin/sshd.start
[root@G6 /root/etc] exit
logout
# cocker -a shutdown -c "calvin=G6"
OK
# cocker -a to_image --from-container "calvin=G6" --version "1.1.0" --to-image "calvin=G6"
OK
# cocker -s images
image_id version modify_datetime size
--------------------------------------------------------------------
calvin=rhel-7.4-x86_64 1.0.0 2018-11-27T08:00:07 273 MB
calvin=yum 1.0.0 2018-11-26T09:10:43 24 MB
calvin=sshd 1.0.0 2018-11-26T09:17:12 335 MB
calvin=G6 1.1.0 2018-11-27T08:03:33 373 MB
最后用镜像创建容器,配置实例化,启动服务
# cocker -a create -m "calvin=rhel-7.4-x86_64,calvin=sshd,calvin=G6" --host G6 --net BRIDGE --vip 166.88.0.2 --port-mapping "8600:8600,2222:222" -c "G6"
OK
# cocker -a boot -c "G6"
OK
# vi G6.conf.map
${ADMIN_PORT} 8600
${FORWARD_PORT} 222
${DEST_IP} 166.88.0.2
${DEST_PORT} 22
# cocker -a rplfile -c "G6" --template-file "/root/etc/G6.conf.tpl" --mapping-file "G6.conf.map" --instance-file "/root/etc/G6.conf"
OK
# cocker -a run -c "G6" --cmd "cat /root/etc/G6.conf"
admin_rule G 127.0.0.1:* - 127.0.0.1:8600 ;
my_rule RR *:* - 0.0.0.0:222 > 166.88.0.2:22 ;
# cocker -a run -c "G6" --cmd "nohup /usr/sbin/sshd -D"
nohup: ignoring input and appending output to 'nohup.out'
# cocker -a run -c "G6" --cmd "G6 -f /root/etc/G6.conf"
OK
另外开一屏连接G6容器
# ssh root@166.88.0.2 -p 2222
root@166.88.0.2's password: (root密码前面被重置成root了)
Last login: Mon Nov 26 13:28:07 2018 from 192.168.6.7
-bash-4.2# exit
用完后关闭服务,最后停止和销毁容器
# cocker -a run -c "G6" --cmd "ps -ef | grep -v grep | grep 'G6 -f' | awk '{if($3==1)print $2}' | xargs kill"
# cocker -a run -c "G6" --cmd "ps -ef | grep -v grep | grep -w sshd | awk '{print $2}' | xargs kill"
# cocker -a shutdown -c G6
3.3.5. 单进程启动容器
拿前面的G6
容器来演示像Docker
那样单进程启动容器
# cocker -a create -m "calvin=rhel-7.4-x86_64,calvin=sshd,calvin=G6" --host G6 --net BRIDGE --vip 166.88.0.2 --port-mapping "8600:8600,2222:222" -c "G6" -b -e "/root/bin/G6 -f /root/etc/G6.conf --no-daemon" -d
OK
用完后停止并销毁容器
# cocker -a destroy -c G6 -h
OK