Our help documentation
kubectl explain pod
FIELDS:
apiVersion <string> version number
kind <string> Resource Type
metadata <Object> Metadata
Desired spec <Object> behavior specified container
status <Object> (read-only) state
Look at an example
---
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
namespace: default
spec:
containers:
- name: nginx-container
image: nginx:latest
metadata.name: is the namespace name must be unique.
metadata.namespace: Specifies the name space
spec.containers: definition of running container behavior
spec.containers.name: container name
Mirror operation containers: spec.containers.image
Everyone's needs are different, not the same resource configuration list. So here only describes how to find help and how to write a document format.
Resources only need to define a list of four, two of which (apiVersion and kind) has been decided. It is only concerned with metadata and spec.
metadata common options:
View Help information: kubectl explain pod.metadata
name <string>
annotations <map[string]string>
labels <map[string]string>
<Map [string] string> Usage: key: value, see metadata.labels format.
示例演示:
---
apiVersion: v1
kind: Pod
metadata:
annotations:
Author: mycloudedu
name: nginx-pod
namespace: default
labels:
app: nginx
tier: frontend
spec:
containers:
- name: nginx-container
image: nginx:latest
验证:
[root@master kubernetes]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-pod 1/1 Running 0 3m46s app=nginx,tier=frontend[root@master kubernetes]# kubectl describe pod nginx-pod
Name: nginx-pod
Namespace: default
Priority: 0
Node: node02/192.168.100.82
Start Time: Wed, 11 Dec 2019 19:13:52 +0800
Labels: app=nginx
tier=frontend
Annotations: Author: mycloudedu…..
spec common options:
apiVersion: v1
kind: Pod
metadata:
name: liveness-exec-pod
namespace: default
spec:
containers:
- name: liveness-exec-container
image: busybox:latest
imagePullPolicy: IfNotPresent
command:
- "/bin/sh"
- "-c"
- "touch /1.txt; sleep 10; rm -f /1.txt; sleep 3600"
livenessProbe:
periodSeconds: 2
exec:
command:
- "cat"
- "/1.txt"
验证:
[root@master kubernetes]# kubectl get pods
NAME READY STATUS RESTARTS AGE
liveness-exec-pod 1/1 Running 0 35s注意看二次查询的结果。重启容器一次
[root@master kubernetes]# kubectl get pods
NAME READY STATUS RESTARTS AGE
liveness-exec-pod 1/1 Running 1 50s
示例readinessProbe演示:(服务是否准备就绪)
---
apiVersion: v1
kind: Pod
metadata:
name: readinessprobe-httpget-pod
namespace: default
spec:
containers:
- name: readinessprobe-httpget-container
image: nginx:latest
ports:
- name: http
containerPort: 80
imagePullPolicy: IfNotPresent
readinessProbe:
httpGet:
port: 80
periodSeconds: 3
initialDelaySeconds: 2
验证:
[root@master kubernetes]# kubectl get pods
NAME READY STATUS RESTARTS AGE
readinessprobe-httpget-pod 1/1 Running 0 19s交互式进入容器,人为破坏主页。
[root@master kubernetes]# kubectl exec readinessprobe-httpget-pod -it -- /bin/sh
# rm /usr/share/nginx/html/index.html
[root@master kubernetes]# kubectl get pod
NAME READY STATUS RESTARTS AGE
readinessprobe-httpget-pod 0/1 Running 0 113s交互式进入容器,人为创建主页。
[root@master kubernetes]# kubectl exec readinessprobe-httpget-pod -it -- /bin/sh
# echo hello > /usr/share/nginx/html/index.html
[root@master kubernetes]# kubectl get pod
NAME READY STATUS RESTARTS AGE
readinessprobe-httpget-pod 1/1 Running 0 2m58s
示例lifecycle演示:(创建容器后立即调用PostStart)
---
apiVersion: v1
kind: Pod
metadata:
name: lifecycle-poststart-pod
namespace: default
labels:
app: nginx
tier: frontend
spec:
containers:
- name: lifecycle-poststart-container
image: nginx:latest
imagePullPolicy: IfNotPresent
lifecycle:
postStart:
exec:
command: ["/bin/sh","-c","echo Hello from the postStart handler > /usr/share/nginx/html/index.html"]
verification:
[root@master kubernetes]# curl 10.244.2.25
Hello from the postStart handler