# 最终结果为 [01234空格56789]0123456789---">
赞
踩
- [root@master ~]# kubectl run myweb --image=myos:nginx --dry-run=client -o yaml
- apiVersion: v1
- kind: Pod
- metadata:
- creationTimestamp: null
- labels:
- run: myweb
- name: myweb
- spec:
- containers:
- - image: myos:nginx
- name: myweb
- resources: {}
- dnsPolicy: ClusterFirst
- restartPolicy: Always
- status: {}
除 Pod 外,其它资源对象模板使用 create 生成
生成模板命令:--dry-run=client -o yaml
- [root@master ~]# kubectl create namespace work --dry-run=client -o yaml
- apiVersion: v1
- kind: Namespace
- metadata:
- creationTimestamp: null
- name: work
- spec: {}
- status: {}
- ---
- kind: # 一级,可以表示为 .kind
- apiVersion: # 一级,可以表示为 .apiVersion
- metadata: # 一级,可以表示为 .metadata
- name: # 二级,可以表示为 .metadata.name
- spec:
- researtPolicy: xx
- containers:
- - name: # 三级,可以表示为 .spec.containers.name
- image:
- status: {}
- spec
- restartPolicy: Always # 获取Pod中该参数的帮助信息
- containers:
- #------------------------------------------------------------------------------
- [root@master ~]# kubectl explain Pod.spec.restartPolicy
- KIND: Pod
- VERSION: v1
-
- FIELD: restartPolicy <string>
-
- DESCRIPTION:
- Restart policy for all containers within the pod. One of Always, OnFailure,
- Never. Default to Always. More info:
- https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#restart-policy
-
- Possible enum values:
- - `"Always"`
- - `"Never"`
- - `"OnFailure"`
- [root@master app]# vim nginx.yaml
- ---
- kind: Pod
- apiVersion: v1
- metadata:
- name: nginx
- spec:
- containers:
- - name: nginx
- image: myos:nginx
- [root@master app]# vim phpfpm.yaml
- ---
- kind: Pod
- apiVersion: v1
- metadata:
- name: php
- spec:
- containers:
- - name: php
- image: myos:php-fpm
- # 使用资源对象文件创建应用
- [root@master app]# kubectl apply -f nginx.yaml -f phpfpm.yaml
- pod/nginx created
- pod/php created
-
- # 删除应用
- [root@master app]# kubectl delete -f /root/app/
- pod "nginx" deleted
- pod "php" deleted
-
- # 合并资源对象文件
- [root@master app]# cat nginx.yaml >> app.yaml
- [root@master app]# cat phpfpm.yaml >> app.yaml
-
- # 调用合并后的文件
- [root@master app]# kubectl apply -f app.yaml
- pod/nginx created
- pod/php created
-
- # 删除资源对象
- [root@master app]# kubectl delete -f app.yaml
- pod "nginx" deleted
- pod "php" deleted
- [root@master ~]# vim mycmd.yaml
- ---
- kind: Pod
- apiVersion: v1
- metadata:
- name: mycmd
- spec:
- containers:
- - name: linux
- image: myos:8.5
- command: ["sleep"] # 自定义命令
- args: ["30"] # 命令参数
Pod 会根据策略决定容器结束后是否重启
- [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 10s
- mycmd 0/1 Completed 0 31s
- mycmd 1/1 Running 1 (1s ago) 32s
-
- -w:实时监视资源的状态变化
选项:重启(Always)、不重启(Never)、失败就重启(OnFailure)
- [root@master ~]# vim mycmd.yaml
- ---
- kind: Pod
- apiVersion: v1
- metadata:
- name: mycmd
- spec:
- restartPolicy: Never # 配置保护策略
- containers:
- - name: linux
- image: myos:8.5
- command: ["sleep"]
- args: ["10"]
- [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 12s
- mycmd 0/1 Completed 0 13s
宽限期是为了避免服务突然中断,造成的事务不一致的问题
- [root@master ~]# kubectl delete -f mycmd.yaml
- [root@master ~]# kubectl apply -f mycmd.yaml
- # 为什么删除要等一会
- [root@master ~]# kubectl delete -f mycmd.yaml
- # 等待 .
- # 等待 ..
- # 等待 ...
- pod "mycmd" deleted
宽限期默认30s,不等待设置为0
- [root@master ~]# vim mycmd.yaml
- ---
- kind: Pod
- apiVersion: v1
- metadata:
- name: mycmd
- spec:
- terminationGracePeriodSeconds: 0 # 设置宽限期
- restartPolicy: Never
- containers:
- - name: linux
- image: myos:8.5
- command: ["sleep"]
- args: ["30"]
-
- [root@master ~]# kubectl apply -f mycmd.yaml
- pod/mycmd created
- [root@master ~]# kubectl delete -f mycmd.yaml
- pod "mycmd" deleted
如果一个Pod的内部程序在运行时出现循环死锁,那么就会永远不停的重复执行,如何避免这种情况发生
允许Pod运行的最大时长
时间到期后会向Pod发送singal,如果Pod无法结束就把它强制关闭,并且设置为Error状态
- [root@master ~]# vim mycmd.yaml
- ---
- kind: Pod
- apiVersion: v1
- metadata:
- name: mycmd
- spec:
- terminationGracePeriodSeconds: 0
- activeDeadlineSeconds: 60 # 可以执行的最大时长
- restartPolicy: Never
- containers:
- - name: linux
- image: myos:8.5
- command: ["sleep"]
- args: ["300"]
-
- [root@master ~]# kubectl delete -f mycmd.yaml
- pod "mycmd" deleted
- [root@master ~]# kubectl apply -f mycmd.yaml
- pod/mycmd created
-
- [root@master ~]# kubectl get pods -w
- mycmd 1/1 Running 0 1s
- mycmd 1/1 Running 0 60s
- mycmd 0/1 Error 0 64s
- ---
- string: > # 最终结果为 [01234空格56789]
- 01234
- 56789
-
- ---
- string2: | # 最终结果为 [01234换行56789]
- 01234
- 56789
- [root@master ~]# vim mycmd.yaml
- ---
- kind: Pod
- apiVersion: v1
- metadata:
- name: mycmd
- spec:
- terminationGracePeriodSeconds: 0
- restartPolicy: Always
- containers:
- - name: linux
- image: myos:8.5
- command: ["/bin/bash"] # 调用bash命令
- args: # 设置命令的参数
- - -c # 从字符串中读取命令
- - | # 以下多行字符串保留原格式
- while sleep 5;do # 脚本指令,注意缩进对齐
- echo "hello world."
- done
-
- [root@master ~]# kubectl apply -f mycmd.yaml
- pod/mycmd created
- [root@master ~]# kubectl get pods
- NAME READY STATUS RESTARTS AGE
- mycmd 1/1 Running 0 3s
- [root@master ~]# kubectl logs mycmd
- hello world.
- hello world.
- hello world.
- [root@master ~]# vim mynginx.yaml
- ---
- kind: Pod
- apiVersion: v1
- metadata:
- name: mynginx
- spec:
- terminationGracePeriodSeconds: 0
- restartPolicy: Always
- containers:
- - name: nginx
- image: myos:nginx
- - name: php
- image: myos:php-fpm
-
- [root@master ~]# kubectl apply -f mynginx.yaml
- pod/mynginx created
- [root@master ~]# kubectl get pods
- NAME READY STATUS RESTARTS AGE
- mynginx 2/2 Running 0 3s
受多容器配置影响,以下命令需要使用<-c 容器名字>
受影响命令:[logs,exec,cp]
- [root@master ~]# echo "hello world" >hello.html
- [root@master ~]# kubectl cp hello.html mynginx:/usr/local/nginx/html/ -c nginx
- [root@master ~]# kubectl exec mynginx -c php -- ps
- PID TTY TIME CMD
- 1 ? 00:00:00 php-fpm
- 7 ? 00:00:00 ps
- [root@master ~]# kubectl logs mynginx -c nginx
- [root@master ~]#
在安装部署程序之后,必须要了解应用程序在部署后的性能以及资源使用情况。你可以通过检测Pod容器或节点来了解整个集群的情况
Kubernetes的Metrics-server组件提供有关节点和Pod的资源使用情况的信息,包括CPU和内存的指标。如果将Metrics-server部署到集群中,就可以查询并使用到这些信息管理应用及服务
Metrics-Server是集群核心监控数据的聚合器。通俗的说,它存储了集群中各节点和Pod的监控数据,并且提供了API以供查询和使用
Metrics-Server通过kubelet获取node和Pod的CPU,内存等监控数据。为调度器、弹性控制器、以及Dashboard等UI组件提供数据来源
kube-apiserver必须启用聚合服务,或使用kube-proxy代理转发
节点必须启用身份验证和授权。kubelet证书需要由集群证书颁发机构签名
Metrics-Server使用443、4443端口,所有节点上kubelet必须能够访问该端口
- # 追加配置参数并重启服务
- [root@master ~]# echo 'serverTLSBootstrap: true' >>/var/lib/kubelet/config.yaml
- [root@master ~]# systemctl restart kubelet
- # 获取证书名称,并签发
- [root@master ~]# kubectl get certificatesigningrequests
- NAME AGE SIGNERNAME REQUESTOR REQUESTEDDURATION CONDITION
- csr-2hg42 14s kubernetes.io/kubelet-serving system:node:master <none> Pending
- [root@master ~]# kubectl certificate approve csr-2hg42
- certificatesigningrequest.certificates.k8s.io/csr-2hg42 approved
- [root@master ~]# kubectl get certificatesigningrequests
- NAME AGE SIGNERNAME REQUESTOR REQUESTEDDURATION CONDITION
- csr-2hg42 28s kubernetes.io/kubelet-serving system:node:master <none> Approved,Issued
将镜像导入到私有仓库plugins目录下
- # 导入镜像
- [root@master metrics]# docker load -i metrics-server.tar.xz
- # 上传镜像到私有仓库
- [root@master metrics]# docker images|while read i t _;do
- [[ "${t}" == "TAG" ]] && continue
- [[ "${i}" =~ ^"harbor:443/".+ ]] && continue
- docker tag ${i}:${t} harbor:443/plugins/${i##*/}:${t}
- docker push harbor:443/plugins/${i##*/}:${t}
- docker rmi ${i}:${t} harbor:443/plugins/${i##*/}:${t}
- done
- # 修改镜像地址
- [root@master metrics]# sed -ri 's,^(\s*image: )(.*/)?(.+),\1harbor:443/plugins/\3,' components.yaml
- 140: image: harbor:443/plugins/metrics-server:v0.6.2
- # 安装插件服务
- [root@master metrics]# kubectl apply -f components.yaml
- # 验证插件 Pod 状态
- [root@master metrics]# kubectl -n kube-system get pods -l k8s-app=metrics-server
- NAME READY STATUS RESTARTS AGE
- metrics-server-ddb449849-c6lkc 1/1 Running 0 64s
- [root@master metrics]# kubectl top nodes
- NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
- master 99m 4% 1005Mi 27%
- node-0001 <unknown> <unknown> <unknown> <unknown>
- node-0002 <unknown> <unknown> <unknown> <unknown>
- node-0003 <unknown> <unknown> <unknown> <unknown>
- node-0004 <unknown> <unknown> <unknown> <unknown>
- node-0005 <unknown> <unknown> <unknown> <unknown>
为所有node节点启用引导令牌
- #--------------- 在所有计算节点配置证书 -----------------
- [root@node ~]# echo 'serverTLSBootstrap: true' >>/var/lib/kubelet/config.yaml
- [root@node ~]# systemctl restart kubelet
签发所有令牌
- [root@master ~]# kubectl certificate approve $(kubectl get csr -o name)
- certificatesigningrequest.certificates.k8s.io/csr-2hg42 approved
- certificatesigningrequest.certificates.k8s.io/csr-9gu29 approved
- ......
- [root@master ~]# kubectl get certificatesigningrequests
- NAME AGE SIGNERNAME REQUESTOR CONDITION
- csr-2hg42 14m kubernetes.io/kubelet-serving master Approved,Issued
- csr-9gu29 28s kubernetes.io/kubelet-serving node-0001 Approved,Issued
- csr-xhp83 21s kubernetes.io/kubelet-serving node-0002 Approved,Issued
- csr-69qhz 15s kubernetes.io/kubelet-serving node-0003 Approved,Issued
- csr-t8799 15s kubernetes.io/kubelet-serving node-0004 Approved,Issued
- csr-8k69w 15s kubernetes.io/kubelet-serving node-0005 Approved,Issued
- # 获取资源指标有延时,等待 15s 即可查看
- [root@master ~]# kubectl top nodes
- NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
- master 83m 4% 1789Mi 50%
- node-0001 34m 1% 747Mi 20%
- node-0002 30m 1% 894Mi 24%
- node-0003 39m 1% 930Mi 25%
- node-0004 45m 2% 896Mi 24%
- node-0005 40m 2% 1079Mi 29%
CPU资源的约束和请求以毫核(m)为单位。在k8s中1m是最小的调度单元,CPU的一个核心可以看作1000m
如果你有2颗CPU,且每CPU为4核心,那么你的CPU资源总量就是8000m
memory的约束和请求以字节为单位
可以使用以下单位来表示内存:E、P、T、G、M、K
也可以使用对应的2的幂数:Ei、Pi、Ti、Gi、Mi、Ki
- [root@master ~]# vim mylinux.yaml
- ---
- kind: Pod
- apiVersion: v1
- metadata:
- name: mylinux
- spec:
- containers:
- - name: linux
- image: myos:8.5
- command: ["awk", "BEGIN{while(1){}}"]
-
- [root@master ~]# kubectl apply -f mylinux.yaml
- pod/mylinux created
- # 查看 CPU 资源消耗
- [root@master ~]# kubectl top pods
- NAME CPU(cores) MEMORY(bytes)
- mylinux 999m 6Mi
-
- # 测试消耗内存资源
- [root@master ~]# kubectl cp memtest.py mylinux:/usr/bin/
- [root@master ~]# kubectl exec -it mylinux -- memtest.py 2500
- use memory success
- press any key to exit :
- #--------------- 在另一个终端查看------------------------
- [root@master ~]# kubectl top pods
- NAME CPU(cores) MEMORY(bytes)
- mylinux 1001m 2503Mi
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。