O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Containerization with LXC

Book Description

Get acquainted with the world of LXC

About This Book

  • Get the most practical and up-to-date resource on LXC and take full advantage of what Linux containers can offer in the day-to-day operations of large-scale applications
  • Learn how to deploy and administer various workloads such as web applications inside LXC
  • Save your organization time and money by building robust and secure containers and by speeding the deployment process of your software

Who This Book Is For

This book is for Linux engineers and software developers who are looking to deploy applications in a fast, secure, and scalable way for use in testing and production.

What You Will Learn

  • Deep dive into the foundations of Linux containers with kernel namespaces and cgroups
  • Install, configure, and administer Linux containers with LXC and libvirt
  • Begin writing applications using Python libvirt bindings
  • Take an in-depth look at container networking
  • Set up monitoring and security with LXC
  • Build and deploy a highly available application with LXC in the cloud

In Detail

In recent years, containers have gained wide adoption by businesses running a variety of application loads. This became possible largely due to the advent of kernel namespaces and better resource management with control groups (cgroups). Linux containers (LXC) are a direct implementation of those kernel features that provide operating system level virtualization without the overhead of a hypervisor layer.

This book starts by introducing the foundational concepts behind the implementation of LXC, then moves into the practical aspects of installing and configuring LXC containers. Moving on, you will explore container networking, security, and backups. You will also learn how to deploy LXC with technologies like Open Stack and Vagrant. By the end of the book, you will have a solid grasp of how LXC is implemented and how to run production applications in a highly available and scalable way.

Style and approach

A practical guide that introduces the core technologies behind Linux containers and provides a deep dive into installation, configuration, and operations of LXC.

Table of Contents

  1. Containerization with LXC
    1. Containerization with LXC
    2. Credits
    3. About the Author
    4. About the Reviewer
    5. www.PacktPub.com
      1. Why subscribe?
    6. Customer Feedback
    7. Dedication
    8. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    9. 1. Introduction to Linux Containers
      1. The OS kernel and its early limitations
      2. The case for Linux containers
      3. Linux namespaces – the foundation of LXC
        1. Mount namespaces
        2. UTS namespaces
        3. IPC namespaces
        4. PID namespaces
        5. User namespaces
        6. Network namespaces
        7. Resource management with cgroups
          1. Limiting I/O throughput
          2. Limiting memory usage
          3. The cpu and cpuset subsystems
          4. The cgroup freezer subsystem
          5. Using userspace tools to manage cgroups and persist changes
          6. Managing resources with systemd
      4. Summary
    10. 2. Installing and Running LXC on Linux Systems
      1. Installing LXC
        1. Installing LXC on Ubuntu with apt
        2. Installing LXC on Ubuntu from source
        3. Installing LXC on CentOS with yum
        4. Installing LXC on CentOS from source
        5. LXC directory installation layout
      2. Building and manipulating LXC containers
        1. Building our first container
        2. Making custom containers with debootstrap on Ubuntu
        3. Making custom containers with yum on CentOS
      3. Summary
    11. 3. Command-Line Operations Using Native and Libvirt Tools
      1. Using the LVM backing store
        1. Creating LXC containers using the LVM backing store
        2. Creating container snapshots on the LVM backing store
        3. Creating block devices using truncate, dd, and losetup
      2. Using the Btrfs backing store
        1. Creating LXC containers using the Btrfs backing store
        2. Creating container snapshots on the Btrfs backing store
      3. Using the ZFS backing store
        1. Creating LXC containers using the ZFS backing store
        2. Creating container snapshots on the ZFS backing store
      4. Autostarting LXC containers
      5. LXC container hooks
      6. Attaching directories from the host OS and exploring the running filesystem of a container
      7. Freezing a running container
      8. Limiting container resource usage
      9. Building and running LXC containers with libvirt
        1. Installing libvirt from packages on Debian and CentOS
        2. Installing libvirt from source
        3. Defining LXC containers with libvirt
        4. Starting and connecting to LXC containers with libvirt
        5. Attaching block devices to running containers with libvirt
        6. Networking with libvirt LXC
        7. Generating config from an existing LXC container with libvirt
        8. Stopping and removing LXC containers with libvirt
      10. Summary
    12. 4. LXC Code Integration with Python
      1. LXC Python bindings
        1. Installing the LXC Python bindings and preparing the development environment on Ubuntu and CentOS
        2. Building our first container with Python
        3. Gathering container information with Python
        4. Starting containers, applying changes, and listing configuration options with Python
        5. Changing container state with Python
        6. Stopping containers with Python
        7. Cloning containers with Python
        8. Destroying containers with Python and cleaning up the virtual environment
      2. Libvirt Python bindings
        1. Installing the libvirt Python development packages
        2. Building LXC containers with libvirt Python
        3. Starting containers and running basic operations with libvirt Python
        4. Collecting container information with libvirt Python
        5. Stopping and deleting LXC containers with libvirt Python
      3. Vagrant and LXC
        1. Configuring Vagrant LXC
      4. Putting it all together – building a simple RESTful API to LXC with Python
        1. API calls to build and configure LXC containers
        2. Cleaning up using the API calls
      5. Summary
    13. 5. Networking in LXC with the Linux Bridge and Open vSwitch
      1. Software bridging in Linux
        1. The Linux bridge
        2. The Linux bridge and the LXC package on Ubuntu
        3. The Linux bridge and the LXC package on CentOS
        4. Using dnsmasq service to obtain an IP address in the container
        5. Statically assigning IP addresses in the LXC container
        6. Overview of LXC network configuration options
        7. Manually manipulating the Linux bridge
        8. Open vSwitch
      2. Connecting LXC to the host network
        1. Configuring LXC using none network mode
        2. Configuring LXC using empty network mode
        3. Configuring LXC using veth mode
        4. Configuring LXC using phys mode
        5. Configuring LXC using vlan mode
        6. Configuring LXC using macvlan mode
      3. Summary
    14. 6. Clustering and Horizontal Scaling with LXC
      1. Scaling applications with LXC
        1. Scaling Apache in minimal root filesystem with libvirt LXC
          1. Creating the minimal root filesystem for the containers
          2. Defining the Apache libvirt container
          3. Starting the Apache libvirt container
        2. Scaling Apache with libvirt LXC and HAProxy
        3. Scaling Apache with a full LXC root filesystem and OVS GRE tunnels
          1. Configuring the load-balancer host
          2. Creating the load-balancer container
          3. Building the GRE tunnels
          4. Configuring the Apache nodes
          5. Installing Apache and HAProxy, and testing connectivity
          6. Scaling the Apache service
      2. Summary
    15. 7. Monitoring and Backups in a Containerized World
      1. Backing up and migrating LXC
        1. Creating LXC backup using tar and rsync
        2. Restoring from the archived backup
        3. Creating container backup using lxc-copy
        4. Migrating LXC containers on an iSCSI target
          1. Setting up the iSCSI target
          2. Setting up the iSCSI initiator
          3. Logging in to the iSCSI target using the presented block device as rootfs for LXC
          4. Building the iSCSI container
          5. Restoring the iSCSI container
        5. LXC active backup with replicated GlusterFS storage
          1. Creating the shared storage
          2. Building the GlusterFS LXC container
          3. Restoring the GlusterFS container
      2. Monitoring and alerting on LXC metrics
        1. Gathering container metrics
          1. Using lxc-monitor to track container state
          2. Using lxc-top to obtain CPU and memory utilization
          3. Using lxc-info to gather container information
          4. Leveraging cgroups to collect memory metrics
          5. Using cgroups to collect CPU statistics
          6. Collecting network metrics
        2. Simple container monitoring and alerting with Monit
        3. Container monitoring and alert triggers with Sensu
          1. Monitoring LXC containers with Sensu agent and server
          2. Monitoring LXC containers using standalone Sensu checks
      3. Simple autoscaling pattern with LXC, Jenkins, and Sensu
      4. Summary
    16. 8. Using LXC with OpenStack
      1. Deploying OpenStack with LXC support on Ubuntu
        1. Preparing the host
        2. Installing the database service
        3. Installing the message queue service
        4. Installing the caching service
        5. Installing and configuring the identity service
        6. Installing and configuring the image service
        7. Installing and configuring the compute service
        8. Installing and configuring the networking service
        9. Defining the LXC instance flavor, generating a key pair, and creating security groups
        10. Creating the networks
        11. Provisioning LXC container with OpenStack
      2. Summary
    17. A. LXC Alternatives to Docker and OpenVZ
      1. Building containers with OpenVZ
      2. Building containers with Docker
      3. Running unprivileged LXC containers
      4. Summary