Incus教程#
技术介绍#
Incus是LXD的一个分支,提供了更先进的容器和虚拟机管理功能。它是一个轻量级的容器管理系统,支持Linux容器和虚拟机,专注于简单性、安全性和可扩展性。Incus提供了丰富的API和命令行工具,用于管理容器和虚拟机的生命周期。
Incus核心概念#
- 实例:Incus中的容器或虚拟机
- 配置文件:定义实例的配置
- 存储池:管理实例的存储
- 网络:管理实例的网络连接
- 镜像:用于创建实例的预配置文件系统
- 配置文件:定义实例的默认配置
Incus架构#
- Incus daemon:运行在主机上的后台服务,负责管理实例
- Incus client:命令行工具,用于与Incus daemon通信
- 存储后端:支持多种存储后端,如dir、zfs、btrfs等
- 网络后端:支持多种网络后端,如bridge、ovn等
入门级使用#
安装Incus#
在不同操作系统上安装Incus:
# Ubuntu/Debian
# 添加Incus仓库
curl -fsSL https://pkgs.zabbly.com/key.asc | sudo gpg --dearmor -o /usr/share/keyrings/zabbly.asc
echo "deb [signed-by=/usr/share/keyrings/zabbly.asc] https://pkgs.zabbly.com/incus/stable/ubuntu $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/zabbly-incus.list
# 安装Incus
apt-get update
apt-get install incus
# CentOS/RHEL
# 添加Incus仓库
yum-config-manager --add-repo=https://pkgs.zabbly.com/incus/stable/centos/7/x86_64/
# 安装Incus
yum install incus
# 初始化Incus
incus admin init
# 验证安装
incus --version基本Incus命令#
使用Incus的基本命令:
# 查看Incus版本
incus --version
# 查看Incus状态
systemctl status incus
# 查看镜像
incus image list
# 下载镜像
incus image copy images:ubuntu/22.04 local: --alias ubuntu2204
# 创建实例
incus launch ubuntu2204 myinstance
# 列出实例
incus list
# 进入实例
incus exec myinstance -- /bin/bash
# 停止实例
incus stop myinstance
# 删除实例
incus delete myinstance
# 查看实例状态
incus info myinstance基本Incus配置#
配置Incus:
# 查看Incus配置
incus config show
# 修改Incus配置
incus config set core.https_address "[::]:8443"
# 查看实例配置
incus config show myinstance
# 修改实例配置
incus config set myinstance limits.cpu 2
incus config set myinstance limits.memory 1GB
# 重启Incus服务
systemctl restart incus初级使用#
Incus网络配置#
配置Incus网络:
# 查看网络
incus network list
# 创建网络
incus network create lxdbr0 ipv4.address=10.10.10.1/24 ipv4.nat=true
# 配置实例网络
incus launch ubuntu2204 myinstance --network=lxdbr0
# 查看实例网络
incus list myinstance
# 修改实例网络
incus config device add myinstance eth1 nic network=lxdbr0 name=eth1
# 删除网络
incus network delete lxdbr0Incus存储配置#
配置Incus存储:
# 查看存储池
incus storage list
# 创建存储池
incus storage create default dir
# 创建自定义存储池
incus storage create mypool zfs source=/dev/sdb1
# 配置实例存储
incus launch ubuntu2204 myinstance --storage=mypool
# 查看实例存储
incus storage info mypool
# 删除存储池
incus storage delete mypoolIncus镜像管理#
管理Incus镜像:
# 查看镜像
incus image list
# 下载镜像
incus image copy images:ubuntu/22.04 local: --alias ubuntu2204
# 上传镜像
incus image export ubuntu2204 ubuntu2204.tar.gz
# 导入镜像
incus image import ubuntu2204.tar.gz --alias ubuntu2204
# 删除镜像
incus image delete ubuntu2204
# 刷新镜像
incus image refresh ubuntu2204中级使用#
Incus配置文件#
使用Incus配置文件:
# 查看配置文件
incus profile list
# 创建配置文件
incus profile create myprofile
incus profile edit myprofile
# 应用配置文件
incus profile apply myinstance myprofile
# 查看配置文件
incus profile show myprofile
# 删除配置文件
incus profile delete myprofileIncus实例迁移#
迁移Incus实例:
# 准备迁移
incus stop myinstance
# 导出实例
incus export myinstance myinstance.tar.gz
# 在目标主机导入实例
incus import myinstance.tar.gz
# 启动实例
incus start myinstance
# 使用incus copy进行迁移
incus copy source:myinstance target: --mode=pullIncus集群#
创建Incus集群:
# 初始化第一个节点
incus admin init
# 获取集群令牌
incus cluster add node2
# 在第二个节点加入集群
incus admin init --preseed < preseed.yaml
# 查看集群状态
incus cluster list
# 部署实例到集群
incus launch ubuntu2204 myinstance --target node2
# 查看集群存储
incus storage list中上级使用#
Incus安全配置#
配置Incus安全:
# 配置实例安全
incus config set myinstance security.privileged false
incus config set myinstance security.nesting false
incus config set myinstance security.syscalls.intercept.mknod true
incus config set myinstance security.syscalls.intercept.setxattr true
# 配置AppArmor
incus config set myinstance raw.lxc lxc.apparmor.profile=unconfined
# 配置Seccomp
incus config set myinstance raw.lxc lxc.seccomp.profile=default
# 配置Capabilities
incus config set myinstance raw.lxc lxc.cap.drop=allIncus资源限制#
限制Incus实例资源:
# 限制CPU
incus config set myinstance limits.cpu 2
incus config set myinstance limits.cpu.allowance 50%
# 限制内存
incus config set myinstance limits.memory 1GB
incus config set myinstance limits.memory.swap false
# 限制磁盘
incus config set myinstance limits.disk.size root=10GB
incus config set myinstance limits.disk.priority 50
# 限制网络
incus config set myinstance limits.network.priority 5
incus config set myinstance limits.network.priority 1000Incus快照#
使用Incus快照:
# 创建快照
incus snapshot create myinstance snap1
# 列出快照
incus snapshot list myinstance
# 恢复快照
incus snapshot restore myinstance snap1
# 从快照创建新实例
incus copy myinstance/snap1 newinstance
# 删除快照
incus snapshot delete myinstance snap1
# 配置自动快照
incus config set myinstance snapshots.schedule 20:00
incus config set myinstance snapshots.expiry 7d高级使用#
Incus虚拟机#
使用Incus虚拟机:
# 启用虚拟机支持
incus config set core.https_address [::]:8443
# 创建虚拟机
incus launch ubuntu2204 myvm --vm
# 查看虚拟机
incus list myvm
# 进入虚拟机
incus exec myvm -- /bin/bash
# 停止虚拟机
incus stop myvm
# 删除虚拟机
incus delete myvm
# 配置虚拟机资源
incus config set myvm limits.cpu 4
incus config set myvm limits.memory 4GB
incus config set myvm limits.hw.nic 2Incus远程操作#
使用Incus远程操作:
# 添加远程服务器
incus remote add remote1 192.168.1.100
# 列出远程服务器
incus remote list
# 从远程服务器拉取镜像
incus image copy remote1:ubuntu/22.04 local:
# 在远程服务器创建实例
incus launch remote1:ubuntu/22.04 myinstance
# 复制实例到远程服务器
incus copy myinstance remote1:
# 删除远程实例
incus delete remote1:myinstanceIncus API使用#
使用Incus API:
import lxc
# 连接到Incus
client = lxc.Client()
# 列出实例
instances = client.instances.all()
for instance in instances:
print(instance.name)
# 创建实例
instance = client.instances.create(
"myinstance",
config={
"source": {
"type": "image",
"alias": "ubuntu/22.04"
}
}
)
# 启动实例
instance.start()
# 停止实例
instance.stop()
# 删除实例
instance.delete()大师级使用#
Incus企业级部署#
企业级Incus部署:
# 配置高可用性集群
incus admin init
# 选择创建集群,添加其他节点
# 配置存储复制
incus storage create mypool ceph source=lxd-ceph
incus storage set mypool replication.enabled true
# 配置负载均衡
incus network create lb0 ipv4.address=10.0.0.1/24 ipv4.nat=true
incus network set lb0 bridge.mode=fan
# 部署应用
incus launch ubuntu2204 app1 --target node1
incus launch ubuntu2204 app2 --target node2
incus launch ubuntu2204 app3 --target node3
# 配置监控
incus config set core.metrics_address [::]:9100
incus config set core.metrics_authentication=falseIncus与Kubernetes集成#
将Incus与Kubernetes集成:
# 安装MicroK8s
apt install snapd
snap install microk8s --classic
# 配置MicroK8s使用Incus
microk8s.enable dns dashboard storage
# 部署应用到Kubernetes
kubectl apply -f deployment.yaml
# 查看Pod
kubectl get pods
# 访问应用
kubectl port-forward pod/app-pod 8080:80Incus安全审计#
进行Incus安全审计:
# 查看安全配置
incus config show myinstance | grep security
# 检查实例隔离
incus info myinstance | grep security
# 使用AppArmor配置
incus config set myinstance raw.lxc lxc.apparmor.profile=lxc-default-with-nesting
# 使用Seccomp配置
incus config set myinstance raw.lxc lxc.seccomp.profile=/etc/lxc/seccomp.conf
# 检查网络隔离
incus network info lxdbr0
# 检查存储隔离
incus storage info default实战案例#
案例一:部署多实例应用#
场景:部署一个包含Web服务器和数据库的多实例应用。
解决方案:使用Incus创建和管理多个实例。
实施步骤:
创建网络:
incus network create appnet ipv4.address=192.168.10.1/24 ipv4.nat=true创建数据库实例:
incus launch ubuntu2204 db --network=appnet incus exec db -- apt update incus exec db -- apt install -y postgresql incus exec db -- sudo -u postgres psql -c "CREATE USER appuser WITH PASSWORD 'password';" incus exec db -- sudo -u postgres psql -c "CREATE DATABASE appdb OWNER appuser;"创建Web服务器实例:
incus launch ubuntu2204 web --network=appnet incus exec web -- apt update incus exec web -- apt install -y nginx php-fpm php-pgsql incus exec web -- systemctl start nginx php8.1-fpm incus exec web -- systemctl enable nginx php8.1-fpm配置Web服务器:
incus file push index.php web/var/www/html/ incus file push nginx.conf web/etc/nginx/sites-available/default incus exec web -- systemctl reload nginx
结果:
- 成功创建了Web服务器和数据库实例
- 配置了实例间的网络连接
- 部署了完整的Web应用
案例二:Incus集群部署#
场景:部署一个Incus集群,实现高可用性。
解决方案:创建Incus集群并部署应用。
实施步骤:
初始化第一个节点:
incus admin init # 选择创建集群,设置集群地址和密码添加其他节点:
incus admin init # 选择加入现有集群,输入集群地址和密码查看集群状态:
incus cluster list部署应用:
# 创建存储池 incus storage create mypool zfs replication.enabled=true # 部署应用到不同节点 incus launch ubuntu2204 app1 --target node1 --storage=mypool incus launch ubuntu2204 app2 --target node2 --storage=mypool incus launch ubuntu2204 app3 --target node3 --storage=mypool
结果:
- 成功创建了Incus集群
- 实现了应用的高可用性
- 配置了存储复制,确保数据安全
案例三:Incus与CI/CD集成#
场景:将Incus与CI/CD集成,实现自动化部署。
解决方案:使用GitLab CI和Incus实现自动化部署。
实施步骤:
配置GitLab CI:
# .gitlab-ci.yml stages: - build - test - deploy build: stage: build script: - incus launch ubuntu2204 builder - incus exec builder -- apt update - incus exec builder -- apt install -y build-essential - incus file push . builder/home/ubuntu/ - incus exec builder -- make - incus file pull builder/home/ubuntu/build/app . - incus delete builder test: stage: test script: - incus launch ubuntu2204 tester - incus file push app tester/home/ubuntu/ - incus exec tester -- ./home/ubuntu/app test - incus delete tester deploy: stage: deploy script: - incus launch ubuntu2204 app - incus file push app app/home/ubuntu/ - incus exec app -- systemctl start app only: - main配置Incus访问:
# 生成证书 incus config trust add cert.pem # 配置GitLab Runner incus config set core.https_address [::]:8443
结果:
- 成功集成Incus与CI/CD
- 实现了自动化构建、测试和部署
- 提高了开发和部署效率
总结#
Incus是一种强大的容器和虚拟机管理工具,通过本教程的学习,您已经掌握了从入门到大师级的Incus使用技术。
主要技术回顾#
- 基础操作:Incus的基本命令和操作
- 网络配置:配置Incus网络
- 存储配置:配置Incus存储
- 实例管理:创建、启动、停止和删除实例
- 高级功能:配置文件、实例迁移、集群管理
- 企业级应用:高可用性、安全审计、CI/CD集成
最佳实践#
- 使用最新版本:定期更新Incus版本,获取最新的安全补丁和功能
- 网络隔离:为不同的应用创建独立的网络,提高安全性
- 存储管理:使用合适的存储后端,如ZFS或btrfs,支持快照和复制
- 资源限制:为实例设置适当的资源限制,避免资源争用
- 安全配置:使用非特权实例,配置AppArmor和Seccomp,提高安全性
- 监控实例:使用Prometheus等工具监控实例状态
- 自动化部署:与CI/CD集成,实现自动化构建、测试和部署
- 定期备份:使用快照定期备份实例,确保数据安全
注意事项#
- 版本管理:确保使用兼容的Incus版本
- 网络配置:合理配置网络,确保实例间通信安全
- 存储管理:定期清理未使用的镜像、实例和快照,释放存储空间
- 安全审计:定期进行Incus安全审计,发现和修复安全问题
- 备份策略:制定实例数据备份策略,确保数据安全
- 灾难恢复:建立实例环境的灾难恢复计划,应对突发情况
通过合理学习和使用Incus,您可以显著提高系统资源利用率,实现更高效的实例部署和管理。Incus提供了强大的容器和虚拟机管理功能,适用于从开发环境到生产环境的各种场景。