KUBERNETES-1-3-資源清單
1.kubectl explain pods可以看到建立pod的五個必要引數,apiVersion,kind ,metadata ,spec , status 。這裡還可以通過增加 . 的方式進一步深度查詢。
[[email protected] ~]# kubectl explain pods
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>
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/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/api-conventions.md#types-kinds
metadata <Object>
Standard object's metadata. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
spec <Object>
Specification of the desired behavior of the pod. More info:
https://git.k8s.io/community/contributors/devel/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/api-conventions.md#spec-and-status
2.這裡我們嘗試通過yaml檔案建立pod。
[[email protected] ~]# mkdir manifests
[[email protected] ~]# cd manifests
[[email protected] manifests]# ls
[[email protected] manifests]# vim pod-demo.yaml
[[email protected] manifests]# cat pod-demo.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-demo
namespace: default
labels:
app: myapp
tier: frontend
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
- name: busybox
image: busybox:latest
command:
- "/bin/sh"
- "-c"
- "echo ${date} >> /usr/share/nginx/html/index.html; sleep 5"
[[email protected] manifests]# kubectl create -f pod-demo.yaml
pod/pod-demo created
3.建立完成後kubectl get pods檢視狀態。kubectl describe pods pod-demo檢視pod詳細資訊。Events:中會顯示過程。 Warning BackOff 11s (x6 over 1m) kubelet, node1.example.com Back-off restarting failed container顯示有容器發生錯誤。
原因在於"echo ${date} >> /usr/share/nginx/html/index.html; sleep 5"這裡pod之間是沒有實現儲存共享的,myapp上有nginx,而busybox上沒有nginx。這裡不展開詳細說明,在後面再進行介紹。
[[email protected] manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 0/1 Completed 0 1d
myapp-848b5b879b-7h254 1/1 Running 1 1d
myapp-848b5b879b-d7rjs 1/1 Running 1 1d
myapp-848b5b879b-wv5cz 1/1 Running 1 1d
nginx-deploy-5b595999-tj8ms 1/1 Running 1 1d
pod-demo 2/2 Running 3 1m
[[email protected] manifests]# kubectl describe pods pod-demo
Name: pod-demo
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: node1.example.com/172.20.0.129
Start Time: Sat, 08 Dec 2018 09:18:36 -0500
Labels: app=myapp
tier=frontend
Annotations: <none>
Status: Running
IP: 10.244.1.11
Containers:
myapp:
Container ID: docker://36fc40d82049266d0975eecb2859b66e13d314e42f466d4c768fcb9766eabf3a
Image: ikubernetes/myapp:v1
Image ID: docker-pullable://ikubernetes/[email protected]:9c3dc30b5219788b2b8a4b065f548b922a34479577befb54b03330999d30d513
Port: <none>
Host Port: <none>
State: Running
Started: Sat, 08 Dec 2018 09:18:37 -0500
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-s5rf4 (ro)
busybox:
Container ID: docker://ff229302ee17d15ec991aad0120ead9de4bb51edfb7553091d501aa05d8e510f
Image: busybox:latest
Image ID: docker-pullable://[email protected]:2a03a6059f21e150ae84b0973863609494aad70f0a80eaeb64bddd8d92465812
Port: <none>
Host Port: <none>
Command:
/bin/sh
-c
echo ${date} >> /usr/share/nginx/html/index.html; sleep 5
State: Waiting
Reason: CrashLoopBackOff
Last State: Terminated
Reason: Completed
Exit Code: 0
Started: Sat, 08 Dec 2018 09:20:02 -0500
Finished: Sat, 08 Dec 2018 09:20:07 -0500
Ready: False
Restart Count: 3
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-s5rf4 (ro)
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
Volumes:
default-token-s5rf4:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-s5rf4
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m default-scheduler Successfully assigned default/pod-demo to node1.example.com
Normal Pulled 2m kubelet, node1.example.com Container image "ikubernetes/myapp:v1" already present on machine
Normal Created 2m kubelet, node1.example.com Created container
Normal Started 2m kubelet, node1.example.com Started container
Normal Pulling 53s (x4 over 2m) kubelet, node1.example.com pulling image "busybox:latest"
Normal Pulled 47s (x4 over 2m) kubelet, node1.example.com Successfully pulled image "busybox:latest"
Normal Created 47s (x4 over 2m) kubelet, node1.example.com Created container
Normal Started 46s (x4 over 2m) kubelet, node1.example.com Started container
Warning BackOff 11s (x6 over 1m) kubelet, node1.example.com Back-off restarting failed container
[[email protected] manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 0/1 Completed 0 1d
myapp-848b5b879b-7h254 1/1 Running 1 1d
myapp-848b5b879b-d7rjs 1/1 Running 1 1d
myapp-848b5b879b-wv5cz 1/1 Running 1 1d
nginx-deploy-5b595999-tj8ms 1/1 Running 1 1d
pod-demo 1/2 CrashLoopBackOff 5 6m
3.除了之前刪pod的方法,也可以通過kubectl delete -f pod-demo.yaml 指定yaml檔案的方式將起所關聯的pod全部刪除。
[[email protected] manifests]# kubectl delete -f pod-demo.yaml
pod "pod-demo" deleted
[[email protected] manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 0/1 Completed 0 1d
myapp-848b5b879b-7h254 1/1 Running 1 1d
myapp-848b5b879b-d7rjs 1/1 Running 1 1d
myapp-848b5b879b-wv5cz 1/1 Running 1 1d
nginx-deploy-5b595999-tj8ms 1/1 Running 1 1d