第35节 client-go 写 k8s 控制器


❤️💕💕新时代拥抱云原生,云原生具有环境统一、按需付费、即开即用、稳定性强特点。Myblog:http://nsddd.topopen in new window


[TOC]

控制器

Contaoller 至少追踪一种类型的 Kubernetes 资源。这些 对象 有一个代表期望状态的 spec 字段,该资源的控制器负责确保当前状态接近期望状态。

kubernetes 官方一个 实现简单的 sample-controlleropen in new window 案例

kubernetes 官方一个 简单的实现 client-go-sampleopen in new window 案例

client-go

我们如何和 Kubernetes 对话,是的,使用 kubectl 可以以 CRL 方式和 Kubernetes 交互。

如何使用 code 实现,是的,可以使用 client-go,这很重要!!!

访问 Kubernetes 集群的方式

访问kubernetes集群有以下几种方式:

方式特点支持者
Kubernetes dashboard直接通过Web UI进行操作,简单直接,可定制化程度低官方支持
kubectl命令行操作,功能最全,但是比较复杂,适合对其进行进一步的分装,定制功能,版本适配最好官方支持
client-goopen in new window从kubernetes的代码中抽离出来的客户端包,简单易用,但需要小心区分kubernetes的API版本官方支持
client-pythonopen in new windowpython客户端,kubernetes-incubator官方支持
Java clientopen in new windowfabric8中的一部分,kubernetes的java客户端Red Hat

简单的实现 client-go-sample

$ go get github.com/rootsongjc/kubernetes-client-go-sample
$ cd $GOPATH/src/github.com/rootsongjc/kubernetes-client-go-sample
$ go build main.go

该命令的用法如下。

 $ ./main
  -app string
        application name (default "app")
  -deployment string
        deployment name
  -image string
        new image name
  -kubeconfig string
        (optional) absolute path to the kubeconfig file (default "/Users/jimmy/.kube/config")

使用不存在的image更新

 $ ./main -deployment filebeat-test -image harbor-001.jimmysong.io/library/analytics-docker-test:Build_9 
Found deployment
name -> filebeat-test
Old image -> harbor-001.jimmysong.io/library/analytics-docker-test:Build_8
New image -> harbor-001.jimmysong.io/library/analytics-docker-test:Build_9

查看Deployment的event。

$ kubectl describe deployment filebeat-test   
Name:            filebeat-test
Namespace:        default
CreationTimestamp:    Fri, 19 May 2017 15:12:28 +0800
Labels:            k8s-app=filebeat-test
Selector:        k8s-app=filebeat-test
Replicas:        2 updated | 3 total | 2 available | 2 unavailable
StrategyType:        RollingUpdate
MinReadySeconds:    0
RollingUpdateStrategy:    1 max unavailable, 1 max surge
Conditions:
  Type        Status    Reason
  ----        ------    ------
  Available     True    MinimumReplicasAvailable
  Progressing     True    ReplicaSetUpdated
OldReplicaSets:    filebeat-test-2365467882 (2/2 replicas created)
NewReplicaSet:    filebeat-test-2470325483 (2/2 replicas created)
Events:
  FirstSeen    LastSeen    Count    From                SubObjectPath    Type        ReasoMessage
  ---------    --------    -----    ----                -------------    --------    ------------
  2h        1m        3    {deployment-controller }            Normal        ScalingReplicaSet    Scaled down replica set filebeat-test-2365467882 to 2
  1m        1m        1    {deployment-controller }            Normal        ScalingReplicaSet    Scaled up replica set filebeat-test-2470325483 to 1
  1m        1m        1    {deployment-controller }            Normal        ScalingReplicaSet    Scaled up replica set filebeat-test-2470325483 to 2

可以看到老的ReplicaSet从3个replica减少到了2个,有2个使用新配置的replica不可用,目前可用的replica是2个。

这是因为我们指定的镜像不存在,查看Deployment的pod的状态。

$ kubectl get pods -l k8s-app=filebeat-test
NAME                             READY     STATUS             RESTARTS   AGE
filebeat-test-2365467882-4zwx8   2/2       Running            0          33d
filebeat-test-2365467882-rqskl   2/2       Running            0          33d
filebeat-test-2470325483-6vjbw   1/2       ImagePullBackOff   0          4m
filebeat-test-2470325483-gc14k   1/2       ImagePullBackOff   0          4m

我们可以看到有两个pod正在拉取image。

还原为原先的镜像

将image设置为原来的镜像。

$ ./main -deployment filebeat-test -image harbor-001.jimmysong.io/library/analytics-docker-test:Build_8
Found deployment
name -> filebeat-test
Old image -> harbor-001.jimmysong.io/library/analytics-docker-test:Build_9
New image -> harbor-001.jimmysong.io/library/analytics-docker-test:Build_8

现在再查看Deployment的状态。

$ kubectl describe deployment filebeat-test   
Name:            filebeat-test
Namespace:        default
CreationTimestamp:    Fri, 19 May 2017 15:12:28 +0800
Labels:            k8s-app=filebeat-test
Selector:        k8s-app=filebeat-test
Replicas:        3 updated | 3 total | 3 available | 0 unavailable
StrategyType:        RollingUpdate
MinReadySeconds:    0
RollingUpdateStrategy:    1 max unavailable, 1 max surge
Conditions:
  Type        Status    Reason
  ----        ------    ------
  Available     True    MinimumReplicasAvailable
  Progressing     True    NewReplicaSetAvailable
OldReplicaSets:    <none>
NewReplicaSet:    filebeat-test-2365467882 (3/3 replicas created)
Events:
  FirstSeen    LastSeen    Count    From                SubObjectPath    Type        ReasoMessage
  ---------    --------    -----    ----                -------------    --------    ------------
  2h        8m        3    {deployment-controller }            Normal        ScalingReplicaSet    Scaled down replica set filebeat-test-2365467882 to 2
  8m        8m        1    {deployment-controller }            Normal        ScalingReplicaSet    Scaled up replica set filebeat-test-2470325483 to 1
  8m        8m        1    {deployment-controller }            Normal        ScalingReplicaSet    Scaled up replica set filebeat-test-2470325483 to 2
  2h        1m        3    {deployment-controller }            Normal        ScalingReplicaSet    Scaled up replica set filebeat-test-2365467882 to 3
  1m        1m        1    {deployment-controller }            Normal        ScalingReplicaSet    Scaled down replica set filebeat-test-2470325483 to 0

可以看到available的replica个数恢复成3了。

END 链接