本文是 Kubernetes api-server源码阅读 系列第一篇,主要讲述如何进行kubernetes源码阅读环境的搭建
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.6 LTS
Release: 20.04
Codename: focal
cp /etc/apt/sources.list /etc/apt/sources.list.bak
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
sudo apt-get update
sudo apt install build-essential
sudo apt-get update
sudo apt-get install \
ca-certificates \
curl \
gnupg \
lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
containerd config default > ~/config.toml
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
endpoint = ["https://hub-mirror.c.163.com","https://registry-1.docker.io"]
[plugins."io.containerd.grpc.v1.cri".image_decryption]
key_model = "node"
[plugins."io.containerd.grpc.v1.cri".registry]
config_path = ""
[plugins."io.containerd.grpc.v1.cri".registry.auths]
[plugins."io.containerd.grpc.v1.cri".registry.configs]
[plugins."io.containerd.grpc.v1.cri".registry.headers]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
endpoint = ["https://hub-mirror.c.163.com","https://registry-1.docker.io"]
[plugins."io.containerd.grpc.v1.cri".x509_key_pair_streaming]
tls_cert_file = ""
tls_key_file = ""
registry.aliyuncs.com/google_containers/pause:3.6
registry.k8s.io/pause:3.6
我们拉取不到,创建的pod会报错 拉取pause镜像失败,一直处于 ContainerCreating 状态。sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.6"
mv ~/config.toml /etc/containerd/config.toml
systemctl restart containerd
systemctl containerd status
cd ~/Downloads 或者 mkdir ~/Downloads
wget https://github.com/WayneD/rsync/archive/refs/tags/v3.2.4.tar.gz
tar -xf v3.2.4.tar.gz
cd rsync-3.2.4
sudo apt install -y gcc g++ gawk autoconf automake python3-cmarkgfm
sudo apt install -y acl libacl1-dev
sudo apt install -y attr libattr1-dev
sudo apt install -y libxxhash-dev
sudo apt install -y libzstd-dev
sudo apt install -y liblz4-dev
sudo apt install -y libssl-dev
./configure
make
sudo cp ./rsync /usr/local/bin/
sudo cp ./rsync-ssl /usr/local/bin/
sudo apt-get install jq
sudo apt install python3-pip
pip install pyyaml
cd ~/Downloads
ETCD_VER=v3.5.4
curl -L https://storage.googleapis.com/etcd/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz -o etcd-${ETCD_VER}-linux-amd64.tar.gz
mkdir ~/etcd
tar xzvf etcd-${ETCD_VER}-linux-amd64.tar.gz -C ~/etcd --strip-components=1
vim ~/.bashrc
# 在~/.bashrc最后添加一句:export PATH="/root/etcd:${PATH}"
source ~/.bashrc
# 检查etcd是否安装成功
etcd --version
cd ~/Downloads
wget https://golang.google.cn/dl/go1.18.2.linux-amd64.tar.gz
sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go1.18.2.linux-amd64.tar.gz
# 创建gopath
mkdir ~/go
mkdir ~/go/src
mkdir ~/go/bin
vim ~/.bashrc
# 在文件最后添加环境变量
export GOPATH="/root/go"
export GOBIN="/root/go/bin"
export PATH="/usr/local/go/bin:$GOPATH/bin:${PATH}"
source ~/.bashrc
vim /etc/sudoers
在secure_path中添加::/usr/local/go/bin:/root/etcd:/root/go/bin
go env -w GO111MODULE="on"
go env -w GOPROXY="https://goproxy.cn,direct"
go install github.com/cloudflare/cfssl/cmd/...@latest
root@graham-virtual-machine:~/go# cfssl
No command is given.
Usage:
Available commands:
bundle
certinfo
ocsprefresh
scan
info
revoke
version
gencrl
ocspdump
print-defaults
crl
sign
serve
genkey
gencert
gencsr
ocspsign
ocspserve
selfsign
Top-level flags:
mkdir $GOPATH/src/k8s.io && cd $GOPATH/src/k8s.io
git clone https://github.com/kubernetes/kubernetes.git
# 从 tag:v1.24.0 中,切出来一个分支 kube1.24,用于我们的学习
git checkout -b kube1.24 v1.24.0
cd $GOPATH/src/k8s.io/kubernetes
sudo ./hack/local-up-cluster.sh
./hack/local-up-cluster.sh
Logs:
/tmp/kube-apiserver.log
/tmp/kube-controller-manager.log
/tmp/kube-proxy.log
/tmp/kube-scheduler.log
/tmp/kubelet.log
export KUBECONFIG=/var/run/kubernetes/admin.kubeconfig
是在指定kubeconfig文件cluster/kubectl.sh
就是kubectl命令的位置,我们使用它就是在使用kubectlTo start using your cluster, you can open up another terminal/tab and run:
export KUBECONFIG=/var/run/kubernetes/admin.kubeconfig
cluster/kubectl.sh
Alternatively, you can write to the default kubeconfig:
export KUBERNETES_PROVIDER=local
cluster/kubectl.sh config set-cluster local --server=https://localhost:6443 --certificate-authority=/var/run/kubernetes/server-ca.crt
cluster/kubectl.sh config set-credentials myself --client-key=/var/run/kubernetes/client-admin.key --client-certificate=/var/run/kubernetes/client-admin.crt
cluster/kubectl.sh config set-context local --cluster=local --user=myself
cluster/kubectl.sh config use-context local
cluster/kubectl.sh
export KUBECONFIG=/var/run/kubernetes/admin.kubeconfig
root@graham-virtual-machine:~/go/src/k8s.io/kubernetes# cluster/kubectl.sh get nodes
NAME STATUS ROLES AGE VERSION
127.0.0.1 Ready <none> 119m v1.24.0
make WHAT="cmd/kube-apiserver"
gogcflags="all=-trimpath=${trimroot} ${GOGCFLAGS:-}"
if [[ "${DBG:-}" == 1 ]]; then
# Debugging - disable optimizations and inlining.
gogcflags="${gogcflags} -N -l"
fi
goldflags="all=$(kube::version::ldflags) ${GOLDFLAGS:-}"
if [[ "${DBG:-}" != 1 ]]; then
# Not debugging - disable symbols and DWARF.
goldflags="${goldflags} -s -w"
fi
gogcflags="all=-trimpath=${trimroot} ${GOGCFLAGS:-}"
# if [[ "${DBG:-}" == 1 ]]; then
# # Debugging - disable optimizations and inlining.
# gogcflags="${gogcflags} -N -l"
# fi
gogcflags="${gogcflags} -N -l"
goldflags="all=$(kube::version::ldflags) ${GOLDFLAGS:-}"
# if [[ "${DBG:-}" != 1 ]]; then
# # Not debugging - disable symbols and DWARF.
# goldflags="${goldflags} -s -w"
# fi
sudo ./hack/local-up-cluster.sh
,不会再去编译生成可执行文件,会使用上次的编译结果。make clean
清理掉之前的编译结果,然后再执行 sudo ./hack/local-up-cluster.sh
才会生成新的可执行文件。