暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

凌达干货 | 下一代容器技术:Podman

凌达Lambda工作室 2021-06-05
737

什么是容器技术?


容器是由 Linux 内核所提供的具有特定隔离功能的进程,容器技术能够让对应用及其整个运行时环境(包括全部所需文件)一起进行打包或隔离。从而在不同环境(如开发、测试和生产等环境)之间轻松迁移应用的同时,还可保留应用的全部功能。


什么是 Podman?


Podman 是一个开源的容器运行时项目,可在大多数 Linux 平台上使用。Podman 提供与 Docker 非常相似的功能。与 Docker 不同,Podman 不需要在系统上运行任何守护进程(daemon),并且也可以在没有 root 权限的情况下运行。

Podman 可以管理和运行任何符合 OCI(Open Container Initiative)规范的容器和容器镜像。



为什么我们需要 Podman?


目前,Docker 是目前最流行的 Linux 容器解决方案,但它也有两个缺点:

Docker 需要在系统上运行一个守护进程。

Docker 是以 root 身份在系统上运行这个守护进程。

这些缺点的存在可能有一定的安全隐患,为了解决这些问题,下一代容器化工具 Podman 出现了。


安装 Podman


Podman 目前已支持大多数发行版本通过软件包来进行安装,以下是几个常用发行版的例子。


Fedora CentOS

    $ sudo yum -y install podman


    Ubuntu

      $ sudo apt-get update -qq
      $ sudo apt-get install -qq -y software-properties-common uidmap  
      $ sudo add-apt-repository -y ppa:projectatomic/ppa
      $ sudo apt-get update -qq
      $ sudo apt-get -qq -y install podman


      RHEL 7

        $ sudo subscription-manager repos --enable=rhel-7-server-extras-rpms
        $ sudo yum -y install podman


        使用 Podman


        Podman 的指令跟 Docker 大多数都是相同的。以下是一个在 RHEL 7 系统上运行持久化 MariaDB 10.2 容器的例子。


        首先,拉取容器镜像。

          [root@localhost ~]# podman pull registry.access.redhat.com/rhscl/mariadb-102-rhel7
          Trying to pull registry.access.redhat.com/rhscl/mariadb-102-rhel7...Getting image source signatures
          Copying blob sha256:367d845540573038025f445c654675aa63905ec8682938fb45bc00f40849c37b
          71.46 MB ? [------------=----------------------------------------------] 10s
          Copying blob sha256:b82a357e4f15fda58e9728fced8558704e3a2e1d100e93ac408edb45fe3a5cb9
          1.27 KB ? [----=--------------------------------------------------------] 0s
          Copying blob sha256:ddec0f65683ad89fc27298921921b2f8cbf57f674ed9eb71eef4e23a9dd9bbfe
          6.40 MB ? [--------------=----------------------------------------------] 1s
          Copying blob sha256:105cfda934d478ffbf65d74a89af55cc5de1d5bc94874c2d163c45e31a937047
          58.25 MB ? [-------------------------------------------=---------------] 10s
          Copying config sha256:7ac0a23445fec91d4b458f3062e64d1ca4af4755387604f8d8cbec08926867d7
          6.79 KB 6.79 KB [========================================================] 0s
          Writing manifest to image destination
          Storing signatures
          7ac0a23445fec91d4b458f3062e64d1ca4af4755387604f8d8cbec08926867d7


          [root@localhost ~]# podman images
          REPOSITORY TAG IMAGE ID CREATED SIZE
          registry.access.redhat.com/rhscl/mariadb-102-rhel7 latest 7ac0a23445fe 9 days ago 445MB


          [root@localhost ~]# podman inspect 7ac0a23445fe
          ...


          为 MariaDB 服务创建用于数据持久化的目录。

            [root@localhost ~]# mkdir mysql-data
            [root@localhost ~]# chown 27:27 mysql-data

            此处“27”是容器内运行 MariaDB 进程的 mysql 用户的 ID。


            运行容器。

              [root@localhost ~]# podman run -d -v root/mysql-data:/var/lib/mysql/data:Z -e MYSQL_USER=user -e MYSQL_PASSWORD=pass -e MYSQL_DATABASE=db -p 3306:3306 registry.access.redhat.com/rhscl/mariadb-102-rhel7
              71da2bb210b36aaab28a2dc81b8e77da4e1024d1f2d025c0a7b97b075dec1425


              [root@localhost ~]# podman ps
              CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
              71da2bb210b3 registry.access.redhat.com/rhscl/mariadb-102-rhel7:latest container-entrypoin... 3 seconds ago Up 3 seconds ago 0.0.0.0:3306->3306/udp, 0.0.0.0:3306->3306/tcp cranky_mahavira


              检查容器的运行日志。

                [root@localhost ~]# podman logs 71da2bb210b3 | head
                => sourcing 20-validate-variables.sh ...
                => sourcing 25-validate-replication-variables.sh ...
                => sourcing 30-base-config.sh ...
                ---> 13:12:43 Processing basic MySQL configuration files ...
                => sourcing 60-replication-config.sh ...
                => sourcing 70-s2i-config.sh ...
                ---> 13:12:43 Processing additional arbitrary MySQL configuration provided by s2i ...
                => sourcing 40-paas.cnf ...
                => sourcing 50-my-tuning.cnf ...
                ---> 13:12:43 Initializing database ...


                最后,连接到容器内的 MariaDB 服务,确保可以正常使用。

                  [root@localhost ~]# mysql --user=user --password=pass -h 127.0.0.1 -P 3306 -t
                  Welcome to the MariaDB monitor. Commands end with ; or \g.
                  Your MariaDB connection id is 8
                  Server version: 10.2.8-MariaDB MariaDB Server


                  Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.


                  Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


                  MariaDB [(none)]> show databases;
                  +--------------------+
                  | Database           |
                  +--------------------+
                  | db                 |
                  | information_schema |
                  | test               |
                  +--------------------+
                  3 rows in set (0.00 sec)


                  MariaDB [(none)]> use test;
                  Database changed


                  MariaDB [test]> show tables;
                  Empty set (0.00 sec)


                  -END-


                  文案:钟庭杰
                  排版:钟庭杰
                  校对:Robinly

                  制作:凌达之珑达工作室



                  凌达新闻 | 凌达大会


                  凌达新闻 | 凌达讲座


                  更多精彩

                  凌达之星|凌达新闻|凌达公告|佛大CTF校队







                  文章转载自凌达Lambda工作室,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                  评论