Kubernetes - Health
Resources
基于 cgroup 技术
内存使用 Ki、Mi、Gi 来表示 KB、MB、GB;Kubernetes 里 vCPU 的最小使用单位为 0.001,即 m
nginx-pod-resources.yaml12345678910111213141516apiVersion: v1kind: Podmetadata: name: nginx-pod-resourcesspec: containers: - name: nginx image: nginx:alpine resources: requests: cpu: 10m memory: 100Mi limits: cpu: 20m memory: 200Mi
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859$ k apply -f nginx-pod-resource ...
Kubernetes - Rollout
版本
使用 Pod Template Hash 作为版本号
123456789101112$ k get deployments.apps -owideNAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTORnginx-deployment 2/2 2 2 57s nginx nginx:alpine app=nginx-deployment$ k get rs -owideNAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTORnginx-deployment-756d6b7586 2 2 2 114s nginx nginx:alpine app=nginx-deployment,pod ...
Kubernetes - StatefulSet
Stateful
任何应用都是有状态的
无状态应用:应用的状态信息不重要,即便不恢复也能正常运行,如 Nginx
有状态应用:应用的状态信息很重要,如 Redis、MySQL 等,其状态即在内存、磁盘上产生的数据
Kubernetes 认为状态不仅仅是数据持久化,还包括多实例的启动顺序、依赖关系和网络标识等
Stateless - Deployment:多个实例之间无关,启动顺序不固定,Pod 的名称、IP 地址也是完全随机
Stateful - StatefulSet:多个实例之间存在依赖关系
使用1234567$ k api-resources --api-group='apps'NAME SHORTNAMES APIVERSION NAMESPACED KINDcontrollerrevisions apps/v1 true ControllerRevisiondaemonsets ds apps/v1 true ...
Kubernetes - NFS
架构
搭建Server1$ sudo apt install nfs-kernel-server
配置 - /etc/exports
1/tmp/nfs 192.168.191.0/24(rw,sync,no_subtree_check,no_root_squash,insecure)
让配置生效
1234$ sudo exportfs -ra$ sudo exportfs -v/tmp/nfs 192.168.191.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,insecure,no_root_squash,no_all_squash)
启动 NFS Server
12345678910111213141516$ sudo systemctl start nfs-server$ sudo systemctl enable nfs-server$ sudo systemctl status nfs-server● nfs-server.service - NFS server and servi ...
Kubernetes - PersistentVolume
概述PersistentVolume
Pod 里的容器由镜像产生,镜像文件本身是只读的,进程要读写磁盘需要一个临时的存储空间
一旦 Pod 销毁,临时存储会被立即释放,数据也就丢失了
PersistentVolume
用来表示持久存储设备,隐藏了存储的底层实现
PersistentVolume 实际是一些存储设备和文件系统,如 Ceph、GlusterFS、NFS、本地磁盘
PersistentVolume 是属于集群的系统资源,是和 Node 平级的对象,Pod 只有使用权
PersistentVolumeClaim + StorageClass
PersistentVolumeClaim 和 StorageClass 是 Pod 与 PersistentVolume 之间的中间层
Pod 用 PersistentVolumeClaim 来向 Kubernetes 申请存储资源
bind:一旦资源申请成功,Kubernetes 会把 PV 和 PVC 关联在一起
StorageClass 类似于 IngressClass,抽象了特定类型的存储系统
在 PVC 和 PV 之间充当协调人的角色 ...
Kubernetes - Another example
架构
MariaDB
ConfigMap
maria-cm.yaml12345678910apiVersion: v1kind: ConfigMapmetadata: name: maria-cmdata: DATABASE: 'db' USER: 'wp' PASSWORD: '123' ROOT_PASSWORD: '123'
12$ k apply -f maria-cm.yamlconfigmap/maria-cm created
Deployment
maria-deploy.yaml1234567891011121314151617181920212223242526apiVersion: apps/v1kind: Deploymentmetadata: name: maria-deploy labels: app: maria-deployspec: replicas: 1 selector: matchLabels: app: maria-deploy tem ...
Kubernetes - Ingress
概述Service
Service 本质上是一个由 kube-proxy 控制的四层负载均衡,在 TCP/IP 协议栈转发流量
四层负载均衡,在功能上非常受限,只能依据 IP 地址和端口号做一些简单的判断和组合
Service 适合代理集群内的服务,如果要将服务暴露到集群外部,需要使用 NodePort 或者 LoadBalancer
Ingress
负责七层负载均衡
流量的总入口(南北向):扇入流量 + 扇出流量
语义:集群内外边界上的入口
Ingress Controller
类比
-
-
-
四层
Service - iptables 规则
kube-proxy
七层
Ingress - HTTP 路由规则
Ingress Controller
Service 本身没有服务能力,只是一些 iptables 规则
真正配置和应用这些 iptables 规则的是 Node 上的 kube-proxy 组件
Ingress 也只是 HTTP 路由规则的集合,相当于是一份静态描述文件
真正使得规则在集群中运行,需要 Ingress Controller,相当于 S ...
Kubernetes - Service
概述
Service 是集群内部的负载均衡机制,用于解决服务发现的关键问题
工作原理
Kubernetes 为 Service 分配一个静态 IP,由 Service 自动管理和维护动态变化的 Pod 集合
当客户端访问 Service 时,Service 会根据某种策略,将流量转发到某个 Pod
Service 使用了 iptables
每个 Node 上的 kube-proxy 自动维护 iptables 规则
负载均衡:Service 会根据 iptables 规则转发请求给它管理的多个 Pod
Service 也可以使用其它技术实现负载均衡
性能更差的 userspace
性能更好的 ipvs
YAML
Service 为 Kubernetes 的核心对象,不关联业务应用
12345678910111213141516171819$ k api-resources --api-group=""NAME SHORTNAMES APIVERSION NAMESPACED KINDbindings ...
Kubernetes - Daemonset
YAML
与 Deployment 非常类似,但缺少 replicas,因为 Daemonset 的意图是在每个 Node 上部署一个 Pod
redis-ds.yml123456789101112131415161718192021apiVersion: apps/v1kind: DaemonSetmetadata: name: redis-ds labels: app: redis-dsspec: selector: matchLabels: name: redis-ds template: metadata: labels: name: redis-ds spec: containers: - name: redis image: redis:5-alpine ports: - containerPort: 6379
使用12345678910$ k apply -f redis-ds.ymldaemonset.apps/redis-ds created$ k get ...
Kubernetes - Deployment
API1234567$ k api-resources --api-group=appsNAME SHORTNAMES APIVERSION NAMESPACED KINDcontrollerrevisions apps/v1 true ControllerRevisiondaemonsets ds apps/v1 true DaemonSetdeployments deploy apps/v1 true Deploymentreplicasets rs apps/v1 true ReplicaSetstatefulsets sts apps/v1 true StatefulSet
YAML12345678910111213141516171819202 ...