模板与帮助信息

● 如何生成资源对象模板? - 资源对象Pod模板使用run生成

[root@master ~]# kubectl run myweb --image=myos:nginx --dry-run=client -o yaml

- 除pod外,自他资源对象模板使用create生成

- 生成模板命令: [--dry-run=client -o yaml]

[root@master ~]# kubectl create namespace work --dry-run=client -o yaml

● 如何获取帮助信息?

- 使用 “.” 分割层级结构关系

- 是以explain资源对象 + 层级关系

[root@master ~]# kubectl explain Pod.spec.restartPolicy

● 如何使用资源对象文件管理

注意:使用资源对象文件的时候需要有先后顺序

[root@master app]# ls
nginx.yaml  phpfpm.yaml
# 使用资源对象文件创建应用
[root@master app]# kubectl apply -f nginx.yaml -f phpfpm.yaml
pod/nginx created
pod/php created
# 删除应用
[root@master app]# kubectl delete -f /root/app/
pod "nginx" deleted
pod "php" deleted

● 合并资源对象文件

- 多个资源文件可以使用 --- 作为分隔符,合并管理

# 合并资源对象文件
[root@master app]# cat nginx.yaml >>app.yaml
[root@master app]# cat phpfpm.yaml >>app.yaml
# 创建资源对象
[root@master ~]# kubectl apply -f app.yaml
pod/nginx created
pod/php created

自定义命令

● Pod自定义命令

- 创建Pod时,可以为其设置启动时要执行的自定义命令。如果配置了自定义命令,那么镜像中自带的默认启动命令将不再执行

- 自定义命令设置在command字段下,如果有命令参数,就填写在args字段下

[root@master ~]# cat mycmd.yaml 
---
kind: Pod
apiVersion: v1
metadata:
    name: mycmd
spec:
   containers:
    - name: linux
      image: myos:8.5
      command: ["sleep"]
      args: ["30"]

● restartPolicy策略

- Pod会根据策略决定容器结束后是否重启

- 重启[Always],不重启[Never],失败就重启[OnFailure] 在k8s中默认执行重启策略

---
kind: Pod
apiVersion: v1
metadata:
    name: mycmd
spec:
   restartPolicy: Never
   containers:
    - name: linux
      image: myos:8.5
      command: ["sleep"]
      args: ["30"]

注:查看策略的执行情况可以看pod的执行状态(status)

[root@master ~]# kubectl get pods -w
NAME    READY   STATUS      RESTARTS     AGE
mycmd   1/1     Running     0            4s
mycmd   0/1     Completed   0            31s
mycmd   1/1     Running     1 (2s ago)   32s

● terminationGracePeriodSeconds 策略

- 宽限期是为了避免服务突然中断,造成的事务不一致的问题

注:这个策略也可以利用explain查找(宽限期默认是30s)

[root@master ~]# kubectl explain pod.spec
terminationGracePeriodSeconds        <integer>
     Optional duration in seconds the pod needs to terminate gracefully. May be
     decreased in delete request. Value must be non-negative integer. The value
     zero indicates stop immediately via the kill signal (no opportunity to shut
     down). If this value is nil, the default grace period will be used instead.
     The grace period is the duration in seconds after the processes running in
     the pod are sent a termination signal and the time when the processes are
     forcibly halted with a kill signal. Set this value longer than the expected
     cleanup time for your process. Defaults to 30 seconds.

● 循环死锁

- 如果一个Pod的内部程序在运行时出现循环死缩,那么就会永远不停的重复zhxiing

● activeDeadlineSeconds策略

- 允许Pod运行的最大时长

- 时间到期后会向Pod发送signal,如果Pod无法结束就把他强制关闭,并且设置为Error状态

[root@master ~]# cat mycmd.yaml 
---
kind: Pod
apiVersion: v1
metadata:
    name: mycmd
spec:
   terminationGracePeriodSeconds: 0
   activeDeadlineSeconds: 60
   restartPolicy: Never
   containers:
    - name: linux
      image: myos:8.5
      command: ["sleep"]
      args: ["300"]
[root@master ~]# kubectl get pods -w
NAME    READY   STATUS    RESTARTS      AGE
mycmd   1/1     Running   0             43s
test2   1/1     Running   2 (89m ago)   2d20h
web1    1/1     Running   0             97m
mycmd   1/1     Running   0             60s
mycmd   1/1     Running   0             63s
mycmd   0/1     Error     0             64s

自定义命令进阶

--- Yaml多行表达式

--- 
string1: > #最终结果为[01234 空格 56789]
      01234
      56789
--- 
string2: | #最终结果为[01234 换行 56789]
      01234
      56789

● Pod中嵌入脚本

[root@master ~]# vim mycmd.yaml

---
kind: Pod
apiVersion: v1
metadata:
  name: mycmd
spec:
  terminationGracePeriodSeconds: 0
  restartPolicy: Always
  containers:
  - name: linux
    image: myos:8.5
    command: ["/bin/bash"]
    args:
    - -c
    - |
      while sleep 5;do
        echo "hello world."
      done
[root@master ~]# kubectl apply -f mycmd.yaml 
pod/mycmd created
[root@master ~]# kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE
mycmd   1/1     Running   0          3s
[root@master ~]# kubectl logs mycmd 
hello world.
hello world.
hello world.

多容器Pod

● 多容器Pod

- Pod就像是盒子一样,它由一个或者多个容器组成

- Pod是一个服务的多个进程的聚合单位

- 同一个Pod共享网络IP及权限

- 同一个Pod共享主机名称

- 同一个Pod共享存储设备

● 创建多容器Pod

[root@master ~]# cat mynginx.yaml 
---
kind: Pod
apiVersion: v1
metadata:
     name:  mynginx
spec: 
  containers:
    - name: nginx
      image: myos:nginx
    - name: php
      image: myos:php-fpm
status: {}
[root@master ~]# kubectl apply -f mynginx.yaml 
pod/mynginx created
[root@master ~]# kubectl get pods
NAME      READY   STATUS    RESTARTS   AGE
mynginx   2/2     Running   0          3s

● 管理多容器Pod

- 受多容器配置影响,以下命令需要使用<-c 容器的名字>

- 受影响命令: [logs,exec,cp]

[root@master ~]# echo "hello world" >hello.html
[root@master ~]# kubectl cp hello.html mynginx:/usr/local/nginx/html/ -c nginx
[root@master ~]# kubectl exec mynginx -c php -- ps
    PID TTY          TIME CMD
      1 ?        00:00:00 php-fpm
      7 ?        00:00:00 ps
[root@master ~]# kubectl logs mynginx -c nginx