本文收錄在容器技術學習系列文章總目錄html
(1)例如查詢如何定義pod資源node
[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> ... ... kind <string> ... ... metadata <Object> ... ... spec <Object> ... ... status <Object> ... ...
(2)能一級一級進入查詢;如查詢定義pod 的metadata字段linux
[root@master ~]# kubectl explain pod.spec KIND: Pod VERSION: v1 RESOURCE: spec <Object> DESCRIPTION: ... ... FIELDS: ... .. affinity <Object> ... ... [root@master ~]# kubectl explain pod.spec.containers KIND: Pod VERSION: v1 RESOURCE: containers <[]Object> DESCRIPTION: ... ... FIELDS: args <[]string> ... ... command <[]string> ... ...
本身定義資源時,不清楚如何定義,能夠進行快速的查詢nginx
(1)查詢集羣中的pod(上篇建立的pod)vim
[root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE client 1/1 Running 0 4h myapp-848b5b879b-9slqg 1/1 Running 0 46m myapp-848b5b879b-wtrjr 1/1 Running 0 46m myapp-848b5b879b-z2sqc 1/1 Running 0 46m
(2)-o yaml輸出爲yaml格式,查看pod建立的操做api
[root@master ~]# kubectl get pod myapp-848b5b879b-9slqg -o yaml apiVersion: v1 #api版本 kind: Pod #資源類別 metadata: #元數據 annotations: cni.projectcalico.org/podIP: 10.244.1.60/32 labels: pod-template-hash: "4046164356" run: myapp name: myapp-848b5b879b-9slqg namespace: default ... ... selfLink: /api/v1/namespaces/default/pods/myapp-848b5b879b-9slqg spec: #規格、規範;指望資源應該用於什麼特性;指望目標狀態 ... ... status: #當前狀態 ... ...
[root@master ~]# mkdir manifests [root@master ~]# cd manifests/
(1)編寫pod-demo.yaml文件tomcat
建立2個容器,一個運行nginx;一個在busybox中執行sleep命令bash
[root@master manifests]# vim pod-demo.yaml apiVersion: v1 kind: Pod metadata: name: pod-demo namespace: default #labels: {app:myapp, tier:frontend} #映射能夠寫爲{}形式; labels: #也能夠在下邊分級寫 app: myapp tier: frontend spec: containers: - name: myapp image: ikubernetes/myapp:v1 - name: busybox image: busybox:latest #command: ["/bin/sh","-c","sleep 3600"] #列表能夠寫爲[]形式; command: #也能夠在下邊分級寫,要加- - "/bin/sh" - "-c" - "sleep 3600"
(2)基於pod-demo.yaml 文件建立create pod服務器
[root@master manifests]# kubectl create -f pod-demo.yaml pod/pod-demo created
(3)驗證app
① 查詢建立pod的信息
[root@master manifests]# kubectl create -f pod-demo.yaml pod/pod-demo created [root@master manifests]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE pod-demo 2/2 Running 0 1m 10.244.1.61 node1 ---查看詳細信息 [root@master manifests]# kubectl describe pods pod-demo Name: pod-demo Namespace: default ... ...
② 訪問pod中的服務
[root@master manifests]# curl 10.244.1.61 Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> ---查詢pod產生的日誌 [root@master manifests]# kubectl logs pod-demo myapp 192.168.130.104 - - [23/Jan/2019:05:35:35 +0000] "GET / HTTP/1.1" 200 65 "-" "curl/7.29.0" "-"
③ 基於yaml文件刪除pod
[root@master manifests]# kubectl delete -f pod-demo.yaml pod "pod-demo" deleted [root@master manifests]# kubectl get pods No resources found.
(1)修改pod-demo.yaml文件
[root@master manifests]# vim pod-demo.yaml apiVersion: v1 kind: Pod metadata: name: pod-demo namespace: default #labels: {app:myapp, tier:frontend} #映射能夠寫爲{}形式; labels: #也能夠在下邊分級寫 app: myapp tier: frontend annotations: along.com/created-by: "cluster admin" spec: containers: - name: myapp image: ikubernetes/myapp:v1 ports: - name: http containerPort: 80 - name: https containerPort: 443 - name: busybox image: busybox:latest imagePullPolicy: IfNotPresent #command: ["/bin/sh","-c","sleep 3600"] #列表能夠寫爲[]形式; command: #也能夠在下邊分級寫,要加- - "/bin/sh" - "-c" - "sleep 3600" nodeSelector: disktype: ssd
(2)將node1節點打上disktype=ssd的標籤
[root@master manifests]# kubectl label node node1 disktype=ssd [root@master manifests]# kubectl get nodes node1 --show-labels NAME STATUS ROLES AGE VERSION LABELS node1 Ready <none> 140d v1.11.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/hostname=node1
(3)基於yaml文件建立pod
[root@master manifests]# kubectl create -f pod-demo.yaml pod/pod-demo created
(4)驗證
--- pod只會建立到node1節點上,由於node1的disktype=ssd標籤 [root@master manifests]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE pod-demo 2/2 Running 0 11s 10.244.1.68 node1 --- -l 指定標籤,實現標籤過濾 [root@master manifests]# kubectl get pods --show-labels -l app NAME READY STATUS RESTARTS AGE LABELS pod-demo 2/2 Running 0 30s app=myapp,tier=frontend
(1)在spec字段下、containers字段配置,可以使用explain查看詳細用法
$ kubectl explain pod.spec.containers.
(2)pod中容器掛了,是否重啓pod
$ kubectl explain pod.spec.restartPolicy.
(1)編寫yaml文件
當探測到/tmp/healthy文件不存在時,認爲服務故障;
容器在30秒後執行刪除/tmp/healthy文件
[root@master manifests]# vim liveness-exec.yaml 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 /tmp/healthy; sleep 30; rm -f /tmp/healthy; sleep 3600"] livenessProbe: exec: command: ["test","-e","/tmp/healthy"] initialDelaySeconds: 1 #在容器啓動後1秒開始檢測 periodSeconds: 3 #每隔3秒探測一次 restartPolicy: Always #老是重啓pod
(2)建立運行pod
[root@master manifests]# kubectl create -f liveness-exec.yaml pod/liveness-exec-pod created [root@master manifests]# kubectl get pods NAME READY STATUS RESTARTS AGE liveness-exec-pod 1/1 Running 0 6s
(3)等30s,容器就會檢測失敗,重啓pod;使用describe能夠查看詳細信息
[root@master manifests]# kubectl describe pods liveness-exec-pod ... ... State: Running Started: Wed, 23 Jan 2019 16:58:09 +0800 Last State: Terminated #上次狀態爲終止 Reason: Error Exit Code: 137 Started: Wed, 23 Jan 2019 16:57:01 +0800 Finished: Wed, 23 Jan 2019 16:58:09 +0800 Ready: True Restart Count: 1 #重啓次數1次 Liveness: exec [test -e /tmp/healthy] delay=1s timeout=1s period=3s #success=1 #failure=3 ... ...
(1)編寫yaml文件,建立並運行pod
當探測不到容器內80端口,和提供80端口的/index.html文件時,認爲服務故障;
[root@master manifests]# vim liveness-httpget.yaml apiVersion: v1 kind: Pod metadata: name: liveness-httpget-pod namespace: default spec: containers: - name: liveness-exec-container image: ikubernetes/myapp:v1 ports: - name: http containerPort: 80 livenessProbe: httpget: port: http path: /index.html initialDelaySeconds: 1 periodSeconds: 3 restartPolicy: Always [root@master manifests]# kubectl create -f liveness-httpget.yaml pod/liveness-httpget-pod created
(2)手動連入容器,刪除index.html文件
[root@master manifests]# kubectl exec -it liveness-httpget-pod -- /bin/sh / # rm -f /usr/share/nginx/html/index.html
(3)容器會檢測失敗,重啓pod;使用describe能夠查看詳細信息
[root@master manifests]# kubectl describe pods liveness-httpget-pod ... ... Port: 80/TCP Host Port: 0/TCP State: Running Started: Wed, 23 Jan 2019 17:10:03 +0800 Last State: Terminated #上次狀態爲終止 Reason: Completed Exit Code: 0 Started: Wed, 23 Jan 2019 17:08:22 +0800 Finished: Wed, 23 Jan 2019 17:10:03 +0800 Ready: True Restart Count: 1 #重啓次數1次 Liveness: http-get http://:http/index.html delay=1s timeout=1s period=3s #success=1 #failure=3 ... ...
(1)編寫yaml文件,建立啓動容器
當探測到/tmp/healthy文件不存在時,就認爲服務就緒不成功;pod啓動失敗;
[root@master manifests]# vim readiness-exec.yaml apiVersion: v1 kind: Pod metadata: name: readiness-exec-pod namespace: default spec: containers: - name: readiness-exec-container image: busybox:latest imagePullPolicy: IfNotPresent #command: ["/bin/sh","-c","touch /tmp/healthy; sleep 30; rm -f /tmp/healthy; sleep 3600"] command: ["sleep 3600"] readinessProbe: exec: command: ["test","-e","/tmp/healthy"] periodSeconds: 3 restartPolicy: Always [root@master manifests]# kubectl create -f readiness-exec.yaml pod/readiness-exec-pod created
(2)查看,pod啓動就緒失敗
[root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE readiness-exec-pod 0/1 RunContainerError 1 12s
$ kubectl explain pod.spec.containers.lifecycle
(1)編寫yaml文件,建立啓動容器
啓動容器前,先建立準備一個httpd服務的主頁面文件/tmp/index.html
[root@master manifests]# vim poststart-pod.yaml apiVersion: v1 kind: Pod metadata: name: poststart-pod namespace: default spec: containers: - name: poststart-container image: busybox:latest imagePullPolicy: IfNotPresent lifecycle: postStart: exec: command: ['/bin/sh','-c','echo hello > /tmp/index.html'] command: ['/bin/sh','-c','/bin/httpd -f -h /tmp'] [root@master manifests]# kubectl create -f poststart-pod.yaml pod/poststart-pod created
(2)驗證,訪問服務
[root@master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE poststart-pod 1/1 Running 0 26s 10.244.2.69 node2 [root@master ~]# curl 10.244.2.69 hello