1.yaml文件
[root@k8s-master01 ~]# cat -n pod.yaml # 新增第20行
1 apiVersion: v1 # 必选,API的版本号
2 kind: Pod # 必选,类型Pod
3 metadata: # 必选,元数据
4 name: nginx # 必选,符合RFC 1035规范的Pod名称
5 # namespace: default # 可选,Pod所在的命名空间,不指定默认为default,可以使用-n 指定namespace
6 labels: # 可选,标签选择器,一般用于过滤和区分Pod
7 app: nginx
8 role: frontend # 可以写多个
9 annotations: # 可选,注释列表,可以写多个
10 app: nginx
11 spec: # 必选,用于定义容器的详细信息
12 # initContainers: # 初始化容器,在容器启动之前执行的一些初始化操作
13 # - command:
14 # - sh
15 # - -c
16 # - echo "I am InitContainer for init some configuration"
17 # image: busybox
18 # imagePullPolicy: IfNotPresent
19 # name: init-container
20 terminationGracePeriodSeconds: 40
21 containers: # 必选,容器列表
22 - name: nginx # 必选,符合RFC 1035规范的容器名称
23 image: nginx:1.15.2 # 必选,容器所用的镜像的地址
24 imagePullPolicy: IfNotPresent # 可选,镜像拉取策略, IfNotPresent: 如果宿主机有这个镜像,那就不需要拉取了. Always: 总是拉取, Never: 不管是否存储都不拉去
25 command: # 可选,容器启动执行的命令 ENTRYPOINT, arg --> cmd
26 - nginx
27 - -g
28 - "daemon off;"
29 workingDir: /usr/share/nginx/html # 可选,容器的工作目录
30 # volumeMounts: # 可选,存储卷配置,可以配置多个
31 # - name: webroot # 存储卷名称
32 # mountPath: /usr/share/nginx/html # 挂载目录
33 # readOnly: true # 只读
34 ports: # 可选,容器需要暴露的端口号列表
35 - name: http # 端口名称
36 containerPort: 80 # 端口号
37 protocol: TCP # 端口协议,默认TCP
38 env: # 可选,环境变量配置列表
39 - name: TZ # 变量名
40 value: Asia/Shanghai # 变量的值
41 - name: LANG
42 value: en_US.utf8
43 # resources: # 可选,资源限制和资源请求限制
44 # limits: # 最大限制设置
45 # cpu: 1000m
46 # memory: 1024Mi
47 # requests: # 启动所需的资源
48 # cpu: 100m
49 # memory: 512Mi
50 startupProbe: # 可选,检测容器内进程是否完成启动。注意三种检查方式同时只能使用一种。
51 # httpGet: # httpGet检测方式,生产环境建议使用httpGet实现接口级健康检查,健康检查由应用程序提供。
52 # path: /api/successStart # 检查路径
53 # port: 80
54 tcpSocket:
55 port: 80
56 # readinessProbe: # 可选,健康检查。注意三种检查方式同时只能使用一种。
57 # httpGet: # httpGet检测方式,生产环境建议使用httpGet实现接口级健康检查,健康检查由应用程序提供。
58 # path: / # 检查路径
59 # port: 80 # 监控端口
60 # livenessProbe: # 可选,健康检查
61 #exec: # 执行容器命令检测方式
62 #command:
63 #- cat
64 #- /health
65 #httpGet: # httpGet检测方式
66 # path: /_health # 检查路径
67 # port: 8080
68 # httpHeaders: # 检查的请求头
69 # - name: end-user
70 # value: Jason
71 # tcpSocket: # 端口检测方式
72 # port: 80
73 # initialDelaySeconds: 60 # 初始化时间
74 # timeoutSeconds: 2 # 超时时间
75 # periodSeconds: 5 # 检测间隔
76 # successThreshold: 1 # 检查成功为1次表示就绪
77 # failureThreshold: 2 # 检测失败2次表示未就绪
78 lifecycle:
79 # postStart: # 容器创建完成后执行的指令, 可以是exec httpGet TCPSocket
80 # exec:
81 # command:
82 # - sh
83 # - -c
84 # - 'mkdir /data/ '
85 preStop:
86 # httpGet:
87 # path: /
88 # port: 80
89 exec:
90 command:
91 - sh
92 - -c
93 - sleep 90
94 restartPolicy: Always # 可选,默认为Always,容器故障或者没有启动成功,那就自动该容器,Onfailure: 容器以不为0的状态终止,自动重启该容器, Never:无论何种状态,都不会重启
95 #nodeSelector: # 可选,指定Node节点
96 # region: subnet7
97 # imagePullSecrets: # 可选,拉取镜像使用的secret,可以配置多个
98 # - name: default-dockercfg-86258
99 # hostNetwork: false # 可选,是否为主机模式,如是,会占用主机端口
100 # volumes: # 共享存储卷列表
101 # - name: webroot # 名称,与上述对应
102 # emptyDir: {} # 挂载目录
103 # #hostPath: # 挂载本机目录
104 # # path: /etc/hosts
105 #
2.启动
[root@k8s-master01 ~]# kubectl create -f pod.yaml
3.查看
[root@k8s-master01 ~]# kubectl get po nginx -oyaml
apiVersion: v1
kind: Pod
metadata:
annotations:
app: nginx
creationTimestamp: "2021-02-06T07:29:53Z"
labels:
app: nginx
role: frontend
managedFields:
- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:metadata:
f:annotations:
.: {}
f:app: {}
f:labels:
.: {}
f:app: {}
f:role: {}
f:spec:
f:containers:
k:{"name":"nginx"}:
.: {}
f:command: {}
f:env:
.: {}
k:{"name":"LANG"}:
.: {}
f:name: {}
f:value: {}
k:{"name":"TZ"}:
.: {}
f:name: {}
f:value: {}
f:image: {}
f:imagePullPolicy: {}
f:lifecycle:
.: {}
f:preStop:
.: {}
f:exec:
.: {}
f:command: {}
f:name: {}
f:ports:
.: {}
k:{"containerPort":80,"protocol":"TCP"}:
.: {}
f:containerPort: {}
f:name: {}
f:protocol: {}
f:resources: {}
f:startupProbe:
.: {}
f:failureThreshold: {}
f:periodSeconds: {}
f:successThreshold: {}
f:tcpSocket:
.: {}
f:port: {}
f:timeoutSeconds: {}
f:terminationMessagePath: {}
f:terminationMessagePolicy: {}
f:workingDir: {}
f:dnsPolicy: {}
f:enableServiceLinks: {}
f:restartPolicy: {}
f:schedulerName: {}
f:securityContext: {}
f:terminationGracePeriodSeconds: {}
manager: kubectl-create
operation: Update
time: "2021-02-06T07:29:53Z"
- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:status:
f:conditions:
k:{"type":"ContainersReady"}:
.: {}
f:lastProbeTime: {}
f:lastTransitionTime: {}
f:message: {}
f:reason: {}
f:status: {}
f:type: {}
k:{"type":"Initialized"}:
.: {}
f:lastProbeTime: {}
f:lastTransitionTime: {}
f:status: {}
f:type: {}
k:{"type":"Ready"}:
.: {}
f:lastProbeTime: {}
f:lastTransitionTime: {}
f:message: {}
f:reason: {}
f:status: {}
f:type: {}
f:containerStatuses: {}
f:hostIP: {}
f:phase: {}
f:podIP: {}
f:podIPs:
.: {}
k:{"ip":"172.16.122.141"}:
.: {}
f:ip: {}
f:startTime: {}
manager: kubelet
operation: Update
time: "2021-02-06T07:29:55Z"
name: nginx
namespace: default
resourceVersion: "173062"
uid: a4126b19-847a-4b89-b4dd-91fc00d9c3aa
spec:
containers:
- command:
- nginx
- -g
- daemon off;
env:
- name: TZ
value: Asia/Shanghai
- name: LANG
value: en_US.utf8
image: nginx:1.15.2
imagePullPolicy: IfNotPresent
lifecycle:
preStop:
exec:
command:
- sh
- -c
- sleep 90
name: nginx
ports:
- containerPort: 80
name: http
protocol: TCP
resources: {}
startupProbe:
failureThreshold: 3
periodSeconds: 10
successThreshold: 1
tcpSocket:
port: 80
timeoutSeconds: 1
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: default-token-k2flf
readOnly: true
workingDir: /usr/share/nginx/html
dnsPolicy: ClusterFirst
enableServiceLinks: true
nodeName: k8s-master02
preemptionPolicy: PreemptLowerPriority
priority: 0
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: default
serviceAccountName: default
terminationGracePeriodSeconds: 40 ######################################## 这个默认是30S,因为我们修改了pod的yaml变成了40
tolerations:
- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 300
- effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 300
volumes:
- name: default-token-k2flf
secret:
defaultMode: 420
secretName: default-token-k2flf
status:
conditions:
- lastProbeTime: null
lastTransitionTime: "2021-02-06T07:29:53Z"
status: "True"
type: Initialized
- lastProbeTime: null
lastTransitionTime: "2021-02-06T07:29:53Z"
message: 'containers with unready status: [nginx]'
reason: ContainersNotReady
status: "False"
type: Ready
- lastProbeTime: null
lastTransitionTime: "2021-02-06T07:29:53Z"
message: 'containers with unready status: [nginx]'
reason: ContainersNotReady
status: "False"
type: ContainersReady
- lastProbeTime: null
lastTransitionTime: "2021-02-06T07:29:53Z"
status: "True"
type: PodScheduled
containerStatuses:
- containerID: docker://09136ee347af9ed83c54a9c96015bddedd2222c2a0319a50c351778c4e0abcdc
image: nginx:1.15.2
imageID: docker-pullable://nginx@sha256:d85914d547a6c92faa39ce7058bd7529baacab7e0cd4255442b04577c4d1f424
lastState: {}
name: nginx
ready: false
restartCount: 0
started: true
state:
running:
startedAt: "2021-02-06T07:29:54Z"
hostIP: 192.168.0.108
phase: Running
podIP: 172.16.122.141
podIPs:
- ip: 172.16.122.141
qosClass: BestEffort
startTime: "2021-02-06T07:29:53Z"
4.查看时间
[root@k8s-master01 ~]# time kubectl delete po nginx
pod "nginx" deleted
real 0m41.190s
user 0m0.086s
sys 0m0.032s
我们发现我们设置的sleep 90 并不生效,如果我们需要定义90的话,我们需要在pod.yaml新增或者修改 terminationGracePeriodSeconds 的值