标签:下载 box group probe org value mina app project
本文收录在容器技术学习系列文章总目录
(1)例如查询如何定义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> ... ... kind <string> ... ... metadata <Object> ... ... spec <Object> ... ... status <Object> ... ...
(2)能一级一级进入查询;如查询定义pod 的metadata字段
[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> ... ...
自己定义资源时,不清楚如何定义,可以进行快速的查询
(1)查询集群中的pod(上篇创建的pod)
[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创建的操作
[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文件
创建2个容器,一个运行nginx;一个在busybox中执行sleep命令
[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)验证
① 查询创建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
kubernetes系列06—kubernetes资源清单定义入门
标签:下载 box group probe org value mina app project
原文地址:https://www.cnblogs.com/along21/p/10313341.html