ekwong.cn

尔康的博客

0%

flannel

由于 flannel 的配置文件直接从 https://raw.githubusercontent.com/ 拉不下来,这里整理一份,已经去掉了备注的配置,对应原地址

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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
---
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
name: psp.flannel.unprivileged
annotations:
seccomp.security.alpha.kubernetes.io/allowedProfileNames: docker/default
seccomp.security.alpha.kubernetes.io/defaultProfileName: docker/default
apparmor.security.beta.kubernetes.io/allowedProfileNames: runtime/default
apparmor.security.beta.kubernetes.io/defaultProfileName: runtime/default
spec:
privileged: false
volumes:
- configMap
- secret
- emptyDir
- hostPath
allowedHostPaths:
- pathPrefix: "/etc/cni/net.d"
- pathPrefix: "/etc/kube-flannel"
- pathPrefix: "/run/flannel"
readOnlyRootFilesystem: false
runAsUser:
rule: RunAsAny
supplementalGroups:
rule: RunAsAny
fsGroup:
rule: RunAsAny
allowPrivilegeEscalation: false
defaultAllowPrivilegeEscalation: false

allowedCapabilities: ['NET_ADMIN']
defaultAddCapabilities: []
requiredDropCapabilities: []

hostPID: false
hostIPC: false
hostNetwork: true
hostPorts:
- min: 0
max: 65535

seLinux:

rule: 'RunAsAny'
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: flannel
rules:
- apiGroups: ['extensions']
resources: ['podsecuritypolicies']
verbs: ['use']
resourceNames: ['psp.flannel.unprivileged']
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- apiGroups:
- ""
resources:
- nodes
verbs:
- list
- watch
- apiGroups:
- ""
resources:
- nodes/status
verbs:
- patch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: flannel
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: flannel
subjects:
- kind: ServiceAccount
name: flannel
namespace: kube-system
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: flannel
namespace: kube-system
---
kind: ConfigMap
apiVersion: v1
metadata:
name: kube-flannel-cfg
namespace: kube-system
labels:
tier: node
app: flannel
data:
cni-conf.json: |
{
"name": "cbr0",
"cniVersion": "0.3.1",
"plugins": [
{
"type": "flannel",
"delegate": {
"hairpinMode": true,
"isDefaultGateway": true
}
},
{
"type": "portmap",
"capabilities": {
"portMappings": true
}
}
]
}
net-conf.json: |
{
"Network": "10.244.0.0/16",
"Backend": {
"Type": "vxlan"
}
}
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: kube-flannel-ds-amd64
namespace: kube-system
labels:
tier: node
app: flannel
spec:
selector:
matchLabels:
app: flannel
template:
metadata:
labels:
tier: node
app: flannel
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: beta.kubernetes.io/os
operator: In
values:
- linux
- key: beta.kubernetes.io/arch
operator: In
values:
- amd64
hostNetwork: true
tolerations:
- operator: Exists
effect: NoSchedule
serviceAccountName: flannel
initContainers:
- name: install-cni
image: quay.io/coreos/flannel:v0.12.0-amd64
command:
- cp
args:
- -f
- /etc/kube-flannel/cni-conf.json
- /etc/cni/net.d/10-flannel.conflist
volumeMounts:
- name: cni
mountPath: /etc/cni/net.d
- name: flannel-cfg
mountPath: /etc/kube-flannel/
containers:
- name: kube-flannel
image: quay.io/coreos/flannel:v0.12.0-amd64
command:
- /opt/bin/flanneld
args:
- --ip-masq
- --kube-subnet-mgr
resources:
requests:
cpu: "100m"
memory: "50Mi"
limits:
cpu: "100m"
memory: "50Mi"
securityContext:
privileged: false
capabilities:
add: ["NET_ADMIN"]
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
volumeMounts:
- name: run
mountPath: /run/flannel
- name: flannel-cfg
mountPath: /etc/kube-flannel/
volumes:
- name: run
hostPath:
path: /run/flannel
- name: cni
hostPath:
path: /etc/cni/net.d
- name: flannel-cfg
configMap:
name: kube-flannel-cfg
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: kube-flannel-ds-arm64
namespace: kube-system
labels:
tier: node
app: flannel
spec:
selector:
matchLabels:
app: flannel
template:
metadata:
labels:
tier: node
app: flannel
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: beta.kubernetes.io/os
operator: In
values:
- linux
- key: beta.kubernetes.io/arch
operator: In
values:
- arm64
hostNetwork: true
tolerations:
- operator: Exists
effect: NoSchedule
serviceAccountName: flannel
initContainers:
- name: install-cni
image: quay.io/coreos/flannel:v0.12.0-arm64
command:
- cp
args:
- -f
- /etc/kube-flannel/cni-conf.json
- /etc/cni/net.d/10-flannel.conflist
volumeMounts:
- name: cni
mountPath: /etc/cni/net.d
- name: flannel-cfg
mountPath: /etc/kube-flannel/
containers:
- name: kube-flannel
image: quay.io/coreos/flannel:v0.12.0-arm64
command:
- /opt/bin/flanneld
args:
- --ip-masq
- --kube-subnet-mgr
resources:
requests:
cpu: "100m"
memory: "50Mi"
limits:
cpu: "100m"
memory: "50Mi"
securityContext:
privileged: false
capabilities:
add: ["NET_ADMIN"]
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
volumeMounts:
- name: run
mountPath: /run/flannel
- name: flannel-cfg
mountPath: /etc/kube-flannel/
volumes:
- name: run
hostPath:
path: /run/flannel
- name: cni
hostPath:
path: /etc/cni/net.d
- name: flannel-cfg
configMap:
name: kube-flannel-cfg
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: kube-flannel-ds-arm
namespace: kube-system
labels:
tier: node
app: flannel
spec:
selector:
matchLabels:
app: flannel
template:
metadata:
labels:
tier: node
app: flannel
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: beta.kubernetes.io/os
operator: In
values:
- linux
- key: beta.kubernetes.io/arch
operator: In
values:
- arm
hostNetwork: true
tolerations:
- operator: Exists
effect: NoSchedule
serviceAccountName: flannel
initContainers:
- name: install-cni
image: quay.io/coreos/flannel:v0.12.0-arm
command:
- cp
args:
- -f
- /etc/kube-flannel/cni-conf.json
- /etc/cni/net.d/10-flannel.conflist
volumeMounts:
- name: cni
mountPath: /etc/cni/net.d
- name: flannel-cfg
mountPath: /etc/kube-flannel/
containers:
- name: kube-flannel
image: quay.io/coreos/flannel:v0.12.0-arm
command:
- /opt/bin/flanneld
args:
- --ip-masq
- --kube-subnet-mgr
resources:
requests:
cpu: "100m"
memory: "50Mi"
limits:
cpu: "100m"
memory: "50Mi"
securityContext:
privileged: false
capabilities:
add: ["NET_ADMIN"]
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
volumeMounts:
- name: run
mountPath: /run/flannel
- name: flannel-cfg
mountPath: /etc/kube-flannel/
volumes:
- name: run
hostPath:
path: /run/flannel
- name: cni
hostPath:
path: /etc/cni/net.d
- name: flannel-cfg
configMap:
name: kube-flannel-cfg
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: kube-flannel-ds-ppc64le
namespace: kube-system
labels:
tier: node
app: flannel
spec:
selector:
matchLabels:
app: flannel
template:
metadata:
labels:
tier: node
app: flannel
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: beta.kubernetes.io/os
operator: In
values:
- linux
- key: beta.kubernetes.io/arch
operator: In
values:
- ppc64le
hostNetwork: true
tolerations:
- operator: Exists
effect: NoSchedule
serviceAccountName: flannel
initContainers:
- name: install-cni
image: quay.io/coreos/flannel:v0.12.0-ppc64le
command:
- cp
args:
- -f
- /etc/kube-flannel/cni-conf.json
- /etc/cni/net.d/10-flannel.conflist
volumeMounts:
- name: cni
mountPath: /etc/cni/net.d
- name: flannel-cfg
mountPath: /etc/kube-flannel/
containers:
- name: kube-flannel
image: quay.io/coreos/flannel:v0.12.0-ppc64le
command:
- /opt/bin/flanneld
args:
- --ip-masq
- --kube-subnet-mgr
resources:
requests:
cpu: "100m"
memory: "50Mi"
limits:
cpu: "100m"
memory: "50Mi"
securityContext:
privileged: false
capabilities:
add: ["NET_ADMIN"]
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
volumeMounts:
- name: run
mountPath: /run/flannel
- name: flannel-cfg
mountPath: /etc/kube-flannel/
volumes:
- name: run
hostPath:
path: /run/flannel
- name: cni
hostPath:
path: /etc/cni/net.d
- name: flannel-cfg
configMap:
name: kube-flannel-cfg
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: kube-flannel-ds-s390x
namespace: kube-system
labels:
tier: node
app: flannel
spec:
selector:
matchLabels:
app: flannel
template:
metadata:
labels:
tier: node
app: flannel
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: beta.kubernetes.io/os
operator: In
values:
- linux
- key: beta.kubernetes.io/arch
operator: In
values:
- s390x
hostNetwork: true
tolerations:
- operator: Exists
effect: NoSchedule
serviceAccountName: flannel
initContainers:
- name: install-cni
image: quay.io/coreos/flannel:v0.12.0-s390x
command:
- cp
args:
- -f
- /etc/kube-flannel/cni-conf.json
- /etc/cni/net.d/10-flannel.conflist
volumeMounts:
- name: cni
mountPath: /etc/cni/net.d
- name: flannel-cfg
mountPath: /etc/kube-flannel/
containers:
- name: kube-flannel
image: quay.io/coreos/flannel:v0.12.0-s390x
command:
- /opt/bin/flanneld
args:
- --ip-masq
- --kube-subnet-mgr
resources:
requests:
cpu: "100m"
memory: "50Mi"
limits:
cpu: "100m"
memory: "50Mi"
securityContext:
privileged: false
capabilities:
add: ["NET_ADMIN"]
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
volumeMounts:
- name: run
mountPath: /run/flannel
- name: flannel-cfg
mountPath: /etc/kube-flannel/
volumes:
- name: run
hostPath:
path: /run/flannel
- name: cni
hostPath:
path: /etc/cni/net.d
- name: flannel-cfg
configMap:
name: kube-flannel-cfg

在很多系统中,vim的快捷键于vi一致,vim有着更强大的颜色显示功能

模式

vi 分为三种模式:

一般模式

使用vi打开一个文件,就是处于一般模式了。在此模式下,你可以使用上下左右按键来移动光标,也可以进行删除字符删除整行操作,可以使用复制粘贴功能;

编辑模式

在一般模式下,按下 i键、a键或r键 都可进入编辑模式。编辑模式下,按下 Esc键 即可退出编辑模式,返回一般模式;

命令模式

在一般模式下,按下 :键、 /键或 ?键 **都可以进入命令模式,在命令模式下,按下Esc键** 即可退出命令模式,返回一般模式;命令模式下,可以进行读取、保存、替换、显示行号等操作;

按键说明

注意,这里输入的字母要区分大小写

序号 按键 效果
1 【h】或左箭头键⬅️ 光标向左移动一个字符
2 【j】键或下箭头键⬇️ 光标向下移动一个字符
3 【k】键或上箭头键⬆️ 光标向上移动一个字符
4 【l】键或右箭头键➡️ 光标向右移动一个字符
5 【Ctrl】+ 【f】 向下移动一页,效果等同【Page Down】
6 【Ctrl】+【b】 向上移动一页,效果等同
7 【Ctrl】+【d】 向下移动半页
8 【Ctrl】+【u】 向上移动半页
9 数值【space】 数字输入后,再按空格键,光标向右移动这一行的数值个字符
10 【Home】 光标移动到当前行的最前一个字符
11 【End】 光标移动到当前行的最后一个字符
12 【H】 光标移动到当前屏幕最上方的一行的第一个字符
13 【M】 光标移动到当前屏幕中央的一行的第一个字符
14 【L】 光标移动到当前屏幕最下方的一行第一个字符
15 数值【G】 光标移动到当前文件的该数值行,例如 【15】【G】,移动到文件第15行
16 【gg】 光标移动到文件到第一行
17 数值【Enter】 光标向下移动数值行,例如 【15】【Enter】
18 【/】字符串 向光标后搜索该字符串
19 【?】字符串 向光标前搜索该字符串
20 【x】 删除当前光标后到一个字符
21 数值【x】 删除当前光标后的数值个字符,例如删除当前光标后15个字符,【15】【x】
22 【dd】 删除当前光标所在行
23 数值【dd】 删除当前光标所在向下的数值行
24 【yy】 复制光标所在的行
25 数值【yy】 复制光标所在向下的数值行
26 【u】 撤销上一个操作
27 【Ctrl】+【r】 重复上一个操作
28 【.】 重复上一个操作
29 【i】 进入编辑模式,从当前光标处开始编辑
30 【a】 进入编辑模式,从当前光标处的下一个字符开始编辑
31 【o】 进入编辑模式,从当前光标处的下一行插入新的一行,开始编辑
32 【r】 进入编辑模式,当前光标所在的字符会被取代一次
33 【Esc】 从编辑模式回到一般模式
34 【:w】 将编辑的数据写入硬盘
35 【:w!】 将编辑的数据强行写入硬盘
36 【:q】 退出vi
37 【:q!】 强行退出vi,不保留修改过的内容
38 【:wq】 保存后,退出vi
39 【:w】文件名 将编辑过的数据保存成另一个文件
40 【:set nu】 显示行号
41 【:set nonu】 取消显示行号

Linux Study 2

常用的远程连接指令

复制本地公钥到远程服务器,后面登录远程服务器就无需输入密码

1
ssh-copy-id username@yourserver

当前厂里需要搭建一个私有的 docker image registry 给内网使用,学习了一下之后,使用 harbor 来实现。

前置条件

硬件

CPU 至少 2 个,推荐 4 个;

内存至少 4G,推荐 8G ;

硬盘至少 40G,如果打算保存的 image ,建议加大硬盘空间;

软件

需要安装以下软件:

名称 版本 描述
docker engine 17.06.0-ce +
docker compose 1.18.0 +
openssl 最新版 用于生成证书和密钥

端口

Harbor 的运行需要占用以下端口:80、443 和 4443

安装

  1. 前往 harbor 的发布页面

  2. 下载最新的离线安装包;

  3. 解压安装包;

    1
    tar -xvf harbor-offline-installer-version.tgz
  4. 获取证书 ca.crtyourdomain.com.crtyourdomain.com.key。若需使用自签名证书,请参考下方证书相关

  5. 把证书复制到 harbor 程序读取到目录,并修改解压出来的文件

    1
    2
    3
    4
    # 复制一份出来修改
    cp harbor.yml.tmpl harbor.yml
    # 然后修改 harbor.yml
    vi harbor.yml

    把 https 部分的配置,指向证书存放目录

    1
    2
    3
    4
    5
    6
    https:
    # https port for harbor, default is 443
    port: 443
    # The path of cert and key files for nginx
    certificate: /your/certificate/path
    private_key: /your/private/key/path
  6. yourdomain.com.crt 转化为 yourdomain.com.cert。由于 Docker daemon 把 .crt 文件解析为证书颁发机构的证书,而把 .cert 文件解析为客户端证书,所以我们需要进行转化

    1
    openssl x509 -inform PEM -in yourdomain.com.crt -out yourdomain.com.cert
  7. 把服务证书、服务私钥和证书颁发机构的证书复制到 Docker 的证书存放目录下(如果目录不存在,需自行创建,目录名称为你的域名 yourdomain.com)

    1
    2
    3
    4
    mkdir -p /etc/docker/certs.d/yourdomain.com/
    cp yourdomain.com.cert /etc/docker/certs.d/yourdomain.com/
    cp yourdomain.com.key /etc/docker/certs.d/yourdomain.com/
    cp ca.crt /etc/docker/certs.d/yourdomain.com/

    放置后,目录应该是这样的:

    1
    2
    3
    4
    5
    /etc/docker/certs.d/
    └── yourdomain.com
    ├── yourdomain.com.cert
    ├── yourdomain.com.key
    └── ca.crt
  8. 重启 Docker 服务

    1
    systemctl restart docker
  9. 更新机器信任证书,需要把服务证书复制到机器的信任区并更新CA信任

    1
    2
    3
    4
    5
    6
    # Ubuntu
    cp yourdomain.com.crt /usr/local/share/ca-certificates/yourdomain.com.crt
    update-ca-certificates
    # CentOS
    cp yourdomain.com.crt /etc/pki/ca-trust/source/anchors/yourdomain.com.crt
    update-ca-trust
  10. 回到 harbor 的安装程序,执行准备工作

    1
    ./prepare

    然后启动 安装

    1
    ./install.sh

证书相关

若需使用自签名证书,可执行以下步骤:

生成证书颁发机构的证书

在生产环境下,应使用受信任的证书颁发机构的证书( CA certificate ),在这里,可以创建自己的的证书颁发机构。要创建证书,可以执行以下步骤:

  1. 生成 CA certificate 的私钥

    1
    openssl genrsa -out ca.key 4096
  2. 生成 CA certificate ,这里会生成一个 ca.crt,即你的证书颁发机构的证书

    这里 -subj 选项是填写你的证书颁发机构的相关信息,如果你使用 FQDN 来连接你的 harbor 主机,请务必指定 common name (CN)。

    C 表示国家缩写码,ST 表示 state 省份或州,L 表示 Location ,O 表示 Organization;

    1
    2
    3
    4
    openssl req -x509 -new -nodes -sha512 -days 3650 \
    -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=yourdomain.com" \
    -key ca.key \
    -out ca.crt

生成服务证书

服务证书通常包含一个 .crt 文件和一个 .key 文件,例如 yourdomain.com.crtyourdomain.com.key

  1. 生成一个服务私钥

    1
    openssl genrsa -out yourdomain.com.key 4096
  2. 生成一个证书签名请求(CSR)

    1
    2
    3
    4
    openssl req -sha512 -new \
    -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=yourdomain.com" \
    -key yourdomain.com.key \
    -out yourdomain.com.csr
  3. 生成一个 x509 v3 拓展文件

    注意这里需要修改DNS成你的域名

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    cat > v3.ext <<-EOF
    authorityKeyIdentifier=keyid,issuer
    basicConstraints=CA:FALSE
    keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
    extendedKeyUsage = serverAuth
    subjectAltName = @alt_names

    [alt_names]
    DNS.1=yourdomain.com
    DNS.2=yourdomain
    DNS.3=hostname
    EOF
  4. 使用 v3.ext 文件生成服务证书

    1
    2
    3
    4
    5
    openssl x509 -req -sha512 -days 3650 \
    -extfile v3.ext \
    -CA ca.crt -CAkey ca.key -CAcreateserial \
    -in yourdomain.com.csr \
    -out yourdomain.com.crt