《Kubernetes进阶实战》第五章《Pod控制器》

Pod控制器

 ReplicaSet:创建一定数量的pod,支持扩缩容
 主要有三个组件:1,用户期望的pod副本 2,标签选择器 3.如果pod不够怎么办,扩建
Pod资源模板可以帮助用户新建
Deployment可以比ReplicaSet更加强大,增加声明式配置和升级后的回滚,是目前最好的Pod控制器
DaemonSet:系统级的Pod,只能在node上运行一个,每个集群精确运行一个
Job:只能执行运行一次的,比如这个程序运行完了,这个容器就没有用了
 CronJob:周期性运行Job和CronJob都是不用长期运行的容器
StatetufulSet:运行mysql,redis等有状态的应用
有状态的运维任务时很困难的,比如mysql和redis集群,一般转换成脚本,有状态的应用托管到kubernetes上是很难的

 演示ReplicaSet在更新中显现出的功能

[root@master ~]# cd mainfests/
[root@master mainfests]# vim rs-rep.yaml 
[root@master mainfests]# cat rs-rep.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myapp
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp
      release: canary
  template:
    metadata:
      name: myapp-pod
      labels:
        app: myapp
        release: canary
        environment: qa
    spec:
      containers:
      - name: myapp-container
        image: ikubernetes/myapp:v1
        ports:
        - name: http
          containerPort: 80
[root@master mainfests]# kubectl create -f rs-rep.yaml 
replicaset.apps/myapp created
[root@master mainfests]# kubectl get pod
NAME                            READY   STATUS             RESTARTS   AGE
client                          0/1     Error              0          23h
liveness-exec-pod               0/1     CrashLoopBackOff   14         19h
myapp-65899575cd-6p6qk          1/1     Running            1          22h
myapp-cd4wf                     1/1     Running            0          5s
myapp-mmjjx                     1/1     Running            0          5s
nginx-deploy-84cbfc56b6-h5h7x   1/1     Running            1          23h
[root@master mainfests]# kubectl get rs
NAME                      DESIRED   CURRENT   READY   AGE
myapp                     2         2         2       83s
myapp-65899575cd          1         1         1       22h
myapp-9b4987d5            0         0         0       22h
nginx-deploy-84cbfc56b6   1         1         1       23h
获取创建的Pod的详细信息
[root@master mainfests]# kubectl describe pods myapp-cd4wf
Name:               myapp-cd4wf
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               node01/10.0.0.101
Start Time:         Fri, 22 Feb 2019 09:01:10 +0800
Labels:             app=myapp
                    environment=qa
                    release=canary
Annotations:        <none>
Status:             Running
IP:                 10.244.1.19
Controlled By:      ReplicaSet/myapp
Containers:
  myapp-container:
    Container ID:   docker://f9c36b45762164e23ba7d7d613054d93a4303ca8f5872e876b313cfa2daeeff6
    Image:          ikubernetes/myapp:v1
    Image ID:       docker-pullable://ikubernetes/myapp@sha256:9c3dc30b5219788b2b8a4b065f548b922a34479577befb54b03330999d30d513
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Fri, 22 Feb 2019 09:01:11 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-h8l2m (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-h8l2m:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-h8l2m
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  114s  default-scheduler  Successfully assigned default/myapp-cd4wf to node01
  Normal  Pulled     113s  kubelet, node01    Container image "ikubernetes/myapp:v1" already present on machine
  Normal  Created    113s  kubelet, node01    Created container
  Normal  Started    113s  kubelet, node01    Started container

[root@master mainfests]# curl 10.244.1.19
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@master mainfests]# curl 10.244.1.19/hostname.html
myapp-cd4wf
[root@master mainfests]# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
myapp-65899575cd-6p6qk          1/1     Running   1          22h
myapp-cd4wf                     1/1     Running   0          3m8s
myapp-mmjjx                     1/1     Running   0          3m8s
nginx-deploy-84cbfc56b6-h5h7x   1/1     Running   1          23h
删除一个Pod,他会自动创建一个
[root@master mainfests]# kubectl delete pods myapp-cd4wf
pod "myapp-cd4wf" deleted
[root@master mainfests]# kubectl get pod
NAME                            READY   STATUS             RESTARTS   AGE
myapp-4tkhm                     1/1     Running            0          6s
myapp-65899575cd-6p6qk          1/1     Running            1          22h
myapp-mmjjx                     1/1     Running            0          3m27s
nginx-deploy-84cbfc56b6-h5h7x   1/1     Running            1          23h
[root@master mainfests]# kubectl get pods --show-labels
NAME                            READY   STATUS             RESTARTS   AGE     LABELS
myapp-4tkhm                     1/1     Running            0          72s     app=myapp,environment=qa,release=canary
myapp-65899575cd-6p6qk          1/1     Running            1          22h     pod-template-hash=65899575cd,run=myapp
myapp-mmjjx                     1/1     Running            0          4m33s   app=myapp,environment=qa,release=canary

自动扩容:将myapp扩容到10个
[root@master ~]# kubectl edit rs myapp
spec:
  replicas: 10
[root@master mainfests]# kubectl edit rs myapp
replicaset.extensions/myapp edited
监控pod创建
[root@master mainfests]# kubectl get pods -w
NAME                            READY   STATUS              RESTARTS   AGE
myapp-4tkhm                     1/1     Running             0          4m22s
myapp-65899575cd-6p6qk          1/1     Running             1          22h
myapp-dh45q                     0/1     ContainerCreating   0          6s
myapp-g6tgq                     0/1     ContainerCreating   0          6s
myapp-k5xtm                     0/1     ContainerCreating   0          6s
myapp-mmjjx                     1/1     Running             0          7m43s
myapp-mpb79                     0/1     ContainerCreating   0          6s
myapp-pbttb                     0/1     ContainerCreating   0          6s
myapp-qnmnb                     0/1     ContainerCreating   0          6s
myapp-r4ldv                     0/1     ContainerCreating   0          6s
myapp-zmzhn                     0/1     ContainerCreating   0          6s
nginx-deploy-84cbfc56b6-h5h7x   1/1     Running             1          23h
myapp-g6tgq   1/1   Running   0     58s
myapp-mpb79   1/1   Running   0     58s
myapp-k5xtm   1/1   Running   0     58s
myapp-dh45q   1/1   Running   0     58s
myapp-qnmnb   1/1   Running   0     60s
myapp-r4ldv   1/1   Running   0     60s
myapp-zmzhn   1/1   Running   0     60s
myapp-pbttb   1/1   Running   0     60s
[root@master mainfests]# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
client                          0/1     Error     0          23h
myapp-4tkhm                     1/1     Running   0          5m50s
myapp-65899575cd-6p6qk          1/1     Running   1          22h
myapp-dh45q                     1/1     Running   0          94s
myapp-g6tgq                     1/1     Running   0          94s
myapp-k5xtm                     1/1     Running   0          94s
myapp-mmjjx                     1/1     Running   0          9m11s
myapp-mpb79                     1/1     Running   0          94s
myapp-pbttb                     1/1     Running   0          94s
myapp-qnmnb                     1/1     Running   0          94s
myapp-r4ldv                     1/1     Running   0          94s
myapp-zmzhn                     1/1     Running   0          94s
nginx-deploy-84cbfc56b6-h5h7x   1/1     Running   1          23h

将myapp版本升级到v2
[root@master mainfests]# kubectl edit rs myapp
spec:
  replicas: 10
  selector:
    matchLabels:
      app: myapp
      release: canary
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: myapp
        environment: qa
        release: canary
      name: myapp-pod
    spec:
      containers:
      - image: ikubernetes/myapp:v2
[root@master mainfests]# kubectl get rs -o wide
NAME                      DESIRED   CURRENT   READY   AGE   CONTAINERS        IMAGES                 SELECTOR
myapp                     10        10        10      11m   myapp-container   
[root@master mainfests]# kubectl get pods -o wide
NAME                            READY   STATUS    RESTARTS   AGE     IP            NODE     NOMINATED NODE   READINESS GATES
client                          0/1     Error     0          23h     <none>        node02   <none>           <none>
myapp-4tkhm                     1/1     Running   0          8m21s   10.244.1.20   node01   <none>           <none>
myapp-65899575cd-6p6qk          1/1     Running   1          22h     10.244.1.16   node01   <none>           <none>
myapp-dh45q                     1/1     Running   0          4m5s    10.244.2.21   node02   <none>           <none>
myapp-g6tgq                     1/1     Running   0          4m5s    10.244.2.23   node02   <none>           <none>
myapp-k5xtm                     1/1     Running   0          4m5s    10.244.2.20   node02   <none>           <none>
myapp-mmjjx                     1/1     Running   0          11m     10.244.2.19   node02   <none>           <none>
myapp-mpb79                     1/1     Running   0          4m5s    10.244.2.22   node02   <none>           <none>
myapp-pbttb                     1/1     Running   0          4m5s    10.244.1.23   node01   <none>           <none>
myapp-qnmnb                     1/1     Running   0          4m5s    10.244.1.22   node01   <none>           <none>
myapp-r4ldv                     1/1     Running   0          4m5s    10.244.1.24   node01   <none>           <none>
myapp-zmzhn                     1/1     Running   0          4m5s    10.244.1.21   node01   <none>           <none>
nginx-deploy-84cbfc56b6-h5h7x   1/1     Running   1          23h     10.244.2.15   node02   <none>           <none>

这里myapp的镜像已经是v2版了,但是我们访问还是v1
[root@master mainfests]# curl  10.244.1.22/hostname.html
myapp-qnmnb
[root@master mainfests]# curl  10.244.1.22
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@master mainfests]# kubectl delete pods myapp-qnmnb
pod "myapp-qnmnb" deleted
[root@master mainfests]# kubectl get pods -o wide
NAME                            READY   STATUS    RESTARTS   AGE     IP            NODE     NOMINATED NODE   READINESS GATES
client                          0/1     Error     0          23h     <none>        node02   <none>           <none>
myapp-27x5t                     1/1     Running   0          24s     10.244.1.25   node01   <none>           <none>
myapp-4tkhm                     1/1     Running   0          10m     10.244.1.20   node01   <none>           <none>
myapp-65899575cd-6p6qk          1/1     Running   1          22h     10.244.1.16   node01   <none>           <none>
myapp-dh45q                     1/1     Running   0          5m50s   10.244.2.21   node02   <none>           <none>
myapp-g6tgq                     1/1     Running   0          5m50s   10.244.2.23   node02   <none>           <none>
myapp-k5xtm                     1/1     Running   0          5m50s   10.244.2.20   node02   <none>           <none>
myapp-mmjjx                     1/1     Running   0          13m     10.244.2.19   node02   <none>           <none>
myapp-mpb79                     1/1     Running   0          5m50s   10.244.2.22   node02   <none>           <none>
myapp-pbttb                     1/1     Running   0          5m50s   10.244.1.23   node01   <none>           <none>
myapp-r4ldv                     1/1     Running   0          5m50s   10.244.1.24   node01   <none>           <none>
myapp-zmzhn                     1/1     Running   0          5m50s   10.244.1.21   node01   <none>           <none>
nginx-deploy-84cbfc56b6-h5h7x   1/1     Running   1          23h     10.244.2.15   node02   <none>           <none>

[root@master mainfests]# curl 10.244.1.25
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
[root@master mainfests]# 

注:也就是我们只有在删除容器时候在创建的时候才是v2版本,如果不删除还是v1版本,这样适合做蓝绿发布,可以做滚动式更新,也可控制更新节奏

注:上述更新镜像和扩容都可以在原配置文件进行修改

注:上述配置文件可以用kubectl  explain ReplicaSet

[root@master mainfests]# kubectl explain ReplicaSet
KIND:     ReplicaSet
VERSION:  extensions/v1beta1

DESCRIPTION:
     DEPRECATED - This group version of ReplicaSet is deprecated by
     apps/v1beta2/ReplicaSet. See the release notes for more information.
     ReplicaSet ensures that a specified number of pod replicas are running at
     any given time.

FIELDS:
   apiVersion	<string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#resources

   kind	<string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds

   metadata	<Object>
     If the Labels of a ReplicaSet are empty, they are defaulted to be the same
     as the Pod(s) that the ReplicaSet manages. Standard object's metadata. More
     info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata

   spec	<Object>
     Spec defines the specification of the desired behavior of the ReplicaSet.
     More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status

   status	<Object>
     Status is the most recently observed status of the ReplicaSet. This data
     may be out of date by some window of time. Populated by the system.
     Read-only. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status

 演示Deployment在更新中显现出的强大功能

​[root@master mainfests]# vim rs-demo.yaml 
[root@master mainfests]# cat rs-demo.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deploy
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp
      release: canary
  template:
    metadata:
      name: myapp-pod
      labels:
        app: myapp
        release: canary
    spec:
      containers:
      - name: myapp
        image: ikubernetes/myapp:v1
        ports:
        - name: http
          containerPort: 80
[root@master mainfests]# kubectl apply -f rs-demo.yaml 
deployment.apps/myapp-deploy created
[root@master mainfests]# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
myapp-65899575cd-6p6qk          1/1     Running   1          22h
myapp-deploy-848fb7b5b6-9tvns   1/1     Running   0          43s
myapp-deploy-848fb7b5b6-mx2bp   1/1     Running   0          43s
nginx-deploy-84cbfc56b6-h5h7x   1/1     Running   1          23h
[root@master mainfests]# kubectl get deploy
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
myapp          1/1     1            1           23h
myapp-deploy   2/2     2            2           61s
nginx-deploy   1/1     1            1           24h
[root@master mainfests]# kubectl get rs
NAME                      DESIRED   CURRENT   READY   AGE
myapp-65899575cd          1         1         1       23h
myapp-deploy-848fb7b5b6   2         2         2       70s
nginx-deploy-84cbfc56b6   1         1         1       24h

如果想更新的话,直接编辑deploy-demo.yaml文件即可

[root@master mainfests]# !vim
vim rs-demo.yaml   
#  replicas: 4
[root@master mainfests]# kubectl apply -f rs-demo.yaml 
deployment.apps/myapp-deploy configured
[root@master mainfests]# kubectl get rs
NAME                      DESIRED   CURRENT   READY   AGE
myapp-65899575cd          1         1         1       23h
myapp-deploy-848fb7b5b6   4         4         4       4m28s
nginx-deploy-84cbfc56b6   1         1         1       24h
[root@master mainfests]# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
myapp-65899575cd-6p6qk          1/1     Running   1          23h
myapp-deploy-848fb7b5b6-9tvns   1/1     Running   0          4m34s
myapp-deploy-848fb7b5b6-l8cfj   1/1     Running   0          13s
myapp-deploy-848fb7b5b6-mx2bp   1/1     Running   0          4m34s
myapp-deploy-848fb7b5b6-qpfxp   1/1     Running   0          13s
nginx-deploy-84cbfc56b6-h5h7x   1/1     Running   1          23h


运行的容器更新版本:
[root@master mainfests]# vim rs-demo.yaml 
image: ikubernetes/myapp:v2
[root@master mainfests]# kubectl apply -f rs-demo.yaml 
deployment.apps/myapp-deploy configured
[root@master mainfests]# kubectl get rs
NAME                      DESIRED   CURRENT   READY   AGE
myapp-65899575cd          1         1         1       23h
myapp-9b4987d5            0         0         0       23h
myapp-deploy-848fb7b5b6   0         0         0       6m19s
myapp-deploy-fc7fc68b9    4         4         4       6s
nginx-deploy-84cbfc56b6   1         1         1       24h


查看历史的滚动记录
[root@master mainfests]# kubectl rollout history deployment myapp-deploy
deployment.extensions/myapp-deploy 
REVISION  CHANGE-CAUSE
0         <none>
1         <none>
2         <none>

如果想返回上面的一个版本:
[root@master mainfests]# kubectl rollout undo deployment myapp-deploy
deployment.extensions/myapp-deploy rolled back
[root@master mainfests]# kubectl get rs -o wide
NAME                      DESIRED   CURRENT   READY   AGE     CONTAINERS        IMAGES                 SELECTOR
myapp                     0         0         0       41m     myapp-container   ikubernetes/myapp:v2   app=myapp,release=canary
myapp-65899575cd          1         1         1       23h     myapp             ikubernetes/myapp:v2   pod-template-hash=65899575cd,run=myapp
myapp-9b4987d5            0         0         0       23h     myapp             ikubernetes/myapp:v1   pod-template-hash=9b4987d5,run=myapp
myapp-deploy-848fb7b5b6   4         4         4       8m18s   myapp             ikubernetes/myapp:v1   app=myapp,pod-template-hash=848fb7b5b6,release=canary
myapp-deploy-fc7fc68b9    0         0         0       2m5s    myapp             ikubernetes/myapp:v2   app=myapp,pod-template-hash=fc7fc68b9,release=canary
nginx-deploy-84cbfc56b6   1         1         1       24h     nginx-deploy      nginx:1.14-alpine      pod-template-hash=84cbfc56b6,run=nginx-deplo
[root@master mainfests]# kubectl rollout history deployment myapp-deploy
deployment.extensions/myapp-deploy 
REVISION  CHANGE-CAUSE
0         <none>
2         <none>
3         <none>

 注:  apply声明式创建,也可以创建,也可以更新

kubernetes应用中的灵活更新

先启动一个监控窗口:
kubectl get pods -l app=myapp -w
 
再启动一个执行更新的窗口:
[root@master test]# kubectl set image deployment myapp-deploy myapp=ikubernetes/myapp:v3 && kubectl rollout pause deployment myapp-deploy
 
deployment.extensions/myapp-deploy image updated
deployment.extensions/myapp-deploy paused
 
监控端:显示现在镜像,创建容器 
 
也可以使用这个命令来查看更新:
[root@master ~]# kubectl rollout status deployment myapp-deploy
Waiting for deployment "myapp-deploy" rollout to finish: 2 out of 4 new replicas have been updated...
 
系统没有继续更新:执行下面的继续更新
[root@master mainfests]# kubectl rollout resume deployment myapp-deploy
deployment.extensions/myapp-deploy resumed

 
[root@master ~]# kubectl rollout status deployment myapp-deploy
Waiting for deployment "myapp-deploy" rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for deployment spec update to be observed...
Waiting for deployment spec update to be observed...
Waiting for deployment "myapp-deploy" rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: 3 old replicas are pending termination...
Waiting for deployment "myapp-deploy" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "myapp-deploy" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "myapp-deploy" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "myapp-deploy" rollout to finish: 3 of 4 updated replicas are available...
deployment "myapp-deploy" successfully rolled out

查看历史版本:
[root@master mainfests]# kubectl get rs -o wide
NAME                      DESIRED   CURRENT   READY   AGE   CONTAINERS        IMAGES                 SELECTOR
myapp-65899575cd          1         1         1       23h   myapp             ikubernetes/myapp:v2   pod-template-hash=65899575cd,run=myapp
myapp-9b4987d5            0         0         0       23h   myapp             ikubernetes/myapp:v1   pod-template-hash=9b4987d5,run=myapp
myapp-deploy-65df9c8bf6   4         4         4       18m   myapp             ikubernetes/myapp:v3   app=myapp,pod-template-hash=65df9c8bf6,release=canary
myapp-deploy-848fb7b5b6   0         0         0       32m   myapp             ikubernetes/myapp:v1   app=myapp,pod-template-hash=848fb7b5b6,release=canary
myapp-deploy-fc7fc68b9    0         0         0       25m   myapp             ikubernetes/myapp:v2   app=myapp,pod-template-hash=fc7fc68b9,release=canary
nginx-deploy-84cbfc56b6   1         1         1       24h   nginx-deploy      nginx:1.14-alpine      pod-template-hash=84cbfc56b6,run=nginx-deploy


通过上面发现,系统的版本都更新到了v3版,原来的版本已经暂停了
日志上显示,下载镜像,删除一个,创建一个,可以控制节奏

回滚历史版本:

[root@master mainfests]# kubectl rollout history deployment myapp-deploy
deployment.extensions/myapp-deploy 
REVISION  CHANGE-CAUSE
0         <none>
2         <none>
3         <none>
4         <none>

[root@master mainfests]# kubectl rollout undo deployment myapp-deploy --to-revision=2
deployment.extensions/myapp-deploy rolled back
[root@master mainfests]# kubectl get rs -o wide
NAME                      DESIRED   CURRENT   READY   AGE   CONTAINERS        IMAGES                 SELECTOR
myapp                     0         0         0       66m   myapp-container   ikubernetes/myapp:v2   app=myapp,release=canary
myapp-65899575cd          1         1         1       23h   myapp             ikubernetes/myapp:v2   pod-template-hash=65899575cd,run=myapp
myapp-9b4987d5            0         0         0       23h   myapp             ikubernetes/myapp:v1   pod-template-hash=9b4987d5,run=myapp
myapp-deploy-65df9c8bf6   0         0         0       19m   myapp             ikubernetes/myapp:v3   app=myapp,pod-template-hash=65df9c8bf6,release=canary
myapp-deploy-848fb7b5b6   0         0         0       33m   myapp             ikubernetes/myapp:v1   app=myapp,pod-template-hash=848fb7b5b6,release=canary
myapp-deploy-fc7fc68b9    4         4         4       27m   myapp             ikubernetes/myapp:v2   app=myapp,pod-template-hash=fc7fc68b9,release=canary
nginx-deploy-84cbfc56b6   1         1         1       24h   nginx-deploy      nginx:1.14-alpine      pod-template-hash=84cbfc56b6,run=nginx-deploy
[root@master mainfests]# 
[root@master mainfests]# kubectl rollout history deployment myapp-deploy
deployment.extensions/myapp-deploy 
REVISION  CHANGE-CAUSE
0         <none>
3         <none>
4         <none>
5         <none>

注:上述其实实质是金丝雀发布

部署redis项目

[root@master mainfests]# vim redis_1.yaml
[root@master mainfests]# cat redis_1.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
      role: logstor
  template:
    metadata:
      labels:
        app: redis
        role: logstor
    spec:
      containers:
      - name: redis
        image: redis:4.0-alpine
        ports:
        - name: redis
          containerPort: 6379

---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: filb-ds
  namespace: default
spec:
  selector:
    matchLabels:
      app: filebeat
      release: stable
  template:
    metadata:
      labels:
        app: filebeat
        release: stable
    spec:
      containers:
      - name: filebeat
        image: ikubernetes/filebeat:5.6.5-alpine
        env:
        - name: REDIS_HOST
          value: redis.default.svc.cluster.local
        - name: REDIS_LOG_LEVEL
          value: info
[root@master mainfests]# kubectl create -f redis_1.yaml 
deployment.apps/redis created
daemonset.apps/filb-ds created
[root@master mainfests]# kubectl get pods -w
NAME                            READY   STATUS              RESTARTS   AGE
client                          0/1     Error               0          24h
filb-ds-qffsb                   0/1     ContainerCreating   0          49s
filb-ds-r4kjg                   0/1     ContainerCreating   0          49s
myapp-65899575cd-6p6qk          1/1     Running             1          23h
myapp-deploy-fc7fc68b9-2gs52    1/1     Running             0          24m
myapp-deploy-fc7fc68b9-6pbws    1/1     Running             0          24m
myapp-deploy-fc7fc68b9-bjgk4    1/1     Running             0          24m
myapp-deploy-fc7fc68b9-g22fs    1/1     Running             0          24m
nginx-deploy-84cbfc56b6-h5h7x   1/1     Running             1          24h
redis-85b846ff9c-rrvm5          0/1     ContainerCreating   0          49s
redis-85b846ff9c-rrvm5   1/1   Running   0     2m52s
filb-ds-r4kjg   1/1   Running   0     3m31s

[root@master mainfests]# kubectl expose deployment redis --port=6379
service/redis exposed
[root@master mainfests]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        3d23h
myapp        NodePort    10.108.224.2    <none>        80:31318/TCP   24h
nginx        ClusterIP   10.107.43.244   <none>        80/TCP         24h
redis        ClusterIP   10.99.139.119   <none>        6379/TCP       35s
[root@master mainfests]# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
filb-ds-qffsb                   1/1     Running   0          14m
filb-ds-r4kjg                   1/1     Running   0          14m
myapp-65899575cd-6p6qk          1/1     Running   1          24h
myapp-deploy-fc7fc68b9-2gs52    1/1     Running   0          38m
myapp-deploy-fc7fc68b9-6pbws    1/1     Running   0          38m
myapp-deploy-fc7fc68b9-bjgk4    1/1     Running   0          38m
myapp-deploy-fc7fc68b9-g22fs    1/1     Running   0          38m
nginx-deploy-84cbfc56b6-h5h7x   1/1     Running   1          24h
redis-85b846ff9c-rrvm5          1/1     Running   0          14m
[root@master mainfests]# kubectl exec -it redis-85b846ff9c-rrvm5 -- /bin/sh
/data # netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      
tcp        0      0 :::6379                 :::*                    LISTEN      
/data #  nslookup redis.default.svc.cluster.local
nslookup: can't resolve '(null)': Name does not resolve

Name:      redis.default.svc.cluster.local
Address 1: 10.99.139.119 redis.default.svc.cluster.local
/data # redis-cli -h redis.default.svc.cluster.local
redis.default.svc.cluster.local:6379> KEYS *
(empty list or set)
[root@master mainfests]# kubectl exec -it filb-ds-qffsb -- /bin/sh
/ # ps aux
PID   USER     TIME   COMMAND
    1 root       0:00 /usr/local/bin/filebeat -e -c /etc/filebeat/filebeat.yml
   11 root       0:00 /bin/sh
   16 root       0:00 ps aux
/ # printenv
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT=443
HOSTNAME=filb-ds-qffsb
SHLVL=1
HOME=/root
NGINX_PORT_80_TCP=tcp://10.107.43.244:80
MYAPP_SERVICE_HOST=10.108.224.2
MYAPP_PORT=tcp://10.108.224.2:80
MYAPP_SERVICE_PORT=80
TERM=xterm
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
MYAPP_PORT_80_TCP_ADDR=10.108.224.2
NGINX_SERVICE_HOST=10.107.43.244
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
REDIS_LOG_LEVEL=info
KUBERNETES_PORT_443_TCP_PROTO=tcp
MYAPP_PORT_80_TCP_PORT=80
MYAPP_PORT_80_TCP_PROTO=tcp
NGINX_SERVICE_PORT=80
NGINX_PORT=tcp://10.107.43.244:80
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
PWD=/
MYAPP_PORT_80_TCP=tcp://10.108.224.2:80
KUBERNETES_SERVICE_HOST=10.96.0.1
REDIS_HOST=redis.default.svc.cluster.local
NGINX_PORT_80_TCP_ADDR=10.107.43.244
FILEBEAT_VERSION=5.6.5
NGINX_PORT_80_TCP_PORT=80
NGINX_PORT_80_TCP_PROTO=tcp
/ # nslookup redis.default.svc.cluster.local
nslookup: can't resolve '(null)': Name does not resolve

nslookup: can't resolve 'redis.default.svc.cluster.local': Name does not resolve
/ # exit
command terminated with exit code 1
[root@master mainfests]#  kubectl get pods -l app=filebeat -o wide
NAME            READY   STATUS    RESTARTS   AGE   IP            NODE     NOMINATED NODE   READINESS GATES
filb-ds-qffsb   1/1     Running   0          12m   10.244.1.37   node01   <none>           <none>
filb-ds-r4kjg   1/1     Running   0          12m   10.244.2.34   node02   <none>           <none>
[root@master mainfests]# kubectl get ds
NAME      DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE
filb-ds   2         2         2       2            2           <none>          13m

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值