初步了解
啥也不说了,看这个视频,迄今为止看到的对Kubernetes最简单清晰的解释。
Kubenetes的components和concepts需要看一下,了解Kubernetes的基本概念和整体架构。
Pods and Controllers. Pod是Kubernetes内最基础的工作单元,拥有唯一的IP/Port或者volumes。通常一个Pod运行一个app container,有的需要运行多个container,或者先运行init container运行启动app container前的先行辅助工作。如果是需要Pod协作比如rollout,那就需要controller。常见的controller为StatefulSet, DaemonSet, Deployment, ReplicaSet等。
Kubenetes的container runtimes,不只是Docker,还有CRI-O(lightweight container runtime for Kubernetes),containerd等。这里谈到了CRI-O/Kubernetes的架构,涉及到了几个概念CNCF(Cloud Native Computing Foundation), CRI(Container Runtime Interface), CNI(Container Network Interface), OCI(Open Container Initiative)。runc为OCI-compliant runtime,除了runc还有Kata Containers。Minikube uses CRI-O as the container runtime。
搭建
如果想快速体验kubernetes,可以用minikube,看这里。如果是生产环境,参考官方文档Building High-Availability Clusters,build large clusters
Kubernetes v1.18 supports clusters with up to 5000 nodes, 150000 pods, 300000 containers, 100 pods per node。
Etcd
说到Kubernetes不得不说etcd,一个由CoreOS团队发起的分布式K/V数据库,kubernetes的configuration data, state data, and metadata都是由etcd来存储,etcd一般放在master,或者单独的节点。移步这里了解更多etcd,包括etcd vs redis, etcd vs zookeeper等
Route
DNS server + kube-proxy: Pod1 + Pod2 … –> service, a service has virtual IP (cluster IP of the service), port and internal domain name (my-svc.my-namespace.svc.cluster-domain.example, dns looking up my-svc.my-namespace works).
ServiceTypes: service — http/https routes, ClusterIP (internal IP, by default), NodePort (requesting NodeIP:NodePort from outside cluster), LoadBalancer (using a cloud provider’s load balancer), ExternalName (CNAME)
Ingress: service — http/https routes, ingress exposes http/https routes from outside the cluster to services within the cluster.
另一篇文章Kubernetes Network进一步介绍了Kubernetes的网络模型。
Storage
PV/PVC是Kubernetes storage的核心概念。PV是volume资源,由admin来provision,PVC是利用PV的声明,表示需要多少volume。



PV可以手动分配,也可以通过StorageClass自动分配。Kubernetes内置了很多StorageClass,也可以用第三方的StorageClass。


Configuration
Kubernetes推荐用YAML写配置,尽量考虑写在一个文件,参考guestbook-all-in-one.yaml。用Deployment ReplicaSets Service,不要用naked nodes。ConfigMap和Secret是两个协助配置的API Object。
ConfigMap
ConfigMap可以配置成环境变量,CLI的参数变量,以及Volume中的配置文件。


container的/config/路径下会有4个文件:

Secret
Kubernetes Secret用来保存管理敏感信息如密码,token,ssh key等。Pod是怎么使用secrets的呢,两种途径:一是以文件形式存在于Volume,使用时被mount到container的某个路径,另一种是kubelet pull image时使用。官方有比较好的使用例子。
注意的是,如果用yaml文件创建secret,可以指定data或者stringData两种map方式。如果是用data类型,那么字符串需要用base64加密。加密解密可以用如下命令行:
$echo -n ‘admin’ | base64
$echo ‘YWRtaW4=’ | base64 –decode
Resources
计算资源的请求和限定,可以这样定义
spec.containers[].resources.limits.cpu
spec.containers[].resources.limits.memory
spec.containers[].resources.limits.hugepages-<size>
spec.containers[].resources.requests.cpu
spec.containers[].resources.requests.memory
spec.containers[].resources.requests.hugepages-<size>
kubernetes也定义了CPU和Memory的单位。One cpu, in Kubernetes, is equivalent to 1 vCPU/Core for cloud providers and 1 hyperthread on bare-metal Intel processors。vCPU即Virtual CPU,是CPU资源的分割单元,不同的云厂商会有不同的分割规则,一般是小于1Core。hyperthread即为超线程,Intel提出的技术,相当于虚拟出来的cpu资源,比如物理CPU有4Core,使用HT技术可以虚拟出8Core。cpu: 0.1 和 cpu: 100m是等同的。接下来的例子是,为这个pod申请0.25cpu,64Mib内存,最大限制是0.5cpu,128Mib内存。

LimitRange
管理员可以为某个namespace中的Pod/container资源占用进行限制,或者设置默认的资源分配(适用于Pod的定义没有指定资源)。这里有很多例子。


ResourceQuota
ResourceQuota是给namespace限定的资源上限,超出上限会报403错误。这里的资源可以是cpu, memory,也可以是storage,甚至Object的个数,比如service, ConfigMap等。如果指定了requests.cpu/requests.memory,那么需要这个namespace的Pod等Object有明确的request定义,limits.cpu/limits.memory相同的要求。了解更多ResourceQuota,看这里。


Security
Controlling access to the Kubernetes API
Overview of cloud native security
Logging
Metrics
Service Catalog
Kubernetes支持安装Service Catalog.
Kubernetes API
Kubernetes API Doc
Kubernetes API Conventions
Example
WordPress + MySQL
PHP Guestbook + Redis
需要注意的是,在WordPress例子中用到了StatefulSet API Object。StatefulSet用来管理stateful应用。Pod的dns不会变,即使重新创建,而且DNS内含一个序号。Pod如果绑定PVC,那么PVC将和这个Pod DNS永久绑定。
参考
架构总览
网络总览
4种网络通信
IBM对Kubernetes的介绍
开发辅助工具
进一步了解kubernetes,参考官方文档,或者IBM的系列文章。