LXC容器的配置

    科技2022-08-11  111

    1. 安装lxd

    sudo apt-get install lxd

    2. 将普通用户添加至lxd,使普通用户也可使用lxd

    # 添加用户,并加入lxd用户组(user1为新用户名)

    useradd -md /home/user1 -s /bin/bash -g lxd user1

    # 设置新用户的密码

    passwd user1

    # 切换到新用户

    su - user1

    3. 初始化lxd

    根据提示回车默认就好

    lxd init

    4. 配置lxd的镜像源,并下载ubuntu16.04镜像

    # 添加lxd镜像的tuna-images源

    lxc remote add tuna-images https://mirrors.tuna.tsinghua.edu.cn/lxc-images/ --protocol=simplestreams --public

    # 查看tuna-images源下所有的镜像列表

    lxc image list tuna-images:

    # 下载ubuntu16.04镜像

    lxc image copy tuna-images:ubuntu/16.04 local: --alias ubuntu/16.04 --copy-aliases --public

    5. 创建镜像模板,方便往后创建新的容器时,还要重复的安装基本环境

    # 创建虚拟机容器,并进入

    lxc init ubuntu/16.04 template -p default

    lxc start template

    lxc exec template /bin/bash

     

    # 下面所有的命令都是在容器里的,需要注意

     

    # 换源 & 更新

    vim /etc/apt/sources.list

    # 拷贝下面的内容代原文件,清华tuna源,也可以选其他的

    # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释

    # 注意:下面链接中的xeial为ubuntu16.04的版本代号,版本代号可使用命令lsb_release -c查看

    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse

    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse

    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse

    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse

    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse

    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse

    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse

    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse

     

    # 更新源

    apt-get update

    # 安装一些基本环境

    apt-get install make cmake openssh-server gcc g++ build-essential python3-pip git

    # 允许ssh的root访问

    vim /etc/ssh/sshd_config

    将文件中的PermitRootLogin prohibit-password改为PermitRootLogin yes

    # 退出容器

    exit

    6. 打包模板

    # 停止容器

    lxc stop template

    # 发布镜像,将容器template发布为template镜像

    lxc publish template --alias template --public

    # 查看本地template镜像是否成功创建

    lxc image list

    # 删除template容器

    lxc delete template

    7. 利用镜像初始化新的容器

    lxc init template <容器名> -p default
    Processed: 0.009, SQL: 9