kubernetes(二)k8s组件

基本概念

Pod

A Pod (as in a pod of whales or pea pod) is a group of one or more containers (such as Docker containers), with shared storage/network, and a specification for how to run the containers

Pod就是一个或多个Container的组合,一个pod里的container共享存储和网络

Pod是k8s中操作的最小单位

ReplicaSet

A ReplicaSet is defined with fields, including a selector that specifies how to identify Pods it can acquire, a number of replicas indicating how many Pods it should be maintaining, and a pod template specifying the data of new Pods it should create to meet the number of replicas criteria. 

k8s中通过ReplicaSet来管理多个Pod
通过pod template创建多个pod副本,然后让ReplicaSet统一管理

Deployment

A Deployment controller provides declarative updates for Pods and ReplicaSets.
You describe a desired state in a Deployment, and the Deployment controller changes the actual state to the desired state at a controlled rate. You can define Deployments to create new ReplicaSets, or to remove existing Deployments and adopt all their resources with new Deployments.

在ReplicaSet之上又通过Deployment为Pod和Replica Set提供声明式更新。我们只需要在 Deployment 中描述想要的目标状态是什么,Deployment controller 就会帮我们将 Pod 和ReplicaSet 的实际状态改变到想要的目标状态。
eg.更新pod中的镜像版本

Label

这么多的Pod,ReplicaSet在管理的时候需要选择具有一致性的Pod,这样管理起来更加方便,这就需要给每个pod一个label

Labels are key/value pairs that are attached to objects, such as pods. 

Service

Pod具有短暂性,每次重启Pod之后其ip都会变化,这样不利于部署时查找到对应的服务

An abstract way to expose an application running on a set of Pods as a network service.
With Kubernetes you don’t need to modify your application to use an unfamiliar service discovery mechanism. Kubernetes gives Pods their own IP addresses and a single DNS name for a set of Pods, and can load-balance across them

Service是定义一系列Pod以及访问这些Pod的策略的一层抽象,可以通过Service映射的地址找到对应的Pods,并且Service会对一组相同label的pods做负载均衡

Node

A node is a worker machine in Kubernetes, previously known as a minion. A node may be a VM or physical machine, depending on the cluster. Each node contains the services necessary to run pods and is managed by the master components

一台运行k8s的机器,称之为一个Node

上述组件之间的关系如图:

集群下的k8s组件

1. kubectl  操作集群的客户端
2. 认证模块 校验请求的合法性
3. APIServer  Master Node中用来接收客户端的请求
4. Scheduler   调度策略  API接收请求之后,用来判断接下来调用哪个Worker Node来创建Pod,Container之类的
5. Controller Manager  分发请求到不同的Worker Node上创建内容
6.  Kubelet  Worker Node接收到请求之后,由kubelet服务负责执行
7. 如果涉及域名解析  DNS
8. 需要有监控面板监测整个集群的状态 Dashboard 
9. 保存集群中的数据  ETCD

之前k8s集群搭建的时候我们就可以看到上述组件的image

架构图如下图:

组件的操作

k8s里所有的组件都是通过yaml文件来进行部署启动的

yaml文件

1.区分大小写;
2.缩进表示层级关系,相同层级的元素左对齐
3.缩进只能使用空格,不能使用TAB
4."#"表示当前行的注释
5.可以和json相互转换
6.---表示分隔符,可以在一个文件中定义多个结构(Pod,Service等)
7.使用key: value,其中":"和value之间要有一个英文空格

# yaml对于Pod的定义:
apiVersion: v1          #必写,版本号
kind: Pod               #必写,类型,eg.Pod,Deployment等
metadata:               #必写,元数据
  name: nginx-pod       #必写,pod名称
  namespace: default    #表示pod名称属于的命名空间
  labels:
    app: nginx                  #自定义标签名字
spec:                           #必写,pod中容器的详细定义
  containers:                   #必写,pod中容器列表
  - name: nginx-container       #必写,容器名称
    image: nginx                #必写,容器的镜像名称
    ports:
    - containerPort: 80         #表示容器的端口

1.Pods

根据该yaml创建一个pod
kubectl apply -f nginx_pod.yaml

查看pod详情
kubectl get pods
kubectl get pods -o wide
kubectl describe pod nginx-pod

发现pod运行在w1节点,可以使用docker的相关命令进入对应的容器中,可以看到确实有对应的容器信息

2.Controllers

(1)ReplicationController(RC)

ReplicationController ensures that a specified number of pod replicas are running at any one time. In other words, a ReplicationController makes sure that a pod or a homogeneous set of pods is always up and available

RC保证指定Pod的副本数量在任意时刻都符合我们的预期值,如果Pod停止,会自动帮我们维护Pod数量,实现了集群中Pod的高可用

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx       ##RC的name
spec:
  replicas: 3       ##表示此RC管理的Pod需要运行的副本数
  selector:         ##表示需要管理的Pod的label
    app: nginx
  template:         ##用于定义Pod的模板
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80

kubectl apply -f nginx_replication.yaml

kubectl get pods -o wide

删除一个pod之后,RC会自动帮我们生成新的pod加入到里面

kubectl delete pods nginx-zzwzl

我们可以通过RC对pod进行动态扩缩容

kubectl scale rc nginx --replicas=5

不过在学习过程中发现一个很奇怪的现象:

我先启动一个Nginx Pod节点,然后再启动一个包含3个副本的RC,最后k8s集群上只会有3个节点??

(2)ReplicaSet(RS)

在Kubernetes v1.2时,RC就升级成了Replica Set,两者没有本质的区别,kubectl中绝大部分作用于RC的命令同样适用于RS
RS与RC唯一的区别是:RS支持基于集合的Label Selector(set-based selector),而RC只支持基于等式的Label Selector,Replica Set的功能更加强大

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: frontend
  labels:
    app: guestbook
    tier: frontend
spec:
  # modify replicas according to your case
  replicas: 3
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      labels:
        tier: frontend
...

(3)Deployments
 

我们一般不会直接使用ReplicaSet来管理Pod,一般使用Deployment来操作ReplicaSet,从而管理Pod
 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

查看pod:

kubectl get pods -o wide
kubectl get deployment
kubectl get rs
kubectl get deployment -o wide

前面说过deployment可以统一管理Pod,更新部署的pod 里的nginx版本:
kubectl set image deployment nginx-deployment nginx=nginx:1.9.1

Namespace

kubectl get pods
kubectl get pods -n kube-system


可以看到不同的namespace下运行的pod不一样,Pod分别属于不同的Namespace

查看当前centos的namespace
kubectl get namespaces/ns

可以通过namespace来隔离不同的资源,在输入命令的时候指定命名空间"-n",如果不指定,则使用默认的命名空间default
Namespace也是k8s中的资源组件,也可以通过yaml创建,如下:

#创建一个cppns的命名空间
apiVersion: v1
kind: Namespace
metadata:
  name: cppns

在指定的命名空间下创建nginx-pod

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  namespace: cppns
spec:
  containers:
  - name: nginx-container
    image: nginx
    ports:
    - containerPort: 80

查看cppns命名空间下的Pod和资源 
kubectl get pods -n myns
kubectl get all -n myns

发布了47 篇原创文章 · 获赞 12 · 访问量 5087

猜你喜欢

转载自blog.csdn.net/qq_35448165/article/details/103242834