多容器Pod
案例 3 排错
[root@master ~]# vim web2.yaml
---
kind: Pod
apiVersion: v1
metadata:
name: web2
namespace: default
spec:
containers:
- name: nginx
image: myos:nginx
- name: apache
image: myos:httpd
status: {}
[root@master ~]# kubectl apply -f web2.yaml
pod/web2 created
[root@master ~]# kubectl get pods web2
NAME READY STATUS RESTARTS AGE
web2 1/2 Error 1 (4s ago) 8s
自定义任务
[root@master ~]# vim mycmd.yaml
---
kind: Pod
apiVersion: v1
metadata:
name: mycmd
spec:
containers:
- name: linux
image: myos:httpd
command: [sleep] # 配置自定义命令
args: # 设置命令参数
- "30"
[root@master ~]# kubectl apply -f mycmd.yaml
pod/mycmd created
[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
嵌入式脚本
[root@master ~]# vim mycmd.yaml
---
kind: Pod
apiVersion: v1
metadata:
name: mycmd
spec:
containers:
- name: linux
image: myos:8.5
command: [sh]
args:
- -c
- |
ID=${RANDOM}
for i in {1..9};do
echo "${ID} : hello world."
sleep 5
done
[root@master ~]# kubectl replace --force -f mycmd.yaml
pod "mycmd" deleted
pod/mycmd replaced
[root@master ~]# kubectl logs mycmd
hello world.
hello world.
hello world.
容器保护策略
[root@master ~]# vim mycmd.yaml
---
kind: Pod
apiVersion: v1
metadata:
name: mycmd
spec:
restartPolicy: OnFailure # 配置重启策略
containers:
- name: linux
image: myos:8.5
command: [sh]
args:
- -c
- |
ID=${RANDOM}
for i in {1..9};do
echo "${ID} : hello world."
sleep 5
done
exit $((ID%2))
[root@master ~]# kubectl replace --force -f mycmd.yaml
pod "mycmd" deleted
pod/mycmd replaced
[root@master ~]# kubectl get pods -w
NAME READY STATUS RESTARTS AGE
mycmd 1/1 Running 0 4s
mycmd 0/1 Completed 0 31s
宽限期策略
[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
- |
ID=${RANDOM}
for i in {1..9};do
echo "${ID} : hello world."
sleep 5
done
exit $((ID%2))
[root@master ~]# kubectl delete pods mycmd
pod "mycmd" deleted
[root@master ~]# kubectl apply -f mycmd.yaml
pod/mycmd created
[root@master ~]# kubectl delete pods mycmd
pod "mycmd" deleted
最大生命周期
[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
- |
ID=${RANDOM}
for i in {1..9};do
echo "${ID} : hello world."
sleep 5
done
exit $((ID%2))
[root@master ~]# kubectl replace --force -f mycmd.yaml
pod "mycmd" deleted
pod/mycmd replaced
[root@master ~]# kubectl get pods -w
mycmd 1/1 Running 0 1s
mycmd 1/1 Running 1 30s
mycmd 0/1 Error 1 62s
上方是续上一篇内容的案例
一.自定义任务
1.Pod自定义命令
-创建 Pod 时,可以为其设置启动时要执行的自定义命令, 如果配置了自定义命令,那么镜像中自带的默认启动命令将不再执行
-自定义命令设置在 command
字段下,如果命令有参数,需要填写在 args
字段下
2.循环死锁
-如果一个Pod的内部程序在运行时出现循环死锁,那么就会永远不停的重复执行,如何避免这种情况发生捏?
a.使用activeDeadl ineSeconds策略
-允许Pod运行的最大时长
-时间到期后会向Pod发送signal,如果Pod无法结束就把它强制关闭,并且设置为Error状态
eg:activeDeadl ineSeconds策略:
---
kind: Pod
apiVersion: v1
metadata:
name: mycmd
spec:
activeDeadlineSeconds: 60 # 可以执行的最大时长
terminationGracePeriodSeconds: 0
restartPolicy: OnFailure
containers:
- name: linux
image: myos:8.5
command: ["sh"]
......
[root@master ~]# kubectl replace --force -f mycmd.yaml
pod "mycmd" deleted
pod/mycmd created
# 超时后被设置为 Error 状态
[root@master ~]# kubectl get pods -w
mycmd 1/1 Running 0 1s
mycmd 1/1 Running 0 60s
mycmd 0/1 Error 0 64s
案例:自定义Pod脚本
a.创建一个Pod,完成以下功能
b.该 Pod 每隔 5 秒获取一次当前系统内存使用情况如果内存使用量小于 1000Mi 就输出: INFO: running normally如果内存使用量大于 1000Mi 就输出: WARN: high memory usage
提示:
可以使用 /public/memtest.py
脚本协助完成测试
答案:
---
kind: Pod
apiVersion: v1
metadata:
name: mymem
spec:
restartPolicy: OnFailure
containers:
- name: linux
image: myos:8.5
command: [sh]
args:
- -c
- |
while sleep 5;do
use=$(free -m |awk '$1=="Mem:"{print $3}')
if (( ${use} < 1000 ));then
echo -e "\x1b[32mINFO:\x1b[39m running normally"
else
echo -e "\x1b[31mWARN:\x1b[39m high memory usage"
fi
done
二、Pod调度策略
1.调度策略概述
a.什么是调度分配?
在 k8s 中,调度是将 Pod 分配到合适的计算节点上,然后对应节点上的 Kubelet 运行这些 Pod
kube-scheduler 是默认调度器,是集群的核心组件
b.调度器是如何工作的?
调度器通过 k8s 的监测(Watch)机制来发现集群中尚未被调度到节点上的 Pod。调度器依据调度原则将 Pod 分配到一个合适的节点上运行
c.调度流程
-调度器给一个Pod做调度选择包含两个步骤:过滤和打分
d.第一步过滤(筛选)
首先要筛选出满足 Pod 所有的资源请求的节点,这里包含计算资源、内存、存储、网络、端口号等等,如果没有节点能满足 Pod 的需求,Pod 将一直停留在 Pending 状态,直到调度器能够找到合适的节点运行它。
e.第二步打分(优选):
在打分阶段,调度器会根据打分规则,为每一个可调度节点进行打分。选出其中得分最高的节点来运行 Pod。如果存在多个得分最高的节点,调度器会从中随机选取一个。
f.绑定:
在确定了某个节点运行 Pod 之后,调度器将这个调度决定通知给 kube-apiserver,这个过程叫做绑定。
2.Pod定向调度
a.基于节点名称的调度
-在创建POd的过程中,我们可以配置相关的调度规则,从而让Pod运行在制定的节点上。
-nodeName标签,让Pod运行在制定的节点上。
eg:
kind:Pod
apiVersion: v1
metadata:
name: myhttp
spec:
nodeName: node-0001 #在Pod.spec下声明目标主机的名字
containers:
........
b.测试验证
-注意:如果标签指定的节点无法运行Pod,它不会迁移到其他节点,将一直等待下去
三.标签与Pod调度
1.标签管理
a.标签是什么
-标签(Labels)是附加到Kubernetes对象上的键值对
b.标签的用途
-K8s在创建、删除、修改资源对象的时候可以使用标签来确定要修改的资源对象。在Pod调度的任务中,使用标签可以更加灵活的完成调度任务。
-标签可以在创建时附加到对象,也可以在创建之后随时添加和修改。标签可以用于组织和选择对象的子集。
c.管理标签语法格式:
-设置标签:
kubectl label 资源类型 [资源名称 ] <key>=<value>
-删除标签:
kubectl label 资源类型 [资源名称 ] <key>-
-查看标签:
kubectl get 资源类型 [资源名称 ] --show-labels
-使用标签选择
kubectl get 资源类型 [资源名称 ] -l <key>=<value>
2.Pod标签调度
a.标签选择运算符
-与名称和UID不同,标签不支持唯一性。通常,我们希望许多对象携带相同的标签。
-通过标签选择运算符,客户端/用户可以识别一组对象
-标签选择算符可以由多个需求组成。在多个需求的情况下,必须满足所有要求,相当于逻辑与(&&)运算符。
b.使用系统标签调度
-查看node-0002的标签
[root@master ~]# kubectl get nodes --show-labels
NAME LABELS
master kubernetes.io/hostname=master
node-0001 kubernetes.io/hostname=node-0001
node-0002 kubernetes.io/hostname=node-0002
node-0003 kubernetes.io/hostname=node-0003
c.通过标签调度Pod
eg:
[root@master ~]# vim myhttp.yaml
---
kind: Pod
apiVersion: v1
metadata:
name: myhttp
spec:
nodeSelector:
kubernetes.io/hostname: node-0002
containers:
- name: apache
image: myos:httpd
d.通过标签调度Pod
# 创建 Pod
[root@master ~]# kubectl replace --force -f myhttp.yaml
pod "myhttp" deleted
pod/myhttp created
# 查询调度节点
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS IP NODE
myhttp 1/1 Running 0 10.244.2.11 node-0002
案例:Pod调度策略
-已知节点node-0002和node-0003使用的是ssd硬盘
-把Pod调度到有ssd硬盘的节点上:
Pod调度策略
基于名称调度
[root@master ~]# vim myhttp.yaml
---
kind: Pod
apiVersion: v1
metadata:
name: myhttp
spec:
nodeName: node-0001 # 基于节点名称进行调度
containers:
- name: apache
image: myos:httpd
[root@master ~]# kubectl apply -f myhttp.yaml
pod/myhttp created
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
myhttp 1/1 Running 0 3s 10.244.1.6 node-0001
标签管理
# 查看标签
[root@master ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
myhttp 1/1 Running 0 2m34s <none>
# 添加标签
[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 replace --force -f myhttp.yaml
pod "myhttp" deleted
pod/myhttp replaced
[root@master ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
myhttp 1/1 Running 0 7s app=apache
# 使用标签过滤资源对象
[root@master ~]# kubectl get pods -l app=apache
NAME READY STATUS RESTARTS AGE
myhttp 1/1 Running 0 6m44s
[root@master ~]# kubectl get nodes -l kubernetes.io/hostname=master
NAME STATUS ROLES AGE VERSION
master Ready control-plane 5d6h v1.29.2
[root@master ~]# kubectl get namespaces -l kubernetes.io/metadata.name=default
NAME STATUS AGE
default Active 5d6h
基于标签调度
# 查询 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 上的标签调度 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