设为首页 收藏本站
查看: 773|回复: 0

[经验分享] Docker容器引导完整CentOS

[复制链接]

尚未签到

发表于 2018-4-24 11:19:43 | 显示全部楼层 |阅读模式
  因为现在的环境还在CentOS5下,但新的库只有在CentOS6下才能用。然后用Docker启动完整OS,这样相对于ESX省资源一点点。
  目前的环境是宿主机是ubuntu 16.04, 然后容器就 centos5 和 centos6
  首先直接从官方下载相关底包
docker pull centos:5.11
docker pull centos:6.8  然后直接在底包上组安装相关软件
yum groupinstall ...  CentOS6 直接组安装完成后,直接启动可能会卡在 sulogin,此时可以编辑
vi /etc/rc.d/rc.sysinig
   426  if [ -z "$fastboot" -a "$READONLY" != "yes" ]; then
   427
   428          STRING=$"Checking filesystems"
   429          echo $STRING
   430          fsck -T -t noopts=_netdev -A $fsckoptions
                #不清楚为什么还要检查磁盘,加入下面这行算是禁用一下。
   431          echo "Disable FSCK" > /dev/null
   432          rc=$?
   433
   434          if [ "$rc" -eq "0" ]; then
   435                  success "$STRING"
   436                  echo  

  引导完整系统官方是不推荐这样做的,毕竟Docker是轻量级的,这样违背了他的初衷了。
  要引导完整系统run后面的参数就是 /sbin/init
  

  在安装好 autofs 后,想启动它发现提示:
Starting automount: automount: test mount forbidden or incorrect kernel protocol version, kernel protocol version 5.00 or above required.
                                                           [FAILED]  搜索后发现原来是权限不够。
  直接在 run 时加上 --privileged 即可,如:
docker run --rm --privileged -p 3000:22 -v /root/centos6:/root centos6/d1103:D /sbin/init  容器启动后,在宿主机发现 agetty 进程CPU占用100%,再次放狗,解决办法如下
systemctl list-units *getty*
systemctl stop getty@tty1.service  #主要是这个停止后就OK
systemctl stop system-getty.slice
systemctl stop getty.target  

  另外容器还要固定IP,然后用的macvlan办法,写了一个小脚本用来启动容器并设置IP
#!/bin/bash
# start container and setting container IP address
if [[ $# -lt 2 ]] || [[ $# -gt 3 ]]
    then
    echo "./script.sh image_name container_ip hostname [ip_dev_name]"
    echo ""
    echo "./script.sh centos5/new:A 192.168.10.5/26 test1 [enp4s0]"
    echo ""
    exit
fi
if [[ $# -eq 4 ]]
    then
    if [[ `ip link | awk -vdev=$4 '$2==dev":"{a=1}END{print a}'` -ne 1 ]]
        then
        echo "ip dev name error"
        exit
    fi
    ip_dev=$4
fi
if [[ $# -eq 3 ]]
    then
    ip_dev=`ip -4 a | awk -F'[ :]+' '/UP group/{print $2;exit}'`
fi
config=/docker
if [[ `docker images "$1" 2>/dev/null | wc -l` -ne 2 ]]
    then
    echo "Docker Images Not Found"
    exit
fi
if [[ `ping ${2%/*} -c 3 | grep -c "100% packet loss"` -eq 0 ]]
    then
    echo "IP address Already Use"
    exit
fi
docker run -d --privileged --net=none --name ${1%/*} -v $config/config/${1%/*}:/root -v $config/local_home:/local_home -h $3 $1 /sbin/init
sleep 8
docker_pid=$(docker inspect -f '``.`State`.`Pid`' ${1%/*})
ip link add "$ip_dev".d link "$ip_dev" type macvlan mode bridge
ip link set netns "$docker_pid" "${ip_dev}.d"
nsenter -t "$docker_pid" -n ip link set "${ip_dev}.d" up
nsenter -t "$docker_pid" -n ip route del default
nsenter -t "$docker_pid" -n ip addr add "$2" dev "${ip_dev}.d"
nsenter -t "$docker_pid" -n ip route add default via `ip r | awk '/default/{print $3}'` dev "${ip_dev}.d"
# enter container
#nsenter --target=$docker_pid --net --mount --uts --pid  上面的脚本用了一段时间,感觉还是太麻烦,要敲太多东西了.然后又折腾了一个python的
  这个脚本不要再想最新的镜像是什么,然后比较方便的commit
#!/usr/bin/env python
import time
import sys
import os
ser = {"centos5":["test09","192.168.10.5/24"], "centos6":["test10","192.168.10.6/24"]}
config = {0:None, 1:"run", 2:None, 3:None} # 0:os, 1:run_type, 2:ip, 3:netcard
if len(sys.argv) == 1:
    print '''
    ./script.py os      [run(default)|commit]  [ipaddress]  [netcard]
    ./script.py centos5
'''
    sys.exit(1)
for i,j in enumerate(sys.argv[1:]):
    config = j
if config[0] not in ser:
    print "Docker Image Not Found"
    sys.exit(1)   
if not config[3]:
    config[3] = os.popen("ip -4 a | awk -F'[ :]+' '/UP group/{print $2;exit}'").read()[:-1]
if not config[2]:
    config[2] = ser[config[0]][1]
image = os.popen("""docker images | awk '/%s/{print $1":"$2;exit}'""" % sys.argv[1]).read()[:-1]
if image:
    if sys.argv[1]  in os.popen("docker ps").read():
        print "Docker Container Allready Running!"
        sys.exit()
    os.system("docker rm %s" % sys.argv[1])
    if config[1] == "run":
        os.system("docker run -d --privileged --net=none --name %s -v /docker/config/%s:/root -v /local_home:/local_home --hostname %s %s /sbin/init" % (config[0],config[0],ser[config[0]][0],image))
        time.sleep(8)
        pid = os.popen("docker inspect -f '``.`State`.`Pid`' %s" % sys.argv[1]).read()[:-1]
        os.system('ip link add %s.d link %s type macvlan mode bridge' % (config[3],config[3]))
        os.system('ip link set netns %s %s.d' % (pid,config[3]))
        os.system('nsenter -t %s -n ip link set %s.d up' % (pid,config[3]))
        os.system('nsenter -t %s -n ip route del default' % pid)
        os.system('nsenter -t %s -n ip addr add %s dev %s.d' % (pid,config[2],config[3]))
        os.system("nsenter -t %s -n ip route add default via `ip r | awk '/default/{print $3}'` dev %s.d" % (pid,config[3]))
    else:
        os.system("docker run --rm -it --privileged --name %s -v /docker/config/%s:/root -v /local_home:/local_home %s bash" % (config[0],config[0],image))
# enter container
#nsenter --target=$docker_pid --net --mount --uts --pid  

  

  在使用过程中发现因为没有设置主机名从而导致NFS出现下面的错误:

Dec  1 13:44:39 localhost rpc.statd[8211]: gethostbyname error for localhost.localdomain
Dec  1 13:44:39 localhost rpc.statd[8211]: STAT_FAIL to localhost.localdomain for SM_MON of 192.168.10.7
Dec  1 13:44:54 localhost rpc.statd[8211]: gethostbyname error for localhost.localdomain
Dec  1 13:44:54 localhost rpc.statd[8211]: STAT_FAIL to localhost.localdomain for SM_MON of 192.168.10.7
Dec  1 13:44:54 localhost kernel: [2062569.786565] lockd: cannot monitor test04  更改上面的docker启动脚本, 加入 -h 这个参数,但发现 -h 只会更改 /etc/hostname
  而 /etc/hosts 和 /etc/sysconfig/network 没有变化。于是把下面的脚本加入 /etc/rc.local 搞定.
#!/bin/bash
# setting hostname
hostname `cat /etc/hostname`
name=`hostname`
echo "127.0.0.1   ${name} ${name}.test.net" >> /etc/hosts
sed -i "s/\(HOSTNAME=\).*$/\1$name/" /etc/sysconfig/network  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-451339-1-1.html 上篇帖子: 将CentOS设置为用光盘做yum源 下篇帖子: centos7配置iptables禁用firewalld
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表