再见Docker!Containerd安装与使用

打印 上一主题 下一主题

主题 893|帖子 893|积分 2679

Containerd 的技术方向和目标


  • 简洁的基于 gRPC 的 API 和 client library
  • 完整的 OCI 支持(runtime 和 image spec)
  • 同时具备稳定性和高性能的定义良好的容器核心功能
  • 一个解耦的系统(让 image、filesystem、runtime 解耦合),实现插件式的扩展和重用
 
  为什么需要独立的 containerd:

  • 以往隶属于docker项目中,现如今从整体 docker 引擎中分离出的项目(开源项目的思路)
  • 可以被 Kubernets CRI 等项目使用(通用化)
  • 为广泛的行业合作打下基础(就像 runC 一样)
 
  containerd的架构设计图:

 
 
安装containerd

       验证仓库版本:
  1. root@containerd:~# apt-cache madison containerd
复制代码

  ubuntu在线仓库版本不是最新,可以使用github仓库中的新版本,使用二进制方式部署
下载二进制安装包
       github链接地址:https://github.com/containerd/containerd/releases
      
       选择64位x86架构系统安装包


   上传安装包到服务器并开始解压安装


 
  解压缩并将containerd执行文件放入系统默认命令路径下
  1. root@containerd:/tools# tar xf containerd-1.6.6-linux-amd64.tar.gz
  2. root@containerd:/tools# cp -r bin/* /usr/local/bin/
复制代码


 创建containerd systemd service启动管理文件:
      修改ExecStart=/usr/local/bin/containerd为当前containerd文件路径
  1. root@containerd:/tools# cd /etc/systemd/system/
  2. root@containerd:/etc/systemd/system# cat containerd.service
  3. # Copyright The containerd Authors.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. #     http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. [Unit]
  17. Description=containerd container runtime
  18. Documentation=https://containerd.io
  19. After=network.target local-fs.target
  20. [Service]
  21. ExecStartPre=-/sbin/modprobe overlay
  22. ExecStart=/usr/local/bin/containerd
  23. Type=notify
  24. Delegate=yes
  25. KillMode=process
  26. Restart=always
  27. RestartSec=5
  28. # Having non-zero Limit*s causes performance problems due to accounting overhead
  29. # in the kernel. We recommend using cgroups to do container-local accounting.
  30. LimitNPROC=infinity
  31. LimitCORE=infinity
  32. LimitNOFILE=infinity
  33. # Comment TasksMax if your systemd version does not supports it.
  34. # Only systemd 226 and above support this version.
  35. TasksMax=infinity
  36. OOMScoreAdjust=-999
  37. [Install]
  38. WantedBy=multi-user.target
复制代码
 
  重新加载系统管理服务文件
  1. root@containerd:/etc/systemd/system# systemctl daemon-reload
复制代码
 
  创建配置文件
  1. root@containerd:/etc/systemd/system# mkdir /etc/containerd
复制代码
  
  生成模板配置文件
  1. root@containerd:/etc/systemd/system# containerd config default > /etc/containerd/config.toml
复制代码


 
  修改配置文件
  1. root@containerd:/etc/systemd/system# cd /etc/containerd/
  2. root@containerd:/etc/containerd# vim config.toml
复制代码
  vim下搜索/mirrors,添加镜像加速,使用docker镜像源即可,上下级配置,缩进两个空格。
  1.    [plugins."io.containerd.grpc.v1.cri".registry.mirrors]
  2.         [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
  3.           endpoint = ["https://dxc7f1d6.mirror.aliyuncs.com"]
复制代码
  
  如果是从docker.io下载进行,则使用endpoint配置的镜像站点加速下载



  启动containerd并设置开机自启动
  1. root@containerd:/etc/containerd# systemctl enable containerd --now
复制代码




 
 

安装runc

  github下载链接:https://github.com/opencontainers/runc/releases

 
  下载最新版本


 
       上传到服务器

 
  1. root@containerd:/tools# chmod +x runc.amd64
  2. root@containerd:/tools# cp runc.amd64 /usr/local/bin/runc
复制代码
 
 
验证使用containerd
  containerd是ctrl工具在服务器上创建、管理和使用容器
  1. root@containerd:~# ctr --help
  2. NAME:
  3.    ctr -
  4.         __
  5.   _____/ /______
  6. / ___/ __/ ___/
  7. / /__/ /_/ /
  8. \___/\__/_/
  9. containerd CLI
  10. USAGE:
  11.    ctr [global options] command [command options] [arguments...]
  12. VERSION:
  13.    v1.6.6
  14. DESCRIPTION:
  15.    
  16. ctr is an unsupported debug and administrative client for interacting
  17. with the containerd daemon. Because it is unsupported, the commands,
  18. options, and operations are not guaranteed to be backward compatible or
  19. stable from release to release of the containerd project.
  20. COMMANDS:
  21.    plugins, plugin            provides information about containerd plugins
  22.    version                    print the client and server versions
  23.    containers, c, container   manage containers
  24.    content                    manage content
  25.    events, event              display containerd events
  26.    images, image, i           manage images
  27.    leases                     manage leases
  28.    namespaces, namespace, ns  manage namespaces
  29.    pprof                      provide golang pprof outputs for containerd
  30.    run                        run a container
  31.    snapshots, snapshot        manage snapshots
  32.    tasks, t, task             manage tasks
  33.    install                    install a new package
  34.    oci                        OCI tools
  35.    shim                       interact with a shim directly
  36.    help, h                    Shows a list of commands or help for one command
  37. GLOBAL OPTIONS:
  38.    --debug                      enable debug output in logs
  39.    --address value, -a value    address for containerd's GRPC server (default: "/run/containerd/containerd.sock") [$CONTAINERD_ADDRESS]
  40.    --timeout value              total timeout for ctr commands (default: 0s)
  41.    --connect-timeout value      timeout for connecting to containerd (default: 0s)
  42.    --namespace value, -n value  namespace to use with commands (default: "default") [$CONTAINERD_NAMESPACE]
  43.    --help, -h                   show help
  44.    --version, -v                print the version
复制代码
  
  拉取镜像
         与docker区别在于拉取官方镜像必须指定镜像的完整名称包括镜像仓库地址
  1. root@containerd:~# ctr images pull docker.io/library/nginx:latest
复制代码

  
查看本地的镜像
  1. root@containerd:~# ctr images ls
复制代码

 
  运行容器
  1. root@containerd:~# ctr run -t  docker.io/library/nginx:latest container1 bash
复制代码


 

container客户端工具

  客户端工具有两种,分别是crictl和nerdctl
       推荐使用nerdctl,使用效果与docker命令的语法一致
       github下载链接:https://github.com/containerd/nerdctl/releases
  下载安装nerdctl


  
  解压安装nerdctl
  
 
  
  拷贝nerdctl到系统二进制命令路径下
  1. root@containerd:/tools# cp nerdctl /usr/local/bin/
复制代码
 
  
  验证版本


  
  
  查看nerdctl使用帮助,与docker客户端工具使用方法基本一致
  1. root@containerd:~# nerdctl --help
  2. nerdctl is a command line interface for containerd
  3. Config file ($NERDCTL_TOML): /etc/nerdctl/nerdctl.toml
  4. Usage:
  5.   nerdctl [flags]
  6.   nerdctl [command]
  7. Management commands:
  8.   apparmor    Manage AppArmor profiles
  9.   builder     Manage builds
  10.   container   Manage containers
  11.   image       Manage images
  12.   ipfs        Distributing images on IPFS
  13.   namespace   Manage containerd namespaces
  14.   network     Manage networks
  15.   system      Manage containerd
  16.   volume      Manage volumes
  17. Commands:
  18.   build       Build an image from a Dockerfile. Needs buildkitd to be running.
  19.   commit      Create a new image from a container's changes
  20.   completion  Generate the autocompletion script for the specified shell
  21.   compose     Compose
  22.   cp          Copy files/folders between a running container and the local filesystem.
  23.   create      Create a new container. Optionally specify "ipfs://" or "ipns://" scheme to pull image from IPFS.
  24.   events      Get real time events from the server
  25.   exec        Run a command in a running container
  26.   help        Help about any command
  27.   history     Show the history of an image
  28.   images      List images
  29.   info        Display system-wide information
  30.   inspect     Return low-level information on objects.
  31.   kill        Kill one or more running containers
  32.   load        Load an image from a tar archive or STDIN
  33.   login       Log in to a Docker registry
  34.   logout      Log out from a Docker registry
  35.   logs        Fetch the logs of a container. Currently, only containers created with `nerdctl run -d` are supported.
  36.   pause       Pause all processes within one or more containers
  37.   port        List port mappings or a specific mapping for the container
  38.   ps          List containers
  39.   pull        Pull an image from a registry. Optionally specify "ipfs://" or "ipns://" scheme to pull image from IPFS.
  40.   push        Push an image or a repository to a registry. Optionally specify "ipfs://" or "ipns://" scheme to push image to IPFS.
  41.   rename      rename a container
  42.   restart     Restart one or more running containers
  43.   rm          Remove one or more containers
  44.   rmi         Remove one or more images
  45.   run         Run a command in a new container. Optionally specify "ipfs://" or "ipns://" scheme to pull image from IPFS.
  46.   save        Save one or more images to a tar archive (streamed to STDOUT by default)
  47.   start       Start one or more running containers
  48.   stats       Display a live stream of container(s) resource usage statistics.
  49.   stop        Stop one or more running containers
  50.   tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  51.   top         Display the running processes of a container
  52.   unpause     Unpause all processes within one or more containers
  53.   update      Update one or more running containers
  54.   version     Show the nerdctl version information
  55.   wait        Block until one or more containers stop, then print their exit codes.
  56. Flags:
  57.   -H, --H string                 Alias of --address (default "/run/containerd/containerd.sock")
  58.   -a, --a string                 Alias of --address (default "/run/containerd/containerd.sock")
  59.       --address string           containerd address, optionally with "unix://" prefix [$CONTAINERD_ADDRESS] (default "/run/containerd/containerd.sock")
  60.       --cgroup-manager string    Cgroup manager to use ("cgroupfs"|"systemd") (default "cgroupfs")
  61.       --cni-netconfpath string   cni config directory [$NETCONFPATH] (default "/etc/cni/net.d")
  62.       --cni-path string          cni plugins binary directory [$CNI_PATH] (default "/opt/cni/bin")
  63.       --data-root string         Root directory of persistent nerdctl state (managed by nerdctl, not by containerd) (default "/var/lib/nerdctl")
  64.       --debug                    debug mode
  65.       --debug-full               debug mode (with full output)
  66.   -h, --help                     help for nerdctl
  67.       --host string              Alias of --address (default "/run/containerd/containerd.sock")
  68.       --hosts-dir strings        A directory that contains <HOST:PORT>/hosts.toml (containerd style) or <HOST:PORT>/{ca.cert, cert.pem, key.pem} (docker style) (default [/etc/containerd/certs.d,/etc/docker/certs.d])
  69.       --insecure-registry        skips verifying HTTPS certs, and allows falling back to plain HTTP
  70.   -n, --n string                 Alias of --namespace (default "default")
  71.       --namespace string         containerd namespace, such as "moby" for Docker, "k8s.io" for Kubernetes [$CONTAINERD_NAMESPACE] (default "default")
  72.       --snapshotter string       containerd snapshotter [$CONTAINERD_SNAPSHOTTER] (default "overlayfs")
  73.       --storage-driver string    Alias of --snapshotter (default "overlayfs")
  74.   -v, --version                  version for nerdctl
  75. Use "nerdctl [command] --help" for more information about a command.
复制代码
 
  
  查看镜像、容器:
  
 
  拉取镜像:

 

安装cni网络插件


  CNI:Container network interface容器网络接口,为容器分配ip地址网卡等
       github链接: 
  1. https://github.com/containernetworking/plugins/releases
复制代码
  
 
  
  下载安装cni,并解压到/usr/local/cni/bin目录下
  1. root@containerd:/tools# mkdir /opt/cni/bin -p       
  2. root@containerd:/tools# tar xf cni-plugins-linux-amd64-v1.1.1.tgz -C /opt/cni/bin/
复制代码
 
  
  查看解压后的cni插件文件:
  

  
  注意:必须将cni解压到/opt/cni/bin,否则nerdctl为容器映射端口时,会出现找不到cni插件的报错
  1. root@containerd:~# nerdctl run -d -p 80:80 --name=web --restart=always nginx:latest
  2. FATA[0000] needs CNI plugin "bridge" to be installed in CNI_PATH ("/opt/cni/bin"), see https://github.com/con stat /opt/cni/bin/bridge: no such file or directory
复制代码


  
  
  验证:使用nerdctl运行一个容器

 
  
  宿主机访问容器映射到宿主机80端口

  

  
  以上就是关于container的介绍与安装。如果对你有帮助或有建议疑问可以评论区留言!


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

络腮胡菲菲

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表