惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Know Your Adversary
Know Your Adversary
小众软件
小众软件
L
LangChain Blog
月光博客
月光博客
博客园 - Franky
Microsoft Azure Blog
Microsoft Azure Blog
Y
Y Combinator Blog
有赞技术团队
有赞技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
MongoDB | Blog
MongoDB | Blog
Recorded Future
Recorded Future
V
Visual Studio Blog
TaoSecurity Blog
TaoSecurity Blog
S
Schneier on Security
C
Cybersecurity and Infrastructure Security Agency CISA
P
Privacy & Cybersecurity Law Blog
T
Threat Research - Cisco Blogs
D
DataBreaches.Net
L
LINUX DO - 热门话题
C
Check Point Blog
F
Fortinet All Blogs
Hugging Face - Blog
Hugging Face - Blog
The Hacker News
The Hacker News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Microsoft Security Blog
Microsoft Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
V
V2EX
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The GitHub Blog
The GitHub Blog
P
Proofpoint News Feed
L
Lohrmann on Cybersecurity
博客园 - 司徒正美
T
Threatpost
P
Palo Alto Networks Blog
A
About on SuperTechFans
Spread Privacy
Spread Privacy
Engineering at Meta
Engineering at Meta
N
News | PayPal Newsroom
T
Tailwind CSS Blog
The Last Watchdog
The Last Watchdog
Blog — PlanetScale
Blog — PlanetScale
A
Arctic Wolf
量子位
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
博客园 - 聂微东
Google Online Security Blog
Google Online Security Blog
Google DeepMind News
Google DeepMind News
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
Vulnerabilities – Threatpost
H
Hacker News: Front Page

lvbibir's Blog

shell | 磁盘空间分析脚本 Linux cat 和 tee 命令写入文件 python | 使用 uv 管理你的 python 环境 Claude Code 完整配置指南 windows | mihomo 内核独立部署指南 linux | 磁盘扩容 wsl | 释放长久运行占用的磁盘空间 mysql | 线程上限问题处理 麒麟 7.6 安装谷歌 OTP 认证模块 suse 12sp5 升级 openssh 及 openssl suse 12sp5 部署 mysql 5.7 《谁的青春不迷茫》 docker | centos7 部署 docker vim | 基础配置和使用 windows | rime 输入法 & 雾凇方案 shell | sshpass 批量传输文件及执行命令 wsl | 原生 linux 方式安装 docker nodejs | fnm + pnpm 开发环境配置 wsl | 安装配置 miniconda 虚拟环境 wsl | 自动更新系统代理 wsl | bashrc 环境变量不正确加载的处理方法 wsl | win10 安装 wsl2 troubleshooting | ssh 成功但是 scp 失败 linux | 常用命令总结 docker 部署 piclist shell | 检测网站存活并自动钉钉告警 Zabbix | 监控端口连通性并自动追踪 TCP 路由 windows | 自定义开机快速启动项 windows | miniconda 配置 python 虚拟环境 Zabbix | 监控主机到指定 ip 的流量大小 shell | centos 初始化 loki (二) 部署 loki (一) 简介 prometheus (六) Alertmanager prometheus (五) 记录规则与告警规则 prometheus (四) 黑盒监控 prometheus (三) 服务发现 prometheus (二) 静态配置 prometheus (一) 简介及部署 traefik (四) TraefikService 服务 traefik (三) Middleware 中间件 traefik (二) ingressRoute 路由 traefik (一) 简介、部署和配置 kubernetes | Gateway API 简介及部署 linux | dns 配置文件中 search 和 options ndots 详解 kubernetes | command args 和 dockerfile 中的 ENTRYPOINT CMD kubernetes | statefulset 控制器详解 docker | dockerfile 最佳实践 docker | dockerfile 指令详解 linux | kill 命令详解以及 linux 中的信号 troubleshooting | elasticsearch 安装插件报错 shell | if 条件判断 docker | 下载外网镜像的几种方式 python | 鬼谷子数学问题 steam挂刀教程 linux | history 命令的格式化输出 shell | 将本地镜像批量推送到 harbor windows | hosts 文件修复 kubernetes | configmap & secret kubernetes | RBAC 鉴权和 NetworkPolicy kubernetes | service & ingress kubernetes | 存储 kubernetes | 滚动升级和自动伸缩 kubernetes | 控制器 kubernetes | 日志 kubernetes | 调度 kubernetes | pod linux | set 命令详解 ceph | openeuler 部署 ceph-v16 ceph | ceph-v16 离线安装解决方案 pxe 如何应对复杂的服务器硬件环境 python | 批量修改目录下文件名 《微习惯》 ceph | openeuler (aarch64) 部署 ceph-v16 troubleshooting | glibc 误升级后修复 pxe 安装配置大全 《人间失格》 跑步日常 [置顶] Hello, hugo! shell | 检索某 url 中所有文件的内容 shell | 不同执行方式的区别 shell | 开启 debug 模式 vscode | 常见问题 windows & linux 多网卡时设置默认路由以及添加静态路由 mysql (二) 主从复制原理 GTID 并行复制 mysql (一) 部署 mysql | 杂记 cicd | jenkins 部署 mall-swarm 项目 httpd 源码打包编译成 rpm 包 openssl 源码打包编译成 rpm 包 centos7 | 修改网卡名称 ceph | pool pg_num 配置 docker | 脚本方式批量导出/导入镜像 centos 密码尝试次数过多问题处理 centos7 | 升级内核至 5.10 CVE-1999-0526 ceph | centos7 部署 ceph-v12 python | 修改 pip 源 troubleshooting | 安装 cloud-init 后导致 ssh 连接失败 windows | autohotkey 常用脚本
kubernetes | 杂记
lvbibir · 2022-10-01 · via lvbibir's Blog

1 kubectl 命令的自动补全

yum install bash-completion
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)

2 镜像拉取策略

imagePullPolicy: Always|Never|IfNotPresent

3 修改 nodePort 范围

vim /etc/kubernetes/manifests/kube-apiserver.yaml
# spec.containers.command 增加
- --service-node-port-range=1-65535

3 command 和 args

containers.command 等同于 Dockerfile 中的 ENTRYPOINT

containers.args 等同于 Dockerfile 中的 CMD

如果 Dockerfile 中默认的 ENTRYPOINT 被覆盖,则默认的 CMD 指令同时也会被覆盖

4 label 标签选择运算符

官方文档

基于等值, 有三种运算符 = == !=

前两种是等效的,示例

[root@k8s-node1 ~]# kubectl get nodes -l kubernetes.io/hostname=k8s-node1
NAME        STATUS   ROLES                  AGE   VERSION
k8s-node1   Ready    control-plane,master   21d   v1.22.3
[root@k8s-node1 ~]# kubectl get nodes -l kubernetes.io/hostname!=k8s-node1
NAME        STATUS   ROLES    AGE   VERSION
k8s-node2   Ready    <none>   21d   v1.22.3
k8s-node3   Ready    <none>   21d   v1.22.3

基于集合, 同样三种运算符 in notin exists

exists 只用于判断 key 是否存在

hello in (foo, bar)    # 所有包含了 hello 标签且值等于 foo 或者 bar 的资源
hello notin (foo, bar) # 所有包含了 hello 标签且值不等于 foo 或者 bar 的资源;以及没有 hello 标签的资源
hello                  # 所有包含了 hello 标签的资源;不校验值
!hello                 # 所有未包含 hello 标签的资源;不校验值

示例

[root@k8s-node1 ~]# kubectl get nodes -l "kubernetes.io/hostname in (k8s-node1, k8s-node2)"
NAME        STATUS   ROLES                  AGE   VERSION
k8s-node1   Ready    control-plane,master   21d   v1.22.3
k8s-node2   Ready    <none>                 21d   v1.22.3
[root@k8s-node1 ~]# kubectl get nodes -l "kubernetes.io/hostname notin (k8s-node1, k8s-node2)"
NAME        STATUS   ROLES    AGE   VERSION
k8s-node3   Ready    <none>   21d   v1.22.3
[root@k8s-node1 ~]# kubectl get nodes -l kubernetes.io/hostname
NAME        STATUS   ROLES                  AGE   VERSION
k8s-node1   Ready    control-plane,master   21d   v1.22.3
k8s-node2   Ready    <none>                 21d   v1.22.3
k8s-node3   Ready    <none>                 21d   v1.22.3
[root@k8s-node1 ~]# kubectl get nodes -l \!kubernetes.io/hostname
No resources found

5 常见报错

5.1 NodeNotReady

5.1.1 Image garbage collection failed once

参考地址

报错:

# kubectl describe node k8s-node01
Events:
  Type    Reason                   Age   From     Message
  ----    ------                   ----  ----     -------
  Normal  Starting                 11m   kubelet  Starting kubelet.
  Normal  NodeHasSufficientMemory  11m   kubelet  Node k8s-node01 status is now: NodeHasSufficientMemory
  Normal  NodeHasNoDiskPressure    11m   kubelet  Node k8s-node01 status is now: NodeHasNoDiskPressure
  Normal  NodeHasSufficientPID     11m   kubelet  Node k8s-node01 status is now: NodeHasSufficientPID
  Normal  NodeAllocatableEnforced  11m   kubelet  Updated Node Allocatable limit across pods
  
# journalctl -u kubelet | grep garbage
Mar 06 09:50:33 k8s-node01 kubelet[45471]: E0306 09:50:33.106476   45471 kubelet.go:1343] "Image garbage collection failed once. Stats initialization may not have completed yet" err="failed to get imageFs info: unable to find data in memory cache"

解决:

  1. 未部署 CNI 组件

  2. docker 镜像或容器未能正确删除导致的

docker system prune
systemctl stop kubelet
systemctl stop docker
systemctl start docker
systemctl start kubelet

5.2 node 无法 ping 通 pod

所有 calico 的 pod 运行都是 running 状态, 使用 calicoctl node status 看到的网卡绑定也是没问题的.

calico 的 pod 有如下报错

[root@k8s-node1 ~]# kubectl logs calico-node-l66pn -n kube-system
2023-04-08 04:28:47.660 [INFO][65] felix/int_dataplane.go 1600: Received interface update msg=&intdataplane.ifaceUpdate{Name:"tunl0", State:"down", Index:4}
bird: Netlink: Network is down
bird: Netlink: Network is down
bird: Netlink: Network is down
bird: Netlink: Network is down

我这里是通过关闭 NetworkManager 解决的.关闭后 pod 日志立即就恢复正常了

[root@k8s-node1 ~]# systemctl stop NetworkManager
[root@k8s-node1 ~]# systemctl disable NetworkManager

5.3 虚拟机挂起导致 calico 网络不可用

出现在我的虚拟机测试集群上,挂起虚拟机过段时间后重新启动虚拟机,发现集群状态是正常的 (node 是 ready 状态),然而 calico-kube-controllers metric-server nfs-provisiner 等功能组件陷入了 CrashLoopBackOff 状态,报错基本上都是无法连接到 api-server

但是 calicoctl 看到 calico 集群是没什么问题的,之前遇到几次都是暴躁重启 docker 解决的,后面发现重启 calico 相关容器就可以了,具体原因还没找到,估计与 vmware 虚拟机挂起操作有关。

kubectl delete pods -n kube-system -l "k8s-app in (calico-node, calico-kube-controllers)"

6 kubectl 命令

一些常用命令

# 查看某个资源的详细信息
kubectl describe <type> <name> -n <namespace>
# 查看pod的日志
kubectl logs <pod> -n <namespace>
# 查看当前支持的api版本
kubectl api-versions

6.1 get

options:

-w/--watch: # 实时更新,类似tail的-f选项
-o wide: # 查看更为详细的信息,比如ip和分配的节点
-o json: # 以json格式输出
-o jsonpath='{}' # 输出指定的json内容
-l key=vaule # 根据lable筛选
--show-lables # 显示资源的所有label

示例:

# 查看所有支持的资源
kubectl api-resources
# 查看service映射的pod的端口和ip
kubectl get cp/endpoints
# 查看pod
kubectl get pod <podname> -n <namespace>
  -o jsonpath='{.metadata.uid}' # 查看pod的id
# 查看指定pod的事件
kubectl get events --field-selector involvedObject.name=demo-probes

6.1.1 获取所有 pod 的 cpu request 并排序

kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{.metadata.namespace}{"\t"}{.metadata.name}{"\t"}{.spec.containers[*].resources.requests.cpu}{"\n"}{end}' | awk -F'\t' '{            sum = 0
  split($3, containers, ",")
  for (i in containers) {
    val = containers[i]
    if (val ~ /m/) {
      sum += val + 0
    } else {
      sum += val * 1000
    }
  }
  print $1 "\t" $2 "\t" sum
}' | sort -k3 -nr | head -n 20

6.2 create

kubectl create <resource> [Options]
  --dry-run=client:  仅尝试运行,不实际运行
  -o, --output='': 输出为指定的格式

快速创建一系列资源

[root@k8s-node1 ~]# kubectl create namespace test
namespace/test created
[root@k8s-node1 ~]# kubectl create deployment my-dep --image=nginx:1.22. --replicas=3 -n test
deployment.apps/my-dep created
[root@k8s-node1 ~]# kubectl expose deployment my-dep --port=80 --target-port=8080 --type=NodePort -n test
service/my-dep exposed
[root@k8s-node1 ~]# kubectl get pods,deployment,svc -n test
NAME                          READY   STATUS    RESTARTS   AGE
pod/my-dep-5f8dfc8c78-7w5nz   1/1     Running   0          41s
pod/my-dep-5f8dfc8c78-gt65r   1/1     Running   0          41s
pod/my-dep-5f8dfc8c78-n4vjd   1/1     Running   0          41s

NAME                     READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/my-dep   3/3     3            3           41s

NAME             TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/my-dep   NodePort   10.110.205.138   <none>        80:31890/TCP   17s

6.3 expose

kubectl expose deployment my-dep --port=80 --target-port=8080 --type=NodePort -n test
# --port 表示service暴露的端口
# --target-port 表示后端镜像实际提供服务的端口

6.4 label

kubectl label nodes [node] key=value # 打lable, value可以是空
kubectl label nodes [node] key- # 删除label
kubectl get nodes -l key=value # 根据label筛选
kubectl get nodes --show-labesl # 显示资源的所有标签

6.5 run

kubectl run -it  test --image busybox --rm -- ping 10.244.107.207

7 calicoctl

下载地址

# 查看集群信息
DATASTORE_TYPE=kubernetes KUBECONFIG=~/.kube/config calicoctl get nodes

# 使用配置文件的方式
[root@k8s-node1 ~]# mkdir /etc/calico
[root@k8s-node1 ~]# cat > /etc/calico/calicoctl.cfg <<EOF
> apiVersion: projectcalico.org/v3
> kind: CalicoAPIConfig
> metadata:
> spec:
>   datastoreType: "kubernetes"
>   kubeconfig: "/root/.kube/config"
> EOF

# 查看集群信息
[root@k8s-node1 ~]# calicoctl --allow-version-mismatch get nodes
NAME
k8s-node1
k8s-node2
k8s-node3
[root@k8s-node1 ~]# calicoctl --allow-version-mismatch node status
IPv4 BGP status
+--------------+-------------------+-------+----------+-------------+
| PEER ADDRESS |     PEER TYPE     | STATE |  SINCE   |    INFO     |
+--------------+-------------------+-------+----------+-------------+
| 1.1.1.2      | node-to-node mesh | up    | 03:53:45 | Established |
| 1.1.1.3      | node-to-node mesh | up    | 03:53:51 | Established |
+--------------+-------------------+-------+----------+-------------+

8 namespace

k8s 与 docker 的 namespace 不同

docker 中的 namespace 用于容器间的资源隔离

k8s 中的 namespace 用于

  • k8s 的抽象资源间的资源隔离,比如 pods、控制器、service 等
  • 资源隔离后,对这一组资源进行权限控制

9 yaml 编写

通过创建资源获取 yaml

kubectl create deployment web --image=nginx:1.19 --dry-run=client -o yaml > deploy.yaml

通过已有资源获取 yaml

kubectl get deployment nginx-deployment -o yaml > deploy2.yaml

查看 api 中的资源及解释

kubectl explain pods.spec.container
kubectl explain deployment

yaml 报错排查

error: error parsing pod-configmap.yaml: error converting YAML to JSON: yaml: line 19: did not find expected '-' indicator

解决

由于 yaml 文件列表对齐不统一导致的

yaml 文件格式要对齐,同一级别的对象要放在同一列,几个空格不重要,不要用 tab 制表符

# 格式1
ports:
  - port: 80
  
# 格式2
ports:
- port: 80

以上.