K8s - Pod 基本操作

1.定义创建pod

创建一个Hello World Pod,运行一个输出“Hello World的容器”

  • 定义hello-world-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: hello-world
spec:
  restartPolicy: OnFailure
  containers:
  - name: hello
    image: "ubuntu"
    command: ["/bin/echo","hello”,”world"]

  • 字段解释
apiVersion: 声明K8s的API版本
kind: 声明API对象的类型,这里是Pod
metadata:设置Pod的元数据
  name: hello-world 指定Pod的名称Pod名称必粗在Namespace内唯一
spec:配置Pod的具体规格
  restartPolicy: 重启策略
  containers:容器规格,数组形式,每一项定义一个容器
  - name:指定容器的名称,在Pod的定义中唯一
    image:设置容器镜像
    command:设置容器的启动命令
  • 创建
kubectl create -f hello-world-pod.yaml

2.查询Pod

# 简要查询
kubectl get pod hello-world

#JSON格式显示Pod的完整信息
kubectl get pod hello-world --output yaml

#YAML方式显示Pod的完整信息
kubectl get pod hello-world --output json
  • 状态和生命周期查询
kubectl describe pod hello-world

3.更新Pod

  • 更新
kubectl replace -f hello-world-pod.yaml

但是由于Pod的很多属性没办法修改,比如容器镜像,这时候可以采用--force参数

  • 重建Pod
kubectl replace --force -f hello-world-pod.yaml

4.删除Pod

  • 通过kubectl delete删除指定Pod
kubectl delete pod hello-world
  • 通过kubectl delete批量删除全部Pod
kubectl delete pod --all

猜你喜欢

转载自blog.csdn.net/qq_41210783/article/details/104494075