containerd学习

  • 参考文档

    https://github.com/containerd/containerd/blob/master/README.md
    https://github.com/opencontainers/runc/blob/master/README.md
    https://github.com/opencontainers/runtime-spec/blob/master/config.md
    https://godoc.org/github.com/containerd/containerd/cmd/ctr/commands

    containerd.png

  • 编译containerd/runc

    1. 在云厂商购买一台虚拟机,绑fip(116.196.115.229)
    2. 登录vm安装go,并设置GOROOT/GOPATH环境变量
      [root@containerd bin]# wget https://dl.google.com/go/go1.12.5.linux-amd64.tar.gz
      [root@containerd bin]# tar -xvf go1.12.5.linux-amd64.tar.gz -C /usr/local
      [root@containerd bin]# /usr/local/go/bin/go version
      go version go1.12.5 linux/amd64
      编辑/root/.bash_profile文件,添加如下设置后sourcePATH=$PATH:/usr/local/go/bin
      export PATH
      export GOROOT=/usr/local/go/
      export GOPATH=/root/go[root@containerd ~]# source .bash_profile
    3. Build the development environment
      [root@containerd ~]# go get github.com/containerd/containerd
      [root@containerd ~]# wget -c https://github.com/google/protobuf/releases/download/v3.5.0/protoc-3.5.0-linux-x86_64.zip
      [root@containerd ~]# unzip protoc-3.5.0-linux-x86_64.zip -d /usr/local
      [root@containerd ~]# yum install -y btrfs-progs-devel libseccomp-devel
    4. 编译runc
      [root@containerd ~]# go get github.com/opencontainers/runc[root@containerd containerd]# cd $GOPATH/src/github.com/opencontainers/runc
      [root@containerd runc]# make
      go build -buildmode=pie -ldflags "-X main.gitCommit="eb4aeed24ffbf8e2d740fafea39d91faa0ee84d0" -X main.version=1.0.0-rc8+dev " -tags "seccomp" -o runc .
      [root@containerd runc]# make install
      install -D -m0755 runc /usr/local/sbin/runc [root@containerd runc]# ls -rtl /usr/local/sbin
      total 11752
      -rwxr-xr-x 1 root root 12031832 May 10 17:33 runc
      [root@containerd runc]# runc --version
      runc version 1.0.0-rc8+dev
      commit: eb4aeed24ffbf8e2d740fafea39d91faa0ee84d0
      spec: 1.0.1-dev
    5. 编译containerd
      [root@containerd runc]# cd $GOPATH/src/github.com/containerd/containerd
      [root@containerd containerd]# make
      +bin/ctr
      +bin/containerd
      +bin/containerd-stress
      +bin/containerd-shim
      +bin/containerd-shim-runc-v1
      +bin/containerd-shim-runc-v2
      +binaries
      [root@containerd containerd]# make install
      +install bin/ctr bin/containerd bin/containerd-stress bin/containerd-shim bin/containerd-shim-runc-v1 bin/containerd-shim-runc-v2
      [root@containerd containerd]# ls -rtl /usr/local/bin
      total 133152
      -rwxr-x--- 1 root root 4433736 Nov 14 2017 protoc
      -rwxr-xr-x 1 root root 29709728 May 10 17:35 ctr
      -rwxr-xr-x 1 root root 51831136 May 10 17:35 containerd
      -rwxr-xr-x 1 root root 25094688 May 10 17:35 containerd-stress
      -rwxr-xr-x 1 root root 7302016 May 10 17:35 containerd-shim
      -rwxr-xr-x 1 root root 8980256 May 10 17:35 containerd-shim-runc-v1
      -rwxr-xr-x 1 root root 8980320 May 10 17:35 containerd-shim-runc-v2
      [root@containerd containerd]# containerd --version
      containerd github.com/containerd/containerd v1.2.0-551-g57fbb16 57fbb16234fa6c8a61e5e907a4148ea3b05bce1d
    6. containerd以daemon方式运行
      a.准备containered.service文件
      [root@containerd ~]# cat /usr/lib/systemd/system/containerd.service
      [Unit]
      Description=containerd container runtime
      Documentation=https://containerd.io
      After=network.target
      [Service]
      ExecStartPre=-/sbin/modprobe overlay
      ExecStart=/usr/local/bin/containerd
      Delegate=yes
      KillMode=process
      LimitNPROC=infinity
      LimitCORE=infinity
      LimitNOFILE=1048576
      [Install]
      WantedBy=multi-user.target
      [Install]
      WantedBy=multi-user.target
      b.enable设置开机自启动
      [root@containerd system]# systemctl enable containerd.service
      Created symlink from /etc/systemd/system/multi-user.target.wants/containerd.service to /usr/lib/systemd/system/containerd.service.
      c.准备containerd配置文件
      [root@containerd ~]# cat /etc/containerd/config.toml
      subreaper = true
      oom_score = -999
      [debug]
      level = "debug"
      [metrics]
      address = "127.0.0.1:1338"
      [plugins.linux]
      runtime = "runc"
      shim_debug = true
      d.启动containerd服务
      [root@containerd system]# systemctl start containerd.service
  • 准备rootfs和spec

    找另外一台安装了整套docker的机器(此处用本地oracle linux vm 10.12.162.67),制作测试所需的rootfs(bundle)文件
    1.创建 busybox/rootfs目录
    [root@localhost ~]# mkdir -p busybox/rootfs
    2.拉取busybox镜像
    [root@localhost ~]# docker pull busybox
    Using default tag: latest
    latest: Pulling from library/busybox
    53071b97a884: Pull complete
    Digest: sha256:32f65f5aae307c171fc69ce52be3c8b09675164a610a88efa607449311186378
    Status: Downloaded newer image for busybox:latest
    3.创建docker
    [root@localhost ~]# docker create --name tempbusybox busybox
    85b6e32db75da001669656b452a9a65fc2de7f1a9faac95c5aedf6de1127fa15
    4.导出rootfs和spec
    [root@localhost ~]# docker export tempbusybox | tar -C busybox/rootfs -xf -
    [root@localhost ~]# cd busybox/ && /usr/bin/docker-runc spec
    [root@localhost busybox]# ls
    config.json rootfs
    [root@localhost busybox]# ls rootfs/
    bin dev etc home proc root sys tmp usr var
    5.将rootfs和spec 拷贝到containered测试节点 116.196.115.229
    [root@localhost ~]# scp -r busybox/ root@116.196.115.229:/root

  • 测试

    1.[root@containerd busybox]# cd /root/busybox
    2.利用containerd cli (/usr/local/bin/ctr)运行容器
    [root@containerd busybox]# ctr run -t -d --rootfs rootfs busybox /bin/sh
    [root@containerd busybox]# ctr container list
    CONTAINER IMAGE RUNTIME
    busybox - io.containerd.runtime.v1.linux
    [root@containerd busybox]# ps -ef |grep container
    root 9539 1 0 21:22 ? 00:00:03 /usr/local/bin/containerd
    root 15285 9539 0 22:02 ? 00:00:00 containerd-shim -namespace default -workdir /var/lib/containerd/io.containerd.runtime.v1.linux/default/busybox -address /run/containerd/containerd.sock -containerd-binary /usr/local/bin/containerd -debug
    3.登录容器验证
    [root@containerd busybox]# ctr tasks list
    TASK PID STATUS
    busybox 15301 RUNNING
    [root@containerd busybox]# ctr tasks exec -t --exec-id 15301 busybox /bin/sh
    / # hostname
    containerd
    / # ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
    valid_lft forever preferred_lft forever
    4.stop容器
    [root@containerd busybox]# ctr tasks kill --signal 9 busybox
    [root@containerd busybox]# ctr tasks list
    TASK PID STATUS
    busybox 15301 STOPPED
    5.删除容器
    [root@containerd busybox]# ctr container delete busybox
    [root@containerd busybox]# ctr container list
    CONTAINER IMAGE RUNTIME

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,825评论 4 377
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,887评论 2 308
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,425评论 0 255
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,801评论 0 224
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,252评论 3 299
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,089评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,216评论 2 322
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 31,005评论 0 215
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,747评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,883评论 2 255
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,354评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,694评论 3 265
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,406评论 3 246
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,222评论 0 9
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,996评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,242评论 2 287
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 36,017评论 2 281