【转载】轻量级Kubernetes之k3s:16:源码编译k3s

  • Post author:
  • Post category:其他


轻量级Kubernetes之k3s:16:源码编译k3s

原创liumiaocn 最后发布于2019-12-06 20:53:15 阅读数 63  收藏

展开

这篇文章介绍一下源码编译k3s的方法。

事前准备

go语言环境

go语言版本需要go 1.12,本文示例使用1.13.5[root@liumiaocn ~]# go version

go version go1.13.5 linux/amd64

[root@liumiaocn ~]# ,安装步骤如下所示

步骤1: 下载go语言安装包

执行命令:wget https://dl.google.com/go/go1.13.5.linux-amd64.tar.gz

步骤2: 设定环境变量

在/etc/profile中设定GOROOT和PATH,详细如下所示

[root@liumiaocn ~]# tail -n2 /etc/profile

export GOROOT=/usr/local/go

export PATH=$PATH:${GOROOT}/bin

[root@liumiaocn ~]#

1

2

3

4

步骤3:确认版本

重新登录之后即可执行go version确认版本

[root@liumiaocn ~]# go version

go version go1.13.5 linux/amd64

[root@liumiaocn ~]#

1

2

3

安装gcc

安装命令:yum install gcc

安装libseccomp开发包

CentOS/RHEL下: yum install libseccomp-devel

Ubuntu/Debian下: apt-get install libseccomp-dev

下载k3s源码包

[root@liumiaocn ~]# git clone http://github.com/rancher/k3s.git

Cloning into ‘k3s’…

remote: Enumerating objects: 37, done.

remote: Counting objects: 100% (37/37), done.

remote: Compressing objects: 100% (29/29), done.

remote: Total 926826 (delta 10), reused 18 (delta 5), pack-reused 926789

Receiving objects: 100% (926826/926826), 518.39 MiB | 22.51 MiB/s, done.

Resolving deltas: 100% (676821/676821), done.

Checking out files: 100% (12503/12503), done.

[root@liumiaocn ~]#

1

2

3

4

5

6

7

8

9

10

或者使用如下命令下载源码1.0.0的源码压缩包

wget https://github.com/rancher/k3s/archive/v1.0.0.tar.gz

编译k3s

[root@liumiaocn ~]# cd k3s

[root@liumiaocn k3s]# ls

BUILDING.md                 LICENSE    Vagrantfile  docker-compose.yml  go.sum       main.go    pkg      vendor

Dockerfile.dapper           Makefile   cmd          e2e                 install.sh   manifests  scripts  vendor.go

Dockerfile.sonobuoy.dapper  README.md  contrib      go.mod              k3s.service  package    tests

[root@liumiaocn k3s]# go build -o k3s

go: downloading github.com/sirupsen/logrus v1.4.2

go: downloading github.com/rancher/kubernetes/staging/src/k8s.io/client-go v1.16.3-k3s.2

go: downloading github.com/pkg/errors v0.8.1

go: downloading github.com/rancher/kubernetes/staging/src/k8s.io/apimachinery v1.16.3-k3s.2

go: downloading github.com/rootless-containers/rootlesskit v0.6.0

…省略

[root@liumiaocn k3s]# ls

bin          cmd                 Dockerfile.dapper           etc     install.sh   LICENSE   manifests  README.md  Vagrantfile

build        contrib             Dockerfile.sonobuoy.dapper  go.mod  k3s          main.go   package    scripts    vendor

BUILDING.md  docker-compose.yml  e2e                         go.sum  k3s.service  Makefile  pkg        tests      vendor.go

[root@liumiaocn k3s]# ./k3s –version

k3s version dev (HEAD)

[root@liumiaocn k3s]#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

注:如果希望构建完整版本的二进制文件,直接执行make即可。

常见错误

缺少libseccomp开发库

Package libseccomp was not found in the pkg-config search path.

Perhaps you should add the directory containing `libseccomp.pc’

to the PKG_CONFIG_PATH environment variable

No package ‘libseccomp’ found

1

2

3

4

CentOS/RHEL下: yum install libseccomp-devel

Ubuntu/Debian下: apt-get install libseccomp-dev

make构建完整版本二进制文件时需要docker启动状态

[root@liumiaocn k3s]# make

./.dapper ci

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

FATA[0005] exit status 1

make: *** [ci] Error 1

[root@liumiaocn k3s]# systemctl start docker

[root@liumiaocn k3s]#

1

2

3

4

5

6

7

内存不够

很多显示为signal killed的错误可能都是因为可用内存过少导致的,可考虑增大可用内存再试

/usr/local/go/pkg/tool/linux_amd64/link: signal: killed

1

内存实在不足时,也可以考虑增加swap来应对,只是速度会慢一些,比如使用如下的方式增加1G的swap

[root@liumiaocn ~]# swapon

NAME  TYPE SIZE USED PRIO

/swap file 132M   0B   -1

[root@liumiaocn ~]#

[root@liumiaocn ~]# dd if=/dev/zero of=/root/swapfile bs=1M count=1024

1024+0 records in

1024+0 records out

1073741824 bytes (1.1 GB) copied, 2.52969 s, 424 MB/s

[root@liumiaocn ~]#

[root@liumiaocn ~]# mkswap /root/swapfile

Setting up swapspace version 1, size = 1048572 KiB

no label, UUID=feb9aea6-2e80-4473-ae58-56c18d49699b

[root@liumiaocn ~]#

[root@liumiaocn ~]# swapon /root/swapfile

swapon: /root/swapfile: insecure permissions 0644, 0600 suggested.

[root@liumiaocn ~]#

[root@liumiaocn ~]# vi /etc/fstab

[root@liumiaocn ~]#

[root@liumiaocn ~]# grep swapfile /etc/fstab

/root/swapfile swap swap defaults 0 0

[root@liumiaocn ~]#

[root@liumiaocn ~]# swapon

NAME           TYPE  SIZE USED PRIO

/swap          file  132M   0B   -1

/root/swapfile file 1024M   0B   -2

[root@liumiaocn ~]#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

参考内容

https://docs.rancher.cn/k3s/building.html#_1-kubernetes-source

————————————————

版权声明:本文为CSDN博主「liumiaocn」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/liumiaocn/article/details/103415814