K8s进阶
文章目录
pod分类
自主式pod
自主式pod总是在前台运行,同时接受k8s管理与调度,当集群中的pod因为某种原因停止,k8s会根据其副本的数量,重新的生成对应的pod
查看pod清单中有哪些字段
[root@master ~]# kubectl explain pod
KIND: Pod
VERSION: v1
DESCRIPTION:
Pod is a collection of containers that can run on a host. This resource is
created by clients and scheduled onto hosts.
FIELDS:
apiVersion <string> ##api版本
APIVersion defines the versioned schema of this representation of an
object. Servers should convert recognized schemas to the latest internal
value, and may reject unrecognized values. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
kind <string> ##资源类型
Kind is a string value representing the REST resource this object
represents. Servers may infer this from the endpoint the client submits
requests to. Cannot be updated. In CamelCase. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
metadata <Object> ##元数据
Standard object's metadata. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
spec <Object> ##详细信息-这里是需要重点配置的
Specification of the desired behavior of the pod. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
status <Object> ##状态(只读)-这部分不需要配置
Most recently observed status of the pod. This data may not be up to date.
Populated by the system. Read-only. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
想要查看详细信息就使用kubectl explain pod
想要查看下面的子字段的话kubectl explain pod.apiVersion
即可~~ apiVersion \ kind \ metadata \ spec 这四个字段是必须有的,下面就看看一份简单的pod清单应该长什么样
apiVersion: v1 ##版本为v1
kind: Pod ##资源类型为Pod
metadata: ## 元数据,下面是需要配置的子字段,这里缩进字符要保持一致
name: pod-nginx ## pod名
namespace: default ##pod所在的名称空间
labels: ##标签-- 下面全用KV格式书写,这里我们给这个pod 打上两个标签 ↓
app: pod-nginx
release: bata
spec:
containers: ##容器
- name: app-container ##容器名
image: docker.io/library/nginx:latest ##镜像地址
现在我们就试试用这个文件创建第一个自主式pod 创建命令 kubectl create -f pod-demo.yaml
删除命令 kubectl delete -f pod-demo.yaml
[root@master ~]# kubectl create -f pod-nginx.yaml
pod/pod-nginx created
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-6799fc88d8-mndrf 1/1 Running 2 21h
pod-nginx 1/1 Running 0 35s
这样一个自主式pod就创建好了,下面列举了常用的自主式pod可以定义的字段
spec.nodeSelector <map[string]string>
pod.spec.nodeSelector: #指定对象的调度节点,节点必须存在
pod.spec.restartPolicy <string>
pod.spec.restartPolicy:#容器的重启策略。有三种Always(只有退出就重启),OnFailure(失败退出时不重启),Never(只要退出就不重启),kubelet重新启动的已退出容器将以指数退避延迟(10秒,20秒,40秒......)重新启动,上限为五分钟,并在成功执行十分钟后重置
spec.containers.image.imagePullPolicy <string> #镜像的下载策略。有三种:Always(总是去仓库下载) ,Never(从不去仓库下载) , IfNotPresent(如果本地没有就去仓库下载)
spec.containers.ports: #容器公开的端口