kubelet分析(一)-config-v1.5.2

kubelet在Kubernetes中负责具体与物理设备打交道,负责pod所对应的相关资源的管理,如container, volume, network等。本次分析将介绍kubelet的config,看kubelet是如何把pod的信息从apiserver,或file,或http同步到kublet中的。其中,apiserver渠道代表从ETCD中获取pod的变化;file渠道代表从文件获取源的变化,如static pod;http渠道我还没用过。

阅读全文

storage解读(六)-strategy-v1.5.2

什么是strategy

本次分析将介绍storage的strategy。在创建,更新,删除资源时,我们常常需要有对资源进行预处理的需求。Kubernetes会对系统中的资源都定义一个strategy,用来表明在操作资源时该资源应有的处理方式。在Kubernetes中,主要有RESTCreateStrategy,RESTGracefulDeleteStrategy,RESTUpdateStrategy等。

阅读全文

kubernetes-listwatch机制-v1.5.2

引子

之前,我们分析过Kubernetes的reflect机制。reflector可以消费watch channel中的内容,并存储到store中。本次分析就将介绍Kubernetes的listwatch机制的主要调用流程,将按下面两部分进行分析:

阅读全文

kubernetes-添加controller-demo-v1.5.2

上一次分析介绍了如何添加资源Application,现在就要在kube-controller-manager中添加一个controller对Appliaction进行一些操作。

添加application controller

在/pkg/controller目录下新建application目录,然后在application目录下添加application_controller.go:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
Copyright 2015 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package application contains all the logic for handling Kubernetes Application.
package application
import (
"time"
"strings"
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/client/cache"
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
unversionedcore "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
"k8s.io/kubernetes/pkg/client/record"
"k8s.io/kubernetes/pkg/apis/extensions"
"k8s.io/kubernetes/pkg/controller"
"k8s.io/kubernetes/pkg/controller/informers"
"k8s.io/kubernetes/pkg/util/metrics"
utilruntime "k8s.io/kubernetes/pkg/util/runtime"
"k8s.io/kubernetes/pkg/util/wait"
"k8s.io/kubernetes/pkg/util/workqueue"
)
// ApplicationController is responsible for synchronizing Application objects.
type ApplicationController struct {
client clientset.Interface
eventRecorder record.EventRecorder
syncHandler func(dKey string) error
// Applications that need to be synced
queue workqueue.RateLimitingInterface
}
// NewApplicationController creates a new ApplicationController.
func NewApplicationController(aInformer informers.ApplicationInformer, client clientset.Interface) *ApplicationController {
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(glog.Infof)
eventBroadcaster.StartRecordingToSink(&unversionedcore.EventSinkImpl{Interface: client.Core().Events("")})
if client != nil && client.Core().RESTClient().GetRateLimiter() != nil {
metrics.RegisterMetricAndTrackRateLimiterUsage("application_controller", client.Core().RESTClient().GetRateLimiter())
}
ac := &ApplicationController{
client: client,
eventRecorder: eventBroadcaster.NewRecorder(api.EventSource{Component: "application-controller"}),
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "application"),
}
aInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: ac.addApplicationNotification,
UpdateFunc: ac.updateApplicationNotification,
// This will enter the sync loop and no-op, because the application has been deleted from the store.
DeleteFunc: ac.deleteApplicationNotification,
})
ac.syncHandler = ac.syncApplication
return ac
}
// Run begins watching and syncing.
func (ac *ApplicationController) Run(workers int, stopCh <-chan struct{}) {
defer utilruntime.HandleCrash()
glog.Infof("Starting application controller")
go wait.Until(ac.worker, time.Second, stopCh)
<-stopCh
glog.Infof("Shutting down application controller")
}
func (ac *ApplicationController) worker() {
work := func() bool {
key, quit := ac.queue.Get()
if quit {
return true
}
defer ac.queue.Done(key)
ac.syncHandler(key.(string))
return false
}
for {
if quit := work(); quit {
return
}
}
}
func (ac *ApplicationController) enqueueApplication(application *extensions.Application) {
key, err := controller.KeyFunc(application)
if err != nil {
glog.Errorf("Couldn't get key for object %#v: %v", application, err)
return
}
ac.queue.Add(key)
}
func (ac *ApplicationController) addApplicationNotification(obj interface{}) {
d := obj.(*extensions.Application)
glog.V(4).Infof("Adding application %s", d.Name)
ac.enqueueApplication(d)
}
func (ac *ApplicationController) updateApplicationNotification(old, cur interface{}) {
oldD := old.(*extensions.Application)
glog.V(4).Infof("Updating application %s", oldD.Name)
// Resync on application object relist.
ac.enqueueApplication(cur.(*extensions.Application))
}
func (ac *ApplicationController) deleteApplicationNotification(obj interface{}) {
d, ok := obj.(*extensions.Application)
if !ok {
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
glog.Errorf("Couldn't get object from tombstone %#v", obj)
return
}
d, ok = tombstone.Obj.(*extensions.Application)
if !ok {
glog.Errorf("Tombstone contained object that is not a Application %#v", obj)
return
}
}
glog.V(4).Infof("Deleting application %s", d.Name)
ac.enqueueApplication(d)
}
func (ac *ApplicationController) syncApplication(key string) error {
segs := strings.Split(key, "/")
namespace := segs[0]
name := segs[1]
application, err := ac.client.Extensions().Applications(namespace).Get(name)
if err != nil {
return err
}
glog.V(0).Infof("This is from syncApplication: %s/%s", application.Namespace, application.Name)
return nil
}

阅读全文

kubernetes-添加资源-demo-v1.5.2

本次分析将介绍如何在kubernetes v1.5.2中通过代码添加一种资源。我们假设要添加的资源为”Application”,只有一个”replicas”属性,即如下所示:

1
2
3
4
5
6
7
8
9
10
11
apiVersion: extensions/v1beta1
kind: Application
metadata:
creationTimestamp: 2017-12-06T11:37:22Z
generation: 1
name: ubuntu
namespace: default
resourceVersion: "448526"
selfLink: /apis/extensions/v1beta1/namespaces/default/applications/ubuntu
uid: d2fb278c-da79-11e7-adb3-0800274a4ec3
replicas: 20

阅读全文

libnetwork源码分析(二)-network-v1-12-3

之前分析了controller的主要功能,在分析管理network时,并未对network作出详细分析。本次将对network作出分析。

network

libnetwork代表一个网络,网络中可以放入endpoint。在bridge模式下,可以把network理解成bridge。network定义在/libnetwork/network.go中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
type network struct {
ctrlr *controller
name string
networkType string
id string
scope string
labels map[string]string
ipamType string
ipamOptions map[string]string
addrSpace string
ipamV4Config []*IpamConf
ipamV6Config []*IpamConf
ipamV4Info []*IpamInfo
ipamV6Info []*IpamInfo
enableIPv6 bool
postIPv6 bool
epCnt *endpointCnt
generic options.Generic
dbIndex uint64
dbExists bool
persist bool
stopWatchCh chan struct{}
drvOnce *sync.Once
internal bool
inDelete bool
ingress bool
driverTables []string
dynamic bool
sync.Mutex
}

阅读全文

libnetwork源码分析(一)-controller(5)-v1-12-3

在controller.go中的New()函数中,会调用c.startExternalKeyListener()启动Unix sock监听。本次分析就介绍controller是如何完成外部net namespace导入到libnetork中的。

(五) 监听unix socket

libnetwork-setkey

我们先来看libnetwork-setkey子命令的执行流程。libnetwork-setkey子命令用于runc向libnetwork报告创建好的net namespace。

阅读全文

libnetwork源码分析(一)-controller(4)-v1-12-3

本次文档将接着上次分析,介绍controller对store的管理。

(四) 管理store

controller中有stores字段,所有store都注册在该字段中。当然,这里所说的store是datastore,datastore中包含consul, zookeeper, etcd及boltdb这些底层的store。

阅读全文

libnetwork源码分析(一)-controller(3)-v1-12-3

本次文档将接着上次分析,介绍controller对sandbox的管理及sandbox的相关代码的分析。

(三) 管理sandbox

在controller中,有sandboxes字段存储sandbox。

阅读全文

libnetwork源码分析(一)-controller(2)-v1.12.3

之前分析了controller的第一部分,现在接着分析第二部分:管理network。

(二) 管理network

controller和network的联系是通过store进行的,也就是说network是存储在数据库中的,而controller通过store管理network。

阅读全文