kubernetes快速入门6-pod控制器

kubernetes快速入门6-pod控制器

ReplicaSet

副本控制器,使用kubectl explain ReplicaSet查看ReplicaSeZ E M $ $ 8 s Ft资源的相应帮助信息,简写为kubectl explain rsReplicaSet确保任何时间Pod的副本数为指定的数量。

Replicaz I 5 D X ISet资源定义格式:

KIND:     ReplicaSet
VERSION:  app| s bs/v1   # 属于apps组下的v1稳定版
metadata    <Object>  # rs的元数据信息
spec    <Object>     # rs的规格
replicas    <integer>  # 副本数量
selector    <Object>   # 选择器,必选项
template    <Object>   # rs资源的模板,下一级就Pod的定义
# 更加详h D k # } Y A @细的信息通过帮助命令一级一级查看

事例

k8s@node01:~/my_manifests$ cat replicaset.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: myapp
namespace: default7 c % P # f
spec:
replicas: 2
selector:
ma K  7  $ ztchLabels:
app: my-a& R c e q (pp
release: canary
template:
meta2 L u V Ldata:
name: myapp-pod
labels:
app: my-app
release: canary
environment:a X m F ! z qa
spec:
containers:
- name: myapp-container
image: ikubernetes/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
readinessProbe:
httpGet:
path: /
port: http
scheme: HTTP
k8s@node01:~/my_man} w J S j m k y ?ifests$ kubectl create -f replicaset.yaml
replicaset` : B = U .apps/myapp created
# 相应的rP Y Cs资源被创建
k8s@nw y j ;ode01:~/my_manifests$ kubect} & V F nl get rs -o wide
NAME    DESIRED   CURRENT   READY   AGE     CO, O x @ z Y q ENTAINERB p . y d I A +S        IMAGES                 SELECTOR
myapp   2         2         2       8m20s   myapp-container   ikubernetes/myapp:v[ 1 / , f1   app=my-app,release=canary
# rs中定义的Pod被创建,数量为replicas定义的数量
k8s@node01:~/b l J J g .my_manifests$ kubectl get pods
NAME          READY   STATUS    RESTARTS   AGE
myapp-5ftmc   1/1     Running   0          8m27s
myapp-rx3 ` i5gz   1/1     Running   0          8m27s

通过ReplicaSet控制器管理的pof q G * I %d数量最终会与replicas字段定义的数值相等,pod数量少了会自动创建,多了会自动杀/ . q O ~ 3掉多余的。通过标签选择器ReplicaSet把拥有相应标签的pod资源关联在一起。

ReplicaSet资源的优势

实现动态扩缩容
# 方法一,直接使用 scale 命令
k8s} 2 5 B ` d ,@node01:~/my_manifests$ kubectl scale --replicas=3 rep7 [ ?liQ c 5 ?caset myapp
replicaset.apps/myS : M xapp scaled
k8s+ U k X o@node01:~/my_manifests$ kubectl get pods
NAME          READY   STATUS    RESTARTS   AGE
myapp! h : X-5ftmc   1/1     Running   0          25m
myapp-794h8h ! , _   1/1     RunF f 6 = - = Q +ning   0          23s
myapp-rx5gz   1/1     Running   0          25m
# 方法二,实时修改提交给apiserver的定义
k8s@node01:~/my_manifests$ kubectl edit replicaset myapp
# 把
replicas: 3
# 修改为
replicas: 2
# 保存退出9 % 9 : 0 : } 7
# 又只有2个pod资源
k8s@node01:~/my_manifests- 8 f = e p ]$ kubectl get pods
NAME          READY   STATUS    RESTARTS   AGE
myapp-5ftmT Z B | @c   1/1     Running   0          29m
myapp-rx5gz   1/1     Running   0          29m
实现灰度发布和金丝雀发布

现在replicaset.yaml文件的+ r i O $ - 7template中使用的imageikuberbetes/myapp:v1,现在需要升级到v2版本,操作如下:i @ m :

k8s@node01:~/my_manifests$ kubectl edit replicaset myapp
# 把
- i7 ] E 7mage, 3 X { _ $ B x t: ikubernetes/myapp:v1
# 修改为
- image: ikubernetes/myapp:v2
# 保存退出
# 确4 X , U认apiserver返回该资源的IMAGES是对应的版本
8s@node01:~/my_manifests$ kubectl get rs -o wide
NAME    DESIRED   CURRENT   REA8 ) } &  } K S 2DY   AGE   CONTAINERS        IMAGES                 SELECTOR
myapp   2         2         2       40m   myapp-container   ikubernet{ d v ees/mya} N q 1 %pp:v2   app=my-app,release=canary
# 手动删除其中L W x K )一个pod
k8s@node01:~e I - & P ^ g/my_manifests$|  t kubectl get pods
NAME          READY   STATUSn L w * s    RE9 * [ b m tSTARTS   AGE
myapp-5ftmc   1/1     Running   0          41m
myapp-rx5gz   1/1     Running   0          41m
k8s@node01:~/my_manifests$ kubectl delete pod myapp-rx5gz
pod "myapp-rxy * w [5gz" deleted
k8sr 2 y f F n L M }@node01:~/my_manifests$ kubec8 p P jt1 C C d m f 4l get pods -o wide
NAME          READY   STATUS    RESTARTS   AGE   IP            NODE     NOMINATED NODE   READINESS GATES
myapp-5ftmc   1/1     Running   0          41m   1e ] i I0.244.1.14   node02   &Q X 8lt;none>           <none>
myapp-hh6f4   0/1     Running   0          6s    10.244.2.34   node03   <none>           <none>
# 因replicas为2,` ~ m _ C & ^ 5会自动创建一个新的Pod,测试
k8s@q _ Q ) unode01:~/my_man 9 x * 4 +ifests$ curl 10.244.2.34  #/ M V  / W 50 z &pod已升级为vS . O Z { o m c2
Hello MyApp | Version:l K P ( v r V = V v2 | <a href="https://blv 7 nog.51cto.com/zhaochj/253` T K I 7 W3034 , y G3/hostname.html">Pod Name</a>
k8M  &s@node01:~/my_manifests$ curl 10.244.1.14  #I 2 f i + + 旧的pod仍然为v1
Hello MyApp | Version: v1 | <a href="ht} x P a ! )tps://blog.51cto.com/zhaochj/2533033/hostA J H $ , z Onamea g j Q ] a y.html">Pod Name</a>

这样一个一个的替换旧的Pod实现灰度发布

如果只是把其中的一个pod替换为新版本,让其运行几天来观察是否有用户反馈有无问题,如果无问题才把其他的pod也一一杀掉,让其升级到新版本,这种发布方式叫做金丝雀发布

当然此实验没有引入service,实验并不完整。

实现蓝绿发布

这里要引入se] 9 brvice的概念才好描述,rs1资源通过标签选择器选择关联到pod1和pod2,serivce通过标签选择器也关联到pod1和pod2;要进行发布时重新创建rs2资源,并关联pod3和pod4,当pod3和pod4就绪可接收服务请求后,再修改service的标签选择器让其关联到pod3和pod4,再删除rs1资源。

Depl$ ] B - +oyment

建构在ReplicaSetE [ t O B C q *之上e k # W 2 U 4 0,可管理多个rs,仅有1个处于活动状态,并可回滚版本,默认最多可保留10个版本,可自定义。

Deployment能提供滚动式自定义控制的更新发布,并可控制更新节奏和更新逻辑。在更新a J ! i J e +期间能设定pod数量可以多于; ^ u H r s * =或少于replicas规定的数量

通过控制更新的节奏和逻辑就能实现蓝绿,金丝雀发布

kubernetes快速入门6-pod控制器

deploymentx l l J TM ) V源使用说明

deployment可简写为deploy,可通过U z 7 b ` . s 3 Jkubectl explain deploy查看相应的帮助信息。

KIND:     Deployment
VERSION:  apps/v1    所属于群组和版本
FIELDS:
spec* ^ s F q * e =    <Object>
revisionHistoryLimit    <integer>  更新保留的版本数,默认为10个
selector    <Object> 必选项,指定标签选择器的标签
template    <O/ p z ~ W # Mbject> 必选项,定义pod的模板
replicas    <integer> pod副本数
st} k ? 5rategy    <Object>   新pod替换旧pod的部署策略
type    &, / I o 7 G T @ _lt;string>     部署类型,"Recreate" or "RollingUpdate".默认为 RollingUpdate
Re% @ # $create 表示重建式更新,删一个pod,新建一个pod
RollingUpdatd 9 ] v h Ee 表示滚动式更新,更新策略由 rollingUpdate 字段q { I F设定
rollingUpdate   <ObM T B L j 4ject>   滚动更新
maxSurge    <string>  指定在更新时允许超出replicah = i ;set设置的目标副本数的个数或百j V 分比,默认25%
maxUnavailable  <string>  指在更新时最多不可用pod数或百分比,默认25%

deployment资源创建事例

kubectl create -f YAML_FILEkubectl apply -f YAML_FILE的区别:

create只能创建指定的资源,S a X后续yaml清单里的资源属性发生变动,不能再继续使用该文件应用到相应的资源使其对相应属性的变更,只能调用一次create命令;

apply则如果资源不7 M k a 3 x - @ ,存在就创建,资源存在就调& # - )用apiserver获取存放在etcd中的资源清单,对比出两次yaml清单不同的部分,以此不同部分来对资源做应用变更,可以多次调用apply命令。

k8s@node01:~/my_manifests$ cat deployment-demo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy
namespace: default
spec:
replicas: 2
revisionHistoryLimit: 5! J E
sZ d O m | 8 i *elector:
matchLabels:
app: myapp
release: cn ( F f p panary
template:
metadata:
labels:
app: myapp
release: canary
spec:
cont ) 1 !ainers:
- name: myapp
image: ikubernetes/myapp:v1
pA n I h D 9 {orts:
- name: http
containerPort: 80
readinessProbe:
httpGet:
port: http
path: /
scheme: HTTP
# 应用清单文件
k8s@node01:~/my} % N 4 [ * k A I_manifests$ kubectl apply -f deploymenX  n D ) )t-D % o # w ) Q {demo.yaml
deployment.apps/myapp-deploy created
k8s@n8 V * Jode01:~/my_manifests$ kubectl get deploy
NAME           READY   UP-TO-DATE   AVA} 4 _ S A j xILABLE   AGE
myapp-deploy   2/2     2            2           31s
# 在清单文件中没有定义replicaset资源,但创建dS } x 3 o Z K 3 Teployment资源后会自动创建rs资8 | S源,期望的副本数为清单文件中replicas定义的数量
# rs名称为资源清单中定义的deploy名称加上template的hash值
k8s@node01:~/my_manifests$ kubectl get rs
NAME                      DESIRED   CURRENT   READY   AGE
myap} $ M ^ +p-deploy-679fcdf84   2         2         2       40s
# 创建了相应副本数的Pod资源,pod名称为 rs 名称加上 hash码
k8s@nodem m 2 R01:~/my_manifests$ kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
myapp-deploy-679fcP m ! {df84-46z7s7 & t c   1/1     Running   0          58s
myapp-deploy-679fcdf84-5vw2z   1/1X f ] ^ 0 E @ 4     Runnin$ s | a ) & u 6 Fg   0          61s

如果想对定义的deploy资源清单中的属性进行更改,想扩容pod的数量为3,直接修改yaml文件再次执行apply操作即可

k8s@node01:~/myn D z t_manifests$ kubectl get pods  # 扩容后变为3个pod
NAME                           READY   STATUS    RESTARTS   AGE
myapp-deploy-679D h B Mfcdf84-46z7s   1/1     Running6 a 3 N l 7 B   0          3m15s
myapp-deL ? f { eploy-679fcdf84-5vw2z   1/1     RunnQ y i ^ x Gingf R 8 d [ u s c +   0          3m18s
myapp-deploy-679fcdf84-x7wfb   1/1     Running   0          16s
k8s@node01:~/my_manifests$ kubectl describe deploy myapp-deploy  # 查看deploy的详细信息
Nam$ } ] v 8 s 0e:                   myapp-deploy
Na? 6 p f 4mespace:              default
Creatp  ` t _ J s dionTimestamp:      Sun, 26 Jul 2020 14:33:17 +0800
Labels:                 <5 4 * ; P jnone>
AnnotaK ) )tions:            deployment.kubernetes.io/revision: 1
Selector:               app=myapp,release=canary
RT z | G . m H 0eplicas:               3 desired | 2 updated | 2 total | 2 available | 1 unavailable
StrategyType:           RollingUpdate
MinL  j i JReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
Labels:  app=myapp
release=canary
Containers:
myapp:
Image:        ikubernep 5 B Ktes/m* V a $ k N Oyapp:v1
Port:         80/TCP
......

deploymen| S !t pod更新

滚动更新

推荐直接编辑yaml文件后再apply。如需要把myapp:v1版本升级到myapp:v2版本,直接修改yaml后再次appla l x # qy,通过kubectl get pods -l app=myapp -w来监控更新过程

k8s@node01:~$ kubW R ; u $ je/  ;  ~ 3ctl get pods -l# C | Z # app=myapp -w
NAME                           READY   STATUS    RESTARTS   AGE
myapp-deploy-679fc_ @ P ; k .df84-46z7s   1/1     Running   0          4m7s
myapp-deploy-679fcdf84-5vw2z   1/1     Running   0          4m10s
myapp-deploy-679fcdf84-x7wfb   1/1     RunnX B A 0ing   0          68s
myapp-dZ n E Beploy-9d8d5cb4c-rgxpa K , 5 ; & 6 ,2   0/1     Pending   0          0s
myapp-deploy-9d8d5cb4c-rgxp2   0/1     Pending   0          2s
myapp-deploy-9d8d5cb4c-rgxp2   0/1     CoX u | c Z a 3 ]ntainerCreating   0          5s
myapp-deply y Aoy-9d8v , r B N (d5cb4c-r, / t o H ; & igxp2   0/1     Running             0          10s
myapp-deploy-9d8d5cb4c-rgxp2   1/1` E z K P + N     Running             0          10s
myapp-deploy-679fcdf84-x7wfb   1/ - a W +1     Terminating         0          3m31s
myapp-deploy-679fcdf84-xC j C W A7wfb   0/1     Terminating         0          3m32s
myapp-deploy-9d8d5cb4c-xsm6bG m 8 3 o d W   0/1     Pending             0          1s
myapp-deploy-679fcdf84-x7wfb   0/1     Termin^ w K } !ating         0          3m35s
myapp-deploy-u 0 r F679fcdf84-x7wfb   0/1     Terminating         0          3m35sk - P
mya3 m f % Spp-deploy-9d8d5cb4c-xsm6b   0/1     Pending             0          2s
myapp-deploy-9d8d5cb4c-xsm6b   0/1     Containe$ t @ % WrCreating   0          3s
myapp-deploy-9d8d5cb4c-xsm6b   0/1     Running             0          7s
myapp-deploy-9d8d5cb4c-xsm6b   1/1     Running             0          12s
myapp-deploy-679fcdf84-46z7s   1/1     Terminating         0          6m46s
myapp-deploy-M 6 W 9 # P % L9d8d5cb4c-kn7fk   0/1     Pending             0          0s
myapp-deploy-9d8d5cb4% E # A S ~ g 3 :c-kn7fk   0/1     Pen* a e & P x ~ding             0          3s
myapp-deploy-679fcdf84-46z7s   0/1     Terminating         0          6m49s
myapp-dH V l $ 6 / L { aeploy-679fcdf84-46z7s   0/1     Terminating         0          6m49s
myapp-deploy-679fcdf84-46z7s   0/1     Terminating         0          6m49s
myapp-deploy-9d8d5cb4c-kn7fk   0/1     ContaH U N k L 1 VinerCreating   0          4s
myapp-deploy-* x e 0 A I g9d8d5cb4c-kn7fk   0/1     Running             0          9s
myapp-deploy-9d8d5cb4c-kn7fk   1/8 ! c1     RD `  e uunning             0          11s
myapp-deploy-679fcdf84-5vw2z   1/1     Terminating         0          9m38s
myapp-deploy-679fcdf84-5vw2z   0/1     Terminating         0          9mK N C C d f 941s
myapp-deploy-679fcdf84-5vw2z   0/1     Terminating         0          9m49s
myapp-deploy-679fcdf84-5vw2z   0/1     Terminating         0          9m49s
# 查看deploy资源,镜像已是v2版本
k8/ N Qs@node01:~$ kubectl get deploy -o wide
NAME           READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                 SELECTOR
myapp-de7 + G ( l ` z E kploy   3/3     3            3           12m   myapp        ikubernetes/myapp:v2   app=myapp,re5 - v e N * G 8lease=canary
# ^ * S * 查看rR o F , c 8 a geplicaset资源,保留了相应的历史版本,如果有需要可以进行版本回退
k8s@node01:~$ kubectl get rs -o wide
NAME_ T N O y r V                     DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES                 SEL2 / ? A b t 2ECTOR
myapp-deploy-679fcdf84   0         1         1       12m     myapp        ikubernete8 d g A Q {s/myap3 o v s l e h ?p:v1   apH A 7 1 6 V mp=myapp,pod-template-hash=G c 2 9 O )679fcdf84,release=canary
myapp-deploy-9d8d5cb4c   3         3         3       6, 1 ; w ] ^ qm18s   myapp        ikubernetes/myapp:v2   app=myapp,pod-template-hasC g a  Sh=9d8d5cb4c,release=can; ~ e U  tary
# 查看den M f ) $ o ] #ploy的滚动历史
k8s@node01:~$ kubectl rollout history deploy myapp-de ] k + @ 0ploy
deployment.apps/myapp-deploy
REVISION  CHANGE-CAUSE
1         <noneD i N K>
2         <none>

通过直接修改yaml清单文件对资源进行变更,也可以使用kubectlu n e * r patch打补丁的方式对资源进行变更,此种操作的资源变化不能反映在ym I ,aml文件中,一般用于暂时性测试某个功能时使用,比如把replicas扩容到5个

k8s@no| = B %de01:~$ kubectl patch deploy mo b / 8 Iyapp-depB K J c g P ] 7loy -p '{"sp: h tec":{"B F H W x k ?replicas":5}}' #-p 选项的值与yaml清单文件中结构对应
deployment.apps/e @ 4 / = n =myapp-deploy patched
k8s@node01:~$ kubectl get pods
NAME                           READY   STATUS    RESTARTS   AGE
myapp-det u = l w I 0ploy-9d8d5cb4c-7zv7s   1/1     Running   0          18s
myapp-deploy-9d8d5cb4c-89brd   1/1     Running   0          18s
myapp-deploy-9d8d5cb4c-kn7fe - U Uk   1/1     Running[ , ? ~   0          6m59s
myai c e 4 pp-deploO l 4 S T wy-9d8d5cb4c-rgxp2   1/1     Running   0          7m30s
myapp-deploy-9d8d5cb4c-xsm6b   1/1     Running   0          7m12s

以打补丁的方N h | d式修改deploy的更新策略

k8s@node01:~$ kubectl patch deploy myapp+ [ (-deploy -p '{"spec":{@ X l S t f -"stS : * 3 { r * f Trategy":{"roll( _  { f V 4ingUpdate":{"maxSurge":1,"maxUnavailable":0}}}}'
deployment.apps/myapp-deploy patched
k8s@n7 : 8 8ode01:~$ kubectl describe deploy myaH b 7 A ,pp-deploy  # 查看详细信息,确认更新策略是否被更新
...
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  0 max unavailable, 1 max surge
...
金丝雀更u L 5 2 ^ x

myapp-deploy资源目前的更新策略为{"maxSurge":1,"maxUnavailab| i [ Z / ^ n % Ele":0},即至多可多于rep= 0 7 jlicas一个,pod数不能少于replicas数量。金丝雀发布在此种策略下就是创建一个新版本的pod后自动暂停r - 7 8 o后边的滚动更新,让pod多一个,此pod是运行新版本的程序,等运行一段时间后测试无问题再继续暂停的后续更新操作。

# 监视相应pods
k8s@node01:~$ kubec# A O o j * $ htl get pods -l app=myapp -w
# 也可用以下命令监视,不过需要在执行“kubectl set image deploy myapp-deploy ...”之后再执行
k8+ D ` T K S h b os@nod% U D ) h f -e01:~$ kubectl rollout status deployment myapp-d: 3 z # g   Qeploy
# 更新到v3版本
k8s@node01:~/my_manifests$ kubectl set image deploy myo M a P e w { * (app-deploy mya4 ^ o ) s 8  ?pp=ikubernetes/myapp:v3 && kubectl rollout pause deployment myapp-deploy
# 前一个命令执行后接着就执行 pause 操作,如果监视窗口中发现未有新的Pod被创建,那就是还没来得及创建就被paut 0 % ; 8 k l r -se了9 b # r 8 , 4 f,这时就需要把这两个命令分开写,先执行第一条命令,执行后v { w {就去观察监视pods变化的终端,一旦发现有新的pod被创建后就需要立刻执行 pause 指令让后边的更新暂停。
# 查看poF s z 7 & _ ds状态,前3个是才创建的pod,后3个是旧的Pod,共6个,因执行pause的时机问题,可能会多创4 W $ .建新pod,如果觉得过多可手动删除
k8s@node01:~/my_manifests$ kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
myapp-deploy-577b6ccbf8-4pzk6   1/1     R= & O ! } 4unning   0          42s
myapp-deploy-577b6ccbf8-l7d42   1/1     Running   0          17s
myapp-deploy-577b6ccbf80 , 9 ^ ]-lqfnk   1/1     Running   0          31s
myapp-f 2 F x = Ldeploy-9d8d5cb4c-kn7fk    1/1     Rug ! 1 q m j Wnning   0          16m
myapp-deploy-I $ ^ j m9d8d5c1 9 ! ) 2 `b4c-rgxp2    1/1     Running   0          17m
myapp-deploy-9d8d5cb4c-xsm6b    1/1z k -     Running   0          16m
# 查看replicaset,可以看到3个p2 C K 7 ] h : X [od是v2版本,另3个是v3版本
k8s@L M 8 H x _ cnode01:~/my_manifest] J ] 9 3 i l + $s$ kubectl get rs -o wide
Ng 4 n { f 2 /AME                      DESIRED   CURRENT[ A y % A 4   READY   AGE1 4 n t   CONTAIy e N Y s ~ W INERS   IMAGES                 SELECTOR
myapp-deploy-577b6ccbf8o 3 9 % d | X ~ S   3         3         3       62s   myapp        ikubernetes/myapp:v3   app=myapp,pod{ z @ | Q O b-template-hash=577b6cf X V - cbf8,release=canarv K 0 j ; D Hy
myapp-deploy-679fcdf84    0         0         0       23m   myapp        ikubernetes/myapp:v1   app=myapp,pod-template-hash=679fcdf84a  v,release=canarW w ,y
myapp-deploy-9I T & L ] h _ #d8d5cb4c    3         3         3       17m   myappL B 1 ; 5        ikuberneteH Q 1s/myapp:v2   app=myapp,pod-templatek H @ t-hash=9d] W W h8d5cb4c,release=canary
# 如果经过一段时间的运行,可以把剩下的v2版本的Pod升级为v3,那执行以下命令
k8s. [ K 6 T _@node01:~$ kubectl rollout resume deployment myapp-deploy

当全部pod都升级到新版本后,突然又发现新版本有问题,需要回退版本,那可采取以下操作:

  1. a $ J , )果直接回退到上一个版本,kubectl rollout提供了undo操作,默认回退到上一个版本,使用--to-revis. F [ 8ion=VERSION_NUMBER选项指定回退到哪一个版本,- ? J , v & L使用kub~ V : Jectl rollout history TYPE/] D Z JNAME查看相应资源的版本号
  2. 使用kubectl set image设定容器( n *所需要回滚的镜像版本,让其快速滚动更新
  3. 修改yam+ m n q U .l资源配置清` ! . S ] 7 t单,推荐使用配置清单。

DaemonSet

每一个满足条件的工作节点上只运行一个pod,实现工} B z 5 , = _ T作节点的管理/ + X O @ ~ N功能。日志收集程序(filebeat)适用于此种控制器

daemonset 可简写为ds,使用kube# 3 ; 7 S + cctl explain ds查看资源清单中相应字段的帮助信息。

这里通过一个事例来说明daemonsx N @ net控制器的使用场景

1. file% v A mbeat的pod需要使S u . Q 7 { O f用daemons- = l G - d # W oet控制器管理,实现每个工作节点只运行一R v 1 e H ~ }个副本
2. filebeat需要使用redis服务,把收集到的日志写入redis队列
3. filebeat连接redis时使用域名,此域名即为redis的service的名称,service作为fil. 7 , r F { 3 vebeat的连接端点
注: filebeat这个D2 N + m = ! K JaemonY P ! S SSet资源应该实现存储卷挂载工作节点的“/var/logu ; 6 b/”目录,并对此目录下相应的日志文件进行收集,本配置清单0 N a | v % x _ j中未实现卷的挂载,只在容器内部测试日志的收集。
# 一个yaml文件里可以定义多个类型的资源清单,使用“X * j q P w x A---”分隔
k8s@node01:~/my_m- F `anifests$ cat daemonset-demo.yaml
apiVersion: apps/vn # R Y T +1
kind: Deployment
metadata:
name: redis-dep p 3  A l % gloy
namespace: default
spec:
selector:
matchLabels:
app: redI q ^ ]is
role: logstore
replii ; # ) 6 y S = Mcas: 1
template:
metadata:
labels:
app: redis
role: logstore
spec:
containers:
-I r s 9 ) + L w 3 name: redis4
image: redis:6.0.6-alpine
ports:
- name: redis-port
containerPort: 6379
---
apiVersion: v1
kind: Se6 ; $  Ervice
met@ c = B 2 Kadata:
name: redis
namespace: default
spec:
selector:
app: redis
role: logstore
ports:
- name: svc-port
protocol: TCP
port: 6379
targetPort: 6379
---
apiVersion: app  s _s/v1
kind: DaemonSet
metadata:; ? ^ r
name: filebeat-ds
namespace: default
spec:
selector:
matchLabels:
app: filebeat
release: stable
template:
metadata:
labels:
app: fili G L k X u ebeat
release: stable
spec:
containers:
- name: filebeat-pod
image: ikubernetes/filebeat:5.6.5-q ` L _ - Kalpine
env:
- name: REDIS_HOST
value: reC S q H ? x b F 4dis.default.svc.cluster.local
- ne C t M T Y w -ame: REZ I / ) % @ % oDIS_LOG_LEVEL
value: info
# 应用清单
k8s@noN ( M 7 ! ` de01:~Q u M I n V 9 :/my_manifests$ kubectl apply -f daemonset-demo.yaml
deployment.apps/redis-deploy create- d a W i 0 x 0 Id
seV B +rvice/redis created
daemonset.apps/m t X O vfilebeat-ds created
# 连接到filebeat的pod中查看filebeat收集的日志有哪些,并触发相应的日志写入,filebeat写redis
k8s@node01:~/my_manifests$ kubectl exec -it filebeat= ? ~ 1-ds-8wwnz -- /bin/sh
/ # ps
PID   USER     TIME   COMMAND
1 root       0:00 /usr/local/bin$ a : Q/filebeat -e -c /etc/filebeat/filebeat.yml
11 root       0:00 /bin/sh
16 root       0:00 ps
/ # cat /etc/7 } X ^ + ^ w x Bfilebeat/filebeaR  F L +t.yml
filebeat.registry_fij 0 g 9 ? P i T Fle: /var/log/containers/filebeat_registry
filebeat.idB Y V ! Wle_timeout: 5s
filebeat.spool_size: 2048
logging.level: info
filebeat.~ ; F i j k q S Bprospectors:
- input_type: log
paths:
- "/var/log/conC  Jtainers/*.log"
- "/var/log/docker/containef f v s R rrs/*.log"
- "/var/log/startupscript.log"
-s 6 o 7 + $ D : "/var/log/kubelet.log"
- "/var/loG W K Yg/kube-proxy.log"
- "/var/log/kube-apiserver.log"
-- . a ; 0 "/vaD  b H Z b e C Pr/log/kube-controlle| U , | 0r-manageR 8 3 nr.log"
- "/var/log/kube-schedules I [r.log"
- "/var/log/rescheduler.log"
- "/var/log/glbc.log$ y ; *"
- "/var/log/cluster-autoscz q G : c v Y oalerN 7 ].log"
symlinks: true
jsn 3 son.me] : y rssage_key: log
json.keys_under_root: true
json.add_error_key: true
multiline.pattern: '^\s'
multiline.match: afteL @ Vr
documenP + S M B i / nt_type: kube-logs
tail_files: true
fields_under_root: true
output.redis:
hosts: ${REDIS_HOST:?No RS ^ k z wedis host configured. Use env var REDIS_HOST to set host.}
key: "fil` M w P jebeat"
# 手动追加一个日志信息,触发, 4 | [ C h写redis操作
/ # echo "2020/07/26 17:12 test log data" > /var/log/containers/test.log
# 连接到redis中查看是否有数据
k8s@node01:~$ kubectl exec -it redis-deploy-66856679cd-hth25 -- /bin/sho = %
/data # redis-cli
127.0.0.1:63V 9 G q79> keyN R 7s *
1) "filebeat"
127.0.0.1:63q I | c79> LLEN filebeat
(integer) 1
127.0.0.1:6379&{ K ` / 2 A D ggt; LINDEX filebeat 0
"{\"@timestamp\":\"2020-07-26T09:12:49.745Z\",\"beat\! Q H v ] s , D":{\"hostname\":\"filebeat-ds-8wwnz\",\"name\":\"filebeat-ds-8wwnz\",\"version\":\"5.6.5\"},\"input_type\":\"log\",\"json_er: o 0 Q ) & ( yror\":\"Error decoding JSON: jso2 @ s e X ~ G +n: cannot unmarshal number into Go value of type map[string]int= U 1 ,erface {}\",\"log\"y f + V:\"2020/07/26 17:12 test log data\",\"offset\":31,\"source\":\"/var/loh o 1 ^ w /g/U ^ qcontainers/test.log\",\"type\":\"kube-logs\"}"
127.0.0.1:6379>

Job

只一次运行任务,运行完成后pod被中止,但并不会被删除pod。

Cronjob) J 7 _ A = 7 4 I

周期性运行任务

StatefulJ E 5 D p u t bSet

有状态控制器,管理有状态服务,如: redis cluster