14 k8s学习笔记 - xiaoxin01/Blog GitHub Wiki

etcd

cis, raft

滚动更新

deployment滚动更新,会新建一个RS2,逐个的建立pod和删除旧的RS的pod。注意,此时旧的RS不会删除,只是标记为停用,后续可以回滚

HPA作用于RS和deployment

StatefulSet:解决有状态服务问题

  • 稳定的持久化存储,基于PVC实现
  • 稳定的网路标识,基于Headless Service实现
  • 有序部署
  • 有序删除

DaemonSet

网络

服务发现

网络通讯

  1. 同一个pod不同容器:lo/localhost:port
  2. 不同pod
    1. 在同一台机器:docker0网桥/overlay network
    2. 不在同一台机器:借助于Flannel等
  3. pod与service:各节点的iptables/lvs规则

Flannel解决方案,当不同节点的pod需要通讯时,通过Flannel再封装一层,通过node->docker->pod传递。

Flannel需要ETCD来实现功能,因为:

  1. ETCD存储Flannel可以分配的IP地址资源
  2. ETCD存储pod与node的路由表,以便让Flannel知道接收的pod在哪个node上

注:在集群中,可以直接通过pod的ip访问pod

k8s安装

软路由 koolshare

用于ssr,拉取k8s镜像等

安装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

Harbor

使用私有docker registry

cat /etc/docker/daemon.json
{
  "insecure-registries":["http://xxxxx"]
}

service

创建service

kubectl expose deployment xxx --port=30001 --target-port=80
# 查看ipvs列表
ipvsadmin -Ln

资源

# 查看支持的api版本
kubectl api-versions

# 打印资源的用法
kubectl explain [pod|xxx]

pod

生命周期

  1. init容器
    1. 在网络和数据卷初始化之后启动(这个由pause容器完成)
    2. 必须成功
    3. 顺序执行
    4. 可以用来提前执行一些操作,比如等待数据库启动完成等
  2. main容器
    1. start
    2. liveness
    3. readiness
    4. stop

三种探针:

  1. ExecAction:执行命令,0为成功
  2. TCPSocketAction:TCP端口检查
  3. HTTPGetAction:200<=response code<400

controller

  • 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

Service

RR轮询算法

  • ClusterIP
  • NodePort
  • LoadBalence:使用云服务的LB
  • ExternalName:访问集群外部的服务

api server(watch services and endpoints) -> kube-proxy -> manage iptables

VPI和Service代理

  • userspace
  • iptables
  • ipvs:在1.14版本后已经是默认模式,但需要先在节点安装ipvs模块,否则将退回iptables模式

ClusterIP

kube-proxy负责监控service,pod的变化,将信息写入到iptables中

Headless Service

NodePort

client -> node port -> kube-proxy ->

Ingress-Nginx

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

ConfigMap

  1. 以目录方式创建,key就是文件名,value就是文件内容,如果有多个文件,会合并到一个configmap中

    kubectl create configmap xxx --from-file [dir]

  2. 以文件方式创建

    kubectl create configmap xxx --from-file [file]

  3. 以键值对方式创建

    kubectl create configmap xxx --from-literal=person.name=json --from-literal=person.age=18

  4. 以声明式方式创建(yaml文件)

使用configmap:

  1. 配置环境变量
  2. 配置容器启动参数
env:
  - name: key1
    configMapKeyRef:
	  name: config-name
	  key: config-key1
  - name: key2
    configMapKeyRef:
	  name: config-name
	  key: config-key2
envFrom:
  configMapRef:
    name: config-name2
  1. 在volume中使用,将configmap的文件挂载到volume中
containers:
  - volumeMounts:
	- name: config-volume
	  path: /etc/config
volumes:
  - name: config-volume
	configMap: config-name3

ConfigMap的热更新

  • env不会自动更新
  • volume过一段时间会更新(大约10s)

secret

3种类型:

  1. Service Account
  2. Opaque(base64加密存储)
  3. kubernetes.io/dockerconfigjson(私有docker仓库认证信息)

volumne

persistentVolume

将released状态的PV的claimRef节点删除,即可将PV状态变为available

dig工具检测dns服务

scheduler

先排除,再优选

控制pod的调度

assign-pod-node

三种方法:

  1. nodeSelector
  2. Affinity and anti-affinity
    1. Node affinity:控制pod和node的亲和性
    2. Inter-pod affinity and anti-affinity:控制pod和pod之间的亲和性
  3. Taints and Tolerations(污点和容忍)

集群安全

api-server authentication:

  1. http token auth:检查http header
  2. http base auth:用户名密码
  3. 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

RBAC, 56

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

https://helm.sh

https://hub.helm.sh

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

集群的容器镜像

镜像

init容器

Init 容器

⚠️ **GitHub.com Fallback** ⚠️