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之間要有一個英文空格
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)
A?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的labelapp: nginxtemplate: ##用于定義Pod的模板metadata:name: nginxlabels:app: nginxspec:containers:- name: nginximage: nginxports:- containerPort: 80kubectl 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的功能更加強大
(3)Deployments
?
我們一般不會直接使用ReplicaSet來管理Pod,一般使用Deployment來操作ReplicaSet,從而管理Pod
?
查看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創建,如下:
在指定的命名空間下創建nginx-pod
apiVersion: v1 kind: Pod metadata:name: nginx-podnamespace: cppns spec:containers:- name: nginx-containerimage: nginxports:- containerPort: 80查看cppns命名空間下的Pod和資源?
kubectl?get?pods?-n?myns
kubectl?get?all?-n?myns
總結
以上是生活随笔為你收集整理的kubernetes(二)k8s组件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 分布式一致性算法Raft简介(下)
- 下一篇: kubernetes(三)k8s中通信和