k8s-pod参数详解

概述

  k8s中的pod参数详解。官方文档
  版本 k8s 1.27.x 、busybox:stable-musl、nginx:stable-alpine3.19-perl

创建Pod

编写一个简单的Pod

apiVersion: v1
kind: Pod
# 元数据
metadata:
  # pod 名称 唯一
  name: busybox
  # 命名空间
  namespace: test
  # 标签
  labels:
    app: busybox

spec:
  containers:
  - name: busybox
    image: harbor.easzlab.io.local:8443/library/busybox:stable-musl
    imagePullPolicy: IfNotPresent
    command:
      - sleep
      - "3600"
  restartPolicy: Always
[root@hadoop01 pod]# kubectl get pod -n test
NAME                           READY   STATUS    RESTARTS   AGE
busybox                        1/1     Running   0          25s
tomcat-demo-7ddd4cf4f5-bpskf   1/1     Running   0          7d20h

[root@hadoop01 pod]# kubectl exec -it -n test busybox -- sh
/ # 
/ # ls
bin   dev   etc   home  proc  root  sys   tmp   usr   var
/ # 

添加常用参数

apiVersion: v1
kind: Pod
# 元数据
metadata:
  # pod 名称 唯一
  name: busybox
  # 命名空间
  namespace: test
  # 标签
  labels:
    app: busybox

spec:
  containers:
  - name: busybox
    image: harbor.easzlab.io.local:8443/library/busybox:stable-musl
    # 可选值: Always|IfNotPresent|Never
    imagePullPolicy: IfNotPresent
    # 环境变量
    env:
    - name: app
      value: busybox
    # 运行终端 守护进程,不会运行完就结束
    tty: true
    # 特权模式 对宿主机有 root 权限
    securityContext:
      privileged: true
    # 工作目录
    workingDir: /test
    # 命令
    command: ["/bin/sh"]
    # 参数
    args: ["-c", "while true; do echo hello;sleep 10;done"]
  restartPolicy: Always
[root@hadoop01 pod]# kubectl apply -f arg-busbox-pod.yaml
pod/busybox created
[root@hadoop01 pod]# kubectl get pod -n test
NAME                           READY   STATUS    RESTARTS   AGE
busybox                        1/1     Running   0          7s
tomcat-demo-7ddd4cf4f5-bpskf   1/1     Running   0          7d22h
[root@hadoop01 pod]# kubectl logs -f busybox -n test
hello
hello
hello
hello
hello
hello
hello

为Pod的容器分配资源

apiVersion: v1
kind: Pod
# 元数据
metadata:
  # pod 名称 唯一
  name: busybox
  # 命名空间
  namespace: test
  # 标签
  labels:
    app: busybox

spec:
  restartPolicy: Always
  containers:
  - name: busybox
    image: harbor.easzlab.io.local:8443/library/busybox:stable-musl
    imagePullPolicy: IfNotPresent
    command:
      - sleep
      - "3600"
    resources:
      # 注意MiB 和 Mb 有区别,1MiB=1024k,而1Mb=100k k8s 使用 Mi 大的
      # cpu的单位 ,1c=1000m
      # 最小需要
      requests:
        memory: "100Mi"
        cpu: "1000m"
      # 最大限制
      limits:
        memory: "200Mi"
        cpu: "1000m"

在这里插入图片描述
注意:因为安装的不是 docker ,所以 crictl stats 并不详细
在这里插入图片描述
如 docker stats 能看出资源限制后的变化。
在这里插入图片描述

网络相关

网络默认配置使用的是 k8s 集群的配置。

在这里插入图片描述
自定义网络配置如下:

apiVersion: v1
kind: Pod
# 元数据
metadata:
  # pod 名称 唯一
  name: nginx
  # 命名空间
  namespace: test
  # 标签
  labels:
    app: nginx 

spec:
  # 使用宿主机的网络,与宿主机共享局域网
  hostNetwork: false
  # 可选值:Default | ClusterFirst | ClusterFirstWithHostNet | None
  dnsPolicy: "None"
  dnsConfig:
    nameservers:
    - 8.8.8.8
  # 域名映射
  hostAliases:
    - ip: 192.168.1.18
      hostnames:
      - "foo.local"
      - "bar.local"
  containers:
  - name: nginx
    image: harbor.easzlab.io.local:8443/library/nginx:stable-alpine3.19-perl
    imagePullPolicy: IfNotPresent
    ports:
    - name: default
      containerPort: 80
      # 如果使用hostnetwork 那这里就不能指定端口,配置了也不会生效
      hostPort: 8080

在这里插入图片描述

Pod健康检查

  有一种情况,容器还是正常运行的,但是服务无法使用;这种场景,一般称之为容器死锁,为了能够迟早的发现这种问题,可以通过对容器做健康检查来迟早发现问题。

在这里插入图片描述

  • 启动探针:启动成功之后才是就绪与存活探针,启动失败,会根据策略重启

  • 就绪探针:如初始化 mysql 等,不会重启,会让服务不可用

  • 存活探针:通过 http tcp grpc 等方式验证容器是否可用

  • 探针示例,后续补充

启动探针

spec:
  containers:
  - name: your-container
    startupProbe:
      httpGet:
        path: /healthz
        port: 8080
      failureThreshold: 30
      initialDelaySeconds: 120
      periodSeconds: 10
      timeoutSeconds: 1
      successThreshold: 1

存活探针

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: myapp:latest
    ports:
    - containerPort: 80
    livenessProbe:
      httpGet:
        path: /healthz
        port: 80
      initialDelaySeconds: 5
      periodSeconds: 10

  Liveness探针每10秒检查一次http://myapp-pod:8080/healthz,initialDelaySeconds用于指定探针初次执行前的延迟时间。

就绪探针

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: myapp:latest
    ports:
    - containerPort: 80
    readinessProbe:
      httpGet:
        path: /readyz
        port: 80
      initialDelaySeconds: 5
      periodSeconds: 10

  Readiness探针每5秒检查一次http://myapp-pod:8080/readyz,initialDelaySeconds用于指定探针初次执行前的延迟时间。

作用整个Pod参数配置

创建docker-registry

kubectl create secret docker-registry regcred \
--docker-server=<你的镜像仓库服务器> \
--docker-username=<你的用户名> \
--docker-password=<你的密码> \
--docker-email=<你的邮箱地址> \
-n test

卷挂载

结束

相关推荐

  1. <span style='color:red;'>k</span><span style='color:red;'>8</span><span style='color:red;'>s</span>-<span style='color:red;'>Pod</span>

    k8s-Pod

    2024-06-07 07:36:06      32 阅读
  2. <span style='color:red;'>K</span><span style='color:red;'>8</span><span style='color:red;'>S</span> <span style='color:red;'>POD</span>

    K8S POD

    2024-06-07 07:36:06      24 阅读
  3. K8S Pod

    2024-06-07 07:36:06       17 阅读
  4. <span style='color:red;'>k</span><span style='color:red;'>8</span><span style='color:red;'>s</span>-<span style='color:red;'>Pod</span>

    k8s-Pod

    2024-06-07 07:36:06      16 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-07 07:36:06       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-07 07:36:06       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-07 07:36:06       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-07 07:36:06       20 阅读

热门阅读

  1. 数组知识点

    2024-06-07 07:36:06       8 阅读
  2. 天气数据集2-应用RNN做天气预测

    2024-06-07 07:36:06       9 阅读
  3. 数据结构:共享栈

    2024-06-07 07:36:06       9 阅读