K8s(3):资源清单

本文详细介绍了Kubernetes资源清单的格式和使用,包括如何生成模板、创建自主式Pod、设定监听端口、限制内存和CPU,以及如何通过nodeSelector进行节点调度。通过示例展示了如何创建、修改和删除Pod,以及处理ImagePullBackOff问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.资源清单格式

     在我们的日常工作中,可能要重复简历或者对pod节点进行操作,如果一直使用命令行来实现的话是不高效的工作方法。
     资源清单就好比shell脚本一样,会按照你的清单执行命令,相比较命令行更加方便,也易于修改。

书写格式

apiVersion: group/version //指明api资源属于哪个群组和版本,同一个组可以有多个版本      

$ kubectl api-versions        //查询命令

kind:         //标记创建的资源类型,k8s主要支持以下资源类别   Pod,ReplicaSet,Deployment,StatefulSet,DaemonSet,Job,Cronjob    

metadata:    //元数据        

name:    //对像名称      

 namespace:    //对象属于哪个命名空间        

labels:    //指定资源标签,标签是一种键值数据 

spec:         //定义目标资源的期望状态 

$ kubectl explain pod        //查询帮助文档

二.资源清单使用





2.1生成模板

kubectl explain pod

 书写时候的查找方法,可以之直接通过explain一步一步查找参数内容用法

 

2.2自主式Pod资源清单

编辑清单,文件格式为.yaml
创建名为pod-example的pod,选取镜像为myapp:v1

 此处的清单作用为创建名为pod-example的pod,选取镜像为myapp:v1

apiVersion: v1
kind: Pod
metadata:
  name: pod-example
spec:
  containers:

  - name: myapp
    image: myapp:v1

 

应用脚本并查看

kubectl apply -f pod.yaml
   57  kubectl get pod

delete deployments.apps webserver   删除之前的容器副本

并查看是否成功

 

 测试:

kubectl run nginx --image=nginx
   63   kubectl get pod
   64  kubectl get pod nginx -o yaml

 curl 10.244.2.9

删除:

[root@server5 pod]# kubectl delete pod nginx
pod "nginx" deleted
[root@server5 pod]# kubectl delete -f pod.yaml
pod "pod-example" deleted

 一直出现ContainerCreating、ImagePullBackOff可能是harbor仓库没有镜像,按照步骤上传镜像

 docker tag busybox:latest reg.westos.org/library/busybox:latest
   26  docker images | grep busybox
   27  docker login reg.westos.org
   29  docker push reg.westos.org/library/busybox

重新编写yaml文件

[root@server5 pod]# cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-example
spec:
  containers:
  - name: nginx
    image: nginx
  - name: busybox
    image: busybox
    stdin: true
    tty: true

重新执行 apply

 进入busybox终端,-c指定容器 -i打开输入端口 -t打开伪终端
终端内访问localhost

 kubectl attach pod-example -c busybox -i -t         //// ip addr

 资源清单中,默认值,IfNotPresent:本地有则使用本地镜像,不拉取

kubectl delete -f pod.yaml
vi pod.yaml 

apiVersion: v1
kind: Pod
metadata:
  name: pod-example
spec:
  containers:

  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent ##如果本地存在镜像,则不拉取;本地不存在镜像,再拉取;默认会一直拉取镜像
  - name: busybox
    image: busyboxplus
    imagePullPolicy: IfNotPresent
    tty: true
    stdin: true

可以看到详细信息:直接创建,开启容器,没有拉取动作!
kubectl describe pod pod-example

2.3设定监听端口

kubectl delete -f pod.yaml
   46  vi pod.yaml
   47  kubectl apply -f pod.yaml
   48  kubectl get pod -o wide

可以看到分配给了server7

apiVersion: v1
kind: Pod
metadata:
  name: pod-example
spec:

  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
    ports:
      - name: http
        hostPort: 80
        containerPort: 80

     去server7,分别用两种方式查询端口,一种直接查询,一种查询策略.netstat -antlp没有80!但是在路由策略里面有80

 netstat -antlp
 iptables -t nat -nL | grep :80

 2.4限制内存和CPU

 kubectl delete -f pod.yaml
   53  vi pod.yaml
   54  kubectl apply -f pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: pod-example
spec:

  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
    ports:
      - name: http
        hostPort: 80
        containerPort: 80
    resources:
       limits:
        cpu: 0.5
        memory: 200Mi
       requests:
        cpu: 0.2
        memory: 100Mi
# 限制内存和cpu使用量,requests为最低限制,limit为最高限制

  55  kubectl describe pod pod-example  查看限制信息 :查看限制cpu的信息

 

 2.5锁定pod节点

1.锁定节点

kubectl delete -f pod.yaml
vi pod.yaml
kubectl apply -f pod.yaml
kubectl get pod -o wide

apiVersion: v1
kind: Pod
metadata:
  name: pod-example
spec:
  nodeSelector:
    kubernetes.io/hostname: server6
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
    ports:
      - name: http
        hostPort: 80
        containerPort: 80
    resources:
       limits:
        cpu: 0.5
        memory: 200Mi
       requests:
        cpu: 0.2
        memory: 100Mi

 此时可以看到server6被安排了

2.设置虚拟ip

若在后面server6后面加hostnetwork: true
则 ip变为 虚拟机ip

kubectl get pod -o wide可以看到此时ip变成虚拟的了

spec:
  nodeSelector:
    kubernetes.io/hostname: server6
  hostNetwork: true   #####一定要注意这里的缩进

 

2.6   nodeSelector调度

nodeSelector 是节点选择约束的最简单推荐形式

kubectl delete -f pod.yaml
vi pod.yaml

kubectl apply -f pod.yaml
kubectl get pod -o wide  #匹配的标签不存在,则容器将不会运行,一直处于Pending 状态。

 解决办法:

kubectl label nodes server2 disktype=ssd  ##通过label来给节点打上标签

再次查看,容器就运行了

 kubectl label nodes server6 disktype=ssd
 kubectl get nodes -l disktype

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值