【LXC容器】

在这里插入图片描述

1 LXC介绍

1.1 LXC是什么

LXC,其名称来自 Linux 软件容器的缩写,是一种操作系统层虚拟化技术,为Linux内核容器功能的一个用户空间接口。它将应用软件系统打包成一个软件容器,内含应用软件本身的代码,以及所需要的操作系统核心和库。

1.2 LXC作用

通过统一的名字空间和共享 API 来分配不同软件容器的可用硬件资源,创造出应用程序的独立沙箱运行环境,使得 Linux 用户可以容易的创建和管理系统或应用容器。
在 Linux 内核中,提供了 cgroups 功能,来达成资源的区隔化。它同时也提供了名称空间区隔化的功能,使应用程序看到的操作系统环境被区隔成独立区间,包括行程树,网络,用户 id,以及挂载的文件系统。但是 cgroups 并不一定需要引导任何虚拟机。
LXC 利用 cgroups 与名称空间的功能,提供应用软件一个独立的操作系统环境。LXC 不需要 Hypervisor 这个软件层,软件容器(Container)本身极为轻量化,提升了创建虚拟机的速度。软件 Docker 被用来管理 LXC 的环境。

2 常见指令

# ubuntu版本
sudo lsb_release -a
# 初始化配置 Initial configuration
sudo lxd init
# container 创建ubuntu容器样例
lxc launch ubuntu:16.04 first
# 显示可用镜像
lxc image list images: | less
lxc image list images: ‘centos’
# LXD/LXC 2.0使用清华镜像加速的方法:
创建一个remote链接,指向镜像站即可,或替换掉默认的images链接。
lxc remote add tuna-images  https://mirrors.tuna.tsinghua.edu.cn/lxc-images/ --protocol=simplestreams --public
# 显示可用清华镜像
lxc image list tuna-images:
# 显示当前远程可用
lxc remote list
# 显示容器网络
lxc network show lxdbr0
# 显示容器
lxc list
# 运行容器bash
lxc exec first – /bin/bash
# 直接运行容器命令
lxc exec first – apt-get update
# 文件传输下载
lxc file pull first/etc/hosts .
# 文件传输上传
lxc file push hosts first/tmp/
# 停止容器
lxc stop first
# 删除容器
lxc delete first
# 显示容器信息
lxc info first
# 本地配置
lxc config edit <container>
# 显示容器详细信息
lxc config show first
lxc config show <container>
# 内存查看 free -m
lxc exec first – free -m
# 设置内存最大值
lxc config set first limits.memory 2048MB
# 执行升级
lxc exec first – apt-get update
lxc exec first – apt-get dist-upgrade -y
lxc exec first – apt-get autoremove --purge -y
# 暂停
lxc pause <container>
# 快照 Snapshots -stateful选项包含所有状态
lxc snapshot first clean
# 列出所有的快照
lxc info <container>
lxc snapshot <container> <snapshot name>
# 快照恢复
lxc restore first clean
# 从快照生成容器镜像
lxc publish first/clean --alias clean-ubuntu
# 启动快照容器镜像
lxc launch clean-ubuntu second
# 删除容器镜像方式一
lxc delete --force second
# 删除容器镜像方式二
lxc delete -f second
# 删除一个快照
lxc delete <container>/<snapshot name>
# 从快照中创建一个新的容器
lxc copy <source container>/<snapshot name> <destination container>
# 给快照重命名
lxc move <container>/<snapshot name> <container>/<new snapshot name>
# lxc运行docker
lxc config set first security.nesting true
lxc restart first
**# 本地镜像导出
lxc publish ${HOST}/auto-backup --alias ${BACKUP_NAME}
lxc image list
lxc image export ${BACKUP_NAME} 
**# 本地镜像删除
lxc image delete <container>

3 可能出现的问题

3.1 服务器关机重启后,gpu需重新挂载


# 先停止容器运行
sudo lxc stop zhangsan
# 移除原挂载的gpu
sudo lxc config device remove zhangsan gpu
# 重新挂载新的gpu
sudo lxc config device add zhangsan gpu gpu
# 重新开启运行
sudo lxc start zhangsan 

3.2 ssh远程连接不上

# 先停止容器运行
sudo lxc stop zhangsan 
# 重新开启运行
sudo lxc start zhangsan 

3.3 创建容器常见指令

#创建容器
sudo lxc launch ad94f0c6c53d zhangsan #(ad94f0c6c53d是镜像的fingerprint,可通过lxc image list查看 )
#分配gpu
sudo lxc config device add zhangsan gpu gpu
#挂载硬盘
sudo lxc config set zhangsan security.privileged true && sudo lxc config device add zhangsan share-host disk source=/home/share path=/home/shell
#运行
sudo lxc exec zhangsan bash
#输出列表,查看容器运行状态
sudo lxc list
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值