14 k8s学习笔记 - xiaoxin01/Blog GitHub Wiki
cis, raft
deployment滚动更新,会新建一个RS2,逐个的建立pod和删除旧的RS的pod。注意,此时旧的RS不会删除,只是标记为停用,后续可以回滚
HPA作用于RS和deployment
- 稳定的持久化存储,基于PVC实现
- 稳定的网路标识,基于Headless Service实现
- 有序部署
- 有序删除
- 同一个pod不同容器:lo/localhost:port
- 不同pod
- 在同一台机器:docker0网桥/overlay network
- 不在同一台机器:借助于Flannel等
- pod与service:各节点的iptables/lvs规则
Flannel解决方案,当不同节点的pod需要通讯时,通过Flannel再封装一层,通过node->docker->pod传递。
Flannel需要ETCD来实现功能,因为:
- ETCD存储Flannel可以分配的IP地址资源
- ETCD存储pod与node的路由表,以便让Flannel知道接收的pod在哪个node上
注:在集群中,可以直接通过pod的ip访问pod
用于ssr,拉取k8s镜像等
加载docker镜像脚本
ls /kubeadmin-basic.images > /tmp/image-list.txt
for i in $(cat /tmp/image-list.txt)
do
# 加载打包好的镜像文件
docker load -i $i
done
监控pod
kubectl get pod -w
使用私有docker registry
cat /etc/docker/daemon.json
{
"insecure-registries":["http://xxxxx"]
}
创建service
kubectl expose deployment xxx --port=30001 --target-port=80
# 查看ipvs列表
ipvsadmin -Ln
# 查看支持的api版本
kubectl api-versions
# 打印资源的用法
kubectl explain [pod|xxx]
- init容器
- 在网络和数据卷初始化之后启动(这个由pause容器完成)
- 必须成功
- 顺序执行
- 可以用来提前执行一些操作,比如等待数据库启动完成等
- main容器
- start
- liveness
- readiness
- stop
三种探针:
- ExecAction:执行命令,0为成功
- TCPSocketAction:TCP端口检查
- HTTPGetAction:200<=response code<400
- ReplicationController和ReplicaSet
- Deployment
- DaemonSet:确保全部/一些node上运行一个pod的副本
- 运行集群存储,如ceph,glusterd
- 在node上进行日志收集,如fluentd,logstash
- 在每个node上运行监控daemon,如prometheus
- Job
- CronJob
- StatefulSet:解决有状态服务的问题
- 稳定的持久化存储,基于PVC
- 稳定的网络标识,基于Headless Service
- 有序部署,有序扩展,基于init containers来实现
- 有序收缩,有序删除
- Horizetal Pod Autoscaling(HPA)
问题:kubectl scale deployment xxx --replica=3,没有改变rs,改变的是deployment中的replica?HPA如何协同工作的?
更新镜像:kubectl set image xxx xxx
# 可以通过 $? 获取下面命令的返回值,0为成功
kubectl rollout status
kubectl rollout history
kubectl rollout undo deployment xxx --to-version=2
28
RR轮询算法
- ClusterIP
- NodePort
- LoadBalence:使用云服务的LB
- ExternalName:访问集群外部的服务
api server(watch services and endpoints) -> kube-proxy -> manage iptables
- userspace
- iptables
- ipvs:在1.14版本后已经是默认模式,但需要先在节点安装ipvs模块,否则将退回iptables模式
kube-proxy负责监控service,pod的变化,将信息写入到iptables中
Headless Service
client -> node port -> kube-proxy ->
api-server -> Store(协程) -> updateChannel -> NginxController -> syncQueue -> SyncQueue(协程) -> 需要reload/不需要reload
问题:ingress-nginx部署以后,会占用node的80,443 port?还是
可以利用apache的httpd创建basic auth文件,放入secret,然后加入ingress-nginx以实现认证的功能
- configMap
- secret
- volume
- presistent volume
-
以目录方式创建,key就是文件名,value就是文件内容,如果有多个文件,会合并到一个configmap中
kubectl create configmap xxx --from-file [dir]
-
以文件方式创建
kubectl create configmap xxx --from-file [file]
-
以键值对方式创建
kubectl create configmap xxx --from-literal=person.name=json --from-literal=person.age=18
-
以声明式方式创建(yaml文件)
使用configmap:
- 配置环境变量
- 配置容器启动参数
env:
- name: key1
configMapKeyRef:
name: config-name
key: config-key1
- name: key2
configMapKeyRef:
name: config-name
key: config-key2
envFrom:
configMapRef:
name: config-name2
- 在volume中使用,将configmap的文件挂载到volume中
containers:
- volumeMounts:
- name: config-volume
path: /etc/config
volumes:
- name: config-volume
configMap: config-name3
- env不会自动更新
- volume过一段时间会更新(大约10s)
3种类型:
- Service Account
- Opaque(base64加密存储)
- kubernetes.io/dockerconfigjson(私有docker仓库认证信息)
将released状态的PV的claimRef节点删除,即可将PV状态变为available
dig工具检测dns服务
先排除,再优选
三种方法:
- nodeSelector
- Affinity and anti-affinity
- Node affinity:控制pod和node的亲和性
- Inter-pod affinity and anti-affinity:控制pod和pod之间的亲和性
- Taints and Tolerations(污点和容忍)
api-server authentication:
- http token auth:检查http header
- http base auth:用户名密码
- http certification auth
- pod -> service account -> service-account-token -> api-server
- scheduler/controller -> api-server(127.0.0.1)
- kubelet/kubectl/kube-proxy -> certification -> kubeconfig -> api-server
api-server会把client证书的CN字段作为user,names.O字段作为group
- Role/ClusterRole
- RoleBinding/CusterRoneBinding
- Resources
- get /api/vi/namespaces/{namespace}/pods/{pod}/log
- resources: ["pods", "pods/log"]
cfssl/cfssljson可以根据json格式的文件生成证书
helm client --(gRPC)--> Tiller -> api-server
可以用helm创建自己的chart
helm fetch下载chart包到本地
helm安装dashboard管理k8s集群
通过fluentd收集集群日志到ELK
可以永久保存该上下文中所有后续 kubectl 命令使用的命名空间。
kubectl config set-context --current --namespace=<insert-namespace-name-here>
# Validate it
kubectl config view | grep namespace:
显示额外栏位值:
kubectl get pods -Lapp -Ltier -Lrole
按栏位值过滤:
kubectl get pods -lapp=guestbook,role=slave
批量更新标签(把app=nginx的pod添加新的标签tier=fe):
kubectl label pods -l app=nginx tier=fe