Skip to content
This repository was archived by the owner on Mar 26, 2024. It is now read-only.

Commit 8902afa

Browse files
committed
build: initilization the project
issue #1
1 parent fa92fc9 commit 8902afa

40 files changed

+5199
-0
lines changed

.doc/1-objects-in-kubernetes.md

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
# Objects In Kubernetes
2+
3+
## Reference
4+
5+
https://kubernetes.io/docs/concepts/overview/working-with-objects/
6+
7+
## Kubernetes Object Management
8+
9+
```shell
10+
kubectl create deployment nginx --image nginx
11+
kubectl create -f nginx.yaml # create a object
12+
kubectl delete -f nginx.yaml -f redis.yaml # delete a object
13+
kubectl replace -f nginx.yaml # update a object
14+
kubectl diff -f configs/ && kubectl apply -f configs/ # make a patch & apply it
15+
```
16+
17+
## Object Names and IDs
18+
19+
```yaml
20+
apiVersion: v1
21+
kind: Pod
22+
metadata:
23+
name: nginx-demo # RFC1123 & RFC1035(start with char), len(name) <= 63
24+
spec:
25+
containers:
26+
- name: nginx
27+
image: nginx:1.14.2
28+
ports:
29+
- containerPort: 80
30+
```
31+
32+
## Labels and Selectors
33+
34+
```yaml
35+
apiVersion: v1
36+
kind: Pod
37+
metadata:
38+
name: label-demo
39+
labels:
40+
environment: production # [a-z0-9A-Z], (-, _, .), len(key) <= 63
41+
app: nginx
42+
spec:
43+
containers:
44+
- name: nginx
45+
image: nginx:1.14.2
46+
ports:
47+
- containerPort: 80
48+
```
49+
50+
> Note: For some API types, such as ReplicaSets, the label selectors of two instances must not overlap within a namespace, or the controller can see that as conflicting instructions and fail to determine how many replicas should be present.
51+
52+
> Caution: For both equality-based and set-based conditions there is no logical OR (||) operator. Ensure your filter statements are structured accordingly.
53+
54+
```yaml
55+
apiVersion: v1
56+
kind: Pod
57+
metadata:
58+
name: cuda-test
59+
spec:
60+
containers:
61+
- name: cuda-test
62+
image: "registry.k8s.io/cuda-vector-add:v0.1"
63+
resources:
64+
limits:
65+
nvidia.com/gpu: 1
66+
nodeSelector:
67+
accelerator: nvidia-tesla-p100 # accelerator==nvidia-tesla-p100, in,notin and exists
68+
```
69+
```yaml
70+
labelSelector=environment%3Dproduction,tier%3Dfrontend
71+
```
72+
73+
```shell
74+
kubectl get pods -l environment=production,tier=frontend
75+
```
76+
77+
```yaml
78+
?labelSelector=environment+in+%28production%2Cqa%29%2Ctier+in+%28frontend%29
79+
```
80+
81+
```shell
82+
kubectl get pods -l 'environment in (production),tier in (frontend)'
83+
```
84+
85+
examples: https://github.com/kubernetes/examples/tree/master/guestbook/
86+
87+
```shell
88+
kubectl label pods -l app=nginx tier=fe # select all ngnix and add tier to fe
89+
kubectl get pods -l app=nginx -L tier # (--label-columns) to see it
90+
```
91+
92+
## Namespaces
93+
94+
- default
95+
- kube-node-lease: [Lease](https://kubernetes.io/docs/concepts/architecture/leases/) Objects to send [heartbeats](https://kubernetes.io/docs/concepts/architecture/nodes/#heartbeats)
96+
- kube-public
97+
- kube-system
98+
99+
```shell
100+
kubectl get namespace
101+
```
102+
103+
DNS: <service-name>.<namespace-name>.svc.cluster.local
104+
105+
> By creating namespaces with the same name as [public top-level domains](https://data.iana.org/TLD/tlds-alpha-by-domain.txt), Services in these namespaces can have short DNS names that overlap with public DNS records. Workloads from any namespace performing a DNS lookup without a [trailing dot](https://datatracker.ietf.org/doc/html/rfc1034#page-8) will be redirected to those services, taking precedence over public DNS.
106+
To mitigate this, limit privileges for creating namespaces to trusted users. If required, you could additionally configure third-party security controls, such as [admission webhooks](https://kubernetes.io/docs/reference/access-authn-authz/extensible-admission-controllers/), to block creating any namespace with the name of [public TLDs](https://data.iana.org/TLD/tlds-alpha-by-domain.txt).
107+
108+
namespace resources are not themselves in a namespace. And low-level resources, such as [nodes](https://kubernetes.io/docs/concepts/architecture/nodes/) and [persistentVolumes](https://kubernetes.io/docs/concepts/storage/persistent-volumes/), are not in any namespace.
109+
```shell
110+
kubectl api-resources --namespaced=true # in a namespace
111+
kubectl api-resources --namespaced=false # not in a namespace
112+
```
113+
114+
`kubernetes.io/metadata.name` is atomic labelling which is set by the control plane.
115+
116+
## Annotations
117+
118+
You can use Kubernetes annotations to attach arbitrary non-identifying metadata to [objects](https://kubernetes.io/docs/concepts/overview/working-with-objects/#kubernetes-objects). Clients such as tools and libraries can retrieve this metadata.
119+
120+
```yaml
121+
apiVersion: v1
122+
kind: Pod
123+
metadata:
124+
name: annotations-demo
125+
annotations:
126+
imageregistry: "https://hub.docker.com/" # it shows the imageregistry
127+
spec:
128+
containers:
129+
- name: nginx
130+
image: nginx:1.14.2
131+
ports:
132+
- containerPort: 80
133+
```
134+
135+
# Field Selectors
136+
137+
```shell
138+
kubectl get pods --field-selector metadata.name=my-service
139+
kubectl get pods --field-selector metadata.namespace!=default
140+
kubectl get pods --field-selector status.phase=Pending
141+
```
142+
143+
```shell
144+
kubectl get pods,statefulsets,services --all-namespaces --field-selector=status.phase!=Running,spec.restartPolicy=Always
145+
```
146+
147+
# Finalizers
148+
149+
Finalizers are namespaced keys that tell Kubernetes to wait until specific conditions are met before it fully deletes resources marked for deletion. Finalizers alert controllers to clean up resources the deleted object owned.
150+
151+
When you tell Kubernetes to delete an object that has finalizers specified for it, the Kubernetes API marks the object for deletion by populating `.metadata.deletionTimestamp`, and returns a 202 status code (HTTP "Accepted"). The target object remains in a terminating state while the control plane, or other components, take the actions defined by the finalizers. After these actions are complete, the controller removes the relevant finalizers from the target object. When the `metadata.finalizers` field is empty, Kubernetes considers the deletion complete and deletes the object.
152+
153+
> When you DELETE an object, Kubernetes adds the deletion timestamp for that object and then immediately starts to restrict changes to the .metadata.finalizers field for the object that is now pending deletion. You can remove existing finalizers (deleting an entry from the finalizers list) but you cannot add a new finalizer. You also cannot modify the deletionTimestamp for an object once it is set.
154+
After the deletion is requested, you can not resurrect this object. The only way is to delete it and make a new similar object.
155+
156+
> Note: In cases where objects are stuck in a deleting state, avoid manually removing finalizers to allow deletion to continue. Finalizers are usually added to resources for a reason, so forcefully removing them can lead to issues in your cluster. This should only be done when the purpose of the finalizer is understood and is accomplished in another way (for example, manually cleaning up some dependent object).
157+
158+
# Owners and Dependents
159+
160+
A valid owner reference (`metadata.ownerReferences`) consists of the object name and a UID within the same namespace as the dependent object.
161+
`ownerReferences.blockOwnerDeletion`
162+
163+
> Note: Cross-namespace owner references are disallowed by design. Namespaced dependents can specify cluster-scoped or namespaced owners. A namespaced owner must exist in the same namespace as the dependent. If it does not, the owner reference is treated as absent, and the dependent is subject to deletion once all owners are verified absent.
164+
Cluster-scoped dependents can only specify cluster-scoped owners. In v1.20+, if a cluster-scoped dependent specifies a namespaced kind as an owner, it is treated as having an unresolvable owner reference, and is not able to be garbage collected.
165+
In v1.20+, if the garbage collector detects an invalid cross-namespace ownerReference, or a cluster-scoped dependent with an ownerReference referencing a namespaced kind, a warning Event with a reason of OwnerRefInvalidNamespace and an involvedObject of the invalid dependent is reported. You can check for that kind of Event by running `kubectl get events -A --field-selector=reason=OwnerRefInvalidNamespace`.
166+
167+
# Recommended Labels
168+
169+
Shared labels and annotations share a common prefix: `app.kubernetes.io`. Labels without a prefix are private to users. The shared prefix ensures that shared labels do not interfere with custom user labels.
170+
171+
```yaml
172+
# This is an excerpt StatefulSet object
173+
apiVersion: apps/v1
174+
kind: StatefulSet
175+
metadata:
176+
labels:
177+
app.kubernetes.io/name: mysql
178+
app.kubernetes.io/instance: mysql-abcxzy # every instance of an application must have a unique name.
179+
app.kubernetes.io/version: "5.7.21"
180+
app.kubernetes.io/component: database
181+
app.kubernetes.io/part-of: wordpress
182+
app.kubernetes.io/managed-by: helm
183+
```
184+
185+
```yaml
186+
# This is an excerpt Deployment object, to oversee the pods running the application itself
187+
apiVersion: apps/v1
188+
kind: Deployment
189+
metadata:
190+
labels:
191+
app.kubernetes.io/name: wordpress
192+
app.kubernetes.io/instance: wordpress-abcxzy # every instance of an application must have a unique name.
193+
app.kubernetes.io/version: "4.9.4"
194+
app.kubernetes.io/component: server
195+
app.kubernetes.io/part-of: wordpress
196+
app.kubernetes.io/managed-by: helm
197+
```
198+
199+
```yaml
200+
# This is an excerpt Service object, to expose the application
201+
apiVersion: apps/v1
202+
kind: Service
203+
metadata:
204+
labels:
205+
app.kubernetes.io/name: wordpress
206+
app.kubernetes.io/instance: wordpress-abcxzy # every instance of an application must have a unique name.
207+
app.kubernetes.io/version: "4.9.4"
208+
app.kubernetes.io/component: server
209+
app.kubernetes.io/part-of: wordpress
210+
app.kubernetes.io/managed-by: helm
211+
```

.doc/2-controller.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Controller
2+
3+
## Reference
4+
5+
https://kubernetes.io/docs/concepts/architecture/controller/
6+
7+
In robotics and automation, a control loop is a non-terminating loop that regulates the state of a system.
8+
9+
LOOP
10+
Current State => Job Controller (via Calling API Server) => Desired State
11+
12+
> Note: There can be several controllers that create or update the same kind of object. Behind the scenes, Kubernetes controllers make sure that they only pay attention to the resources linked to their controlling resource.
13+
For example, you can have Deployments and Jobs; these both create Pods. The Job controller does not delete the Pods that your Deployment created, because there is information (labels) the controllers can use to tell those Pods apart.
14+
15+
Kubernetes comes with a set of built-in controllers that run inside the kube-controller-manager. These built-in controllers provide important core behaviors.
16+
17+
The Deployment controller and Job controller are examples of controllers that come as part of Kubernetes itself ("built-in" controllers). Kubernetes lets you run a resilient control plane, so that if any of the built-in controllers were to fail, another part of the control plane will take over the work.
18+
19+
The most common way to deploy an operator is to add the Custom Resource Definition and its associated Controller to your cluster. The Controller will normally run outside of the control plane, much as you would run any containerized application. For example, you can run the controller in your cluster as a Deployment.
20+
21+
> Note: This section links to third party projects that provide functionality required by Kubernetes. The Kubernetes project authors aren't responsible for these projects, which are listed alphabetically. To add a project to this list, read the [content guide](https://kubernetes.io/docs/contribute/style/content-guide/#third-party-content) before submitting a change. [More information](https://kubernetes.io/docs/concepts/extend-kubernetes/operator/#third-party-content-disclaimer).
22+
23+
> Note: [Operator SDK](https://github.com/operator-framework/operator-sdk/blob/v1.33.0/) uses the [kubebuilder](https://github.com/kubernetes-sigs/kubebuilder/tree/v3.12.0) plugin feature to include non-Go operators e.g. operator-sdk's Ansible and Helm-based language Operators. To learn more see [how to create your own plugins](https://book.kubebuilder.io/plugins/creating-plugins.html).

.doc/3-operator.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Operator
2+
3+
## Reference
4+
5+
https://kubernetes.io/docs/concepts/extend-kubernetes/operator/
6+
7+
Operators are software extensions to Kubernetes that make use of [custom resources](https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/) to manage applications and their components. Operators follow Kubernetes principles, notably the control loop.
8+
9+
Kubernetes' operator pattern concept lets you extend the cluster's behaviour without modifying the code of Kubernetes itself by linking controllers to one or more custom resources. Operators are clients of the Kubernetes API that act as controllers for a [Custom Resource](https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/).
10+

.doc/4-installation.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Installation
2+
3+
## Reference
4+
5+
https://sdk.operatorframework.io/docs/installation/
6+
7+
## Installation
8+
9+
```shell
10+
brew install operator-sdk
11+
```

.doc/5-tutorial.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Tutorial
2+
3+
## Reference
4+
5+
https://sdk.operatorframework.io/docs/building-operators/golang/tutorial/
6+
7+
```shell
8+
mkdir -p $HOME/Developer/custom-kubernetes-controller
9+
cd $HOME/Developer/custom-kubernetes-controller
10+
# we'll use a domain of smhmayboudi.github.io
11+
# so all API groups will be <group>.smhmayboudi.github.io
12+
operator-sdk init --domain=smhmayboudi.github.io --repo=github.com/smhmayboudi/custom-kubernetes-controller
13+
```
14+
15+
### MacOS
16+
17+
https://kubebuilder.io/plugins/available-plugins
18+
19+
```shell
20+
mkdir -p $HOME/Developer/custom-kubernetes-controller
21+
cd $HOME/Developer/custom-kubernetes-controller
22+
# we'll use a domain of smhmayboudi.github.io
23+
# so all API groups will be <group>.smhmayboudi.github.io
24+
operator-sdk init --domain=smhmayboudi.github.io --repo=github.com/smhmayboudi/custom-kubernetes-controller --plugins=go/v4
25+
```
26+
27+
## Manager
28+
29+
https://book.kubebuilder.io/cronjob-tutorial/empty-main.html
30+
31+
https://sdk.operatorframework.io/docs/building-operators/golang/operator-scope/
32+
33+
## Question
34+
35+
https://book.kubebuilder.io/cronjob-tutorial/empty-main.html
36+
37+
```go
38+
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
39+
Scheme: scheme,
40+
Cache: cache.Options{
41+
DefaultNamespaces: map[string]cache.Config{
42+
namespace: {},
43+
},
44+
},
45+
Metrics: server.Options{
46+
BindAddress: metricsAddr,
47+
},
48+
WebhookServer: webhook.NewServer(webhook.Options{Port: 9443}),
49+
HealthProbeBindAddress: probeAddr,
50+
LeaderElection: enableLeaderElection,
51+
LeaderElectionID: "80807133.tutorial.kubebuilder.io",
52+
})
53+
```
54+
55+
The above example will change the scope of your project to a single Namespace. In this scenario, it is also suggested to restrict the provided authorization to this namespace by replacing the default ClusterRole and ClusterRoleBinding to Role and RoleBinding respectively. For further information see the Kubernetes documentation about Using [RBAC Authorization](https://kubernetes.io/docs/reference/access-authn-authz/rbac/).
56+
57+
58+
```go
59+
var namespaces []string // List of Namespaces
60+
defaultNamespaces := make(map[string]cache.Config)
61+
62+
for _, ns := range namespaces {
63+
defaultNamespaces[ns] = cache.Config{}
64+
}
65+
66+
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
67+
Scheme: scheme,
68+
Cache: cache.Options{
69+
DefaultNamespaces: defaultNamespaces,
70+
},
71+
Metrics: server.Options{
72+
BindAddress: metricsAddr,
73+
},
74+
WebhookServer: webhook.NewServer(webhook.Options{Port: 9443}),
75+
HealthProbeBindAddress: probeAddr,
76+
LeaderElection: enableLeaderElection,
77+
LeaderElectionID: "80807133.tutorial.kubebuilder.io",
78+
})
79+
```
80+
81+
Also, it is possible to use the [DefaultNamespaces](https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/cache#Options) from cache.Options{} to cache objects in a specific set of namespaces. For further information see [cache.Options{}](https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/cache#Options)
82+
83+
```shell
84+
operator-sdk create api --group=cache --version=v1alpha1 --kind=Memcached --resource --controller
85+
```

.doc/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# References
2+
3+
1. https://kubernetes.io/docs/concepts/overview/working-with-objects/
4+
2. https://kubernetes.io/docs/concepts/architecture/controller/
5+
3. https://kubernetes.io/docs/concepts/extend-kubernetes/operator/
6+
4. https://sdk.operatorframework.io/docs/installation/
7+
5. https://sdk.operatorframework.io/docs/building-operators/golang/tutorial/

.dockerignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file
2+
# Ignore build and test binaries.
3+
bin/

.gitignore

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
# Binaries for programs and plugins
3+
*.exe
4+
*.exe~
5+
*.dll
6+
*.so
7+
*.dylib
8+
bin/*
9+
Dockerfile.cross
10+
11+
# Test binary, build with `go test -c`
12+
*.test
13+
14+
# Output of the go coverage tool, specifically when used with LiteIDE
15+
*.out
16+
17+
# Kubernetes Generated files - skip generated files, except for vendored files
18+
19+
!vendor/**/zz_generated.*
20+
21+
# editor and IDE paraphernalia
22+
.idea
23+
.vscode
24+
*.swp
25+
*.swo
26+
*~

0 commit comments

Comments
 (0)