飞天使-k8s基础组件分析-pod

文章目录

pod介绍

复制代码
最小的容器单元
为啥需要pod?
答: 多个进程丢一个容器里,会因为容器里个别进程出问题而出现蝴蝶效应,pod 是更高级的处理方式


pod 如何共享相同的ip和端口
答: 由于它们在相同的网络名称和空间中运行

如何查看pod 版本
答: kubectl explain pod
[root@k8s-01 chapter03]# kubectl explain pod
KIND:     Pod
VERSION:  v1

如何查看pod 的信息
答:kubectl get pod,svc
[root@k8s-01 chapter03]# kubectl get pod,svc
NAME                        READY   STATUS     RESTARTS   AGE
pod/busybox                 1/1     Running    11         11h
pod/nginx-97499b967-jzxwg   1/1     Running    0          11h
pod/two-containers          1/2     NotReady   0          34m

NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        11h
service/nginx        NodePort    10.104.210.165   <none>        80:30001/TCP   11h



pod创建的小案例演示一下? 
答:
cat two-container-pod.yaml
[root@k8s-01 chapter03]# cat two-container-pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: two-containers
spec:
  restartPolicy: Never
  volumes:
  - name: shared-data
    emptyDir: {}
  containers:
  - name: nginx-container
    image: nginx
    volumeMounts:
    - name: shared-data
      mountPath: /usr/share/nginx/html

  - name: debian-container
    image: debian
    volumeMounts:
    - name: shared-data
      mountPath: /pod-data
    command: ["/bin/sh"]
    args: ["-c", "echo Hello from the debian container > /pod-data/index.html"]
    
执行如下命令创建Pod
# kubectl apply --f two-container-pod.yaml

查看关于Pod的信息
# kubectl get pod two-containers ---output=yaml

进入nginx的容器进行校验结果
# kubectl exec --it two-containers --c nginx-container -- /bin/bash
# apt-get update
# apt-get install curl procps
# ps aux
# curl localhost 

pod 生命周期

复制代码
pod 可能存在的状态
Pending           等待中
Running           运行中
Succeeded      正常终止
Failed              异常停止
Unkonwn         未知状态

Pending

Pod已经被创建,但还没有完成调度,或者说有一个或多个镜像正处于从远程仓库下载的过程。

处在这个阶段的Pod可能正在写数据到etcd中、调度、pull镜像或启动容器。

 

Running

该 Pod 已经绑定到了一个节点上,Pod 中所有的容器都已被创建。至少有一个容器正在运行,或者正处于启动或重启状态。

 

Succeeded

Pod中的所有的容器已经正常的执行后退出,并且不会自动重启,一般会是在部署job的时候会出现。

 

Failed

Pod 中的所有容器都已终止了,并且至少有一个容器是因为失败终止。也就是说,容器以非0状态退出或者被系统终止。

 

Unkonwn

API Server无法正常获取到Pod对象的状态信息,通常是由于其无法与所在工作节点的kubelet通信所致。
 

容器的探测
为了探测Pod是否处于健康状态,kubernetes提供三种探测方式:
ExecAction
TCPSocketAction
HTTPGetAction

探测有以下三种结果之一
Success
Failure
Unknown

探测的三种类型
livenessProbe
readlinessProbe
startupProbe

StartupProbe:k8s 1.16版本后新加的探测方式,用于判断容器内应用程序是否已经启动。如果配置了startupProbe,就会先禁止其他的探测,直到它成功为止,成功后将不再进行探测。比较适用于容器启动时间长的场景。
LivenessProbe:用于探测容器是否运行,如果探测失败,kubelet会根据配置的重启策略进行相应的处理。若没有配置该探针,默认就是success。
ReadinessProbe:一般用于探测容器内的程序是否健康,它的返回值如果为success,那么久代表这个容器已经完成启动,并且程序已经是可以接受流量的状态。

 

pod 的生命周期如何被停止
答; 人为的停止它
或者删除它所属的控制器
在一段时间内,阶段为成功或失败的pod 将过期并自动销毁


pod 生命周期有啥案列分享? 
使用命令行方式
[root@k8s-01 chapter03]# cat exec-liveness.yaml 
apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: liveness-exec
spec:
  containers:
  - name: liveness  # 使用liveness的方式进行健康探测
    image: busybox
    args:
    - /bin/sh
    - -c
    - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600  # 由于这里创建一个文件后,5秒钟后又把文件删除,所以5分钟后探测应处于失败状态
    livenessProbe:
      exec:  # 执行如下命令行,如果返回结果的状态码为0,证明探测成功,否则证明失败。执行重启策略,默认为always.
        command:
        - cat
        - /tmp/healthy
      initialDelaySeconds: 5   # 第一次探测在等待容器启动后多少秒开启执行,此处设置为5秒。
      periodSeconds: 5  # 设置多长时间探测一次,这里设置为5秒。



Events:
  Type     Reason     Age                 From               Message
  ----     ------     ----                ----               -------
  Normal   Scheduled  106s                default-scheduler  Successfully assigned default/liveness-exec to k8s-02
  Normal   Pulled     88s                 kubelet, k8s-02    Successfully pulled image "busybox"
  Normal   Created    87s                 kubelet, k8s-02    Created container liveness
  Normal   Started    87s                 kubelet, k8s-02    Started container liveness
  Warning  Unhealthy  45s (x3 over 55s)   kubelet, k8s-02    Liveness probe failed: cat: can't open '/tmp/healthy': No such file or directory
  Normal   Killing    45s                 kubelet, k8s-02    Container liveness failed liveness probe, will be restarted
  Normal   Pulling    15s (x2 over 106s)  kubelet, k8s-02    Pulling image "busybox"
[root@k8s-01 chapter03]# kubectl describe pod liveness-exec

使用http方式进行探测
[root@k8s-01 chapter03]# cat http-liveness.yaml 
apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: liveness-http
spec:
  containers:
  - name: liveness
    image: nginx
    livenessProbe:
      httpGet:
        path: /
        port: 80
      initialDelaySeconds: 3
      periodSeconds: 3

使用tcp方式
[root@k8s-01 chapter03]# cat tcp-liveness-readiness.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80
    readinessProbe:
      tcpSocket:
        port: 80
      initialDelaySeconds: 5
      periodSeconds: 10
    livenessProbe:
      tcpSocket:
        port: 80
      initialDelaySeconds: 15
      periodSeconds: 20


init 容器

复制代码
就是初始化容器,启动之前就会开始执行
init 容器始终运行到完成
每个init 容器必须在下一个容器启动之前成功完成 



创建pod
# kubectl apply --f init-pod.yaml

查看pod的状态
# kubectl get --f init-pod.yaml

查看Pod的详细信息
# kubectl describe --f init-pod.yaml

查看pod中的init容器日志
# kubectl logs myapp-pod --c init-myservice
# kubectl logs myapp-pod --c init-mydb

创建mydb和myservice服务,并再次查看pod状态
# Kubectl apply --f init-pod-service.yaml
# Kubectl get --f init-pod.yaml



有没有案列来告诉?
答: 
[root@k8s-01 chapter03]# cat init-pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: busybox:1.28
    command: ['sh', '-c', 'echo The app is running! && sleep 3600']
  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', 'until nslookup myservice; do echo waiting for myservice; sleep 2; done;']
  - name: init-mydb
    image: busybox:1.28
    command: ['sh', '-c', 'until nslookup mydb; do echo waiting for mydb; sleep 2; done;'][root@k8s-01 chapter03]# 
[root@k8s-01 chapter03]# 
[root@k8s-01 chapter03]# cat init-pod-service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: myservice
spec:
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9376
---
apiVersion: v1
kind: Service
metadata:
  name: mydb
spec:
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9377


演示效果:
**nslookup: can't resolve 'myservice'**
waiting for myservice
[root@k8s-01 chapter03]# kubectl create -f init-
init-containers.yaml   init-pod-service.yaml  init-pod.yaml          
[root@k8s-01 chapter03]# kubectl create -f init-pod-service.yaml 
service/myservice created
service/mydb created
[root@k8s-01 chapter03]# kubectl get pod
NAME                    READY   STATUS    RESTARTS   AGE
busybox                 1/1     Running   12         12h
myapp-pod               1/1     Running   0          8m27s
nginx-97499b967-jzxwg   1/1     Running   0          12h

容器handler

复制代码
它是用来处理程序附加到容器生命周期中的事件,支持postStart和preStop事件。比如在容器启动后立即发送postStart事件,在容器终止前发送preStop事件

[root@k8s-01 chapter03]# cat lifecycle-events.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: lifecycle-demo
spec:
  containers:
  - name: lifecycle-demo-container
    image: nginx
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]
      preStop:
        exec:
          command: ["/bin/sh","-c","nginx -s quit; while killall -0 nginx; do sleep 1; done"]



创建pod
# kubectl apply --f lifecycle-events.yaml

校验Pod中的容器是否运行
# kubectl get pod lifecycle-demo

通过以下命令校验postStart和preStop是否执行
# kubectl exec --it lifecycle-demo -- /bin/bash
/# cat /usr/share/message

pod中容器共享进程空间

复制代码
当启用进程名称空间共享时,容器中的进程对该pod中的所有容器都是可见的。
[root@k8s-01 chapter03]# cat share-process-namespace.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  shareProcessNamespace: true
  containers:
  - name: nginx
    image: nginx
  - name: shell
    image: busybox
    securityContext:
      capabilities:
        add:
        - SYS_PTRACE
    stdin: true
    tty: true


创建pod
# kubectl apply --f share-process-namespace.yaml


关联shell容器和运行ps
# kubectl attach --it nginx --c shell
/# ps ax

sidecar 容器共享

复制代码
Sidecar容器在不改变现有容器的情况下扩展和增加pod的功能,也就是说其中一个容器增加了另一个预存在的容器功能


[root@k8s-01 chapter03]# cat sidecar.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: pod-with-sidecar
spec:
  volumes:
  - name: shared-logs
    emptyDir: {}
  containers:
  - name: sidecar-container
    image: alpine
    command: ["/bin/sh"]
    args: ["-c", "while true; do date >> /var/log/app.txt; sleep 5;done"]
    volumeMounts:
    - name: shared-logs
      mountPath: /var/log
  - name: app-container
    image: nginx:1.7.9
    ports:
      - containerPort: 80
    volumeMounts:
    - name: shared-logs
      mountPath: /usr/share/nginx/html

参考链接

复制代码
https://edu.csdn.net/learn/27762/375863?spm=3001.4143
相关推荐
努力搬砖的咸鱼2 小时前
容器之间怎么通信?Docker 网络全解析
网络·docker·云原生·容器
liming4954 小时前
Ubuntu18.04部署k8s
云原生·容器·kubernetes
元直数字电路验证6 小时前
ASP.NET Core Web APP(MVC)开发中无法全局配置 NuGet 包,该怎么解?
前端·javascript·ui·docker·asp.net·.net
YC运维6 小时前
Kubernetes资源管理全解析
java·容器·kubernetes
Leinwin6 小时前
微软发布Azure Kubernetes Service Automatic国际版
microsoft·kubernetes·azure
chinesegf8 小时前
Docker篇6-项目app.py和flask_app.service配置和映射到docker中
docker·容器·flask
退役小学生呀8 小时前
二十二、DevOps:基于Tekton的云原生平台落地(三)
linux·云原生·容器·kubernetes·k8s·devops·tekton
维尔切8 小时前
搭建 k8s
云原生·容器·kubernetes
hwj运维之路8 小时前
《Kubernetes面试题汇总系列》
云原生·容器·kubernetes
闲人编程8 小时前
Docker化你的Python应用:从开发到生产
python·docker·eureka·开发·生产·codecapsule