赞
踩
往期回顾:
云端技术驾驭DAY01——云计算底层技术奥秘、云服务器磁盘技术、虚拟化管理、公有云概述
云端技术驾驭DAY02——华为云管理、云主机管理、跳板机配置、制作私有镜像模板
云端技术驾驭DAY03——云主机网站部署、web集群部署、Elasticsearch安装
云端技术驾驭DAY04——Logstash安装部署及插件模块
云端技术驾驭DAY06——容器技术概述、镜像与容器管理、定制简单镜像、容器内安装部署服务
云端技术驾驭DAY07——Dockerfile详解、容器镜像制作、私有仓库
云端技术驾驭DAY08——部署容器服务、Compose微服务管理、harbor仓库部署及管理
云端技术驾驭DAY09——k8s集群安装部署、calico插件部署、计算节点配置管理
云端技术驾驭DAY10——kubectl命令详解、Pod创建过程、Pod的生命周期、定制Pod、资源对象文件
云端技术驾驭DAY11——资源对象文件、Pod自定义命令、多容器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
--show-labels
查询标签[root@master ~]# kubectl -n kube-system get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
calico-kube-controllers-fc945b5f7-kv9gc 1/1 Running 3 (17m ago) 4d19h k8s-app=calico-kube-controllers,pod-template-hash=fc945b5f7
calico-node-2wz65 1/1 Running 3 (17m ago) 4d17h controller-revision-hash=766786f67c,k8s-app=calico-node,pod-template-generation=1
calico-node-6jp85 1/1 Running 3 (17m ago) 4d17h controller-revision-hash=766786f67c,k8s-app=calico-node,pod-template-generation=1
... ...
命令 | 解释 |
---|---|
kubectl label 资源类型 [资源名称] <key>=<value> | 设置标签 |
kubectl label 资源类型 [资源名称] <key>- | 删除标签 |
kubectl label 资源类型 [资源名称] --show-labels | 查看标签 |
kubectl label 资源类型 [资源名称] -l <key>=<value> | 筛选标签 |
[root@master ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
mylinux 1/1 Running 0 7s <none>
[root@master ~]# kubectl label pods mylinux app=apache // 设置标签
pod/mylinux labeled
[root@master ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
mylinux 1/1 Running 0 42s app=apache
[root@master ~]# kubectl label pods mylinux app- // 删除标签
pod/mylinux unlabeled
[root@master ~]# kubectl get nodes -l kubernetes.io/hostname=master // 使用-l筛选标签
NAME STATUS ROLES AGE VERSION
master Ready control-plane 4d19h v1.26.0
[root@master ~]# vim myhttp.yaml --- kind: Pod apiVersion: v1 metadata: name: myhttp labels: app: httpd name: apache spec: containers: - name: apache image: myos:httpd [root@master ~]# kubectl apply -f myhttp.yaml pod/myhttp created [root@master ~]# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS myhttp 1/1 Running 0 90s app=httpd,name=apache
[root@master ~]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
master Ready control-plane 4d20h v1.26.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=master,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node.kubernetes.io/exclude-from-external-load-balancers=
node-0001 Ready <none> 4d18h v1.26.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node-0001,kubernetes.io/os=linux
node-0002 Ready <none> 4d18h v1.26.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node-0002,kubernetes.io/os=linux
node-0003 Ready <none> 4d18h v1.26.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node-0003,kubernetes.io/os=linux
node-0004 Ready <none> 4d18h v1.26.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node-0004,kubernetes.io/os=linux
node-0005 Ready <none> 4d18h v1.26.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node-0005,kubernetes.io/os=linux
[root@master ~]# vim myhttp.yaml
---
kind: Pod
apiVersion: v1
metadata:
name: myhttp
labels:
app: httpd
name: apache
spec:
nodeSelector:
kubernetes.io/hostname: node-0002 // 选择标签
containers:
- name: apache
image: myos:httpd
[root@master ~]# kubectl delete pod myhttp
pod "myhttp" deleted
[root@master ~]# kubectl apply -f myhttp.yaml
pod/myhttp created
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myhttp 1/1 Running 0 23s 10.244.147.5 node-0002 <none> <none>
[root@master ~]# kubectl label nodes node-0002 node-0003 disktype=ssd // 给node-0002和node-0003打标签 node/node-0002 labeled node/node-0003 labeled [root@master ~]# vim mywebs.yaml --- kind: Pod apiVersion: v1 metadata: name: myhttp labels: app: apache spec: nodeSelector: disktype: ssd containers: - name: apache image: myos:httpd
[root@master ~]# sed "s,myhttp,web1," mywebs.yaml | kubectl apply -f -
pod/web1 created
[root@master ~]# sed "s,myhttp,web2," mywebs.yaml | kubectl apply -f -
pod/web2 created
[root@master ~]# sed "s,myhttp,web3," mywebs.yaml | kubectl apply -f -
pod/web3 created
[root@master ~]# sed "s,myhttp,web4," mywebs.yaml | kubectl apply -f -
pod/web4 created
[root@master ~]# sed "s,myhttp,web5," mywebs.yaml | kubectl apply -f -
pod/web5 created
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myhttp 1/1 Running 0 13m 10.244.147.5 node-0002 <none> <none>
web1 1/1 Running 0 51s 10.244.243.198 node-0003 <none> <none>
web2 1/1 Running 0 43s 10.244.243.199 node-0003 <none> <none>
web3 1/1 Running 0 39s 10.244.147.6 node-0002 <none> <none>
web4 1/1 Running 0 34s 10.244.243.200 node-0003 <none> <none>
web5 1/1 Running 0 30s 10.244.147.7 node-0002 <none> <none>
[root@master ~]# vim minpod.yaml --- kind: Pod apiVersion: v1 metadata: name: minpod spec: terminationGracePeriodSeconds: 0 nodeSelector: kubernetes.io/hostname: node-0003 containers: - name: linux image: myos:8.5 command: ["awk", "BEGIN{while(1){}}"] resources: requests: // 配额策略 memory: 1100Mi // 内存资源配额 cpu: 800m // 计算资源配额
[root@master ~]# for i in app{1..5};do sed "s,minpod,${i}," minpod.yaml;done | kubectl apply -f -
pod/app1 created
pod/app2 created
pod/app3 created
pod/app4 created
pod/app5 created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
app1 1/1 Running 0 4s
app2 1/1 Running 0 4s
app3 0/1 Pending 0 4s
app4 0/1 Pending 0 4s
app5 0/1 Pending 0 4s
[root@master ~]# vim maxpod.yaml
---
kind: Pod
apiVersion: v1
metadata:
name: maxpod
... ...
command: ["awk", "BEGIN{while(1){}}"]
resources:
limits:
cpu: 800m // 计算资源限额
memory: 2000Mi // 内存资源限额
[root@master ~]# kubectl apply -f maxpod.yaml
pod/maxpod created
[root@master ~]# kubectl create namespace work namespace/work created [root@master ~]# vim limit.yaml --- kind: LimitRange apiVersion: v1 metadata: name: mylimit // 策略名称 namespace: work // 规则生效的名称空间 spec: limits: // 全局规则 - type: Container // 资源类型 default: // 对没有限制策略的容器添加规则 cpu: 300m // 计算资源限额 memory: 500Mi // 内存资源限额 defaultRequest: cpu: 8m // 计算资源配额 memory: 8Mi // 内存资源配额 [root@master ~]# kubectl apply -f limit.yaml -n work limitrange/mylimit created
[root@master ~]# vim limit.yaml --- kind: LimitRange apiVersion: v1 metadata: name: mylimit namespace: work spec: limits: - type: Container default: cpu: 300m memory: 500Mi defaultRequest: cpu: 8m memory: 8Mi max: // 资源限额 cpu: 800m // 计算资源限额 memory: 1000Mi // 内存资源限额 min: // 资源配额 cpu: 2m // 计算资源配额 memory: 8Mi // 内存资源配额 [root@master ~]# kubectl apply -f limit.yaml -n work limitrange/mylimit configured
... ...
spec:
limits:
- type: Pod // 限制Pod资源总量
max:
cpu: 1200m
memory: 1200Mi
min:
cpu: 2m
memory: 8Mi
[root@master ~]# vim quota.yaml
---
apiVersion: v1
kind: ResourceQuota // 全局资源对象
metadata:
name: myquota // 规则名称
namespace: work // 规则作用的名称空间
spec:
hard: // 创建强制规则
requests.cpu: 1000m // 计算资源配额总数
requests.memory: 2000Mi // 内存资源配额总数
limits.cpu: 5000m // 计算资源限额总数
limits.memory: 8Gi // 内存资源限额总数
pods: 3 // 限制创建资源对象总量
[root@master ~]# kubectl -n work apply -f quota.yaml resourcequota/myquota created [root@master ~]# kubectl describe namespaces work Name: work Labels: kubernetes.io/metadata.name=work Annotations: <none> Status: Active Resource Quotas Name: myquota Resource Used Hard -------- --- --- limits.cpu 0 5 limits.memory 0 8Gi pods 0 3 requests.cpu 0 1 requests.memory 0 2000Mi
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。