k8s的核心概念

前言

k8s是一个基于容器的自动化的容器编排平台,负责应用的部署、弹性、管理

核心功能
  • 服务发现与负载均衡
  • 容器自动装箱
  • 存储编排
  • 自动容器恢复
  • 自动发布与回滚
  • 配置与密文管理
  • 批量执行
  • 水平伸缩
核心架构

k8s的核心概念_第1张图片

k8s之master架构

k8s的核心概念_第2张图片

  • API Server
    处理api操作,k8s中所有的组件都会和API Server进行连接,组件和组件之间,一般不进行独立的连接,都依赖于API Server进行消息的传递

  • Controller
    控制器,用来完成对集群状态的一些管理

  • Scheduler
    调度器,完成调度的操作

  • Etcd
    分布式存储系统,API Server中这些所需要的原信息都被放置在etcd中,其本身是一个高可用系统,通过etcd保证整个k8s的master组件的高可用性

k8s之Node架构

k8s的核心概念_第3张图片

真正运行业务负载的地方,每个业务负载会以Pod的形式运行;一个Pod中运行的一个或多个容器,真正去运行这些Pod的组件的为kubelet,kubelet也为Node上最为关键的组件,通过API Server接收到所需要Pod运行的状态,然后提交到Container Runtime组件中
在OS上创建容器所需要的运行环境,把容器或者Pod运行起来,k8s不会直接运行网络存储的操作,会靠Storage Plugin或者Network Plugin来进行操作
在k8s自己的环境中,也会有Network,提供Service network来进行搭网、组网,其组件为Kube-proxy,利用iptable的能力来进行组建k8s的Network,即为cluster network

k8s架构解析

k8s的核心概念_第4张图片

用户可以通过UI或者CLI提交一个Pod给k8s进行部署,这个Pod请求首先会通过CLI或者UI提交给k8s API Server,下一步API Server会把这个信息写入到他的存储系统etcd,之后Scheduler会通过API Server的watch或者叫做notification机制得到这个信息: 有一个Pod需要被调度

此时Scheduler会根据他的内存状态进行一次调度决策,在完成这次调度之后,回向API Server report说:“OK! 这个Pod需要被调度到某一个节点上”

此时API Server接收到这次操作之后,会把这次的结果再次写到etcd中,然后API Server会通知相应的节点进行这次Pod真正的执行启动,相应节点的kubelet会得到这个通知,kubelet就回去调Container runtime来真正去启动配置这个容器和这个容器的运行环境,去调度Storage Plugin来配置存储,Network Plugin去配置网络

Pod
  • 最小的调度及资源单元
  • 有一个或者多个容器组成
  • 定义容器的运行方式(Command、环境变量等)
  • 提供给容器共享的运行环境(网络、进程空间)
Volume

卷/抽象的概念,用来管理k8s存储,用来声明在Pod中的容器可以访问文件目录,一个卷可以被挂载在Pod中一个或者多个容器的指定路径下面,一个Volume可以支持多种的后端存储,eg:本地存储、ceph、GlusterFS

  • 声明Pod中的容器可访问的文件目录
  • 可以被挂载在Pod中一个(或者多个)容器的指定路径下
  • 支持多种后端存储的抽象(eg: 本地存储、ceph、GlusterFS)
Deployment
  • 定义一组Pod的副本数目、版本等
  • 通过控制器(Controller)维持Pod的数目(自动恢复失败的Pod)
  • 通过控制器以指定的策略控制版本(滚动升级、重新生成、回滚等)
Service
  • 提供访问一个或者多个Pod实例的稳定访问地址
  • 支持多种访问方式实现(ClusterIP、NodePort、LoadBalancer)
Namespace
  • 一个集群内部的逻辑隔离机制(鉴权、资源额度管理)
  • 每个资源都属于一个Namespace
  • 同一个Namespace中的资源命名唯一
  • 不同Namespace中的资源可重名
升级、回滚示例
部署nginx
]# cat > nginx.yaml <<-EOF
apiVersion: apps/v1
kind: Deployment
metadata: 
  name: nginx-deployment
  namespace: default
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.7.9
          ports:
          - containerPort: 80
EOF          
]# kubectl apply -f nginx.yaml

查看deployment变更情况

kubectl get --watch deployment

查看deployment的详细信息

]# kubectl describe deployment nginx-deployment
Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Tue, 12 Jan 2021 12:08:52 +0800
Labels:                 
Annotations:            deployment.kubernetes.io/revision: 1
                        kubectl.kubernetes.io/last-applied-configuration:
                          {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"name":"nginx-deployment","namespace":"cj-ops"},"spec":{"replicas...
Selector:               app=nginx
Replicas:               2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx:1.7.9
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  
    Mounts:       
  Volumes:        
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  
NewReplicaSet:   nginx-deployment-6dd86d77d (2/2 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  45m   deployment-controller  Scaled up replica set nginx-deployment-6dd86d77d to 2
升级nginx
]# cat > nginx_update.yaml <<-EOF
apiVersion: apps/v1
kind: Deployment
metadata: 
  name: nginx-deployment
  namespace: default
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.8	# update nginx version
          ports:
          - containerPort: 80
EOF          
]# kubectl apply -f nginx_update.yaml

查看deployment的详细信息

]# kubectl describe deployment nginx-deployment
Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Tue, 12 Jan 2021 12:08:52 +0800
Labels:                 
Annotations:            deployment.kubernetes.io/revision: 2
                        kubectl.kubernetes.io/last-applied-configuration:
                          {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"name":"nginx-deployment","namespace":"cj-ops"},"spec":{"replicas...
Selector:               app=nginx
Replicas:               2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx:1.8	# succes update nginx version
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  
    Mounts:       
  Volumes:        
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  
NewReplicaSet:   nginx-deployment-7c96f597c8 (2/2 replicas created)
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  50m    deployment-controller  Scaled up replica set nginx-deployment-6dd86d77d to 2
  Normal  ScalingReplicaSet  2m38s  deployment-controller  Scaled up replica set nginx-deployment-7c96f597c8 to 1
  Normal  ScalingReplicaSet  2m24s  deployment-controller  Scaled down replica set nginx-deployment-6dd86d77d to 1
  Normal  ScalingReplicaSet  2m24s  deployment-controller  Scaled up replica set nginx-deployment-7c96f597c8 to 2
  Normal  ScalingReplicaSet  2m9s   deployment-controller  Scaled down replica set nginx-deployment-6dd86d77d to 0
水平扩张
]# cat > nginx_scale.yaml <<-EOF
apiVersion: apps/v1
kind: Deployment
metadata: 
  name: nginx-deployment
  namespace: default
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 4	# scale 2 to 4
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.8
          ports:
          - containerPort: 80
EOF          
]# kubectl apply -f nginx_update.yaml

查看deployment的详细信息

]# kubectl describe deployment nginx-deployment
Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Tue, 12 Jan 2021 12:08:52 +0800
Labels:                 
Annotations:            deployment.kubernetes.io/revision: 2
                        kubectl.kubernetes.io/last-applied-configuration:
                          {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"name":"nginx-deployment","namespace":"cj-ops"},"spec":{"replicas...
Selector:               app=nginx
Replicas:               4 desired | 4 updated | 4 total | 4 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx:1.8
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  
    Mounts:       
  Volumes:        
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Progressing    True    NewReplicaSetAvailable
  Available      True    MinimumReplicasAvailable
OldReplicaSets:  
NewReplicaSet:   nginx-deployment-7c96f597c8 (4/4 replicas created)
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  41m    deployment-controller  Scaled up replica set nginx-deployment-7c96f597c8 to 1
  Normal  ScalingReplicaSet  41m    deployment-controller  Scaled down replica set nginx-deployment-6dd86d77d to 1
  Normal  ScalingReplicaSet  41m    deployment-controller  Scaled up replica set nginx-deployment-7c96f597c8 to 2
  Normal  ScalingReplicaSet  40m    deployment-controller  Scaled down replica set nginx-deployment-6dd86d77d to 0
  Normal  ScalingReplicaSet  2m59s  deployment-controller  Scaled up replica set nginx-deployment-7c96f597c8 to 4
结语

… …

你可能感兴趣的:(k8s,devops)