Pod资源对象+健康检查

Deployment、Service、Pod是k8s最核心的3个资源对象

Deployment:

最常见的无状态的控制器,支持应用的扩容缩容、滚动更新等操作

Service:

为弹性变动且存在生命周期的Pod对象提供了一个固定的访问接口,用户服务发现和服务访问

Pod:

是运行容器以及调度的最小单位,同一个Pod可以同时运行多个容器,这些容器共享NET、UTS、IPC,除此之外还有USER、PID、MOUNT

ReplicationController:(rc)

用于确保每个Pod副本在任意时刻都能满足目标数量,简单点来说,它用于保证每个容器或容器组总是运行并且可以访问:老一代无状态的Pod控制器

ReplicaSet:(rs)

新一代无状态的Pod应用控制器,它与rc的不同之处在于支持的标签选择器不同,rc只支持等值选择器,rs还额外支持基于集合的选择器

StatefulSet:

用于管理有状态的持久化应用,如database服务程序,它与Deployment不同之处在于,它会为每一个Pod创建一个独有的持久性标识符,并确保每个Pod之间的顺序性

DamonSet:

确保每一个节点都运行了某个Pod的一个副本,新增的节点一样会被添加此类Pod,在节点移除时Pod会被收回

Job:

用于管理运行完成后即可终止的银应用,例如批量处理作业任务

volume:

PV PVC

ConfigMap:

Secret:

Role:

ClusterRole:

ClusterRoleBinding:

Service account:

Helm:


Namespace:名称空间


默认的名称空间:Default

//查看名称空间

[root@master ~]# kubectl  get  ns
NAME              STATUS   AGE
default           Active   6d22h
kube-node-lease   Active   6d22h
kube-public       Active   6d22h
kube-system       Active   6d22h

//查看名称空间详细信息

[root@master ~]# kubectl  describe  ns  default

//创建名称空间

[root@master ~]# kubectl create ns bdqn

//使用yaml创建名称空间

[root@master ~]# vim  test-ns.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: test
[root@master ~]# kubectl  apply  -f  test-ns.yaml

PS:

namespace资源对象仅用于资源对象的隔离,并不能隔绝不同名称空间的Pod之间的通信,那是网络策略资源的功能

//删除名称空间:

[root@master ~]# kubectl  delete ns test
[root@master ~]# kubectl delete -f test-ns.yaml

//查看指定名称空间的资源可以使用--namespace或者-n选项

[root@master ~]# kubectl get pod --namespace=bdqn 
[root@master ~]# kubectl get pod -n bdqn

Pod


//通过yaml文件手动创建pod

[root@master ~]# vim  pod.yaml
kind: Pod
apiVersion: v1
metadata:
  name: test-pod
  namespace:  bdqn  //指定名称空间
spec:
  containers:
  - name: test-app
    image:  httpd:v1
[root@master ~]# kubectl apply -f  pod.yaml
[root@master ~]# kubectl get pod -n bdqn
NAME       READY   STATUS    RESTARTS   AGE
test-pod   1/1     Running   0          19s
//删除
[root@master ~]# kubectl delete pod -n bdqn test-pod

Pod中镜像获取策略:


Always:

镜像标签为“lastest”或镜像不存在时,总是从指定的仓库中获取镜像

ifNotPresent:

仅当本地镜像不存在时从目标仓库中下载

Never:

禁止从仓库下载镜像,即只是用本地镜像

kind: Pod
apiVersion: v1
metadata:
  name: test-pod
  namespace:  bdqn
  labels:
    app:  test-web
spec:
  containers:
  - name: test-app
    image:  httpd:v1
    imagePullPolicy:  IfNotPresent  //指定镜像获取策略
    ports:
    - protocol: TCP
      containerPort: 90  //手动创建的Pod,指定容器暴露的端口也不会生效

PS:

对于标签为“latest”或者这标签不存在,其默认镜像下载策略为“Always”,而对于其他标签的镜像,默认策略为“ifNotPresent”

容器的重启策略


Always:

但凡Pod对象终止就将其重启,此为默认设定

OnFailure:

仅在Pod对象出现错误时才将其重启

Never:

从不重启

Pod的默认健康检查


[root@master ~]# vim  healcheck.yaml
apiVersion: v1
kind: Pod
metadata:
  labels:
    test: healcheck
  name: healcheck
spec:
  restartPolicy:  OnFailure
  containers:
  - name: healthcheck
    image:  busybox
    args:  //由镜像运行成容器的时候去做的事情
    - /bin/sh
    - -c
    - sleep 20; exit  1
[root@master ~]# vim healcheck.yaml
[root@master ~]# kubectl get pod -w  //实时查看它的状态
[root@master ~]# kubectl get pod
NAME          READY   STATUS   RESTARTS   AGE
healthcheck   0/1     Error    5          8m44s
//可以看到每过20s就会重启一次

LivenessProbe(活跃度探测)


[root@master ~]# vim  liveness.yaml
kind: Pod
apiVersion: v1
metadata:
  name: liveness
  labels:
    test: liveness
spec:
  restartPolicy:  OnFailure
  containers:
  - name: liveness
    image:  busybox
    args:  //由镜像运行成容器的时候去做的事情
    - /bin/sh
    - -c
    - touch /tmp/test;  sleep 60; rm -rf /tmp/test; sleep 300
    livenessProbe:  //活跃度探测
      exec:
        command:
        - cat
        - /tmp/test
      initialDelaySeconds:  10  //pod运行10秒后开始探测
      periodSeconds:  5  //每5秒探测一次

PS:

Liveness活跃度探测,根据某个文件是否存在,来确认某个服务是否正常运行,如果存在则正常,否则,它会根据你设置的Pod的重启策略操作Pod

Readiness(敏捷探测、就绪性探测)


[root@master ~]# vim  readiness.yaml 
kind: Pod
apiVersion: v1
metadata:
  name: readiness
  labels:
    test: readiness
spec:
  restartPolicy:  OnFailure
  containers:
  - name: readiness
    image:  busybox
    args:
    - /bin/sh
    - -c
    - touch /tmp/test;  sleep 60; rm -rf /tmp/test; sleep 300
    readinessProbe:
      exec:
        command:
        - cat
        - /tmp/test
      initialDelaySeconds:  10
      periodSeconds:  5

PS:总结liveness和readiness探测

1)leveness和readiness是两种健康检查机制,如果不特意配置,k8s两种探测采取相同的默认行为,即通过判断容器启动进程的返回是否为零,来判断探测是否成功

2)两种探测配置方法完全一样,不同之处在于探测失败后的行为:

liveness探测是根据Pod重启策略操作容器,大多数是重启容器

readinesss则是将容器设置为不可用,不接收Service转发的请求

3)两种探测方法可以独立存在,也可以同时使用,用liveness判断容器是否需要实现自愈;用readiness判断容器是否已经准备好对外提供服务

监控检测应用


在scale(扩容、缩容)中的应用

[root@master ~]# vim hcscal.yaml
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: web
spec:
  replicas: 3
  template:
    metadata:
      labels:
        run:  web
    spec:
      containers:
      - name: web
        image:  httpd
        ports:
        - containerPort:  80
        readinessProbe:
          httpGet:
            scheme: HTTP
            path: /healthy
            port: 80
          initialDelaySeconds:  10
          periodSeconds:  5

---
kind: Service
apiVersion: v1
metadata:
  name: web-svc
spec:
  type: NodePort
  selector:
    run:  web
  ports:
  - protocol: TCP
    port: 90
    targetPort: 80
    nodePort: 30321
[root@master ~]# kubectl  exec  web-69d659f974-ktqbz  touch  /usr/local/apache2/htdocs/healthy
[root@master ~]# kubectl  describe  svc
Endpoints:                10.244.1.4:80

在更新过程中的使用


[root@master ~]# vim  app.v1.yaml
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: app
spec:
  replicas: 10
  template:
    metadata:
      labels:
        run:  app
    spec:
      containers:
      - name: app
        image:  busybox
        args:
        - /bin/sh
        - -c
        - sleep 10; touch /tmp/healthy; sleep 3000
        readinessProbe:
          exec:
            command:
            - cat
            - /tmp/healthy
          initialDelaySeconds:  10
          periodSeconds: 5

//第一次升级

[root@master ~]# kubectl  apply  -f  app.v1.yaml  --record
[root@master ~]# cp  app.v1.yaml app.v2.yaml 
[root@master ~]# vim  app.v2.yaml
//修改
        args:
        - /bin/sh
        - -c
        - sleep 3000

//第二次升级

[root@master ~]# cp  app.v1.yaml  app.v3.yaml 
[root@master ~]# vim  app.v3.yaml
//删除探测机制
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: app
spec:
  replicas: 10
  template:
    metadata:
      labels:
        run:  app
    spec:
      containers:
      - name: app
        image:  busybox
        args:
        - /bin/sh
        - -c
        - sleep 3000

maxSurge:此参数控制滚动更新过程中,副本总数超过预期数的值,可以是整数,也可以是百分比,默认为1

maxUnavilable:不可用Pod的值,默认为1

发布了35 篇原创文章 · 获赞 22 · 访问量 2591

猜你喜欢

转载自blog.csdn.net/weixin_45636702/article/details/104061088