了解模板和帮助信息方法

[root@master ~]# kubectl create Tab键     #查找需要使用的相关子命令
[root@master ~]# bubectl 子命令  --help
[root@master ~]# kubectl create 复制查找出来的信息 --dry-run=client -o yaml  # 获取资源对象模板
[root@master ~]# kubectl explain Pod.metadata  # 查询帮助信息

1.配置名称空间

[root@master ~]# vim myweb.yaml
---
kind: Pod
apiVersion: v1
metadata:
  name: myweb
  namespace: default
spec:
  containers:
  - name: nginx
    image: myos:nginx
status: {}

2.多容器Pod

[root@master ~]# vim mynginx.yaml
---
kind: Pod
apiVersion: v1
metadata:
  name: mynginx
  namespace: default
spec:
  containers:
  - name: nginx
    image: myos:nginx
  - name: php
    image: myos:php-fpm
###注意一个Pod容器的共享网络IP,创建的容器的服务不能用相同端口,否测会
###注意多容器的Pod,在用logs,exec,cp等要采用-c去指定哪个容器
# 查看日志
[root@master ~]# kubectl logs mynginx -c nginx
# kubectl exec -it mynginx -c nginx -- pstree -p

3.容器保护策略

[root@master ~]# vim mycmd.yaml
[root@master ~]# vim mycmd.yaml
---
kind: Pod
apiVersion: v1
metadata:
  name: mycmd
spec:
  restartPolicy: OnFailure      # 配置重启策略,表示失败后重启,此外还有Always/Never
  containers:
  - name: linux
    image: myos:httpd
    command: ["sleep"]          # 执行shell命令行命令
    args:
    - "30"

4.宽限期策略

[root@master ~]# vim mycmd.yaml
---
kind: Pod
apiVersion: v1
metadata:
  name: mycmd
spec:
  terminationGracePeriodSeconds: 0  # 设置宽限期,0表示没有宽限期
  restartPolicy: OnFailure
  containers:
  - name: linux
    image: myos:httpd
    command: ["sleep"]
    args:
    - "30"

5.Pod任务脚本

[root@master ~]# vim mycmd.yaml
---
kind: Pod
apiVersion: v1
metadata:
  name: mycmd
spec:
  terminationGracePeriodSeconds: 0
  restartPolicy: OnFailure
  containers:
  - name: linux
    image: myos:8.5
    command: ["sh"]
    args:
    - -c
    - |
      for i in {0..9};do
          echo hello world.
          sleep 3.3
      done
      exit 0

6.最大生命周期

[root@master ~]# vim mycmd.yaml
---
kind: Pod
apiVersion: v1
metadata:
  name: mycmd
spec:
  terminationGracePeriodSeconds: 0
  activeDeadlineSeconds: 60      # 可以执行的最大时长
  restartPolicy: OnFailure
  containers:
  - name: linux
    image: myos:8.5
    command: ["sh"]
    args:
    - -c
    - |
      for i in {0..9};do
          echo hello world.
          sleep 33
      done
      exit 0

7.Pod调度策略

[root@master ~]# vim myhttp.yaml
---
kind: Pod
apiVersion: v1
metadata:
  name: myhttp
spec:
  nodeName: node-0001     # 基于节点名称进行调度
  containers:
  - name: apache
    image: myos:httpd

8.标签管理

# 查看标签
[root@master ~]# kubectl get pods --show-labels

 添加标签
[root@master ~]# kubectl label pod myhttp app=apache
pod/myhttp labeled
[root@master ~]# kubectl get pods --show-labels 
NAME      READY   STATUS    RESTARTS   AGE   LABELS
myhttp    1/1     Running   0          14m   app=apache

# 删除标签
[root@master ~]# kubectl label pod myhttp app-
pod/myhttp unlabeled
[root@master ~]# kubectl get pods --show-labels 
NAME      READY   STATUS    RESTARTS   AGE   LABELS
myhttp    1/1     Running   0          14m   <none>

# 资源清单文件配置标签
[root@master ~]# vim myhttp.yaml 
---
kind: Pod
apiVersion: v1
metadata:
  name: myhttp
  labels:
    app: apache
spec:
  containers:
  - name: apache
    image: myos:httpd
    
# 使用标签过滤资源对象
[root@master ~]# kubectl get pods -l app=apache

9.基于标签调度(k8s做应用分区:给不同node打不同标签,再通过用资源清单文件nodeSelector筛选,创建pod)

# 查询 node 节点上的标签
[root@master ~]# kubectl get nodes --show-labels
NAME        STATUS   ROLES            VERSION   LABELS
master      Ready    control-plane    v1.29.2   kubernetes.io/hostname=master
node-0001   Ready    <none>           v1.29.2   kubernetes.io/hostname=node-0001
node-0002   Ready    <none>           v1.29.2   kubernetes.io/hostname=node-0002
node-0003   Ready    <none>           v1.29.2   kubernetes.io/hostname=node-0003
node-0004   Ready    <none>           v1.29.2   kubernetes.io/hostname=node-0004
node-0005   Ready    <none>           v1.29.2   kubernetes.io/hostname=node-0005

# 使用 node 上的标签调度 Pod
[root@master ~]# vim myhttp.yaml 
---
kind: Pod
apiVersion: v1
metadata:
  name: myhttp
  labels:
    app: apache
spec:
  nodeSelector:
    kubernetes.io/hostname: node-0002
  containers:
  - name: apache
    image: myos:httpd

[root@master ~]# kubectl replace --force -f myhttp.yaml
pod "myhttp" deleted
pod/myhttp replaced
[root@master ~]# kubectl get pods -o wide
NAME      READY   STATUS    RESTARTS   AGE   IP            NODE
myhttp    1/1     Running   0          1s    10.244.2.11   node-0002