k8s tutorial - Learn Kubernetes Basics

流程匯總

  1. 創建cluster
  2. 在cluster上創建一個deployment,然后再部署對應的APP(因為創建deployment時會指定image)
  3. 當創建了Deployment之后,系統會創建對應的pod,視為一組緊密耦合的APP的邏輯主機
  4. 將pod視為一個邏輯主機之后,就很清晰了,kubectl提供了一系列接口以和pod進行通信以及運行APP(這里需要注意Pod和Node的區別)
  5. pod因為只能運行在單個Node上,所以是很脆弱的,因此需要在此基礎上創建service;同時,service使得APP能夠expose到外部網絡)

Learn Kubernetes Basics -- Create a Cluster

Kubernetes automates the distribution and scheduling of application containers across a cluster in a more efficient way.

核心理念:使應用程序容器化,這樣可以使得程序和主機解耦,程序不需要綁定到特定主機,而是通過k8s在cluster上進行調度編排。

cluster的核心組件有兩個:

  • Control Plane:對cluster進行協調;
  • nodes:實際運行程序的worker;(A node is a VM or a physical computer that serves as a worker machine in a Kubernetes cluster)
Cluster Diagram

每個node都需要有一個kubelet,kubelet是node的代理,用來管理node,以及和Control Plane進行通信。

To get started with Kubernetes development, you can use Minikube. Minikube is a lightweight Kubernetes implementation that creates a VM on your local machine and deploys a simple cluster containing only one node.

使用minikube流程:

$ minikube start # minikube啟動一個VM,并創建一個cluster
$ kubectl cluster-info # 查看當前cluster的info
$ kubectl get nodes # 查看當前cluster中的各個nodes

Learn Kubernetes Basics -- Deploy an APP

Cluster

The Deployment instructs Kubernetes how to create and update instances of your application. (除此以外,Deployment為k8s提供了自愈機制,當部署了某個容器化應用的Node掛了之后,Deployment會對Node進行替換)

When you create a Deployment, you'll need to specify the container image for your application and the number of replicas that you want to run.(再創建Deployment的時候,需要指定APP的鏡像,以及需要運行多少個replicate)

A Pod is the basic execution unit of a Kubernetes application. Each Pod represents a part of a workload that is running on your cluster.

Deployment創建成功
  • kubectl create deployment <名稱> --image=<鏡像名>:通過創建一個deployment來部署對應的應用
  • kubectl get deployments: 列出當前的所有deployment
  • kubectl proxy: 創建一個代理,用于在客戶端host和cluster之間進行通信(因為Deployment以及APP是在cluster內部的,默認情況下cluster內部的東西和外部網絡是隔絕的,因此外部需要一個代理能夠將數據發到cluster內部,即和Pods進行通信)

Learn Kubernetes Basics -- Explore Your App

當創建了deployment之后,K8S會創建一個pod,作為各個緊密耦合的APP的邏輯主機。

A Pod is a Kubernetes abstraction that represents a group of one or more application containers (such as Docker), and some shared resources for those containers.(可以把pods視為一個邏輯主機,其可以包括多個容器)

The containers in a Pod share an IP Address and port space, are always co-located and co-scheduled, and run in a shared context on the same Node.(作為一個邏輯主機,pod擁有自己的IP和端口,主機內部的各個容器應用協同工作,緊密耦合)

Pods Overview

前面已經說過,Nodes可以是物理機或者VM。而一個Node上面可以運行多個Pods。Pods是一個抽象的概念,用以聚類多個容器及共享資源;一個pod只能運行在一個Node上面。一個Node必須包含以下兩個要素:

Nodes
Nodes Overview
  • kubectl get pods: 列出當前的pods;
  • kubectl describe pods: 詳細描述每個pod中存在的container
  • kubectl logs <Pod name> <Container name in this Pod>: 輸出Pod中某個容器的log
  • kubectl exec <Pod name> <Container name in this Pod> <Command>: 在某個Pod中的容器內部執行某個命令
  • kubectl exec -it <Pod name> <Container name in this Pod> <Command>: 進入某個容器,交互式運行

Learn Kubernetes Basic -- Expose Your App Publicly

Kubernetes Pods are mortal. Pods in fact have a lifecycle. When a worker node dies, the Pods running on the Node are also lost. (即,Pod是容易down掉的,只要一個node無效了,在這個node上運行的所有pod都會down掉)

the front-end system should not care about backend replicas or even if a Pod is lost and recreated. That said, each Pod in a Kubernetes cluster has a unique IP address, even Pods on the same Node, so there needs to be a way of automatically reconciling changes among Pods so that your applications continue to function.(類似主從的數據同步,提高容錯性)

A Service in Kubernetes is an abstraction which defines a logical set of Pods and a policy by which to access them(即,將多個Pod抽象為一個service,從而使得這個service具備容錯性容災性,外部用戶無需關注service內部的多個Pod是怎么運行的,以及各自之間的主從關系;注意,同一個service下的各個pods是松耦合的)

The set of Pods targeted by a Service is usually determined by a LabelSelector(必須使用labelSelector去標記同一個service下的各個pods)

Although each Pod has a unique IP address, those IPs are not exposed outside the cluster without a Service. Services allow your applications to receive traffic. (只有在Pod之上構建一層service的時候,服務才能夠不局限于cluster)

Services can be exposed in different ways by specifying a type in the ServiceSpec.(將service暴露于外部網絡有幾種方式:ClusterIP:僅在集群內部暴露服務;NodePort:使用NAT技術,將service暴露在選定Node的相同端口上,外部網絡可以通過NodeIP:NodePort訪問service;LoadBalancer; ExternalName

A ReplicaSet's purpose is to maintain a stable set of replica Pods running at any given time(即,維護多個Pods的穩定運行,容災性)

Services match a set of Pods using labels and selectors, a grouping primitive that allows logical operation on objects in Kubernetes.(即,Service通過label和selector來匹配一組Pods)

Service Overview,可以看出,一個service可以使cluster中的多個pods松耦合,以及replica
  • kubectl expose <service名稱> <...>: 讓一個service 能夠expose到外部網絡;(例如:kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080
    得到了一個新的service,kubernetes-bootcamp
新建一個NodePort類型的service
  • kubectl describe <...>: 輸出詳細信息,<...>中可以是deployment, service, ...
    查看service對應的外部端口
將service暴露到外部端口NodePort之后,就可以對其內的APP進行訪問了
  • kubectl label pods $POD_NAME version=v1: 為pods添加新的label(所謂的label就是各種各樣的標簽,比如app的名字,version的信息,等等)
Label還可以在刪除服務的時候起到作用(注意,此處只是刪除了service,但是cluster內部的APP(Pods)還沒有刪除,因此還是可以通過kubectl exec -it進行訪問)

Learn Kubernetes Basics - Scale Your App

Scaling is accomplished by changing the number of replicas in a Deployment(通過增加replica的數量,也就是提升service中的pods的數量,從而擴展APP的服務能力,因為經過擴充之后,一個APP就可以擁有多個實例了,此外,這也使得APP的更新可以實時更新而不必經過一個downtime)

  • kubectl get rs: 獲取當前deployment所配置的replicaSet的信息;

    獲取replicaSet信息(rs的NAME由<deployment名稱+randomHashString>構成);DESIRED指的是在配置deployment的時候所希望的replica數量,而CURRENT指的是當前正在運行的replica數量

  • kubectl scale deployment/<deployment名稱> --replicas=<數量>:對deployment進行擴充;系統自帶了負載均衡器,可以均衡各個replica上的負載;

    擴充replica的數量

Learn Kubernetes Basics - Update Your App

Rolling updates allow Deployments' update to take place with zero downtime by incrementally updating Pods instances with new ones. The new Pods will be scheduled on Nodes with available resources.(可是無downtime式的更新

In Kubernetes, updates are versioned and any Deployment update can be reverted to a previous (stable) version.(可以很方便的回滾

上面兩項也就是CI/CD

給之前的deployment更新一個鏡像
  • kubectl describe pods: 可以查看Image是否已經update成功
  • kubectl rollout undo deployments/kubernetes-bootcamp: 回滾對應的服務
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,428評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,024評論 3 413
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,285評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,548評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,328評論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,878評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 42,971評論 3 439
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,098評論 0 286
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,616評論 1 331
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,554評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,725評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,243評論 5 355
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 43,971評論 3 345
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,361評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,613評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,339評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,695評論 2 370