Sean talks

Attitude is everything

0%

[Note] Kubernetes

[Note] Kubernetes

最近因工作需求必須稍微弄懂 kubernetes 在做些什麼,
因此筆記起來一些基礎的使用與常用語法和遇過的坑。
但要深究細節的話還是得參照官方 doc. : https://kubernetes.io/

what’s kubernetes?

Kubernetes 提供自動化部署及管理 container 的服務,
以協助管理微服務(microservices)系統。

使用 kubernetes 可以提供以下功能:

  • 容器的部署(Deployment)至一或多台機器
  • 工作負載自動化擴展(Scaling)
  • 自動化偵測並重啟故障容器(Management),確保系統持續提供服務

Component of Kubernetes

kubernetes 組成元件由小至大分別為 Pod, Worker Node, Master Node

Pod

Pod 為 kubernetes 運作的最小單位,一個 Pod 對應到一個 Application (e.g. API Server/data transform service),而當多個 Containers 存在於同一個 Pod 時,這些 Containers 會共享資源及網路,以 yaml 定義並部署建立。

Worker Node

Worker Node 是 kubernetes 中最小的硬體單位,一個 Worker Node 對應到的是一台機器,在 GCP 中即好比 Computer Engine,並由 kubelet、kube-proxy、Container Runtime 組成,主要負責運算資源的控制。

Master Node

Master Node 是 kubernetes 中的指揮所,提供包括 API Server、Etcd、叢集資源排程、應用部署管理等工作,負責管理其他 Node。

Connect to Kubernetes - Kubectl

Kubectl 是 kubernetes 的 Command Line 工具, kubectl 可以用來操作 Kubernetes 中的 Cluster,接下來就來說明一下如何建立 Pod、Service、Deployment、Ingerss,並透過 kubectl 建立。

Pod

Pod 就如同前一章所說明的對應到每個 Application,而透過編寫下述 .yaml 檔案可以建立 helloPod 這個服務。

1
2
3
4
5
6
7
8
9
10
11
12
apiVersion: v1
kind: Pod
metadata:
name: helloPod
labels:
app: test
spec:
containers:
- name: k8sContainer
image: helloPod/k8sDemo
ports:
- containerPort: 8787

其中的各項文件分別代表:

  • apiVersion:
    • 版號
  • kind:
    • 屬性 (e.g. Pod, Node, Deployment, Service, Namespace)
  • metadata:
    • name:
      • name of pod
    • labels
      • lable of pod
  • spec :: containers:
    • name:
      • name of container
    • image:
      • image of container from DockerHub
    • ports:
      • port of container that can be access

使用 kubectl 建立 Pod

使用 apply 指令根據 .yaml 建立 Pod

1
kubectl apply -f helloPod.yaml

接著可以使用 get 查看運行的 Pod

1
kubectl get pods

若要在本地端進行 access 可以透過 port-forward 進行服務連線

1
kubectl port-forward helloPod 8787:8787

上述指令代表將 localhost 的 port 8787 mapping 至 helloPod 的 8787 por。

Service

了解完 Pod 的建立後,接下來甚麼來講講甚麼是 Service。 由於 Pod 的生命週期是動態的,而為使系統在升級過程中達到無停機服務遷移,因此需要一個服務來讓每一次的存取可以正確連結到正在運行中的 Pod,這裡指的服務就是 Service。

撰寫 Service 物件 .yaml file:

1
2
3
4
5
6
7
8
9
10
11
12
13
apiVersion: v1
kind: Service
metadata:
name: helloService
labels:
app: test
spec:
ports:
- port: 8787
targetPort: 8787
protocol: TCP
selector:
app: demoApp

Deployment

在討論完 Pod 與 Service 後可以來講講服務的縱向擴展元件 - Deployment,可以將 Deployment 看作將 Pod copy 多份在同一個 cluster 中提供服務,並監控這些 Pod 的運作以達到 自動佈署 (重啟) 與 多份備份 的功能。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
apiVersion: v1
kind: Deployment
metadata:
name: helloDeployment
namespace: hello
labels:
app: test
service: hello
spec:
replicas: 3
revisionHistoryLimit: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
minReadySeconds: 0
selector:
matchLabels:
app: demoApp
service: hello
template:
metadata:
labels:
app: demoApp
service: hello
spec:
serviceAccountName: xxx
containers:
- name: k8sContainer
image: helloPod/k8sDemo
imagePullPolicy: Always
ports:
- name: http
containerPort: 8787
env:
- name: VARIABLE1
value: "test-001"
resources:
requests:
memory: "128Mi"
cpu: "256m"
limits:
memory: "512Mi"
cpu: "512m"
imagePullSecrets:
- name: xxx

其中的各項元件分別代表(僅註未說明):

  • spec
    • replicas
      • 表示指定要建立多少個相同的 Pod ,若運行中的 Pod 低於這個數字,服務將會自動啟用 新的 Pod。
    • strategy
    • template
      • 指定該 Deployment 所建立的 Pods 統一設定,包含 meatadata & Containers,環境變數與 resources limit 等等。

接著可以使用 get 查看運行的 Deployment & Pod

1
2
kubectl get deploy
kubectl get pods

Ingress

Continue …

StatefulSet

Continue …

References