本文参考视频:https://www.bilibili.com/video/BV1WK41137JA?from=search&seid=10758236638727752345

0、一定要用root用户

sudo su

1、配置两个环境变量

export GOROOT=/usr/local/go
export GOPATH=/home/zhouyy/go

2、下载k8s源码(kubernetes文件夹)

将kubernetes文件夹放在GOPATH/src/k8s.io中。即源码目录为:GOPATH/src/k8s.io/kubernetes

3、编译

①全部编译

KUBE_BUILD_PLATFORMS=linux/amd64 make all GOFLAGS=-v GOGCFLAGS="-N -l"

编译的正确输出就是一堆目录,都是k8s.io/kubernetes/开头的。

编译完成后,在源码根目录会生成一个_output文件夹。

②单独编译一个组件

例如,如下命令是单独编译kubelet的:

KUBE_BUILD_PLATFORMS=linux/amd64 make all WHAT=cmd/kubelet GOFLAGS=-v GOGCFLAGS="-N -l"

编译的正确输出也是一堆目录,也都是k8s.io/kubernetes/开头的,但大多和kubelet有关。

4、运行

①安装etcd

cd 源码目录(e.g. /home/zhouyy/go/src/k8s.io/kubernetes)
./hack/install-etcd.sh

正确输出如下:

Downloading https://github.com/coreos/etcd/releases/download/v3.4.13/etcd-v3.4.13-linux-amd64.tar.gz succeed
etcd v3.4.13 installed. To use:
export PATH="/home/zhouyy/go/src/k8s.io/kubernetes/third_party/etcd:${PATH}"

②配置etcd

export PATH="/home/zhouyy/go/src/k8s.io/kubernetes/third_party/etcd:${PATH}"

③本地运行k8s

./hack/local-up-cluster.sh -O

正确输出如下:

skipped the build.
Kubelet cgroup driver defaulted to use: cgroupfs
API SERVER secure port is free, proceeding...
Detected host and ready to start services.  Doing some housekeeping first...
Using GO_OUT /home/zhouyy/go/src/k8s.io/kubernetes/_output/bin
Starting services now!
Starting etcd
etcd --advertise-client-urls http://127.0.0.1:2379 --data-dir /tmp/tmp.jnCu80or9Q --listen-client-urls http://127.0.0.1:2379 --log-level=debug > "/tmp/etcd.log" 2>/dev/null
Waiting for etcd to come up.
+++ [0525 21:28:32] On try 2, etcd: : {"health":"true"}
{"header":{"cluster_id":"14841639068965178418","member_id":"10276657743932975437","revision":"2","raft_term":"2"}}Generating a 2048 bit RSA private key
..................................................+++
.........................................................+++
writing new private key to '/var/run/kubernetes/server-ca.key'
-----
Generating a 2048 bit RSA private key
...................+++
...........................+++
writing new private key to '/var/run/kubernetes/client-ca.key'
-----
Generating a 2048 bit RSA private key
.................................................................+++
...............+++
writing new private key to '/var/run/kubernetes/request-header-ca.key'
-----
2021/05/25 21:28:33 [INFO] generate received request
2021/05/25 21:28:33 [INFO] received CSR
2021/05/25 21:28:33 [INFO] generating key: rsa-2048
2021/05/25 21:28:34 [INFO] encoded CSR
2021/05/25 21:28:34 [INFO] signed certificate with serial number 488965894178000384017857859773876165408907751129
2021/05/25 21:28:34 [WARNING] This certificate lacks a "hosts" field. This makes it unsuitable for
websites. For more information see the Baseline Requirements for the Issuance and Management
of Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser Forum (https://cabforum.org);
specifically, section 10.2.3 ("Information Requirements").
2021/05/25 21:28:34 [INFO] generate received request
2021/05/25 21:28:34 [INFO] received CSR
2021/05/25 21:28:34 [INFO] generating key: rsa-2048
2021/05/25 21:28:34 [INFO] encoded CSR
2021/05/25 21:28:34 [INFO] signed certificate with serial number 264885125868708307869469251678582222107145883647
2021/05/25 21:28:34 [WARNING] This certificate lacks a "hosts" field. This makes it unsuitable for
websites. For more information see the Baseline Requirements for the Issuance and Management
of Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser Forum (https://cabforum.org);
specifically, section 10.2.3 ("Information Requirements").
2021/05/25 21:28:34 [INFO] generate received request
2021/05/25 21:28:34 [INFO] received CSR
2021/05/25 21:28:34 [INFO] generating key: rsa-2048
2021/05/25 21:28:34 [INFO] encoded CSR
2021/05/25 21:28:34 [INFO] signed certificate with serial number 713229295879391239071700507064896098180097516242
2021/05/25 21:28:34 [WARNING] This certificate lacks a "hosts" field. This makes it unsuitable for
websites. For more information see the Baseline Requirements for the Issuance and Management
of Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser Forum (https://cabforum.org);
specifically, section 10.2.3 ("Information Requirements").
2021/05/25 21:28:34 [INFO] generate received request
2021/05/25 21:28:34 [INFO] received CSR
2021/05/25 21:28:34 [INFO] generating key: rsa-2048
2021/05/25 21:28:35 [INFO] encoded CSR
2021/05/25 21:28:35 [INFO] signed certificate with serial number 510763777627184932476115002460038910441764334416
2021/05/25 21:28:35 [WARNING] This certificate lacks a "hosts" field. This makes it unsuitable for
websites. For more information see the Baseline Requirements for the Issuance and Management
of Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser Forum (https://cabforum.org);
specifically, section 10.2.3 ("Information Requirements").
2021/05/25 21:28:35 [INFO] generate received request
2021/05/25 21:28:35 [INFO] received CSR
2021/05/25 21:28:35 [INFO] generating key: rsa-2048
2021/05/25 21:28:35 [INFO] encoded CSR
2021/05/25 21:28:35 [INFO] signed certificate with serial number 661523485757561706763732863455924985762901747490
2021/05/25 21:28:35 [WARNING] This certificate lacks a "hosts" field. This makes it unsuitable for
websites. For more information see the Baseline Requirements for the Issuance and Management
of Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser Forum (https://cabforum.org);
specifically, section 10.2.3 ("Information Requirements").
2021/05/25 21:28:35 [INFO] generate received request
2021/05/25 21:28:35 [INFO] received CSR
2021/05/25 21:28:35 [INFO] generating key: rsa-2048
2021/05/25 21:28:35 [INFO] encoded CSR
2021/05/25 21:28:35 [INFO] signed certificate with serial number 579915653002979021520647597197805503410947245438
2021/05/25 21:28:35 [WARNING] This certificate lacks a "hosts" field. This makes it unsuitable for
websites. For more information see the Baseline Requirements for the Issuance and Management
of Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser Forum (https://cabforum.org);
specifically, section 10.2.3 ("Information Requirements").
2021/05/25 21:28:35 [INFO] generate received request
2021/05/25 21:28:35 [INFO] received CSR
2021/05/25 21:28:35 [INFO] generating key: rsa-2048
2021/05/25 21:28:35 [INFO] encoded CSR
2021/05/25 21:28:35 [INFO] signed certificate with serial number 228895886955268244322921412477911227447746451631
2021/05/25 21:28:35 [WARNING] This certificate lacks a "hosts" field. This makes it unsuitable for
websites. For more information see the Baseline Requirements for the Issuance and Management
of Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser Forum (https://cabforum.org);
specifically, section 10.2.3 ("Information Requirements").
2021/05/25 21:28:35 [INFO] generate received request
2021/05/25 21:28:35 [INFO] received CSR
2021/05/25 21:28:35 [INFO] generating key: rsa-2048
2021/05/25 21:28:36 [INFO] encoded CSR
2021/05/25 21:28:36 [INFO] signed certificate with serial number 86511019963440139103134411786308186921305451303
2021/05/25 21:28:36 [WARNING] This certificate lacks a "hosts" field. This makes it unsuitable for
websites. For more information see the Baseline Requirements for the Issuance and Management
of Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser Forum (https://cabforum.org);
specifically, section 10.2.3 ("Information Requirements").
Waiting for apiserver to come up
+++ [0525 21:28:43] On try 5, apiserver: : ok
clusterrolebinding.rbac.authorization.k8s.io/kube-apiserver-kubelet-admin created
clusterrolebinding.rbac.authorization.k8s.io/kubelet-csr created
Cluster "local-up-cluster" set.
use 'kubectl --kubeconfig=/var/run/kubernetes/admin-kube-aggregator.kubeconfig' to use the aggregated API server
service/kube-dns created
serviceaccount/kube-dns created
configmap/kube-dns created
deployment.apps/kube-dns created
Kube-dns addon successfully deployed.
WARNING : The kubelet is configured to not fail even if swap is enabled; production deployments should disable swap.
2021/05/25 21:28:45 [INFO] generate received request
2021/05/25 21:28:45 [INFO] received CSR
2021/05/25 21:28:45 [INFO] generating key: rsa-2048
2021/05/25 21:28:46 [INFO] encoded CSR
2021/05/25 21:28:46 [INFO] signed certificate with serial number 390801525437068033337403497184568707427779072245
2021/05/25 21:28:46 [WARNING] This certificate lacks a "hosts" field. This makes it unsuitable for
websites. For more information see the Baseline Requirements for the Issuance and Management
of Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser Forum (https://cabforum.org);
specifically, section 10.2.3 ("Information Requirements").
kubelet ( 246938 ) is running.
wait kubelet ready
No resources found
No resources found
No resources found
No resources found
No resources found
127.0.0.1   NotReady   <none>   2s    v0.0.0-master+fa5f52ab4bf9be
2021/05/25 21:28:58 [INFO] generate received request
2021/05/25 21:28:58 [INFO] received CSR
2021/05/25 21:28:58 [INFO] generating key: rsa-2048
2021/05/25 21:28:59 [INFO] encoded CSR
2021/05/25 21:28:59 [INFO] signed certificate with serial number 579005183887936821592823305780316233285535027087
2021/05/25 21:28:59 [WARNING] This certificate lacks a "hosts" field. This makes it unsuitable for
websites. For more information see the Baseline Requirements for the Issuance and Management
of Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser Forum (https://cabforum.org);
specifically, section 10.2.3 ("Information Requirements").
Create default storage class for 
storageclass.storage.k8s.io/standard created
Local Kubernetes cluster is running. Press Ctrl-C to shut it down.

Logs:
  /tmp/kube-apiserver.log
  /tmp/kube-controller-manager.log
  
  /tmp/kube-proxy.log
  /tmp/kube-scheduler.log
  /tmp/kubelet.log

To 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

5、使用本地k8s

安装上一步输出的提示,另外打开一个终端窗口,配置环境变量:

export KUBECONFIG=/var/run/kubernetes/admin.kubeconfig

进入k8s源码目录下的cluster文件夹,里面的kubectl.sh就是kubectl命令了。如下是查看node命令:

./kubectl.sh get node

输出如下:

Logo

开源、云原生的融合云平台

更多推荐