IT评测·应用市场-qidao123.com

标题: 云计算第四阶段-----CLOUND二周目 04-06 [打印本页]

作者: 东湖之滨    时间: 2024-10-20 10:50
标题: 云计算第四阶段-----CLOUND二周目 04-06
cloud 04

本日目标:

一、Pod 生命周期

图解:


  
  1. [root@master ~]# vim web1.yaml
  2. ---
  3. kind: Pod
  4. apiVersion: v1
  5. metadata:
  6.   name: web1
  7. spec:
  8.   initContainers:                 # 定义初始化任务
  9.   - name: task1                   # 如果初始化任务失败,主容器不会启动
  10.     image: myos:latest            # 初始化可以使用不同的镜像
  11.     command: ["sh"]               # 任务,一般用脚本实现
  12.     args:                         # 任务
  13.     - -c                          # 任务
  14.     - |                           # 任务
  15.       sleep 5                     # 任务
  16.       echo "ok"                   # 任务
  17.       exit $((RANDOM%2))          # 状态 0 成功,其他失败,如果失败会重新执行初始化
  18.   containers:
  19.   - name: web
  20.     image: myos:httpd
  21. [root@master ~]# kubectl replace --force -f web1.yaml
  22. pod "web1" deleted
  23. pod/web1 replaced
  24. # 如果初始化任务失败就重复执行,直到成功为止
  25. [root@master ~]# kubectl get pods -w
  26. NAME    READY   STATUS            RESTARTS     AGE
  27. web1    0/1     Init:0/1          0            1s
  28. web1    0/1     Init:Error        0            6s
  29. web1    0/1     Init:0/1          1 (1s ago)   7s
  30. web1    0/1     PodInitializing   0            12s
  31. web1    1/1     Running           0            13s
复制代码
# pod创建乐成后,可以用  kubectl get pods -w  立即查看该资源清单的生命周期,其中
  status 是每个阶段组件的状态;restarts 是该资源清单pod的重启次数。ready1/1表现天生乐成。
    多任务初始化
  1. [root@master ~]# vim web1.yaml
  2. ---
  3. kind: Pod
  4. apiVersion: v1
  5. metadata:
  6.   name: web1
  7. spec:
  8.   restartPolicy: Never            # 任务失败不重启
  9.   initContainers:
  10.   - name: task1
  11.     image: myos:latest
  12.     command: ["sh"]
  13.     args:
  14.     - -c
  15.     - |
  16.       sleep 1
  17.       echo "ok"
  18.       exit $((RANDOM%2))
  19.   - name: task2
  20.     image: myos:latest
  21.     command: ["sh"]
  22.     args:
  23.     - -c
  24.     - |
  25.       sleep 1
  26.       echo "ok"
  27.       exit $((RANDOM%2))
  28.   containers:
  29.   - name: web
  30.     image: myos:httpd
  31. [root@master ~]# kubectl replace --force -f web1.yaml
  32. pod "web1" deleted
  33. pod/web1 replaced
  34. # 初始化任务失败,main 容器不会运行
  35. [root@master ~]# kubectl get pods -w
  36. NAME   READY   STATUS       RESTARTS        AGE
  37. web1   0/1     Init:0/2          0          1s
  38. web1   0/1     Init:1/2          0          3s
  39. web1   0/1     Init:Error        0          5s
复制代码
#该资源清单文件,运行了两个任务,所以status 选项出现了0\2字样,表现两个任务的启动情况,两个完全启动则为2\2.
  启动探针

  
  1. # 用于检测容器启动过程中依赖的某个重要服务,启动成功后结束
  2. [root@master ~]# vim web2.yaml
  3. ---
  4. kind: Pod
  5. apiVersion: v1
  6. metadata:
  7.   name: web2
  8. spec:
  9.   containers:
  10.   - name: web
  11.     image: myos:httpd
  12.     startupProbe:                 # 启动探针
  13.       tcpSocket:                  # 使用 tcp 协议检测
  14.         host: 192.168.1.252       # 主机地址
  15.         port: 80                  # 端口号
  16. [root@master ~]# kubectl apply -f web2.yaml
  17. pod/web2 created
  18. [root@master ~]# kubectl get pods -w
  19. NAME   READY   STATUS      RESTARTS      AGE
  20. web2   0/1     Running     0             7s
  21. web2   0/1     Running     1 (1s ago)    31s
  22. web2   0/1     Running     1 (10s ago)   40s
  23. web2   1/1     Running     1 (11s ago)   41s
复制代码
#启动探针为   startupProbe
  类似 交通工具 的仪表盘 ,通过指针与刻度,我们就知道当前容器(交通工具)的各个组件的启动情况和优劣情况。
  

  

  

  #与平凡容器的区别,必须要运行到完成状态才会停止且按 顺序执行。
  

  
  

  

  就绪探针

  
  1. # 附加条件检测,在 Pod 的全部生命周期中(禁止调用,不重启)
  2. [root@master ~]# vim web3.yaml
  3. ---
  4. kind: Pod
  5. apiVersion: v1
  6. metadata:
  7.   name: web3
  8. spec:
  9.   containers:
  10.   - name: web
  11.     image: myos:httpd
  12.     readinessProbe:               # 定义就绪探针
  13.       periodSeconds: 10           # 检测间隔
  14.       exec:                       # 执行命令进行检测
  15.         command:                  # 检测命令
  16.         - sh
  17.         - -c
  18.         - |
  19.           read ver </var/www/html/version.txt
  20.           if (( ${ver:-0} > 2 ));then
  21.              res=0
  22.           fi
  23.           exit ${res:-1}          # 版本大于 2 成功,否则失败
  24. [root@master ~]# kubectl apply -f web3.yaml
  25. pod/web3 created
  26. [root@master ~]# kubectl get pods -w
  27. NAME   READY   STATUS    RESTARTS   AGE
  28. web3   0/1     Running   0          5s
  29. web3   1/1     Running   0          10s
  30. web3   0/1     Running   0          40s
  31. # 在其他终端执行测试
  32. [root@master ~]# echo 3 >version.txt
  33. [root@master ~]# kubectl cp version.txt web3:/var/www/html/
  34. [root@master ~]# echo 1 >version.txt
  35. [root@master ~]# kubectl cp version.txt web3:/var/www/html/
复制代码

   存活探针

  
  1. # 判断某个核心资源是否可用,在 Pod 的全部生命周期中(重启)
  2. [root@master ~]# vim web4.yaml
  3. ---
  4. kind: Pod
  5. apiVersion: v1
  6. metadata:
  7.   name: web4
  8. spec:
  9.   containers:
  10.   - name: web
  11.     image: myos:httpd
  12.     livenessProbe:                # 定义存活探针
  13.       timeoutSeconds: 3           # 服务影响超时
  14.       httpGet:                    # 使用 HTTP 协议检测
  15.         path: /info.php           # 请求的 URL 路径
  16.         port: 80                  # 服务端口号
  17. [root@master ~]# kubectl apply -f web4.yaml
  18. pod/web4 created
  19. [root@master ~]# kubectl get pods -w
  20. NAME   READY   STATUS    RESTARTS     AGE
  21. web4   1/1     Running   0            4s
  22. web4   1/1     Running   1 (0s ago)   61s
  23. # 在其他终端执行测试
  24. [root@master ~]# kubectl exec -it web4 -- rm -f index.html
复制代码

  

    事件处置惩罚函数

  1. # 在主容器启动之后或结束之前执行的附加操作
  2. [root@master ~]# vim web6.yaml
  3. ---
  4. kind: Pod
  5. apiVersion: v1
  6. metadata:
  7.   name: web6
  8. spec:
  9.   containers:
  10.   - name: web
  11.     image: myos:httpd
  12.     lifecycle:                    # 定义启动后事件处理函数
  13.       postStart:
  14.         exec:
  15.           command:
  16.           - sh
  17.           - -c
  18.           - |
  19.             echo "自动注册服务" |tee -a /tmp/web.log
  20.             sleep 10
  21.       preStop:                    # 定义关闭前事件处理函数
  22.         exec:
  23.           command:
  24.           - sh
  25.           - -c
  26.           - |
  27.             echo "清除已注册的服务" |tee -a /tmp/web.log
  28.             sleep 10
  29. [root@master ~]# kubectl apply -f web6.yaml
  30. pod/web6 created
  31. [root@master ~]# kubectl exec -it web6 -- bash
  32. [root@web6 html]# cat /tmp/web.log
  33. 自动注册服务
  34. [root@web6 html]# cat /tmp/web.log
  35. 自动注册服务
  36. 清除已注册的服务
  37. # 在其他终端执行
  38. [root@master ~]# kubectl delete pods web6
  39. pod "web6" deleted
复制代码

  #popStart 是在主容器创建之后被调用;初始化工作
  prestop是容器被停止之前被调用。清理工作
  二、Pod资源管理


#以后网上买电脑,看cpu就能理解了。

资源配额

#抽象来说,就是我有1块蛋糕,分配给你特定部分,由你自己支配。
  1. [root@master ~]# vim app.yaml
  2. ---
  3. kind: Pod
  4. apiVersion: v1
  5. metadata:
  6.   name: app
  7. spec:
  8.   containers:
  9.   - name: web
  10.     image: myos:httpd
  11.     resources:                  # 配置资源策略
  12.       requests:                 # 配额策略
  13.         cpu: 1500m              # 计算资源配额
  14.         memory: 1200Mi          # 内存资源配额
  15. [root@master ~]# kubectl apply -f app.yaml
  16. pod/app created
  17. [root@master ~]# kubectl describe pods app
  18. ......
  19.     Ready:          True
  20.     Restart Count:  0
  21.     Requests:
  22.       cpu:        1500m
  23.       memory:     1200Mi
  24. # 使用 memtest.py 测试内存
  25. [root@master ~]# kubectl cp memtest.py app:/usr/bin/
  26. [root@master ~]# kubectl exec -it app -- bash
  27. [root@app html]# memtest.py 1500
  28. use memory success
  29. press any key to exit :
  30. [root@app html]# cat /dev/zero >/dev/null
  31. # 在另一个终端
  32. [root@master ~]# kubectl top pods
  33. NAME   CPU(cores)   MEMORY(bytes)   
  34. app    3m           1554Mi
  35. [root@master ~]# kubectl top pods
  36. NAME   CPU(cores)   MEMORY(bytes)   
  37. app    993m         19Mi
  38. 验证配额策略
  39. [root@master ~]# sed "s,app,app1," app.yaml |kubectl apply -f -
  40. pod/app1 created
  41. [root@master ~]# sed "s,app,app2," app.yaml |kubectl apply -f -
  42. pod/app2 created
  43. [root@master ~]# sed "s,app,app3," app.yaml |kubectl apply -f -
  44. pod/app3 created
  45. [root@master ~]# sed "s,app,app4," app.yaml |kubectl apply -f -
  46. pod/app4 created
  47. [root@master ~]# sed "s,app,app5," app.yaml |kubectl apply -f -
  48. pod/app5 created
  49. [root@master ~]# kubectl get pods
  50. NAME   READY   STATUS    RESTARTS   AGE
  51. app    1/1     Running   0          18s
  52. app1   1/1     Running   0          16s
  53. app2   1/1     Running   0          15s
  54. app3   1/1     Running   0          14s
  55. app4   1/1     Running   0          13s
  56. app5   0/1     Pending   0          12s
  57. # 清理实验配置
  58. [root@master ~]# kubectl delete pod --all
复制代码
资源限额

  
  1. [root@master ~]# vim app.yaml
  2. ---
  3. kind: Pod
  4. apiVersion: v1
  5. metadata:
  6.   name: app
  7. spec:
  8.   containers:
  9.   - name: web
  10.     image: myos:httpd
  11.     resources:                  # 配置资源策略
  12.       limits:                   # 限额策略
  13.         cpu: 600m               # 计算资源限额
  14.         memory: 1200Mi          # 内存资源限额
  15.         
  16. [root@master ~]# kubectl apply -f app.yaml
  17. pod/app created
  18. [root@master ~]# kubectl describe pods app
  19. ......
  20.     Ready:          True
  21.     Restart Count:  0
  22.     Limits:
  23.       cpu:     600m
  24.       memory:  1200Mi
  25.     Requests:
  26.       cpu:        600m
  27.       memory:     1200Mi
  28. 验证资源限额
  29. [root@master ~]# kubectl cp memtest.py app:/usr/bin/
  30. [root@master ~]# kubectl exec -it app -- bash
  31. [root@app html]# memtest.py 1500
  32. Killed
  33. [root@app html]# memtest.py 1100
  34. use memory success
  35. press any key to exit :
  36. [root@app html]# cat /dev/zero >/dev/null
  37. # 在其他终端查看
  38. [root@master ~]# kubectl top pods
  39. NAME   CPU(cores)   MEMORY(bytes)   
  40. app    600m         19Mi  
  41. # 清理实验 Pod
  42. [root@master ~]# kubectl delete pods --all
  43. pod "app" deleted
复制代码
##限额好比我用手机给你开热点,限制你用20G你最多也只能用20G。
  Pod 服务质量

  1. BestEffort
  2. [root@master ~]# vim app.yaml
  3. ---
  4. kind: Pod
  5. apiVersion: v1
  6. metadata:
  7.   name: app
  8. spec:
  9.   containers:
  10.   - name: web
  11.     image: myos:httpd
  12. [root@master ~]# kubectl apply -f app.yaml
  13. pod/app created
  14. [root@master ~]# kubectl describe pods app |grep QoS
  15. QoS Class:                   BestEffort
  16. Burstable
  17. [root@master ~]# vim app.yaml
  18. ---
  19. kind: Pod
  20. apiVersion: v1
  21. metadata:
  22.   name: app
  23. spec:
  24.   containers:
  25.   - name: web
  26.     image: myos:httpd
  27.     resources:
  28.       requests:
  29.         cpu: 200m
  30.         memory: 300Mi
  31. [root@master ~]# kubectl replace --force -f app.yaml
  32. pod "app" deleted
  33. pod/app replaced
  34. [root@master ~]# kubectl describe pods app |grep QoS
  35. QoS Class:                   Burstable
  36. Guaranteed
  37. [root@master ~]# vim app.yaml
  38. ---
  39. kind: Pod
  40. apiVersion: v1
  41. metadata:
  42.   name: app
  43. spec:
  44.   containers:
  45.   - name: web
  46.     image: myos:httpd
  47.     resources:
  48.       requests:
  49.         cpu: 200m
  50.         memory: 300Mi
  51.       limits:
  52.         cpu: 200m
  53.         memory: 300Mi
  54. [root@master ~]# kubectl replace --force -f app.yaml
  55. pod "app" deleted
  56. pod/app replaced
  57. [root@master ~]# kubectl describe pods app |grep QoS
  58. QoS Class:                   Guaranteed
复制代码
三、全局资源管理


#好比你玩游戏,游戏背景是天下发生了核战争,地面已经无法生存。进而人类转移到了地下生存,而你作为整个地下堡垒的管理者,你不可能放任成员任意的使用资源;每一项指标都会成为你关注的目标,你必须着眼于全局,对现有的资源进行合理的分配。
ResourceQuota


  
  1. [root@master ~]# vim quota.yaml
  2. ---
  3. kind: ResourceQuota
  4. apiVersion: v1
  5. metadata:
  6.   name: myquota1
  7.   namespace: work
  8. spec:
  9.   hard:
  10.     pods: 3
  11.   scopes:
  12.   - BestEffort
  13. ---
  14. kind: ResourceQuota
  15. apiVersion: v1
  16. metadata:
  17.   name: myquota2
  18.   namespace: work
  19. spec:
  20.   hard:
  21.     pods: 10
  22.     cpu: 2300m
  23.     memory: 6Gi
  24. [root@master ~]# kubectl create namespace work
  25. namespace/work created
  26. [root@master ~]# kubectl apply -f quota.yaml
  27. resourcequota/myquota1 created
  28. resourcequota/myquota2 created
  29. # 查看配额信息
  30. [root@master ~]# kubectl describe namespace work
  31. Resource Quotas
  32.   Name:    myquota1
  33.   Scopes:  BestEffort
  34.   * Matches all pods that do not have resource requirements set ......
  35.   Resource  Used  Hard
  36.   --------  ---   ---
  37.   pods      0     3
  38.   Name:     myquota2
  39.   Resource  Used   Hard
  40.   --------  ---    ---
  41.   cpu       0m     2300m
  42.   memory    0Mi    6Gi
  43.   pods      0      10
  44. ##################  验证配额策略  ######################
  45. [root@master ~]# vim app.yaml
  46. ---
  47. kind: Pod
  48. apiVersion: v1
  49. metadata:
  50.   name: app
  51.   namespace: work
  52. spec:
  53.   containers:
  54.   - name: web
  55.     image: myos:httpd
  56.     resources:
  57.       requests:
  58.         cpu: 300m
  59.         memory: 500Mi
  60. [root@master ~]# kubectl apply -f app.yaml
  61. [root@master ~]# kubectl describe namespace work
  62. Resource Quotas
  63.   Name:    myquota1
  64.   Scopes:  BestEffort
  65.   * Matches all pods that do not have resource requirements set.
  66.   Resource  Used  Hard
  67.   --------  ---   ---
  68.   pods      0     3
  69.   Name:     myquota2
  70.   Resource  Used   Hard
  71.   --------  ---    ---
  72.   cpu       300m   2300m
  73.   memory    500Mi  6Gi
  74.   pods      1      10
  75. 清理实验配置
  76. [root@master ~]# kubectl -n work delete pods --all
  77. [root@master ~]# kubectl delete namespace work
  78. namespace "work" deleted
复制代码

  cloud 05

一、污点与容忍策略

污点先容:

管理污点标签

  
  1. # 设置污点标签
  2. [root@master ~]# kubectl taint node node-0001 k=v:NoSchedule
  3. node/node-0001 tainted
  4. # 查看污点标签
  5. [root@master ~]# kubectl describe nodes node-0001
  6. Taints:             k=v:NoSchedule
  7. # 删除污点标签
  8. [root@master ~]# kubectl taint node node-0001 k=v:NoSchedule-
  9. node/node-0001 untainted
  10. # 查看污点标签
  11. [root@master ~]# kubectl describe nodes node-0001
  12. Taints:             <none>
  13. # 查看所有节点污点标签
  14. [root@master ~]# kubectl describe nodes |grep Taints
  15. Taints:             node-role.kubernetes.io/control-plane:NoSchedule
  16. Taints:             <none>
  17. Taints:             <none>
  18. Taints:             <none>
  19. Taints:             <none>
  20. Taints:             <none>
复制代码
验证污点标签作用
  1. # node-0004 设置污点策略 PreferNoSchedule
  2. [root@master ~]# kubectl taint node node-0004 k=v:PreferNoSchedule
  3. node/node-0004 tainted
  4. # node-0005 设置污点策略 NoSchedule
  5. [root@master ~]# kubectl taint node node-0005 k=v:NoSchedule
  6. node/node-0005 tainted
  7. [root@master ~]# kubectl describe nodes |grep Taints
  8. Taints:             node-role.kubernetes.io/control-plane:NoSchedule
  9. Taints:             <none>
  10. Taints:             <none>
  11. Taints:             <none>
  12. Taints:             k=v:PreferNoSchedule
  13. Taints:             k=v:NoSchedule
复制代码
Pod 资源文件

  1. [root@master ~]# vim myphp.yaml
  2. ---
  3. kind: Pod
  4. apiVersion: v1
  5. metadata:
  6.   name: myphp
  7. spec:
  8.   containers:
  9.   - name: php
  10.     image: myos:php-fpm
  11.     resources:
  12.       requests:
  13.         cpu: 1200m
复制代码
验证污点策略

  1. # 优先使用没有污点的节点
  2. [root@master ~]# sed "s,myphp,php1," myphp.yaml |kubectl apply -f -
  3. pod/php1 created
  4. [root@master ~]# sed "s,myphp,php2," myphp.yaml |kubectl apply -f -
  5. pod/php2 created
  6. [root@master ~]# sed "s,myphp,php3," myphp.yaml |kubectl apply -f -
  7. pod/php3 created
  8. [root@master ~]# kubectl get pods -o wide
  9. NAME   READY   STATUS    RESTARTS   AGE   IP            NODE
  10. php1   1/1     Running   0          9s    10.244.1.35   node-0001
  11. php2   1/1     Running   0          8s    10.244.2.32   node-0002
  12. php3   1/1     Running   0          7s    10.244.3.34   node-0003
  13. # 最后使用 PreferNoSchedule 节点
  14. [root@master ~]# sed 's,myphp,php4,' myphp.yaml |kubectl apply -f -
  15. pod/php4 created
  16. [root@master ~]# kubectl get pods -o wide
  17. NAME   READY   STATUS    RESTARTS   AGE   IP            NODE
  18. php1   1/1     Running   0          13s   10.244.1.35   node-0001
  19. php2   1/1     Running   0          12s   10.244.2.32   node-0002
  20. php3   1/1     Running   0          11s   10.244.3.34   node-0003
  21. php4   1/1     Running   0          10s   10.244.4.33   node-0004
  22. # 不会使用 NoSchedule 节点
  23. [root@master ~]# sed 's,myphp,php5,' myphp.yaml |kubectl apply -f -
  24. pod/php5 created
  25. [root@master ~]# kubectl get pods -o wide
  26. NAME   READY   STATUS    RESTARTS   AGE   IP            NODE
  27. php1   1/1     Running   0          23s   10.244.1.35   node-0001
  28. php2   1/1     Running   0          22s   10.244.2.32   node-0002
  29. php3   1/1     Running   0          21s   10.244.3.34   node-0003
  30. php4   1/1     Running   0          20s   10.244.4.33   node-0004
  31. php5   0/1     Pending   0          15s   <none>        <none>
  32. 验证污点策略
  33. # NoSchedule 不会影响已经创建的 Pod
  34. [root@master ~]# kubectl taint node node-0003 k=v:NoSchedule
  35. node/node-0003 tainted
  36. [root@master ~]# kubectl describe nodes |grep Taints
  37. Taints:             node-role.kubernetes.io/control-plane:NoSchedule
  38. Taints:             <none>
  39. Taints:             <none>
  40. Taints:             k=v:NoSchedule
  41. Taints:             k=v:PreferNoSchedule
  42. Taints:             k=v:NoSchedule
  43. [root@master ~]# kubectl get pods -o wide
  44. NAME   READY   STATUS    RESTARTS   AGE   IP            NODE
  45. php1   1/1     Running   0          33s   10.244.1.35   node-0001
  46. php2   1/1     Running   0          32s   10.244.2.32   node-0002
  47. php3   1/1     Running   0          31s   10.244.3.34   node-0003
  48. php4   1/1     Running   0          29s   10.244.4.33   node-0004
  49. php5   0/1     Pending   0          25s   <none>        <none>
  50. # NoExecute 会删除节点上的 Pod
  51. [root@master ~]# kubectl taint node node-0001 k=v:NoExecute
  52. node/node-0001 tainted
  53. [root@master ~]# kubectl describe nodes |grep Taints
  54. Taints:             node-role.kubernetes.io/control-plane:NoSchedule
  55. Taints:             k=v:NoExecute
  56. Taints:             <none>
  57. Taints:             k=v:NoSchedule
  58. Taints:             k=v:PreferNoSchedule
  59. Taints:             k=v:NoSchedule
  60. [root@master ~]# kubectl get pods -o wide
  61. NAME   READY   STATUS    RESTARTS   AGE   IP            NODE
  62. php2   1/1     Running   0          53s   10.244.2.35   node-0002
  63. php3   1/1     Running   0          52s   10.244.3.34   node-0003
  64. php4   1/1     Running   0          51s   10.244.4.33   node-0004
  65. php5   0/1     Pending   0          45s    <none>        <none>
复制代码
清理实验设置

  1. [root@master ~]# kubectl delete pod --all
  2. [root@master ~]# kubectl taint node node-000{1..5} k-
  3. [root@master ~]# kubectl describe nodes |grep Taints
  4. Taints:             node-role.kubernetes.io/control-plane:NoSchedule
  5. Taints:             <none>
  6. Taints:             <none>
  7. Taints:             <none>
  8. Taints:             <none>
  9. Taints:             <none>
复制代码
容忍策略

  1. 设置污点标签
  2. # 设置污点标签
  3. [root@master ~]# kubectl taint node node-0001 k=v1:NoSchedule
  4. node/node-0001 tainted
  5. [root@master ~]# kubectl taint node node-0002 k=v2:NoSchedule
  6. node/node-0002 tainted
  7. [root@master ~]# kubectl taint node node-0003 k=v3:NoSchedule
  8. node/node-0003 tainted
  9. [root@master ~]# kubectl taint node node-0004 k=v4:NoSchedule
  10. node/node-0004 tainted
  11. [root@master ~]# kubectl taint node node-0005 k=v5:NoExecute
  12. node/node-0005 tainted
  13. [root@master ~]# kubectl describe nodes |grep Taints
  14. Taints:             node-role.kubernetes.io/control-plane:NoSchedule
  15. Taints:             k=v1:NoSchedule
  16. Taints:             k=v2:NoSchedule
  17. Taints:             k=v3:NoSchedule
  18. Taints:             k=v4:NoSchedule
  19. Taints:             k=v5:NoExecute
复制代码
#精确策略,好比征婚,好比有车有房高富帅。白富美,年龄结合在一起时比力精确严格的。

  #模糊策略 , 是个男的或者女的,能结婚就行,不追求细节方面的考究。就是所谓的模糊策略。  ^_^
  
  精确匹配策略

  1. # 容忍 k=v1:NoSchedule 污点
  2. [root@master ~]# vim myphp.yaml
  3. ---
  4. kind: Pod
  5. apiVersion: v1
  6. metadata:
  7.   name: myphp
  8. spec:
  9.   tolerations:
  10.   - operator: Equal      # 完全匹配键值对
  11.     key: k               # 键
  12.     value: v1            # 值
  13.     effect: NoSchedule   # 污点标签
  14.   containers:
  15.   - name: php
  16.     image: myos:php-fpm
  17.     resources:
  18.       requests:
  19.         cpu: 1200m
  20. [root@master ~]# sed "s,myphp,php1," myphp.yaml |kubectl apply -f -
  21. pod/php1 created
  22. [root@master ~]# sed "s,myphp,php2," myphp.yaml |kubectl apply -f -
  23. pod/php2 created
  24. [root@master ~]# kubectl get pods -o wide
  25. NAME   READY   STATUS    RESTARTS   AGE   IP            NODE
  26. php1   1/1     Running   0          6s    10.244.1.10   node-0001
  27. php2   1/1     Pending   0          6s    <none>        <none>
复制代码
模糊匹配策略

  1. # 容忍 k=*:NoSchedule 污点
  2. [root@master ~]# vim myphp.yaml
  3. ---
  4. kind: Pod
  5. apiVersion: v1
  6. metadata:
  7.   name: myphp
  8. spec:
  9.   tolerations:
  10.   - operator: Exists     # 部分匹配,存在即可
  11.     key: k               # 键
  12.     effect: NoSchedule   # 污点标签
  13.   containers:
  14.   - name: php
  15.     image: myos:php-fpm
  16.     resources:
  17.       requests:
  18.         cpu: 1200m
  19. [root@master ~]# kubectl delete pods php2
  20. pod "php2" deleted
  21. [root@master ~]# sed "s,myphp,php2," myphp.yaml |kubectl apply -f -
  22. pod/php2 created
  23. [root@master ~]# sed "s,myphp,php3," myphp.yaml |kubectl apply -f -
  24. pod/php3 created
  25. [root@master ~]# sed "s,myphp,php4," myphp.yaml |kubectl apply -f -
  26. pod/php4 created
  27. [root@master ~]# sed "s,myphp,php5," myphp.yaml |kubectl apply -f -
  28. pod/php5 created
  29. [root@master ~]# kubectl get pods -o wide
  30. NAME   READY   STATUS    RESTARTS   AGE   IP            NODE
  31. php1   1/1     Running   0          6s    10.244.1.12   node-0001
  32. php2   1/1     Running   0          5s    10.244.2.21   node-0002
  33. php3   1/1     Running   0          4s    10.244.3.18   node-0003
  34. php4   1/1     Running   0          3s    10.244.4.24   node-0004
  35. php5   1/1     Pending   0          2s    <none>        <none>
复制代码
  1. 所有污点标签
  2. # 容忍所有 node 上的污点
  3. [root@master ~]# vim myphp.yaml
  4. ---
  5. kind: Pod
  6. apiVersion: v1
  7. metadata:
  8.   name: myphp
  9. spec:
  10.   tolerations:
  11.   - operator: Exists     # 模糊匹配
  12.     key: k               # 键
  13.     effect: ""           # 设置空或删除,代表所有污点标签
  14.   containers:
  15.   - name: php
  16.     image: myos:php-fpm
  17.     resources:
  18.       requests:
  19.         cpu: 1200m
  20. [root@master ~]# sed "s,myphp,php5," myphp.yaml |kubectl replace --force -f -
  21. pod "php5" deleted
  22. pod/php5 replaced
  23. [root@master ~]# kubectl get pods -o wide
  24. NAME   READY   STATUS    RESTARTS   AGE   IP            NODE
  25. php1   1/1     Running   0          36s   10.244.1.15   node-0001
  26. php2   1/1     Running   0          36s   10.244.2.16   node-0002
  27. php3   1/1     Running   0          36s   10.244.3.19   node-0003
  28. php4   1/1     Running   0          36s   10.244.4.17   node-0004
  29. php5   1/1     Running   0          36s   10.244.5.18   node-0005
复制代码
  1. 清理实验设置[root@master ~]# kubectl delete pod --all
  2. [root@master ~]# kubectl taint node node-000{1..5} k-
  3. [root@master ~]# kubectl describe nodes |grep Taints
  4. Taints:             node-role.kubernetes.io/control-plane:NoSchedule
  5. Taints:             <none>
  6. Taints:             <none>
  7. Taints:             <none>
  8. Taints:             <none>
  9. Taints:             <none>
复制代码

  
二、Pod抢占与优先级

   

  #还是没搞懂? 
   玩游戏你知道有点脚色大招无法被打断,有的可以被打断。
  生活中,女士优先显得男士比力绅士。坐交通工具时,老弱病残孕收到厚待,都是优先级的表现。
  

  

   非抢占优先级

  1. # 定义优先级(队列优先)
  2. [root@master ~]# vim mypriority.yaml
  3. ---
  4. kind: PriorityClass
  5. apiVersion: scheduling.k8s.io/v1
  6. metadata:
  7.   name: high-non
  8. preemptionPolicy: Never
  9. value: 1000
  10. ---
  11. kind: PriorityClass
  12. apiVersion: scheduling.k8s.io/v1
  13. metadata:
  14.   name: low-non
  15. preemptionPolicy: Never
  16. value: 500
  17. [root@master ~]# kubectl apply -f mypriority.yaml
  18. priorityclass.scheduling.k8s.io/high-non created
  19. priorityclass.scheduling.k8s.io/low-non created
  20. [root@master ~]# kubectl get priorityclasses.scheduling.k8s.io
  21. NAME                      VALUE        GLOBAL-DEFAULT   AGE
  22. high-non                  1000         false            12s
  23. low-non                   500          false            12s
  24. system-cluster-critical   2000000000   false            45h
  25. system-node-critical      2000001000   false            45h
  26. Pod 资源文件
  27. # 无优先级的 Pod
  28. [root@master ~]# vim php1.yaml
  29. ---
  30. kind: Pod
  31. apiVersion: v1
  32. metadata:
  33.   name: php1
  34. spec:
  35.   nodeSelector:
  36.     kubernetes.io/hostname: node-0002
  37.   containers:
  38.   - name: php
  39.     image: myos:php-fpm
  40.     resources:
  41.       requests:
  42.         cpu: "1200m"
  43. # 低优先级 Pod
  44. [root@master ~]# vim php2.yaml
  45. ---
  46. kind: Pod
  47. apiVersion: v1
  48. metadata:
  49.   name: php2
  50. spec:
  51.   nodeSelector:
  52.     kubernetes.io/hostname: node-0002
  53.   priorityClassName: low-non      # 优先级名称
  54.   containers:
  55.   - name: php
  56.     image: myos:php-fpm
  57.     resources:
  58.       requests:
  59.         cpu: "1200m"
  60. # 高优先级 Pod
  61. [root@master ~]# vim php3.yaml
  62. ---
  63. kind: Pod
  64. apiVersion: v1
  65. metadata:
  66.   name: php3
  67. spec:
  68.   nodeSelector:
  69.     kubernetes.io/hostname: node-0002
  70.   priorityClassName: high-non     # 优先级名称
  71.   containers:
  72.   - name: php
  73.     image: myos:php-fpm
  74.     resources:
  75.       requests:
  76.         cpu: "1200m"
复制代码
验证非抢占优先

  1. [root@master ~]# kubectl apply -f php1.yaml
  2. pod/php1 created
  3. [root@master ~]# kubectl apply -f php2.yaml
  4. pod/php2 created
  5. [root@master ~]# kubectl apply -f php3.yaml
  6. pod/php3 created
  7. [root@master ~]# kubectl get pods
  8. NAME   READY   STATUS    RESTARTS   AGE
  9. php1   1/1     Running   0          9s
  10. php2   0/1     Pending   0          6s
  11. php3   0/1     Pending   0          4s
  12. [root@master ~]# kubectl delete pod php1
  13. pod "php1" deleted
  14. [root@master ~]# kubectl get pods
  15. NAME   READY   STATUS    RESTARTS   AGE
  16. php2   0/1     Pending   0          20s
  17. php3   1/1     Running   0          18s
  18. # 清理实验 Pod
  19. [root@master ~]# kubectl delete pod php2 php3
  20. pod "php2" deleted
  21. pod "php3" deleted
复制代码
抢占策略

  1. [root@master ~]# vim mypriority.yaml
  2. ---
  3. kind: PriorityClass
  4. apiVersion: scheduling.k8s.io/v1
  5. metadata:
  6.   name: high-non
  7. preemptionPolicy: Never
  8. value: 1000
  9. ---
  10. kind: PriorityClass
  11. apiVersion: scheduling.k8s.io/v1
  12. metadata:
  13.   name: low-non
  14. preemptionPolicy: Never
  15. value: 500
  16. ---
  17. kind: PriorityClass
  18. apiVersion: scheduling.k8s.io/v1
  19. metadata:
  20.   name: high
  21. preemptionPolicy: PreemptLowerPriority
  22. value: 1000
  23. ---
  24. kind: PriorityClass
  25. apiVersion: scheduling.k8s.io/v1
  26. metadata:
  27.   name: low
  28. preemptionPolicy: PreemptLowerPriority
  29. value: 500
  30. [root@master ~]# kubectl apply -f mypriority.yaml
  31. [root@master ~]# kubectl get priorityclasses.scheduling.k8s.io  
  32. NAME                      VALUE        GLOBAL-DEFAULT   AGE
  33. high                      1000         false            4s
  34. high-non                  1000         false            2h
  35. low                       500          false            4s
  36. low-non                   500          false            2h
  37. system-cluster-critical   2000000000   false            21d
  38. system-node-critical      2000001000   false            21d
复制代码
验证抢占优先级

  1. # 替换优先级策略
  2. [root@master ~]# sed 's,-non,,' -i php?.yaml
  3. # 默认优先级 Pod
  4. [root@master ~]# kubectl apply -f php1.yaml
  5. pod/php1 created
  6. [root@master ~]# kubectl get pods
  7. NAME   READY   STATUS    RESTARTS   AGE
  8. php1   1/1     Running   0          6s
  9. # 高优先级 Pod
  10. [root@master ~]# kubectl apply -f php3.yaml
  11. pod/php3 created
  12. [root@master ~]# kubectl get pods
  13. NAME   READY   STATUS    RESTARTS   AGE
  14. php3   1/1     Running   0          9s
  15. # 低优先级 Pod
  16. [root@master ~]# kubectl apply -f php2.yaml
  17. pod/php2 created
  18. [root@master ~]# kubectl get pods
  19. NAME   READY   STATUS    RESTARTS   AGE
  20. php2   0/1     Pending   0          3s
  21. php3   1/1     Running   0          9s
  22. # 清理实验 Pod
  23. [root@master ~]# kubectl delete pod --all
  24. [root@master ~]# kubectl delete -f mypriority.yaml
  25. priorityclass.scheduling.k8s.io "high-non" deleted
  26. priorityclass.scheduling.k8s.io "low-non" deleted
  27. priorityclass.scheduling.k8s.io "high" deleted
  28. priorityclass.scheduling.k8s.io "low" deleted
  29. Pod 安全
复制代码
三、Pod安全性

特权容器

设置主机名 和 /etc/hosts 文件
# VIP的含金量!root用户的含金量!项目负责人的含金量!  O(∩_∩)O
  
  1. [root@master ~]# vim root.yaml
  2. ---
  3. kind: Pod
  4. apiVersion: v1
  5. metadata:
  6.   name: root
  7. spec:
  8.   hostname: myhost         # 修改主机名
  9.   hostAliases:             # 修改 /etc/hosts
  10.   - ip: 192.168.1.30       # IP 地址
  11.     hostnames:             # 名称键值对
  12.     - harbor               # 主机名
  13.   containers:
  14.   - name: apache
  15.     image: myos:httpd
  16. [root@master ~]# kubectl apply -f root.yaml
  17. pod/root created
  18. [root@master ~]# kubectl exec -it root -- /bin/bash
  19. [root@myhost html]# hostname
  20. myhost
  21. [root@myhost html]# cat /etc/hosts
  22. ... ...
  23. # Entries added by HostAliases.
  24. 192.168.1.30    harbor
  25. [root@master ~]# kubectl delete pod root
  26. pod "root" deleted
复制代码
root特权容器
   
  1. [root@master ~]# vim root.yaml
  2. ---
  3. kind: Pod
  4. apiVersion: v1
  5. metadata:
  6.   name: root
  7. spec:
  8.   hostPID: true            # 特权,共享系统进程
  9.   hostNetwork: true        # 特权,共享主机网络
  10.   containers:
  11.   - name: apache
  12.     image: myos:httpd
  13.     securityContext:       # 安全上下文值
  14.       privileged: true     # root特权容器
  15. [root@master ~]# kubectl replace --force -f root.yaml
  16. [root@master ~]# kubectl get pods
  17. NAME   READY   STATUS    RESTARTS   AGE
  18. root   1/1     Running   0          26s
  19. [root@master ~]# kubectl exec -it root -- /bin/bash
  20. [root@node-0001 /]#
  21. # 系统进程特权
  22. [root@node-0001 /]# pstree -p
  23. systemd(1)-+-NetworkManager(510)-+-dhclient(548)
  24.            |                     |-{NetworkManager}(522)
  25.            |                     `-{NetworkManager}(524)
  26.            |-agetty(851)
  27.            |-chronyd(502)
  28.            |-containerd(531)-+-{containerd}(555)
  29.            ... ...
  30. # 网络特权
  31. [root@node-0001 /]# ifconfig eth0
  32. eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
  33.         inet 192.168.1.51  netmask 255.255.255.0  broadcast 192.168.1.255
  34.         ether fa:16:3e:70:c8:fa  txqueuelen 1000  (Ethernet)
  35.         ... ...
  36. # root用户特权
  37. [root@node-0001 /]# mkdir /sysroot
  38. [root@node-0001 /]# mount /dev/vda1 /sysroot
  39. [root@node-0001 /]# mount -t proc proc /sysroot/proc
  40. [root@node-0001 /]# chroot /sysroot
  41. sh-4.2# : 此处已经是 node 节点上的 root 用户了
  42. # 删除特权容器
  43. [root@master ~]# kubectl delete pod root
  44. pod "root" deleted
复制代码
Pod 安全策略

  1. # 生产环境设置严格的准入控制
  2. [root@master ~]# kubectl create namespace myprod
  3. namespace/myprod created
  4. [root@master ~]# kubectl label namespaces myprod pod-security.kubernetes.io/enforce=restricted
  5. namespace/myprod labeled
  6. # 测试环境测试警告提示
  7. [root@master ~]# kubectl create namespace mytest
  8. namespace/mytest created
  9. [root@master ~]# kubectl label namespaces mytest pod-security.kubernetes.io/warn=baseline
  10. namespace/mytest labeled
  11. # 创建特权容器
  12. [root@master ~]# kubectl -n myprod apply -f root.yaml
  13. Error from server (Failure): error when creating "root.yaml": host namespaces (hostNetwork=true, hostPID=true), privileged (container "linux" must not set securityContext.privileged=true), allowPrivilegeEscalation != false (container "linux" must set securityContext.allowPrivilegeEscalation=false), unrestricted capabilities (container "linux" must set securityContext.capabilities.drop=["ALL"]), runAsNonRoot != true (pod or container "linux" must set securityContext.runAsNonRoot=true), seccompProfile (pod or container "linux" must set securityContext.seccompProfile.type to "RuntimeDefault" or "Localhost")
  14. [root@master ~]#
  15. [root@master ~]# kubectl -n myprod get pods
  16. No resources found in myprod namespace.
  17. [root@master ~]# kubectl -n mytest apply -f root.yaml                                    
  18. Warning: would violate "latest" version of "baseline" PodSecurity profile: host namespaces (hostNetwork=true, hostPID=true), privileged (container "linux" must not set securityContext.privileged=true)
  19. pod/root created
  20. [root@master ~]#
  21. [root@master ~]# kubectl -n mytest get pods               
  22. NAME   READY   STATUS    RESTARTS   AGE
  23. root   1/1     Running   0          7s
  24. [root@master ~]#
  25. 安全的 Pod
  26. [root@master ~]# vim nonroot.yaml
  27. ---
  28. kind: Pod
  29. apiVersion: v1
  30. metadata:
  31.   name: nonroot
  32. spec:
  33.   containers:
  34.   - name: php
  35.     image: myos:php-fpm
  36.     securityContext:                      # 声明安全策略
  37.       allowPrivilegeEscalation: false     # 容器内没有权限提升的行为
  38.       runAsNonRoot: true                  # 容器运行在非 root 用户下
  39.       runAsUser: 65534                    # 运行容器用户的 UID
  40.       seccompProfile:                     # 容器使用了默认的安全配置
  41.         type: "RuntimeDefault"
  42.       capabilities:                       # 容器禁用了所有特权能力
  43.         drop: ["ALL"]
  44. [root@master ~]# kubectl -n myprod apply -f nonroot.yaml
  45. pod/nonroot created
  46. [root@master ~]# kubectl -n myprod get pods
  47. NAME      READY   STATUS    RESTARTS   AGE
  48. nonroot   1/1     Running   0          6s
  49. [root@master ~]# kubectl -n myprod exec -it nonroot -- id
  50. uid=65534(nobody) gid=65534(nobody) groups=65534(nobody)
复制代码
#清理实验设置,删除 Pod
  

  

   

  课后总结:
   

  
cloud 06


一、持久卷管理



   #docker是海盗船,k8s是船长,而卷则是 船上的金银珠宝,都被存放到了一个装备中,这个装备就是卷。
  

  

   
  1. Pod 资源文件
  2. [root@master ~]# vim web1.yaml
  3. ---
  4. kind: Pod
  5. apiVersion: v1
  6. metadata:
  7.   name: web1
  8. spec:
  9.   containers:
  10.   - name: nginx
  11.     image: myos:nginx
复制代码
持久卷

  hostPath 卷

  

  1. [root@master ~]# vim web1.yaml
  2. ---
  3. kind: Pod
  4. apiVersion: v1
  5. metadata:
  6.   name: web1
  7. spec:
  8.   volumes:                     # 卷定义
  9.   - name: logdata              # 卷名称
  10.     hostPath:                  # 资源类型
  11.       path: /var/weblog        # 宿主机路径
  12.       type: DirectoryOrCreate  # 目录不存在就创建
  13.   containers:
  14.   - name: nginx
  15.     image: myos:nginx
  16.     volumeMounts:                       # mount 卷
  17.     - name: logdata                     # 卷名称
  18.       mountPath: /usr/local/nginx/logs  # 容器内路径
  19. 验证 hostPath 卷
  20. [root@master ~]# kubectl apply -f web1.yaml
  21. pod/web1 created
  22. [root@master ~]# kubectl get pods -o wide
  23. NAME   READY   STATUS    RESTARTS   AGE     IP             NODE
  24. web1   1/1     Running   0          45m   10.244.2.16    node-0002
  25. [root@master ~]# curl http://10.244.2.16/
  26. Nginx is running !
  27. # 删除Pod ,日志数据也不会丢失
  28. [root@master ~]# kubectl delete pod web1
  29. pod "web1" deleted
  30. # 来到 node 上查看日志
  31. [root@node-0002 ~]# cat /var/weblog/access.log
  32. 10.244.0.0 - - [27/Jun/2022:02:00:12 +0000] "GET / HTTP/1.1" 200 19 "-" "curl/7.29.0"
复制代码
NFS 卷

  

  名称IP地址设置nfs192.168.1.101CPU,1G内存   
  1. 配置 NFS 服务
  2. # 创建共享目录,并部署测试页面
  3. [root@nfs ~]# mkdir -p /var/webroot
  4. [root@nfs ~]# echo "nfs server" >/var/webroot/index.html
  5. # 部署 NFS 服务
  6. [root@nfs ~]# dnf install -y nfs-utils
  7. [root@nfs ~]# vim /etc/exports
  8. /var/webroot    192.168.1.0/24(rw,no_root_squash)
  9. [root@nfs ~]# systemctl enable --now nfs-server.service
  10. #----------------------------------------------------------#
  11. # 所有 node 节点都要安装 nfs 软件包
  12. [root@node ~]# dnf install -y nfs-utils
  13. Pod调用NFS卷
  14. [root@master ~]# vim web1.yaml
  15. ---
  16. kind: Pod
  17. apiVersion: v1
  18. metadata:
  19.   name: web1
  20. spec:
  21.   volumes:
  22.   - name: logdata
  23.     hostPath:
  24.       path: /var/weblog
  25.       type: DirectoryOrCreate
  26.   - name: website              # 卷名称
  27.     nfs:                       # NFS 资源类型
  28.       server: 192.168.1.10     # NFS 服务器地址
  29.       path: /var/webroot       # NFS 共享目录
  30.   containers:
  31.   - name: nginx
  32.     image: myos:nginx
  33.     volumeMounts:
  34.     - name: logdata
  35.       mountPath: /usr/local/nginx/logs
  36.     - name: website                     # 卷名称
  37.       mountPath: /usr/local/nginx/html  # 路径
  38. [root@master ~]# kubectl apply -f web1.yaml
  39. pod/web1 created
  40. [root@master ~]# kubectl get pods -o wide
  41. NAME   READY   STATUS    RESTARTS   AGE   IP            NODE
  42. web1   1/1     Running   0          12m   10.244.1.19    node-0001
  43. 访问验证 nfs 卷
  44. [root@master ~]# curl http://10.244.1.19
  45. nfs server
复制代码
PV/PVC

  

  


  持久卷

  1. [root@master ~]# vim pv.yaml
  2. ---
  3. kind: PersistentVolume
  4. apiVersion: v1
  5. metadata:
  6.   name: pv-local
  7. spec:
  8.   volumeMode: Filesystem
  9.   accessModes:
  10.     - ReadWriteOnce
  11.   capacity:
  12.     storage: 30Gi
  13.   persistentVolumeReclaimPolicy: Retain
  14.   hostPath:
  15.     path: /var/weblog
  16.     type: DirectoryOrCreate
  17. ---
  18. kind: PersistentVolume
  19. apiVersion: v1
  20. metadata:                       
  21.   name: pv-nfs
  22. spec:
  23.   volumeMode: Filesystem
  24.   accessModes:
  25.     - ReadWriteOnce
  26.     - ReadOnlyMany
  27.     - ReadWriteMany
  28.   capacity:
  29.     storage: 20Gi
  30.   persistentVolumeReclaimPolicy: Retain
  31.   mountOptions:
  32.     - nolock
  33.   nfs:
  34.     server: 192.168.1.10
  35.     path: /var/webroot
  36. [root@master ~]# kubectl apply -f pv.yaml
  37. persistentvolume/pv-local created
  38. persistentvolume/pv-nfs created
  39. [root@master ~]# kubectl get persistentvolume
  40. NAME       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS       AGE
  41. pv-local   30Gi       RWO            Retain           Available    2s
  42. pv-nfs     20Gi       RWO,ROX,RWX    Retain           Available    2s
复制代码
持久卷声明

  1. [root@master ~]# vim pvc.yaml
  2. ---
  3. kind: PersistentVolumeClaim
  4. apiVersion: v1
  5. metadata:
  6.   name: pvc1
  7. spec:
  8.   volumeMode: Filesystem
  9.   accessModes:
  10.     - ReadWriteOnce
  11.   resources:
  12.     requests:
  13.       storage: 25Gi
  14. ---
  15. kind: PersistentVolumeClaim
  16. apiVersion: v1
  17. metadata:
  18.   name: pvc2
  19. spec:
  20.   volumeMode: Filesystem
  21.   accessModes:
  22.     - ReadWriteMany
  23.   resources:
  24.     requests:
  25.       storage: 15Gi
  26. [root@master ~]# kubectl apply -f pvc.yaml
  27. persistentvolumeclaim/pvc1 created
  28. persistentvolumeclaim/pvc2 created
  29. [root@master ~]# kubectl get persistentvolumeclaims
  30. NAME   STATUS   VOLUME     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
  31. pvc1   Bound    pv-local   30Gi       RWO                           8s
  32. pvc2   Bound    pv-nfs     20Gi       RWO,ROX,RWX                   8s
  33. Pod 挂载 PVC
  34. [root@master ~]# vim web1.yaml
  35. ---
  36. kind: Pod
  37. apiVersion: v1
  38. metadata:
  39.   name: web1
  40. spec:
  41.   volumes:                   # 卷定义
  42.   - name: logdata            # 卷名称
  43.     persistentVolumeClaim:   # 通过PVC引用存储资源
  44.       claimName: pvc1        # PVC名称
  45.   - name: website            # 卷名称
  46.     persistentVolumeClaim:   # 通过PVC引用存储资源
  47.       claimName: pvc2        # PVC名称
  48.   containers:
  49.   - name: nginx
  50.     image: myos:nginx
  51.     volumeMounts:
  52.     - name: logdata
  53.       mountPath: /usr/local/nginx/logs
  54.     - name: website
  55.       mountPath: /usr/local/nginx/html
  56. 服务验证
  57. [root@master ~]# kubectl delete pods web1
  58. pod "web1" deleted
  59. [root@master ~]# kubectl apply -f web1.yaml
  60. pod/web1 created
  61. [root@master ~]# kubectl get pods -o wide
  62. NAME   READY   STATUS    RESTARTS   AGE   IP             NODE
  63. web1   1/1     Running   0          45m   10.244.2.16    node-0002
  64. [root@master ~]# curl http://10.244.2.16
  65. nfs server
复制代码
#以前大多数练习情况中,我们是直接将数据写入到真机中的,而写入容器的特定卷组,也可以在一定程度上,包管数据的完整性和一致性。
  二、临时卷管理


#存储少量数据可采用。  (*^▽^*)
临时卷

configMap

  
  1. # 使用命令创建 configMap
  2. [root@master ~]# kubectl create configmap tz --from-literal=TZ="Asia/Shanghai"
  3. configmap/tz created
  4. # 使用资源对象文件创建
  5. [root@master ~]# vim timezone.yaml
  6. ---
  7. kind: ConfigMap
  8. apiVersion: v1
  9. metadata:
  10.   name: timezone
  11. data:
  12.   TZ: Asia/Shanghai
  13. [root@master ~]# kubectl apply -f timezone.yaml
  14. configmap/timezone created
  15. [root@master ~]# kubectl get configmaps
  16. NAME               DATA   AGE
  17. kube-root-ca.crt   1      9d
  18. timezone           1      15s
  19. tz                 1      50s
复制代码
修改系统时区

  1. [root@master ~]# vim web1.yaml
  2. ---
  3. kind: Pod
  4. apiVersion: v1
  5. metadata:
  6.   name: web1
  7. spec:
  8.   volumes:
  9.   - name: logdata
  10.     persistentVolumeClaim:
  11.       claimName: pvc1
  12.   - name: website
  13.     persistentVolumeClaim:
  14.       claimName: pvc2
  15.   containers:
  16.   - name: nginx
  17.     image: myos:nginx
  18.     envFrom:              # 配置环境变量
  19.     - configMapRef:       # 调用资源对象
  20.         name: timezone    # 资源对象名称
  21.     volumeMounts:
  22.     - name: logdata
  23.       mountPath: /usr/local/nginx/logs
  24.     - name: website
  25.       mountPath: /usr/local/nginx/html
  26. [root@master ~]# kubectl delete pods web1
  27. pod "web1" deleted
  28. [root@master ~]# kubectl apply -f web1.yaml
  29. pod/web1 created
  30. [root@master ~]# kubectl exec -it web1 -- date +%T
  31. 10:41:27
复制代码
nginx 解析 php

  添加容器

  1. # 在 Pod 中增加 php 容器,与 nginx 共享同一块网卡
  2. [root@master ~]# vim web1.yaml
  3. ---
  4. kind: Pod
  5. apiVersion: v1
  6. metadata:
  7.   name: web1
  8. spec:
  9.   volumes:
  10.   - name: logdata
  11.     persistentVolumeClaim:
  12.       claimName: pvc1
  13.   - name: website
  14.     persistentVolumeClaim:
  15.       claimName: pvc2
  16.   containers:
  17.   - name: nginx
  18.     image: myos:nginx
  19.     envFrom:
  20.     - configMapRef:
  21.         name: timezone
  22.     volumeMounts:
  23.     - name: logdata
  24.       mountPath: /usr/local/nginx/logs
  25.     - name: website
  26.       mountPath: /usr/local/nginx/html
  27.   - name: php                            # 以下为新增加内容
  28.     image: myos:php-fpm
  29.     envFrom:                             # 不同容器需要单独配置时区
  30.     - configMapRef:
  31.         name: timezone
  32.     volumeMounts:
  33.     - name: website                      # 不同容器需要单独挂载NFS
  34.       mountPath: /usr/local/nginx/html
  35. [root@master ~]# kubectl delete pod web1
  36. pod "web1" deleted
  37. [root@master ~]# kubectl apply -f web1.yaml
  38. pod/web1 created
  39. [root@master ~]# kubectl get pods
  40. NAME   READY   STATUS    RESTARTS   AGE
  41. web1   2/2     Running   0          5s
  42. [root@master ~]# kubectl exec -it web1 -c nginx -- ss -ltun
  43. Netid     State      Recv-Q     Send-Q    Local Address:Port     ... ...
  44. tcp       LISTEN     0          128             0.0.0.0:80       ... ...
  45. tcp       LISTEN     0          128           127.0.0.1:9000     ... ...
复制代码
  1. 创建 ConfigMap
  2. # 使用 nginx 配置文件创建 configMap
  3. [root@master ~]# kubectl cp -c nginx web1:/usr/local/nginx/conf/nginx.conf nginx.conf
  4. [root@master ~]# vim nginx.conf
  5.         location ~ \.php$ {
  6.             root            html;
  7.             fastcgi_pass    127.0.0.1:9000;
  8.             fastcgi_index   index.php;
  9.             include         fastcgi.conf;
  10.         }
  11. # 使用命令创建 configMap
  12. [root@master ~]# kubectl create configmap nginx-php --from-file=nginx.conf
  13. configmap/nginx-php created
  14. 挂载 ConfigMap
  15. [root@master ~]# vim web1.yaml
  16. ---
  17. kind: Pod
  18. apiVersion: v1
  19. metadata:
  20.   name: web1
  21. spec:
  22.   volumes:
  23.   - name: logdata
  24.     persistentVolumeClaim:
  25.       claimName: pvc1
  26.   - name: website
  27.     persistentVolumeClaim:
  28.       claimName: pvc2
  29.   - name: nginx-php     # 卷名称
  30.     configMap:          # 引用资源对象
  31.       name: nginx-php   # 资源对象名称
  32.   containers:
  33.   - name: nginx
  34.     image: myos:nginx
  35.     envFrom:
  36.     - configMapRef:
  37.         name: timezone
  38.     volumeMounts:
  39.     - name: nginx-php                              # 卷名称
  40.       subPath: nginx.conf                          # 键值(文件名称)
  41.       mountPath: /usr/local/nginx/conf/nginx.conf  # 路径
  42.     - name: logdata
  43.       mountPath: /usr/local/nginx/logs
  44.     - name: website
  45.       mountPath: /usr/local/nginx/html
  46.   - name: php
  47.     image: myos:php-fpm
  48.     envFrom:
  49.     - configMapRef:
  50.         name: timezone
  51.     volumeMounts:
  52.     - name: website
  53.       mountPath: /usr/local/nginx/html
复制代码
secret 卷

  

   

  设置登录秘钥

  1. [root@master ~]# kubectl create secret docker-registry harbor-auth --docker-server=harbor:443 --docker-username="用户名" --docker-password="密码"
  2. secret/harbor-auth created
  3. [root@master ~]# kubectl get secrets harbor-auth -o yaml
  4. apiVersion: v1
  5. data:
  6.   .dockerconfigjson: <经过加密的数据>
  7. kind: Secret
  8. metadata:
  9.   name: harbor-auth
  10.   namespace: default
  11.   resourceVersion: "1558265"
  12.   uid: 08f55ee7-2753-41fa-8aec-98a292115fa6
  13. type: kubernetes.io/dockerconfigjson
复制代码
  
  1. 认证私有仓库
  2. [root@master ~]# vim web2.yaml
  3. ---
  4. kind: Pod
  5. apiVersion: v1
  6. metadata:
  7.   name: web2
  8. spec:
  9.   imagePullSecrets:
  10.   - name: harbor-auth
  11.   containers:
  12.   - name: apache
  13.     image: harbor:443/private/httpd:latest
  14. [root@master ~]# kubectl apply -f web2.yaml
  15. pod/web2 created
  16. [root@master ~]# kubectl get pods
  17. NAME   READY   STATUS    RESTARTS   AGE
  18. web1   2/2     Running   0          33m
  19. web2   1/1     Running   0          18m
复制代码
emptyDir 卷

  临时空间

  1. [root@master ~]# vim web2.yaml
  2. ---
  3. kind: Pod
  4. apiVersion: v1
  5. metadata:
  6.   name: web2
  7. spec:
  8.   imagePullSecrets:
  9.   - name: harbor-auth
  10.   volumes:               # 卷配置
  11.   - name: cache          # 卷名称
  12.     emptyDir: {}         # 资源类型
  13.   containers:
  14.   - name: apache
  15.     image: harbor:443/private/httpd:latest
  16.     volumeMounts:            # 挂载卷
  17.     - name: cache            # 卷名称
  18.       mountPath: /var/cache  # 路径
  19. [root@master ~]# kubectl delete pod web2
  20. pod "web2" deleted
  21. [root@master ~]# kubectl apply -f web2.yaml
  22. pod/web2 created
  23. [root@master ~]# kubectl exec -it web2 -- bash
  24. [root@web2 html]# mount -l |grep cache
  25. /dev/vda1 on /var/cache type xfs (rw,relatime,attr2)
  26. # 清理实验配置
  27. [root@master ~]# kubectl delete pods --all
  28. [root@master ~]# kubectl delete pvc --all
  29. [root@master ~]# kubectl delete pv --all
复制代码

  总结: 
该节内容,同砚们一起学习好以下几点知识面:
1.如何使用查看指针。
2.如何设置污点和容忍策略。
3.如何设置pod的优先级?
4.卷组的创建与选择。


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4