3.3. 场景示例

3.3.1. 交互式构建yum镜像

有了操作系统基础镜像后可以交互式构建其它镜像。大致过程为用基础镜像创建启动容器,在容器内交互式安装和部署,然后停止容器,最后转换容器为新镜像。

  1. # cocker -a create -m "calvin=rhel-7.4-x86_64" --host yum --volume "/mnt/cdrom:/mnt/cdrom" -c "calvin=yum"
  2. OK
  3. # cocker -a boot -c "calvin=yum" -t
  4. [root@yum /root]

在容器内配置好yum,在我的环境里这样配置

  1. [root@yum /root] mkdir -p /etc/yum.repos.d
  2. [root@yum /root] vi /etc/yum.repos.d/cdrom.repo
  3. [cdrom]
  4. name=cdrom
  5. baseurl=file:///mnt/cdrom
  6. gpgcheck=0
  7. enable=1
  1. [root@yum /root] yum search sshd
  2. cdrom | 4.1 kB 00:00:00
  3. (1/2): cdrom/group_gz | 137 kB 00:00:00
  4. (2/2): cdrom/primary_db | 4.0 MB 00:00:00
  5. =============================================================================================================== Matched: sshd ===============================================================================================================
  6. openssh-server.x86_64 : An open source SSH server daemon

转换容器为yum镜像

  1. [root@yum /etc/yum.repos.d] exit
  2. logout
  3. # cocker -a shutdown -c "calvin=yum"
  4. OK
  5. # cocker -s containers
  6. container_id image hostname net netns size status
  7. -----------------------------------------------------------------------------------------------------------
  8. calvin=yum calvin=rhel-7.4-x86_64 yum HOST 24 MB STOPED
  9. # cocker -a to_image --from-container "calvin=yum" --version "1.0.0" --to-image "calvin=yum"
  10. OK
  11. # cocker -s containers
  12. # cocker -s images
  13. image_id version modify_datetime size
  14. --------------------------------------------------------------------
  15. calvin=rhel-7.4-x86_64 1.0.0 2018-11-25T09:55:25 271 MB
  16. calvin=yum 1.0.0 2018-11-25T10:16:59 24 MB

3.3.2. 交互式构建sshd镜像

注意:交互式构建sshd依赖yum。

  1. # 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"
  2. OK
  3. # cocker -a boot -c "calvin=sshd" -t
  4. [root@sshd /root]

在容器内配置好sshd,在我的环境里这样配置

  1. [root@sshd /root] yum install -y openssh-server
  2. ...
  3. [root@sshd /root] ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
  4. ...(一般全直接按回车)
  5. [root@sshd /root] ssh-keygen -t rsa -f /etc/ssh/ssh_host_ecdsa_key
  6. ...(一般全直接按回车)
  7. [root@sshd /root] echo "root:root" | chpasswd
  8. ...(如有卡住,按Ctrl+C结束)
  9. [root@sshd /root] nohup /usr/sbin/sshd -D &

另外开一屏连接sshd容器

  1. # ssh root@166.88.0.2 -p 2222
  2. The authenticity of host '[166.88.0.2]:2222 ([166.88.0.2]:2222)' can't be established.
  3. RSA key fingerprint is SHA256:kSX5DU3MiwEy8dArBoAk00kbB7hBtRXl/Pm4n9jWjBY.
  4. RSA key fingerprint is MD5:27:5d:b6:5a:5a:b1:bc:eb:b9:82:98:58:40:7e:eb:45.
  5. Are you sure you want to continue connecting (yes/no)? yes
  6. Warning: Permanently added '[166.88.0.2]:2222' (RSA) to the list of known hosts.
  7. root@166.88.0.2's password: root密码前面被重置成root了)
  8. Last login: Mon Nov 26 13:28:07 2018 from 192.168.6.7
  9. -bash-4.2#

转换容器为sshd镜像

  1. [root@sshd /root] exit
  2. logout
  3. # cocker -a shutdown -c "calvin=sshd"
  4. OK
  5. # cocker -s containers
  6. container_id image hostname net netns size status
  7. -----------------------------------------------------------------------------------------------------------
  8. calvin=yum calvin=rhel-7.4-x86_64 yum HOST 24 MB STOPED
  9. # cocker -a to_image --from-container "calvin=sshd" --version "1.0.0" --to-image "calvin=sshd"
  10. OK
  11. # cocker -s containers
  12. # cocker -s images
  13. image_id version modify_datetime size
  14. --------------------------------------------------------------------
  15. calvin=rhel-7.4-x86_64 1.0.0 2018-11-25T09:55:25 271 MB
  16. calvin=yum 1.0.0 2018-11-25T10:16:59 24 MB
  17. calvin=sshd 1.0.0 2018-11-26T09:16:59 335 MB

3.3.3. 交互式构建G6镜像

G6是本人以前独立自研的负载均衡器(源码托管地址 : 开源中国github),不少公司在使用,下面介绍交互式构建G6镜像。

  1. # 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"
  2. OK
  3. # cocker -a boot -c "calvin=G6" -t
  4. [root@G6 /root]

在容器内配置好G6,在我的环境里这样配置

  1. [root@G6 /root] yum install -y git
  2. [root@G6 /root] yum install -y make
  3. [root@G6 /root] yum install -y gcc
  4. ...(这里仅作演示,所以把gitgcc都安装在一起了)
  5. [root@G6 /root] mkdir src && cd src
  6. [root@G6 /root/src] git clone https://gitee.com/calvinwillisms/G6 && cd G6
  7. [root@G6 /root/src/G6] cd src
  8. [root@G6 /root/src/G6/src] make -f makefile.Linux install
  9. ...
  10. [root@G6 /root/src/G6/src] mkdir ~/etc/ && vi ~/etc/G6.conf
  11. admin_rule_id G 127.0.0.1:* - 127.0.0.1:8600 ;
  12. my_rule RR *:* - 0.0.0.0:222 > 166.88.0.2:22 ;
  13. [root@G6 /root/src/G6/src] cd ~
  14. [root@G6 /root] nohup /usr/sbin/sshd -D &
  15. [root@G6 /root] G6
  16. G6 v1.0.6 build Nov 26 2018 14:28:18
  17. TCP Bridge && Load-Balance Dispenser
  18. Copyright by calvin 2016
  19. 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 ]
  20. [root@G6 /root] G6 -f ~/etc/G6.conf

另外开一屏连接G6容器

  1. # ssh root@166.88.0.2 -p 2222
  2. root@166.88.0.2's password: (root密码前面被重置成root了)
  3. Last login: Mon Nov 26 13:28:07 2018 from 192.168.6.7
  4. -bash-4.2#

转换容器为G6镜像

  1. [root@G6 /root] ps -ef
  2. ...
  3. [root@G6 /root] ps -ef | grep -v grep | grep "G6 -f" | awk '{if($3==1)print $2}' | xargs kill
  4. [root@G6 /root] ps -ef | grep -v grep | grep -w sshd | awk '{print $2}' | xargs kill
  5. [root@G6 /root] exit
  6. logout
  7. # cocker -a shutdown -c "calvin=G6"
  8. OK
  9. # cocker -s containers
  10. container_id image hostname net netns size status
  11. -----------------------------------------------------------------------------------------------------------
  12. calvin=G6 calvin=rhel-7.4-x86_64,calvin=yum,calvin=sshd G6 BRIDGE nns2513F44178 373 MB STOPED
  13. # cocker -a to_image --from-container "calvin=G6" --version "1.0.0" --to-image "calvin=G6"
  14. OK
  15. # cocker -s containers
  16. # cocker -s images
  17. image_id version modify_datetime size
  18. --------------------------------------------------------------------
  19. calvin=rhel-7.4-x86_64 1.0.0 2018-11-25T09:55:25 271 MB
  20. calvin=yum 1.0.0 2018-11-25T10:16:59 24 MB
  21. calvin=sshd 1.0.0 2018-11-26T09:16:59 335 MB
  22. calvin=G6 1.0.0 2018-11-26T10:01:48 373 MB

3.3.4. 镜像配置实例化容器并启动

还是拿本人的G6做例子,把镜像calvin=G6转化为容器,把配置文件改成模板后再转化回镜像

  1. # cocker -a to_container --from-image "calvin=G6" -m "calvin=rhel-7.4-x86_64" --host G6 --to-container "calvin=G6"
  2. OK
  3. # cocker -a boot -c "calvin=G6" -t
  4. [root@G6 /root] cd etc
  5. [root@G6 /root/etc] mv G6.conf G6.conf.tpl
  6. [root@G6 /root/etc] vi G6.conf.tpl
  7. admin_rule G 127.0.0.1:* - 127.0.0.1:${ADMIN_PORT} ;
  8. my_rule RR *:* - 0.0.0.0:${FORWARD_PORT} > ${DEST_IP}:${DEST_PORT} ;
  9. [root@G6 /root/etc] vi ../bin/sshd.start
  10. nohup /usr/sbin/sshd -D &
  11. [root@G6 /root/etc] chmod +x ../bin/sshd.start
  12. [root@G6 /root/etc] exit
  13. logout
  14. # cocker -a shutdown -c "calvin=G6"
  15. OK
  16. # cocker -a to_image --from-container "calvin=G6" --version "1.1.0" --to-image "calvin=G6"
  17. OK
  18. # cocker -s images
  19. image_id version modify_datetime size
  20. --------------------------------------------------------------------
  21. calvin=rhel-7.4-x86_64 1.0.0 2018-11-27T08:00:07 273 MB
  22. calvin=yum 1.0.0 2018-11-26T09:10:43 24 MB
  23. calvin=sshd 1.0.0 2018-11-26T09:17:12 335 MB
  24. calvin=G6 1.1.0 2018-11-27T08:03:33 373 MB

最后用镜像创建容器,配置实例化,启动服务

  1. # 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"
  2. OK
  3. # cocker -a boot -c "G6"
  4. OK
  5. # vi G6.conf.map
  6. ${ADMIN_PORT} 8600
  7. ${FORWARD_PORT} 222
  8. ${DEST_IP} 166.88.0.2
  9. ${DEST_PORT} 22
  10. # cocker -a rplfile -c "G6" --template-file "/root/etc/G6.conf.tpl" --mapping-file "G6.conf.map" --instance-file "/root/etc/G6.conf"
  11. OK
  12. # cocker -a run -c "G6" --cmd "cat /root/etc/G6.conf"
  13. admin_rule G 127.0.0.1:* - 127.0.0.1:8600 ;
  14. my_rule RR *:* - 0.0.0.0:222 > 166.88.0.2:22 ;
  15. # cocker -a run -c "G6" --cmd "nohup /usr/sbin/sshd -D"
  16. nohup: ignoring input and appending output to 'nohup.out'
  17. # cocker -a run -c "G6" --cmd "G6 -f /root/etc/G6.conf"
  18. OK

另外开一屏连接G6容器

  1. # ssh root@166.88.0.2 -p 2222
  2. root@166.88.0.2's password: (root密码前面被重置成root了)
  3. Last login: Mon Nov 26 13:28:07 2018 from 192.168.6.7
  4. -bash-4.2# exit

用完后关闭服务,最后停止和销毁容器

  1. # cocker -a run -c "G6" --cmd "ps -ef | grep -v grep | grep 'G6 -f' | awk '{if($3==1)print $2}' | xargs kill"
  2. # cocker -a run -c "G6" --cmd "ps -ef | grep -v grep | grep -w sshd | awk '{print $2}' | xargs kill"
  3. # cocker -a shutdown -c G6

3.3.5. 单进程启动容器

拿前面的G6容器来演示像Docker那样单进程启动容器

  1. # 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
  2. OK

用完后停止并销毁容器

  1. # cocker -a destroy -c G6 -h
  2. OK