kubernetes系列之十六:Kubernetes CRD sample-controller的编译和测试

时间:2021-03-05 04:07:36

一、前言

在文章《kubernetes系列之十四:Kubernetes CRD(CustomResourceDefinition)概览》中,对于Kubernetes官方提供的CRD例子进行了分析,包括CRD的定义以及后端controller的实现。同时如下两篇文章也对k8s的controller进行了更详尽的分析和示例:

但是由于k8s官方提供的CRD以及controller的例子没有相关编译和运行的说明,本文将给出step-by-step设置编译环境和进行编译的例子,以及通过Dockerfile进行一键编译和运行的例子。

转载自https://blog.csdn.net/cloudvtech

二、CRD example-controllerde编译

2.1 编译环境

CentOS Linux release 7.4.1708 (Core)  
yum install -y go git 
2.2 下载代码和依赖
mkdir /root/gowork 
export GOPATH=/root/gowork/ 
cd /root/gowork/
git clone https://github.com/kubernetes/sample-controller.git
mv sample-controller src
cd src
mkdir -p k8s.io/sample-controller
cp -a pkg k8s.io/sample-controller 
go get k8s.io/apimachinery

其中安装apimachinery是根据这篇文章的提示,否则会出现类似如下错误:

Error: Failed executing generator: some packages had errors:
type "k8s.io/apimachinery/pkg/runtime.Object" in k8s:deepcopy-gen:interfaces tag of type k8s.io/apimachinery/pkg/runtime.Object is not an interface, but: ""
goroutine 1 [running]:

2.3 运行codegen

./hack/update-codegen.sh 
Generating deepcopy funcs
Generating clientset for samplecontroller:v1alpha1 at k8s.io/sample-controller/pkg/client/clientset
Generating listers for samplecontroller:v1alpha1 at k8s.io/sample-controller/pkg/client/listers
Generating informers for samplecontroller:v1alpha1 at k8s.io/sample-controller/pkg/client/informers
2.4 编译sample-controller
\cp -af vendor/* ./
rm -rf vendor/
go build -v -o samplecontroller
…
k8s.io/sample-controller/pkg/apis/samplecontroller
k8s.io/sample-controller/pkg/apis/samplecontroller/v1alpha1
k8s.io/sample-controller/pkg/client/clientset/versioned/scheme
k8s.io/sample-controller/pkg/client/clientset/versioned/typed/samplecontroller/v1alpha1
k8s.io/sample-controller/pkg/client/clientset/versioned
k8s.io/sample-controller/pkg/client/informers/externalversions/internalinterfaces
k8s.io/sample-controller/pkg/client/listers/samplecontroller/v1alpha1
k8s.io/sample-controller/pkg/client/informers/externalversions/samplecontroller/v1alpha1
k8s.io/sample-controller/pkg/client/informers/externalversions/samplecontroller
k8s.io/sample-controller/pkg/client/informers/externalversions
k8s.io/sample-controller/pkg/signals

编译出来的binary名为samplecontroller,在src目录下面。

转载自https://blog.csdn.net/cloudvtech

三、运行sample-controller并部署CRD

3.1 example CRD的功能

这个CRD的功能就是在Foo类型CRD的实例example-foo被创建的时候,sample-controller根据example-foo里面指定的replica来部署一个具有相应数目replica的nginx的deployment。

3.2 运行sample-controller

sample-controller的参数如下:

./samplecontroller --help
Usage of ./samplecontroller:
  -alsologtostderr
    	log to standard error as well as files
  -kubeconfig string
    	Path to a kubeconfig. Only required if out-of-cluster.
  -log_backtrace_at value
    	when logging hits line file:N, emit a stack trace
  -log_dir string
    	If non-empty, write log files in this directory
  -logtostderr
    	log to standard error instead of files
  -master string
    	The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.
  -stderrthreshold value
    	logs at or above this threshold go to stderr
  -v value
    	log level for V logs
  -vmodule value
    	comma-separated list of pattern=N settings for file-filtered logging

通过kubeconfig可以指定cluster的access,测试Kubernetes的cluster的kubeconfig在如下位置:

/etc/kubernetes/admin.conf 

运行:

./samplecontroller -kubeconfig /etc/kubernetes/admin.conf 

3.3 部署CRD

[root@k8s-master src]# kubectl apply -f artifacts/examples/crd.yaml 
customresourcedefinition.apiextensions.k8s.io "foos.samplecontroller.k8s.io" created
[root@k8s-master src]# kubectl apply -f artifacts/examples/example-foo.yaml 
foo.samplecontroller.k8s.io "example-foo" created

3.4 查看状态

[root@k8s-master src]# kubectl get crd
NAME                           AGE
foos.samplecontroller.k8s.io   52s


[root@k8s-master src]# kubectl get Foo
NAME          AGE
example-foo   44s


[root@k8s-master src]# kubectl describe Foo example-foo
Name:         example-foo
Namespace:    default
Labels:       <none>
Annotations:  kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"samplecontroller.k8s.io/v1alpha1","kind":"Foo","metadata":{"annotations":{},"name":"example-foo","namespace":"default"},"spec":{"deploym...
API Version:  samplecontroller.k8s.io/v1alpha1
Kind:         Foo
Metadata:
  Cluster Name:        
  Creation Timestamp:  2018-05-25T20:28:47Z
  Generation:          1
  Resource Version:    79222
  Self Link:           /apis/samplecontroller.k8s.io/v1alpha1/namespaces/default/foos/example-foo
  UID:                 3a4e7cd3-605a-11e8-9f24-000c29d3e746
Spec:
  Deployment Name:  example-foo
  Replicas:         1
Status:
  Available Replicas:  1
Events:
  Type    Reason  Age               From               Message
  ----    ------  ----              ----               -------
  Normal  Synced  16s (x8 over 1m)  sample-controller  Foo synced successfully

查看deployment和POD

[root@k8s-master src]# kubectl get deployments
NAME          DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
example-foo   1         1         1            1           1m
[root@k8s-master src]# kubectl get pods
NAME                          READY     STATUS    RESTARTS   AGE
example-foo-d74cd7fbc-nql68   1/1       Running   0          2m

转载自https://blog.csdn.net/cloudvtech



四、Dockerfile方式编译和部署

4.1 Dockerfile

FROM centos
ENV PATH=$PATH:/user/bin/
ENV GOPATH=/gowork

WORKDIR /gowork
RUN yum install -y go git
RUN git clone https://github.com/kubernetes/sample-controller.git
RUN mv sample-controller src

WORKDIR /gowork/src
RUN mkdir -p k8s.io/sample-controller
RUN cp -a pkg k8s.io/sample-controller
RUN echo || go get k8s.io/apimachinery
RUN \cp -af vendor/* ./
RUN rm -rf vendor/
RUN go build -v -o samplecontroller

4.2 build

docker build -t samplecontrolle .

4.3 运行

[root@k8s-master sample-controller_docker]# docker run -it -v /etc/kubernetes:/k8s a9
[root@9640be7aa387 src]# /gowork/src/samplecontroller -kubeconfig /k8s/admin.conf 

转载自https://blog.csdn.net/cloudvtech