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

这篇具有很好参考价值的文章主要介绍了飞天使-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

飞天使-k8s基础组件分析-pod,kubernetes,docker,容器
飞天使-k8s基础组件分析-pod,kubernetes,docker,容器文章来源地址https://www.toymoban.com/news/detail-664420.html

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

到了这里,关于飞天使-k8s基础组件分析-pod的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 飞天使-k8s基础组件分析-持久化存储

    emptyDir hostpath pv和pvc介绍 nfs作为静态pv案例 nfs作为动态pv案例 使用本地文件夹作为pv 改变默认存储类及回收策略 参考文档

    2024年02月11日
    浏览(34)
  • 飞天使-k8s基础组件分析-服务与ingress

    服务的介绍 服务代理 服务发现 连接集群外服务 服务发布 无头服务 服务,pod和dns的关系 端口转发 通过expose 暴露应用 服务案例 INGRESS MetalLB使用 参考文档

    2024年02月11日
    浏览(29)
  • 飞天使-k8s知识点17-kubernetes实操2-pod探针的使用

    探针的使用 容器探针启动实验1-启动探针的使用-startupprobe Liveness Probes 和 Readiness Probes 演示 若存在started.html 则进行

    2024年02月20日
    浏览(44)
  • 飞天使-k8s知识点18-kubernetes实操3-pod的生命周期

    探针的生命周期 docker 创建:在创建阶段,你需要选择一个镜像来运行你的应用。这个镜像可以是公开的,如 Docker Hub 上的镜像,也可以是你自己创建的自定义镜像。创建自己的镜像通常需要编写一个 Dockerfile,这个文件定义了创建镜像所需的所有步骤,包括基础镜像、需要安

    2024年02月20日
    浏览(50)
  • 飞天使-k8s知识点20-kubernetes实操5-pod更新与暂停-statefulset

    资源调度 Deployment:扩缩容 资源调度 Deployment:更新的暂停与恢复 资源调度 StatefulSet:定义一个有状态服务 headless service 金丝雀发布 参考文档:https://support.huaweicloud.com/basics-cce/kubernetes_0015.html

    2024年02月20日
    浏览(38)
  • kubernetes(k8s) pod(资源限制、基础概念)

    目录  一、资源限制 1、概念 1.2、Pod和容器的资源请求和限制 1.3、CPU资源单位 1.4、内存资源单位 1.5、CPU和内存的Requests和Limits的特点 1.6、案例 二、pod 的两种使用方式 三、pod 资源共享 四、底层容器Pause 1、pause 共享资源 1.1、网络 1.2、存储 1.3、小结 2、Pause主要功能 3、Pod

    2024年02月05日
    浏览(52)
  • 【Kubernetes】 从基础认识 k8s核心pod相关概念

    提示:此篇帮助朋友们,养成从0到1不断延伸知识的一种方法 最简单的创建pod入手 访问官方文档,直接使用案例,进行修改即可! 官网地址:https://kubernetes.io/zh-cn/docs/tasks/configure-pod-container/assign-pods-nodes/ 删除不需要的部分,根据规则添加需要的参数即可得到需要的,代码如

    2024年01月25日
    浏览(44)
  • 飞天使-k8s知识点22-kubernetes实操7-ingress

    ingress 概念理解 环境准备 准备service和pod tomcat-nginx.yaml 创建ingress-http.yaml 验证效果 https 代理 创建证书 创建ingress-https.yaml 效果 查看映射到公网端口 参考文档: https://znunwm.top/archives/121212#7.4-ingress%E4%BB%8B%E7%BB%8D

    2024年02月22日
    浏览(32)
  • 飞天使-k8s知识点21-kubernetes实操6-daemonset

    daemonset service endpoint pod 之间的关系 service 基于Service访问外部服务

    2024年02月22日
    浏览(37)
  • 飞天使-k8s知识点12-kubernetes资源对象5-Volume与ConfigMap等

    为什么需要volume ConfigMap Volume nfs挂载volume 持久卷的痛点 参考文档: 作者:又拍云 链接:https://juejin.cn/post/7186925237592653884 来源:稀土掘金

    2024年01月18日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包