Kubernetes中的基本概念(二)
对象是抽象的实例化。kubenetes是对”service“的多层抽象。
kubenetes中的对象
命名空间
用来隔离不同team或者工程中的资源。 相同命名空间中的资源通过labels来区分。
当创建一个service时,dns记录中默认使用命名空间来作为空间内各种服务的root。
类似这种格式
<service-name>.<namespace-name>.svc.cluster.local
labels 和 selectors
labels 最主要的用途是组织资源(subsets).
它可以让使用者,以一个非常松散的方式,把组织架构映射到k8s的对象上。
比如
"release" : "stable", "release" : "canary"
"environment" : "dev", "environment" : "qa", "environment" : "production"
"tier" : "frontend", "tier" : "backend", "tier" : "cache"
"partition" : "customerA", "partition" : "customerB"
"track" : "daily", "track" : "weekly"
selectors 选择器一般用来匹配比它底层次的抽象资源。
比如, ingress 通过selectors来匹配service。
service 通过选择器来匹配pods。
pods通过选择器来匹配nodes。
也经常用来相同层次下,资源交叉使用的情况。
在api查询中,也可以作为条件使用
kubectl get pods -l environment=production,tier=frontend
对象创建
对象创建有两种方式
- 使用api
kubectl run nginx –image nginx 或者
kubectl create deployment nginx --image nginx
- 使用yaml描述文件
k create -f nginx.yaml k delete -f nginx.yaml -f redis.yaml
架构(architecture of k8s)
Node
地址(address)
- hostName
node节点的hostname, cat /etc/hosts
- ExternalIP
集群外的IP, 被外部路由器管理
- InternalIP
被集群内部路由表管理
状态(Condition)
节点状态
OutOfDisk、Ready、MemoryPressure、PIDPressure、DiskPressure、NetworkUnavailable
注: 对k8s来说,硬件的状态被抽象为内存、磁盘、PID(进程资源)、网络的状态。
容量(Capacity)
描述资源(CPU,Memory,pods的数量)的数量。
基本信息(Info)
内核版本、k8s(kubelet,kubeproxy)的版本、docker的版本、os的基本信息。
管理(Node Controller)
node作为计算资源,比如 aws的EC2, k8s的cluster服务构建在node层面之上。
- 节点的注册
--kubeconfig - Path to credentials to authenticate itself to the apiserver.
--cloud-provider - How to talk to a cloud provider to read metadata about itself.
--register-node - Automatically register with the API server.
--register-with-taints - Register the node with the given list of taints (comma separated <key>=<value>:<effect>). No-op if register-node is false.
--node-ip - IP address of the node.
--node-labels - Labels to add when registering the node in the cluster (see label restrictions enforced by the NodeRestriction admission plugin in 1.13+).
--node-status-update-frequency - Specifies how often kubelet posts node status to master.
节点之间的通信
-
获取pods的日志
-
为pods扩容
-
为kublet提供端口转发功能
容器(更新 2018/05/10)
镜像更新策略
默认策略 IfNotPresent, 如果本地存在,则不更新。
强制更新: imagePullPolicy: Always
, 缺省情况使用latest tag
构建多层架构镜像
(更新 2018/05/10)
~/.docker/config.json ,添加 "experimental": "enabled"
, 开启试验feature。
使用私有仓库
国内推荐阿里云的CR
docker login –username=account~sample@qq.com registry.cn-beijing.aliyuncs.com
容器运行环境(container environment)
运行时会被注入一些环境变量, 比如 FOO_SERVICE_HOST, FOO_SERVICE_PORT等,这些可以直接在应用中通过env环境变量读取。
容器生命周期hooks
可以订阅这两个消息, PostStart 和 PreStop
hook handler 有两种实现方式
-
pre-stop.sh shell形式
-
HTTP 请求