Use the Docker container to source compile etcd

  • 2020-06-07 05:54:13
  • OfStack

The & # 65279; background

etcd is a distributed key-value pair repository developed by CoreOS. In Kubernetes, we need to use etcd as the persistent store for all REST API objects.

Unfortunately, in github's release, CoreOS puts all of etcd's binary executables on Amazon's S3 storage, which is very slow to access domestically. Therefore, we can only compile etcd through source code.

process

1. Download etcd source code.


$ git clone https://github.com/coreos/etcd.git
$ cd etcd

2. Select the appropriate version according to the actual situation. For example, I used version 2.2.1 here.


$ git checkout -b v2.2.1

3. Compile source code. Run the build executable for this directory.


$ ./build

However, an error occurred running the command, prompting that context could not be found.


$ ./build
gopath/src/github.com/coreos/etcd/cmd/vendor/github.com/coreos/etcd/lease/leasehttp/http.go:19:2: cannot find package "context" in any of:
  /root/etcd/gopath/src/github.com/coreos/etcd/cmd/vendor/context (vendor tree)
  /usr/local/go/src/context (from $GOROOT)
  /root/etcd/gopath/src/context (from $GOPATH)
  /go/src/context
  /go/src/app/_gopath/src/context

After a search on the Internet, etcd can only be compiled in the environment above Golang1.7, but I used Ubuntu16.04. The default Golang version is 1.6, without context package. Therefore, we need to install the Golang1.7 environment.

4. Compile with the Docker container. Because my Golang environment is running other packages as well, in order not to interfere with other packages running. Here I compile etcd directly using the docker container of Golang1.7.


$ sudo docker run -v /home/newbee/etcd/:/opt/etcd -it --rm golang:1.7.5 bash
# cd /opt/etcd
# ./build

In the -v parameter, /home/newbee/etcd is my etcd directory in Docker host (that is, my server's), and /opt/etcd is the mapped directory in my container.

Note: It takes a fixed time to download the golang image from Docker hub. Once the download is complete, you enter the container, and then cd into the /opt/etcd directory to compile.
After compiling, check etcd's bin directory to see if there is an executable file, if so, the compilation is successful. Then exit the container, and the server has its own executable in the etcd directory.

conclusion

etcd needs to compile above Golang1.7.

Docker container can easily provide Golang environment without affecting the original environment, which is a new idea.


Related articles: