-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.html
1652 lines (1179 loc) · 167 KB
/
index.html
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
<link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
<link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
<link rel="mask-icon" href="/images/logo.svg" color="#222">
<link rel="stylesheet" href="/css/main.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/[email protected]/css/all.min.css">
<script class="hexo-configurations">
var NexT = window.NexT || {};
var CONFIG = {"hostname":"lessisbetter.site","root":"/","images":"/images","scheme":"Pisces","version":"8.1.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"gitalk","storage":true,"lazyload":false,"nav":null,"activeClass":"gitalk"},"motion":{"enable":false,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果:${query}","hits_time":"找到 ${hits} 个搜索结果(用时 ${time} 毫秒)","hits":"找到 ${hits} 个搜索结果"}};
</script>
<meta name="description" content="云原生、Go语言、区块链">
<meta property="og:type" content="website">
<meta property="og:title" content="Go语言充电站">
<meta property="og:url" content="http://lessisbetter.site/index.html">
<meta property="og:site_name" content="Go语言充电站">
<meta property="og:description" content="云原生、Go语言、区块链">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="大彬">
<meta property="article:tag" content="云原生 k8s kubernetes 区块链 Go语言 后端 技术 人生 编程">
<meta name="twitter:card" content="summary">
<link rel="canonical" href="http://lessisbetter.site/">
<script class="page-configurations">
// https://hexo.io/docs/variables.html
CONFIG.page = {
sidebar: "",
isHome : true,
isPost : false,
lang : 'zh-CN'
};
</script>
<title>Go语言充电站</title>
<noscript>
<style>
body { margin-top: 2rem; }
.use-motion .menu-item,
.use-motion .sidebar,
.use-motion .post-block,
.use-motion .pagination,
.use-motion .comments,
.use-motion .post-header,
.use-motion .post-body,
.use-motion .collection-header {
visibility: visible;
}
.use-motion .header,
.use-motion .site-brand-container .toggle,
.use-motion .footer { opacity: initial; }
.use-motion .site-title,
.use-motion .site-subtitle,
.use-motion .custom-logo-image {
opacity: initial;
top: initial;
}
.use-motion .logo-line {
transform: scaleX(1);
}
.search-pop-overlay, .sidebar-nav { display: none; }
.sidebar-panel { display: block; }
</style>
</noscript>
</head>
<body itemscope itemtype="http://schema.org/WebPage">
<div class="headband"></div>
<main class="main">
<header class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-container">
<div class="site-nav-toggle">
<div class="toggle" aria-label="切换导航栏">
<span class="toggle-line"></span>
<span class="toggle-line"></span>
<span class="toggle-line"></span>
</div>
</div>
<div class="site-meta">
<a href="/" class="brand" rel="start">
<i class="logo-line"></i>
<h1 class="site-title">Go语言充电站</h1>
<i class="logo-line"></i>
</a>
<p class="site-subtitle" itemprop="description">大彬 less is better</p>
</div>
<div class="site-nav-right">
<div class="toggle popup-trigger">
<i class="fa fa-search fa-fw fa-lg"></i>
</div>
</div>
</div>
<nav class="site-nav">
<ul class="main-menu menu">
<li class="menu-item menu-item-主页"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>主页</a></li>
<li class="menu-item menu-item-关于"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li>
<li class="menu-item menu-item-标签"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li>
<li class="menu-item menu-item-归档"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
<li class="menu-item menu-item-专题"><a href="/subject/" rel="section"><i class="fa fa-calendar fa-fw"></i>专题</a></li>
<li class="menu-item menu-item-大牛博客"><a href="/blogs/" rel="section"><i class="fa fa-calendar fa-fw"></i>大牛博客</a></li>
<li class="menu-item menu-item-search">
<a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
</a>
</li>
</ul>
</nav>
<div class="search-pop-overlay">
<div class="popup search-popup"><div class="search-header">
<span class="search-icon">
<i class="fa fa-search"></i>
</span>
<div class="search-input-container"></div>
<span class="popup-btn-close">
<i class="fa fa-times-circle"></i>
</span>
</div>
<div class="search-result-container">
<div class="algolia-stats"><hr></div>
<div class="algolia-hits"></div>
<div class="algolia-pagination"></div>
</div>
</div>
</div>
</div>
<div class="toggle sidebar-toggle">
<span class="toggle-line"></span>
<span class="toggle-line"></span>
<span class="toggle-line"></span>
</div>
<aside class="sidebar">
<div class="sidebar-inner sidebar-overview-active">
<ul class="sidebar-nav">
<li class="sidebar-nav-toc">
文章目录
</li>
<li class="sidebar-nav-overview">
站点概览
</li>
</ul>
<div class="sidebar-panel-container">
<!--noindex-->
<section class="post-toc-wrap sidebar-panel">
</section>
<!--/noindex-->
<section class="site-overview-wrap sidebar-panel">
<div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
<img class="site-author-image" itemprop="image" alt="大彬"
src="https://lessisbetter.site/images/gzh-qrcode-logo-small.png">
<p class="site-author-name" itemprop="name">大彬</p>
<div class="site-description" itemprop="description">云原生、Go语言、区块链</div>
</div>
<div class="site-state-wrap site-overview-item animated">
<nav class="site-state">
<div class="site-state-item site-state-posts">
<a href="/archives">
<span class="site-state-item-count">138</span>
<span class="site-state-item-name">日志</span>
</a>
</div>
<div class="site-state-item site-state-tags">
<a href="/tags/">
<span class="site-state-item-count">78</span>
<span class="site-state-item-name">标签</span></a>
</div>
</nav>
</div>
<div class="links-of-author site-overview-item animated">
<span class="links-of-author-item">
<a href="https://xargin.com/" title="Xargin曹大博客 → https://xargin.com/" rel="noopener" target="_blank">Xargin曹大博客</a>
</span>
<span class="links-of-author-item">
<a href="https://qcrao.github.io/" title="码农桃花源博客 → https://qcrao.github.io/" rel="noopener" target="_blank">码农桃花源博客</a>
</span>
<span class="links-of-author-item">
<a href="https://book.eddycjy.com/golang/" title="煎鱼博客 → https://book.eddycjy.com/golang/" rel="noopener" target="_blank">煎鱼博客</a>
</span>
</div>
</section>
</div>
</div>
</aside>
<div class="sidebar-dimmer"></div>
</header>
<div class="back-to-top">
<i class="fa fa-arrow-up"></i>
<span>0%</span>
</div>
<noscript>
<div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>
<div class="main-inner index posts-expand">
<div class="post-block">
<article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://lessisbetter.site/2021/02/21/k8s-hostport/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="https://lessisbetter.site/images/gzh-qrcode-logo-small.png">
<meta itemprop="name" content="大彬">
<meta itemprop="description" content="云原生、Go语言、区块链">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Go语言充电站">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2021/02/21/k8s-hostport/" class="post-title-link" itemprop="url">k8s hostport</a>
</h2>
<div class="post-meta-container">
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2021-02-21 10:18:46 / 修改时间:10:20:47" itemprop="dateCreated datePublished" datetime="2021-02-21T10:18:46+08:00">2021-02-21</time>
</span>
</div>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>hostport是一种把host的ip:port绑定到pod的port方法,是端口路由,访问host的ip:port即可访问pod的port。</p>
<p>hostport是一个三元组配置:<code><hostIP, hostPort, protocol></code> ,hostIP不配置时,默认为<code>0.0.0.0</code>,<code>protocol</code>默认为<code>TCP</code>。</p>
<p>如下的Pod声明文件,指nginx这个Pod使用hostport,使用的是<0.0.0.0, 80, TCP>,即host上所有IP的80端口的TCP连接,都会转发给该nginx Pod。</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">apiVersion:</span> <span class="string">v1</span></span><br><span class="line"><span class="attr">kind:</span> <span class="string">Pod</span></span><br><span class="line"><span class="attr">metadata:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">nginx</span></span><br><span class="line"><span class="attr">spec:</span></span><br><span class="line"> <span class="attr">containers:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">image:</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">ports:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">containerPort:</span> <span class="number">80</span></span><br><span class="line"> <span class="attr">hostPort:</span> <span class="number">80</span></span><br></pre></td></tr></table></figure>
<p>特点:</p>
<ol>
<li>host上的任何两个pod不能有相同的hostport。由于端口绑定,host上的某个端口只能被1个Pod所绑定。</li>
<li>host的ip和port资源是有限的,所能支持使用hostPort的pod数也是有限的。</li>
<li>灵活性差,需要维护name service。当pod被迁移到新node时,需要使用新node的ip访问pod。</li>
</ol>
<p>node1的ip为<code>10.0.13.3</code>,则:</p>
<ol>
<li><0.0.0.0, 80, TCP> 和 <10.0.13.3, 80, TCP>是冲突的。</li>
<li><10.0.13.3, TCP> 和 <10.0.13.3, 80, TCP>是冲突的。</li>
<li><0.0.0.0, 80, TCP> 和 <10.0.13.3, 80, UDP>是不冲突的。</li>
<li><10.0.13.3, 80, TCP> 和 <10.0.13.3, 80, UDP>是不冲突的。</li>
<li><0.0.0.0, 80, TCP> 和 <10.0.13.3, 90, TCP>是不冲突的。</li>
</ol>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
</div>
<div class="post-block">
<article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://lessisbetter.site/2020/11/18/modify-oh-my-zsh-themes/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="https://lessisbetter.site/images/gzh-qrcode-logo-small.png">
<meta itemprop="name" content="大彬">
<meta itemprop="description" content="云原生、Go语言、区块链">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Go语言充电站">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2020/11/18/modify-oh-my-zsh-themes/" class="post-title-link" itemprop="url">Oh my zsh</a>
</h2>
<div class="post-meta-container">
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2020-11-18 20:09:49" itemprop="dateCreated datePublished" datetime="2020-11-18T20:09:49+08:00">2020-11-18</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2020-12-13 16:44:06" itemprop="dateModified" datetime="2020-12-13T16:44:06+08:00">2020-12-13</time>
</span>
</div>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="配置文件"><a href="#配置文件" class="headerlink" title="配置文件"></a>配置文件</h3><p>选择oh my zsh的原因在于它提供了很多插件和主题。</p>
<p>zsh的配置文件为:<code>~/.zshrc</code>,oh my zsh的许多配置也在此添加。</p>
<h3 id="主题"><a href="#主题" class="headerlink" title="主题"></a>主题</h3><p>习惯使用<a target="_blank" rel="noopener" href="https://github.com/ohmyzsh/ohmyzsh/blob/master/themes/gallois.zsh-theme">gallois主题</a>,但发现它有一个现在无法忍受的缺点,如果当前目录是git仓库,它会在右边显示分支名称和clean状态。当从终端复制文本出来时,分支名称和左边命令的空白,全是空格填充,复制出来就得手动删除。</p>
<p><img src="https://lessisbetter.site/images/2020-11-old-gallois.png"></p>
<p>oh my zsh的所有主题配置都在<code>.oh-my-zsh/themes/</code>目录,文件名称同主题名称,可以对这些主题的一些配置进行修改。</p>
<p>注释掉配置文件中关于git的设置,打开新终端后,就可以不显示git分支信息了。</p>
<p><img src="https://lessisbetter.site/images/2020-11-new-gallois.png"></p>
<p>从此复制出的代码,在也没有多余文本。</p>
<h3 id="插件"><a href="#插件" class="headerlink" title="插件"></a>插件</h3><p>插件目录在:<code>~/.oh-my-zsh/plugins</code>,从中可以浏览自己需要的插件。</p>
<p>比如我常用的插件为:<code>plugins=(git autojump docker kubectl extract)</code>,添加到<code>~/.zshrc</code>即可。</p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
</div>
<div class="post-block">
<article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://lessisbetter.site/2020/11/17/kustomize/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="https://lessisbetter.site/images/gzh-qrcode-logo-small.png">
<meta itemprop="name" content="大彬">
<meta itemprop="description" content="云原生、Go语言、区块链">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Go语言充电站">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2020/11/17/kustomize/" class="post-title-link" itemprop="url">Kustomize:自定义YAML资源文件</a>
</h2>
<div class="post-meta-container">
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2020-11-17 09:54:12" itemprop="dateCreated datePublished" datetime="2020-11-17T09:54:12+08:00">2020-11-17</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2020-12-13 16:44:06" itemprop="dateModified" datetime="2020-12-13T16:44:06+08:00">2020-12-13</time>
</span>
</div>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="kustomize简介"><a href="#kustomize简介" class="headerlink" title="kustomize简介"></a>kustomize简介</h3><p><a target="_blank" rel="noopener" href="https://github.com/kubernetes-sigs/kustomize">kustomize</a>是一个自定义管理原始的YAML模板资源文件的工具,同时无需修改原始的YAML文件。</p>
<p>对于kustomize的理解是,它借助了docker镜像的类似概念:可以一层层的进行覆盖。</p>
<p>Kustmoize有Base和Overlay 2个概念,被依赖的层成为base,当前进行覆盖操作的层成为overlay。所以1个overlay,也可以是另外overlay的base。</p>
<p><img src="https://lessisbetter.site/images/2020-11-kustomize-base-overlay.png" alt="Kustomize base和overlay"></p>
<p>在kubectl v1.14之后,其中融合了kustomize,也就说如果安装了kubectl无需安装kustomize,即可使用kustomize。</p>
<p>把kustmoize的资源文件部署到集群有2个办法:</p>
<ol>
<li>通过kubectl内置的kustomize:<code>kubectl apply -k $KUSTOMIZE_DIR</code> 应用到集群。</li>
<li>集合kustomize和kubectl: <code>kustomize build $KUSTOMIZE_DIR | kubectl apply -f -</code>应用到集群。</li>
</ol>
<h3 id="base"><a href="#base" class="headerlink" title="base"></a>base</h3><p>nginx-deployment.yaml:</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">apiVersion:</span> <span class="string">apps/v1</span></span><br><span class="line"><span class="attr">kind:</span> <span class="string">Deployment</span></span><br><span class="line"><span class="attr">metadata:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">labels:</span></span><br><span class="line"> <span class="attr">app:</span> <span class="string">nginx</span></span><br><span class="line"><span class="attr">spec:</span></span><br><span class="line"> <span class="attr">selector:</span></span><br><span class="line"> <span class="attr">matchLabels:</span></span><br><span class="line"> <span class="attr">app:</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">template:</span></span><br><span class="line"> <span class="attr">metadata:</span></span><br><span class="line"> <span class="attr">labels:</span></span><br><span class="line"> <span class="attr">app:</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">spec:</span></span><br><span class="line"> <span class="attr">containers:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">image:</span> <span class="string">nginx:1.9.1</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">ports:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">containerPort:</span> <span class="number">80</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">http</span></span><br><span class="line"> <span class="attr">volumeMounts:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">mountPath:</span> <span class="string">/user/share/nginx/html</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">data</span></span><br><span class="line"> <span class="attr">restartPolicy:</span> <span class="string">Always</span></span><br><span class="line"> <span class="attr">volumes:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">data</span></span><br><span class="line"> <span class="attr">emptyDir:</span> {}</span><br></pre></td></tr></table></figure>
<p>kustomization.yaml :</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">commonLabels:</span><br><span class="line"> app: kustomize-nginx</span><br><span class="line"></span><br><span class="line">resources:</span><br><span class="line">- nginx-deployment.yaml</span><br></pre></td></tr></table></figure>
<p>build的效果就是,把列出来的resources中的label,都换成<code>app: kustomize-nginx</code>:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">$ diff <(kustomize build .) nginx-deployment.yaml</span><br><span class="line">4,5d3</span><br><span class="line">< labels:</span><br><span class="line">< app: kustomize-nginx</span><br><span class="line">6a5,6</span><br><span class="line">> labels:</span><br><span class="line">> app: nginx</span><br><span class="line">10c10</span><br><span class="line">< app: kustomize-nginx</span><br><span class="line">---</span><br><span class="line">> app: nginx</span><br><span class="line">14c14</span><br><span class="line">< app: kustomize-nginx</span><br><span class="line">---</span><br><span class="line">> app: nginx</span><br><span class="line">17,24c17,24</span><br><span class="line">< - image: nginx:1.9.1</span><br><span class="line">< name: nginx</span><br><span class="line">< ports:</span><br><span class="line">< - containerPort: 80</span><br><span class="line">< name: http</span><br><span class="line">< volumeMounts:</span><br><span class="line">< - mountPath: /user/share/nginx/html</span><br><span class="line">< name: data</span><br><span class="line">---</span><br><span class="line">> - image: nginx:1.9.1</span><br><span class="line">> name: nginx</span><br><span class="line">> ports:</span><br><span class="line">> - containerPort: 80</span><br><span class="line">> name: http</span><br><span class="line">> volumeMounts:</span><br><span class="line">> - mountPath: /user/share/nginx/html</span><br><span class="line">> name: data</span><br><span class="line">27,28c27,28</span><br><span class="line">< - emptyDir: {}</span><br><span class="line">< name: data</span><br><span class="line">---</span><br><span class="line">> - name: data</span><br><span class="line">> emptyDir: {}</span><br></pre></td></tr></table></figure>
<p>应用到k8s,并且查看label。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">[~/workspace/notes/kubernetes/examples/kustomize/base]$ kubectl apply -k .</span><br><span class="line">deployment.apps/nginx created</span><br><span class="line">[~/workspace/notes/kubernetes/examples/kustomize/base]$ kubectl get deploy -o wide</span><br><span class="line">NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR</span><br><span class="line">nginx 1/1 1 1 7s nginx nginx:1.9.1 app=kustomize-nginx</span><br></pre></td></tr></table></figure>
<h3 id="overlay"><a href="#overlay" class="headerlink" title="overlay"></a>overlay</h3><h4 id="修改副本数量"><a href="#修改副本数量" class="headerlink" title="修改副本数量"></a>修改副本数量</h4><p>kustomization.yaml:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">namePrefix: testing-</span><br><span class="line">commonLabels:</span><br><span class="line"> app: kustomize-nginx</span><br><span class="line"> variant: testing</span><br><span class="line"> group: test</span><br><span class="line"></span><br><span class="line">bases: </span><br><span class="line">- ../../base</span><br><span class="line"></span><br><span class="line">patchesStrategicMerge:</span><br><span class="line">- nginx-deployment.yaml</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<ul>
<li><p><code>namePrefix</code>: 资源前缀,比如deployment名称以<code>namePrefix: testing-</code>开头。</p>
</li>
<li><p><code>commonLabels</code>:会使用的标签</p>
</li>
<li><p><code>bases</code>:所基于的base文件</p>
</li>
<li><p><code>patchesStrategicMerge</code>:用合并的方式做path,列出涉及的文件。</p>
</li>
</ul>
<p>nginx-deployment.yaml 为patch的内容:</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">apiVersion:</span> <span class="string">apps/v1</span></span><br><span class="line"><span class="attr">kind:</span> <span class="string">Deployment</span></span><br><span class="line"><span class="attr">metadata:</span></span><br><span class="line"> <span class="attr">name:</span> <span class="string">nginx</span></span><br><span class="line"><span class="attr">spec:</span></span><br><span class="line"> <span class="attr">replicas:</span> <span class="number">2</span></span><br></pre></td></tr></table></figure>
<p>名称<code>metadata.name</code>应当与base中的名称相同,因为使用名称做匹配,所做的patch是当前overlay,名为nginx的deployment的副本数量为2。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">$ kubectl apply -k .</span><br><span class="line">deployment.apps/testing-nginx created</span><br><span class="line">[~/workspace/notes/kubernetes/examples/kustomize/overlay/testing]$</span><br><span class="line">[~/workspace/notes/kubernetes/examples/kustomize/overlay/testing]$ kubectl get deployment</span><br><span class="line">NAME READY UP-TO-DATE AVAILABLE AGE</span><br><span class="line">nginx 1/1 1 1 19m</span><br><span class="line">testing-nginx 2/2 2 2 8s</span><br></pre></td></tr></table></figure>
<h4 id="覆盖镜像"><a href="#覆盖镜像" class="headerlink" title="覆盖镜像"></a>覆盖镜像</h4><p>kustomization.yaml:</p>
<ul>
<li>前缀设置为develop,更新相应tag</li>
<li>通过<code>images</code>更新镜像</li>
<li>Nginx deployment的副本数量修改为5,这种方式与<a href="#%E4%BF%AE%E6%94%B9%E5%89%AF%E6%9C%AC%E6%95%B0%E9%87%8F">上一个修改副本数量</a>相比更简洁。</li>
</ul>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">namePrefix:</span> <span class="string">dev-</span></span><br><span class="line"><span class="attr">commonLabels:</span></span><br><span class="line"> <span class="attr">app:</span> <span class="string">dev-nginx</span></span><br><span class="line"> <span class="attr">variant:</span> <span class="string">dev</span></span><br><span class="line"> <span class="attr">group:</span> <span class="string">develop</span></span><br><span class="line"></span><br><span class="line"><span class="attr">bases:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">../../base</span></span><br><span class="line"><span class="attr">images:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">newTag:</span> <span class="number">1.19</span><span class="number">.0</span></span><br><span class="line"><span class="attr">replicas:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">nginx</span></span><br><span class="line"> <span class="attr">count:</span> <span class="number">5</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>部署到集群:发现kubectl内置的kustomize的<code>replicas</code>并没有得到支持,可能是内置的kustomize版本较老,使用kustomize + kubectl的方式可以部署到集群。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">[~/workspace/notes/kubernetes/examples/kustomize/overlay]$ kubectl delete -k develop</span><br><span class="line">error: json: unknown field "replicas"</span><br><span class="line">[~/workspace/notes/kubernetes/examples/kustomize/overlay]$ kustomize build develop | kubectl apply -f -</span><br><span class="line">deployment.apps/dev-nginx created</span><br><span class="line">[~/workspace/notes/kubernetes/examples/kustomize/overlay]$</span><br><span class="line">[~/workspace/notes/kubernetes/examples/kustomize/overlay]$ kubectl get deploy -o wide</span><br><span class="line">NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR</span><br><span class="line">dev-nginx 5/5 5 5 17s nginx nginx:1.19.0 app=dev-nginx,group=develop,variant=dev</span><br><span class="line">nginx 1/1 1 1 10h nginx nginx:1.9.1 app=kustomize-nginx</span><br><span class="line">testing-nginx 2/2 2 2 10h nginx nginx:1.9.1 app=kustomize-nginx,group=test,variant=testing</span><br></pre></td></tr></table></figure>
<p><a target="_blank" rel="noopener" href="https://kubectl.docs.kubernetes.io/references/kustomize/images/">https://kubectl.docs.kubernetes.io/references/kustomize/images/</a></p>
<h4 id="更多功能"><a href="#更多功能" class="headerlink" title="更多功能"></a>更多功能</h4><p>参考<a target="_blank" rel="noopener" href="https://kubectl.docs.kubernetes.io/zh/api-reference/kustomization/bases/">kustomize reference</a>。</p>
<h3 id="kustomize常用命令"><a href="#kustomize常用命令" class="headerlink" title="kustomize常用命令"></a>kustomize常用命令</h3><h4 id="build"><a href="#build" class="headerlink" title="build"></a>build</h4><p>使用配置文件生成资源的YAML文件,并打印到标准输出。配合kubectl命令可以把资源部署到集群。</p>
<h4 id="edit"><a href="#edit" class="headerlink" title="edit"></a>edit</h4><p>通过命令行修改<code>kustomization.yaml</code>文件。这种办法可以方便的放到脚本中去修改<code>kustomization.yaml</code>,而不是手动去修改。比如operator-sdk的样例,通过edit命令指定资源所要使用的镜像。</p>
<p><strong>edit的set命令</strong>可以做以下几样事:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Available Commands:</span><br><span class="line"> image Sets images and their new names, new tags or digests in the kustomization file</span><br><span class="line"> nameprefix Sets the value of the namePrefix field in the kustomization file.</span><br><span class="line"> namespace Sets the value of the namespace field in the kustomization file</span><br><span class="line"> namesuffix Sets the value of the nameSuffix field in the kustomization file.</span><br><span class="line"> replicas Sets replicas count for resources in the kustomization file</span><br></pre></td></tr></table></figure>
<p>通过命令行修改前缀和镜像:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">[~/workspace/notes/kubernetes/examples/kustomize/overlay]$ cp -r develop develop-dabin</span><br><span class="line">[~/workspace/notes/kubernetes/examples/kustomize/overlay/develop-dabin]$ kustomize edit set nameprefix "develop-dabin-"</span><br><span class="line">[~/workspace/notes/kubernetes/examples/kustomize/overlay/develop-dabin]$ kustomize edit set image "nginx:1.18.0"</span><br><span class="line">[~/workspace/notes/kubernetes/examples/kustomize/overlay/develop-dabin]$ head kustomization.yaml</span><br><span class="line">namePrefix: develop-dabin-</span><br><span class="line">commonLabels:</span><br><span class="line"> app: dev-nginx</span><br><span class="line"> group: develop</span><br><span class="line"> variant: dev</span><br><span class="line"></span><br><span class="line">images:</span><br><span class="line">- name: nginx</span><br><span class="line"> newTag: 1.18.0</span><br><span class="line">replicas:</span><br></pre></td></tr></table></figure>
<p>edit命令除了set外,还有add、remove、fix,能够更加完整的通过命令编辑<code>kustomization.yaml</code>文件。</p>
<p>本文样例:<a target="_blank" rel="noopener" href="https://github.com/Shitaibin/notes/tree/master/kubernetes/examples/kustomize">examples/kustomize</a> 。</p>
<h3 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h3><ul>
<li><a target="_blank" rel="noopener" href="https://kubectl.docs.kubernetes.io/references/kustomize/nameprefix/">kustomization.yaml的写法</a></li>
</ul>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
</div>
<div class="post-block">
<article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://lessisbetter.site/2020/11/16/k8s-jsonpath/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="https://lessisbetter.site/images/gzh-qrcode-logo-small.png">
<meta itemprop="name" content="大彬">
<meta itemprop="description" content="云原生、Go语言、区块链">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Go语言充电站">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2020/11/16/k8s-jsonpath/" class="post-title-link" itemprop="url">利用JSONPath提取Kubernetes资源信息</a>
</h2>
<div class="post-meta-container">
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2020-11-16 16:52:00" itemprop="dateCreated datePublished" datetime="2020-11-16T16:52:00+08:00">2020-11-16</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2020-12-13 16:44:06" itemprop="dateModified" datetime="2020-12-13T16:44:06+08:00">2020-12-13</time>
</span>
</div>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="JSONPath基础"><a href="#JSONPath基础" class="headerlink" title="JSONPath基础"></a>JSONPath基础</h3><p>XML有一个非常强大的解析工具是XPath,用于提取XML中的内容。之后也出现了一种高效提取JSON内容的工具,它被称为JSONPath。</p>
<p>JSONPath现在有很多不同的实现,不同的实现支持的提取语法略有不同,比如Goessner的JSONPath如下:</p>
<p><img src="https://lessisbetter.site/images/2020-11-goessner-jsonpath.png" alt="goessner jsonpath"></p>
<p><a target="_blank" rel="noopener" href="https://github.com/alibaba/fastjson/wiki/JSONPath#3-%E6%94%AF%E6%8C%81%E8%AF%AD%E6%B3%95">fastjson的JSONPath</a>支持的更加丰富。</p>
<p>示例JSON内容:</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">{</span><br><span class="line"> <span class="attr">"store"</span>: {</span><br><span class="line"> <span class="attr">"book"</span>: [</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">"category"</span>: <span class="string">"reference"</span>,</span><br><span class="line"> <span class="attr">"author"</span>: <span class="string">"Nigel Rees"</span>,</span><br><span class="line"> <span class="attr">"title"</span>: <span class="string">"Sayings of the Century"</span>,</span><br><span class="line"> <span class="attr">"price"</span>: <span class="number">8.95</span></span><br><span class="line"> },</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">"category"</span>: <span class="string">"fiction"</span>,</span><br><span class="line"> <span class="attr">"author"</span>: <span class="string">"Evelyn Waugh"</span>,</span><br><span class="line"> <span class="attr">"title"</span>: <span class="string">"Sword of Honour"</span>,</span><br><span class="line"> <span class="attr">"price"</span>: <span class="number">12.99</span></span><br><span class="line"> },</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">"category"</span>: <span class="string">"fiction"</span>,</span><br><span class="line"> <span class="attr">"author"</span>: <span class="string">"Herman Melville"</span>,</span><br><span class="line"> <span class="attr">"title"</span>: <span class="string">"Moby Dick"</span>,</span><br><span class="line"> <span class="attr">"isbn"</span>: <span class="string">"0-553-21311-3"</span>,</span><br><span class="line"> <span class="attr">"price"</span>: <span class="number">8.99</span></span><br><span class="line"> },</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">"category"</span>: <span class="string">"fiction"</span>,</span><br><span class="line"> <span class="attr">"author"</span>: <span class="string">"J. R. R. Tolkien"</span>,</span><br><span class="line"> <span class="attr">"title"</span>: <span class="string">"The Lord of the Rings"</span>,</span><br><span class="line"> <span class="attr">"isbn"</span>: <span class="string">"0-395-19395-8"</span>,</span><br><span class="line"> <span class="attr">"price"</span>: <span class="number">22.99</span></span><br><span class="line"> }</span><br><span class="line"> ],</span><br><span class="line"> <span class="attr">"bicycle"</span>: {</span><br><span class="line"> <span class="attr">"color"</span>: <span class="string">"red"</span>,</span><br><span class="line"> <span class="attr">"price"</span>: <span class="number">19.95</span></span><br><span class="line"> }</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">"expensive"</span>: <span class="number">10</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>以例子讲解几个最常用的语法:</p>
<table>
<thead>
<tr>
<th>语法</th>
<th>语法含义</th>
<th>例子</th>
<th>例子含义</th>
</tr>
</thead>
<tbody><tr>
<td>$</td>
<td>JSON内容的根对象,所有的JSONPath都是以<code>$</code>为开头。</td>
<td><code>$</code></td>
<td><a target="_blank" rel="noopener" href="http://jsonpath.herokuapp.com/?path=$">JSON内容本身</a>。</td>
</tr>
<tr>
<td>.</td>
<td>后面跟子对象。</td>
<td><code>$.expensive</code></td>
<td><a target="_blank" rel="noopener" href="http://jsonpath.herokuapp.com/?path=$.expensive">提取根对象的expensive字段的值</a>。</td>
</tr>
<tr>
<td>..</td>
<td>递归扫描子对象。</td>
<td><code>$..price</code></td>
<td><a target="_blank" rel="noopener" href="http://jsonpath.herokuapp.com/?path=$..price">提取对象中所有price字段的值,结果会包含所有book和bicycle中的价格</a>。</td>
</tr>
<tr>
<td>[num]</td>
<td>以下标访问数组。语法类似Python,num为负数时,代表倒数。</td>
<td><code>$.store.book[0]</code></td>
<td><a target="_blank" rel="noopener" href="http://jsonpath.herokuapp.com/?path=$.store.book%5B0%5D">获取第一本书</a>。</td>
</tr>
<tr>
<td>[num1, num2,num3]</td>
<td>以下标获取数据中多个数据。</td>
<td><code>$.store.book[0,2]</code></td>
<td><a target="_blank" rel="noopener" href="http://jsonpath.herokuapp.com/?path=$.store.book%5B0,2%5D">获取第1、3本书</a>。</td>
</tr>
<tr>
<td>[start:end]</td>
<td>获取数组区间[start, end)的数据。</td>
<td><code>$.store.book[0:2]</code></td>
<td><a target="_blank" rel="noopener" href="http://jsonpath.herokuapp.com/?path=$.store.book%5B0:2%5D">获取前2本书</a>。</td>
</tr>
<tr>
<td>[start:end:step]</td>
<td>获取数组区间[start, end)的数据,但以step为步长提取数据。<strong>但不是所有JSONPath实现都支持</strong>。</td>
<td><code>$.store.book[0:3:2]</code></td>
<td>获取第1~3本书,以步长为2提取,也即是第1、3本书。</td>
</tr>
<tr>
<td>[*]</td>
<td>通配符,匹配所有子对象。</td>
<td><code>$.store.*.price</code></td>
<td><a target="_blank" rel="noopener" href="http://jsonpath.herokuapp.com/?path=$.store.*.price">匹配store子对象中的价格,因为book的价格,是更下一级,所以只能匹配到bicycle的价格</a>。</td>
</tr>
<tr>
<td>?()</td>
<td>过滤符,可以理解成SQL的Where。</td>
<td><code>$.store.[?(@.category=="fiction")].author</code></td>
<td><a target="_blank" rel="noopener" href="http://jsonpath.herokuapp.com/?path=$.store.%5B?(@.category==%22fiction%22).author%5D">获取类别为fiction的书籍作者列表</a>。</td>
</tr>
<tr>
<td>@</td>
<td>当前对象,配合<code>?()</code>很好用。</td>
<td></td>
<td></td>
</tr>
</tbody></table>
<h3 id="k8s使用jsonpath"><a href="#k8s使用jsonpath" class="headerlink" title="k8s使用jsonpath"></a>k8s使用jsonpath</h3><p>kubectl没有提供查看Pod内容器的名称,怎么办呢?可以利用jsonpath或者go template实现。</p>
<p>json格式输出结果通常是嵌套多层,使用jsonpath可以忽略中间层次,而go template不行,这是jsonpath比go template好用的地方。</p>
<p>看一个略微复杂k8s使用jsonpath列出所有pod的所有容器名称和镜像的样例:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl get pods --all-namespaces -o=jsonpath='{range .items[*]}{"pod: "}{.metadata.name} {"\n"}{range .spec.containers[*]}{"\tcontainer: "}{.name}{"\n\timage: "}{.image}{"\n"}{end}{end}'</span><br></pre></td></tr></table></figure>
<p>发现<code>jsonpath=''</code>与标准的jsonpath并不一样:</p>
<ul>
<li>没有<code>$</code></li>
<li>一堆<code>{}</code></li>
<li>还有<code>range, {"\n"}</code>等</li>
</ul>
<p>那是因为<a target="_blank" rel="noopener" href="https://kubernetes.io/docs/reference/kubectl/jsonpath/">k8s对jsonpath的支持</a>有以下特性:</p>
<ol>
<li>在jsonpath中使用<code>""</code>包含文本,这样在输出的结果可以显示自定义的字符串,还能进行换行、Tab等。</li>
<li>支持使用<code>range .. end</code>迭代数组,原生的jsonpath没有办法提取数组元素中的多个子对象,使用range达成效果,比如想获得容器的名称镜像。</li>
<li>支持<code>-num</code>获取数组的倒数位置的元素</li>
<li>可以省略<code>$</code>,太好了</li>
<li>每一段jsonpath使用<code>{}</code>连接</li>
</ol>
<p>刚开始使用jsonpath时,有种眼花缭乱的感觉,我们就拆解下上面的样例jsonpath。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl get pods --all-namespaces -o=jsonpath='{.items[*].metadata.name}'</span><br></pre></td></tr></table></figure>
<p><img src="https://lessisbetter.site/images/2020-11-1-pod-name.png"></p>
<p>先提取每个pod的名称,这个还和原生的jsonpath一样。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl get pods --all-namespaces -o=jsonpath='{range .items[*]}{"pod: "}{.metadata.name}{"\n"}{end}'</span><br></pre></td></tr></table></figure>
<p><img src="https://lessisbetter.site/images/2020-11-2-pod-name-range.png"></p>
<p>因为每个pod还要取容器名称和镜像,所以最好每个pod占一行,我们需要使用<code>range .. end</code>处理每一个pod,列pod所含的容器。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">kubectl get pods --all-namespaces -o=jsonpath='{range .items[*]}{"pod: "}{.metadata.name}{"\n"}{"\tcontainer: "}{.spec.containers[*].name}, {.spec.containers[*].image}{"\n"}{end}'</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><img src="https://lessisbetter.site/images/2020-11-3-pod-containers.png"></p>
<p>可以看到每个pod内可能包含多个容器,所以也得用<code>range .. end</code>去处理pod的每一个container。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">kubectl get pods --all-namespaces -o=jsonpath='{range .items[*]}{"pod: "}{.metadata.name} {"\n"}{range .spec.containers[*]}{"\tcontainer: "}{.name}{"\n\timage: "}{.image}{"\n"}{end}{end}'</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><img src="https://lessisbetter.site/images/2020-11-4-pod-contianers-image.png"></p>
<p>上面提到使用jsonpath可以简化层级,因为<code>containers</code>这个名词在层级中是独有的,不像<code>name</code>可能是存在于多个层级,所以可以使用<code>..</code>简化:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl get pods --all-namespaces -o=jsonpath='{range .items[*]}{"pod: "}{.metadata.name} {"\n"}{range ..containers[*]}{"\tcontainer: "}{.name}{"\n\timage: "}{.image}{"\n"}{end}{end}'</span><br></pre></td></tr></table></figure>
<p><img src="https://lessisbetter.site/images/2020-11-5-simplify-pod-containers.png"></p>
<p>最后看一下过滤的使用,只想列出<code>weave</code>的pod的容器和镜像:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl get pods --all-namespaces -o=jsonpath='{range .items[?(@.metadata.name=="weave-net-sqjzh")]}{"pod: "}{.metadata.name} {"\n"}{range ..containers[*]}{"\tcontainer: "}{.name}{"\n\timage: "}{.image}{"\n"}{end}{end}'</span><br></pre></td></tr></table></figure>
<p><img src="https://lessisbetter.site/images/2020-11-6-pod-filter.png"></p>
<h3 id="练习"><a href="#练习" class="headerlink" title="练习"></a>练习</h3><p>使用JSONPath获取:</p>
<ol>
<li>Pod的名称和IP</li>
<li>Pod退出原因</li>
</ol>
<h3 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h3><ul>
<li><a target="_blank" rel="noopener" href="https://goessner.net/articles/JsonPath/">goessner: JSONPath - XPath for JSON</a></li>
<li><a target="_blank" rel="noopener" href="https://kubernetes.io/docs/reference/kubectl/jsonpath/">Kubernetes JSONPath Support</a>,<a target="_blank" rel="noopener" href="http://docs.kubernetes.org.cn/67.html">一个中文版本</a></li>
</ul>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
</div>
<div class="post-block">
<article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://lessisbetter.site/2020/11/10/dockerfile-go/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="https://lessisbetter.site/images/gzh-qrcode-logo-small.png">
<meta itemprop="name" content="大彬">
<meta itemprop="description" content="云原生、Go语言、区块链">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Go语言充电站">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2020/11/10/dockerfile-go/" class="post-title-link" itemprop="url">Go程序Dockerfile模板</a>
</h2>
<div class="post-meta-container">
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2020-11-10 13:25:04" itemprop="dateCreated datePublished" datetime="2020-11-10T13:25:04+08:00">2020-11-10</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2020-12-13 14:18:43" itemprop="dateModified" datetime="2020-12-13T14:18:43+08:00">2020-12-13</time>
</span>
</div>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="模板"><a href="#模板" class="headerlink" title="模板"></a>模板</h2><figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># build时设置版本 --build-arg GO_VERSION=1.13,默认为go1.15</span></span><br><span class="line"><span class="keyword">ARG</span> GO_VERSION=<span class="number">1.15</span></span><br><span class="line"><span class="keyword">FROM</span> golang:${GO_VERSION} AS builder</span><br><span class="line"><span class="keyword">ENV</span> GOPROXY=<span class="string">"https://goproxy.cn"</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">ENV</span> APP_PATH=<span class="string">"/app/goapp"</span></span><br><span class="line"><span class="keyword">WORKDIR</span><span class="bash"> <span class="string">"/app"</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 拷贝构建文件</span></span><br><span class="line"><span class="keyword">COPY</span><span class="bash"> . .</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 编译</span></span><br><span class="line"><span class="keyword">RUN</span><span class="bash"> go mod download</span></span><br><span class="line"><span class="keyword">RUN</span><span class="bash"> CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -a -o <span class="variable">${APP_PATH}</span> .</span></span><br><span class="line"><span class="keyword">RUN</span><span class="bash"> ls </span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 构建运行镜像</span></span><br><span class="line"><span class="keyword">FROM</span> alpine:<span class="number">3.10</span> AS final</span><br><span class="line"></span><br><span class="line"><span class="keyword">ENV</span> APP_PATH=<span class="string">"/app/goapp"</span></span><br><span class="line"><span class="keyword">WORKDIR</span><span class="bash"> <span class="string">"/app"</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 拷贝程序,如有必要另外拷贝其他文件</span></span><br><span class="line"><span class="keyword">COPY</span><span class="bash"> --from=builder <span class="variable">${APP_PATH}</span> <span class="variable">${APP_PATH}</span> </span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 运行程序</span></span><br><span class="line"><span class="keyword">ENTRYPOINT</span><span class="bash"> [<span class="string">"/app/goapp"</span>]</span></span><br></pre></td></tr></table></figure>
<p>构建命令:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker build -t app:1.0 --build-arg GO_VERSION=1.13 .</span><br></pre></td></tr></table></figure>
<p>模板说明:</p>
<ol>
<li>构建命令不指定<code>--build-arg GO_VERSION=1.13</code>时,默认使用go1.15进行编译。</li>
<li>使用<code>alpine</code>作为运行基础镜像,减小镜像大小。</li>
<li>Dockerfile文件放到main.go所在目录。</li>
<li>把<code>goapp</code>替换成真正的程序名称。</li>
</ol>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
</div>
<div class="post-block">
<article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://lessisbetter.site/2020/11/10/dockerfile-arg/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="https://lessisbetter.site/images/gzh-qrcode-logo-small.png">
<meta itemprop="name" content="大彬">
<meta itemprop="description" content="云原生、Go语言、区块链">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Go语言充电站">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2020/11/10/dockerfile-arg/" class="post-title-link" itemprop="url">Dockerfile ARG指令</a>
</h2>
<div class="post-meta-container">
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2020-11-10 10:52:35" itemprop="dateCreated datePublished" datetime="2020-11-10T10:52:35+08:00">2020-11-10</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2020-12-13 14:18:43" itemprop="dateModified" datetime="2020-12-13T14:18:43+08:00">2020-12-13</time>
</span>
</div>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>Docker的文档关于<a target="_blank" rel="noopener" href="https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact">ARG和FROM指令配合使用</a>做了详细说明:</p>
<p>ARG用于传入外部参数,定义在FROM指令前,FROM后的其他指令无法使用ARG定义的环境变量,如果FROM指令后的指令要使用ARG定义的值,需要在FROM后再次定义。如果FROM不使用定义的ARG,可以直接定义到FROM后。</p>
<h3 id="传递参数"><a href="#传递参数" class="headerlink" title="传递参数"></a>传递参数</h3><h3 id="定义在FROM前"><a href="#定义在FROM前" class="headerlink" title="定义在FROM前"></a>定义在FROM前</h3><figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ARG</span> UBUNTU_VERSION=<span class="number">16.04</span></span><br><span class="line"><span class="keyword">FROM</span> ubuntu:${UBUNTU_VERSION}</span><br><span class="line"><span class="keyword">RUN</span><span class="bash"> env | grep UBUNTU_VERSION</span></span><br></pre></td></tr></table></figure>
<p>从结果可以看到FROM后的指令<strong>无法访问</strong>ARG定义的<code>UBUNTU_VERSION</code>。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">$ docker build -t test:afterfrom .</span><br><span class="line">Sending build context to Docker daemon 37.38kB</span><br><span class="line">Step 1/3 : ARG UBUNTU_VERSION=16.04</span><br><span class="line">Step 2/3 : FROM ubuntu:${UBUNTU_VERSION}</span><br><span class="line"> ---> 4b22027ede29</span><br><span class="line">Step 3/3 : RUN env | grep UBUNTU_VERSION</span><br><span class="line"> ---> Running in 8e93ca5e376b</span><br><span class="line">The command '/bin/sh -c env | grep UBUNTU_VERSION' returned a non-zero code: 1</span><br></pre></td></tr></table></figure>
<h3 id="定义在FROM后"><a href="#定义在FROM后" class="headerlink" title="定义在FROM后"></a>定义在FROM后</h3><figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ARG</span> UBUNTU_VERSION=<span class="number">16.04</span></span><br><span class="line"><span class="keyword">FROM</span> ubuntu:${UBUNTU_VERSION}</span><br><span class="line"><span class="keyword">ARG</span> UBUNTU_VERSION</span><br><span class="line"><span class="keyword">RUN</span><span class="bash"> env | grep UBUNTU_VERSION</span></span><br></pre></td></tr></table></figure>
<p>从结果可以看到FROM后的指令<strong>可以访问</strong>ARG定义的<code>UBUNTU_VERSION</code>。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">$ docker build -t test:afterfrom .</span><br><span class="line">Sending build context to Docker daemon 37.38kB</span><br><span class="line">Step 1/4 : ARG UBUNTU_VERSION=16.04</span><br><span class="line">Step 2/4 : FROM ubuntu:${UBUNTU_VERSION}</span><br><span class="line"> ---> 4b22027ede29</span><br><span class="line">Step 3/4 : ARG UBUNTU_VERSION</span><br><span class="line"> ---> Using cache</span><br><span class="line"> ---> e3bae0875e66</span><br><span class="line">Step 4/4 : RUN env | grep UBUNTU_VERSION</span><br><span class="line"> ---> Running in 02438aff1f75</span><br><span class="line">UBUNTU_VERSION=16.04</span><br><span class="line">Removing intermediate container 02438aff1f75</span><br><span class="line"> ---> 646eec496165</span><br><span class="line">Successfully built 646eec496165</span><br><span class="line">Successfully tagged test:afterfrom</span><br></pre></td></tr></table></figure>
<h3 id="传递多个ARG参数"><a href="#传递多个ARG参数" class="headerlink" title="传递多个ARG参数"></a>传递多个ARG参数</h3><figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ARG</span> UBUNTU_VERSION=<span class="number">16.04</span></span><br><span class="line"><span class="keyword">ARG</span> FILE_NAME=test</span><br><span class="line"><span class="keyword">FROM</span> ubuntu:${UBUNTU_VERSION}</span><br><span class="line"></span><br><span class="line"><span class="keyword">ARG</span> FILE_NAME</span><br><span class="line"><span class="keyword">WORKDIR</span><span class="bash"> /<span class="built_in">test</span>/</span></span><br><span class="line"><span class="keyword">RUN</span><span class="bash"> touch <span class="variable">${FILE_NAME}</span></span></span><br><span class="line"><span class="keyword">RUN</span><span class="bash"> ls <span class="variable">${FILE_NAME}</span></span></span><br></pre></td></tr></table></figure>
<p>传递多个参数需要多次使用<code>--build-arg</code>,可以看到传递的<code>FILE_NAME=app</code>生效了,而不是默认值。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">$ docker build -t test:twoargs --build-arg UBUNTU_VERSION=18.04 --build-arg FILE_NAME=app .</span><br><span class="line">Sending build context to Docker daemon 37.38kB</span><br><span class="line">Step 1/7 : ARG UBUNTU_VERSION=16.04</span><br><span class="line">Step 2/7 : ARG FILE_NAME=test</span><br><span class="line">Step 3/7 : FROM ubuntu:${UBUNTU_VERSION}</span><br><span class="line">18.04: Pulling from library/ubuntu</span><br><span class="line">171857c49d0f: Pull complete</span><br><span class="line">419640447d26: Pull complete</span><br><span class="line">61e52f862619: Pull complete</span><br><span class="line">Digest: sha256:646942475da61b4ce9cc5b3fadb42642ea90e5d0de46111458e100ff2c7031e6</span><br><span class="line">Status: Downloaded newer image for ubuntu:18.04</span><br><span class="line"> ---> 56def654ec22</span><br><span class="line">Step 4/7 : ARG FILE_NAME</span><br><span class="line"> ---> Running in bee623328f35</span><br><span class="line">Removing intermediate container bee623328f35</span><br><span class="line"> ---> 52f803da2959</span><br><span class="line">Step 5/7 : WORKDIR /test/</span><br><span class="line"> ---> Running in fa4542584af1</span><br><span class="line">Removing intermediate container fa4542584af1</span><br><span class="line"> ---> 5ebd782db9b8</span><br><span class="line">Step 6/7 : RUN touch ${FILE_NAME}</span><br><span class="line"> ---> Running in 0cd5723b744d</span><br><span class="line">Removing intermediate container 0cd5723b744d</span><br><span class="line"> ---> 920c6bd75bab</span><br><span class="line">Step 7/7 : RUN ls ${FILE_NAME}</span><br><span class="line"> ---> Running in b94a33093ddf</span><br><span class="line">app</span><br><span class="line">Removing intermediate container b94a33093ddf</span><br><span class="line"> ---> 804bf831059a</span><br><span class="line">Successfully built 804bf831059a</span><br><span class="line">Successfully tagged test:twoargs</span><br></pre></td></tr></table></figure>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
</div>
<div class="post-block">
<article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://lessisbetter.site/2020/09/28/misunderstanding-iowait/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="https://lessisbetter.site/images/gzh-qrcode-logo-small.png">
<meta itemprop="name" content="大彬">
<meta itemprop="description" content="云原生、Go语言、区块链">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Go语言充电站">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2020/09/28/misunderstanding-iowait/" class="post-title-link" itemprop="url">被误解的iowait</a>
</h2>
<div class="post-meta-container">
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2020-09-28 16:57:19" itemprop="dateCreated datePublished" datetime="2020-09-28T16:57:19+08:00">2020-09-28</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2020-12-13 14:18:43" itemprop="dateModified" datetime="2020-12-13T14:18:43+08:00">2020-12-13</time>
</span>
</div>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>有一个cpu指标叫<code>iowait</code>或者<code>wa</code>,在top、iostat、vmstat命令中都可以看到这一项。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">[~]$ top</span><br><span class="line">top - 08:58:06 up 26 days, 23:20, 1 user, load average: 0.07, 0.23, 0.26</span><br><span class="line">Tasks: 164 total, 1 running, 111 sleeping, 0 stopped, 0 zombie</span><br><span class="line">%Cpu(s): 2.5 us, 1.2 sy, 0.0 ni, 96.2 id, 0.1 wa, 0.0 hi, 0.1 si, 0.0 st</span><br><span class="line">KiB Mem : 8167548 total, 698220 free, 996640 used, 6472688 buff/cache</span><br><span class="line">KiB Swap: 0 total, 0 free, 0 used. 7061988 avail Mem</span><br><span class="line"></span><br><span class="line">[~]$ iostat</span><br><span class="line">Linux 4.15.0-112-generic (shitaibin-x) 09/28/20 _x86_64_ (4 CPU)</span><br><span class="line"></span><br><span class="line">avg-cpu: %user %nice %system %iowait %steal %idle</span><br><span class="line"> 1.02 0.00 0.55 0.86 0.00 97.56</span><br><span class="line"></span><br><span class="line">Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn</span><br><span class="line">loop0 0.00 0.00 0.00 5 0</span><br><span class="line">vda 13.32 2.65 84.15 6182326 196098973</span><br><span class="line"></span><br><span class="line">[~]$</span><br><span class="line">[~]$</span><br><span class="line">[~]$ vmstat</span><br><span class="line">procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----</span><br><span class="line"> r b swpd free buff cache si so bi bo in cs us sy id wa st</span><br><span class="line"> 0 0 0 685616 177356 6306652 0 0 1 21 5 3 1 1 98 1 0</span><br></pre></td></tr></table></figure>
<p>这个指标的字面含义是等待IO的时间(百分比),很多人会认为这个指标暗示这IO瓶颈,然而这是有一定无解的,iowait高不一定有IO瓶颈。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">iowait = CPU空闲时间 / CPU总时间 ,前提CPU在等待至少一项IO操作完成</span><br></pre></td></tr></table></figure>
<p>所以它真正的含义是有未完成的IO操作时,CPU空闲的时间。</p>
<p>这2个资料都把iowait讲解的很清晰,并且举例iowait和IO瓶颈无关的例子。</p>
<ul>
<li><a target="_blank" rel="noopener" href="http://linuxperf.com/?p=33">理解iowait</a></li>
<li><a target="_blank" rel="noopener" href="https://www.inspurpower.com/upload/file/1583309942.pdf">浪潮:CPU iowait详解</a></li>
</ul>
<p>例子:</p>
<ul>
<li>低iowait,高IO的例子:IO高同时CPU计算也高,这样CPU的空闲时间少,造成iowait比较低,CPU密集掩盖了IO密集。</li>
<li>高iowait,低IO的例子:CPU计算很少,CPU基本空闲,但也有1个进程在IO,所以iowait高,但实际IO根本没任何瓶颈。</li>
</ul>
</div>
<footer class="post-footer">
<div class="post-eof"></div>