接下來這篇文章我們即將介紹Kubernetes中常見三個部分: Pod、Service、Deployments
Pod可以說是Kubernetes中可創建和部署的最小單位,Pod中包含一個或多個容器的集合,還包括存儲、網路等各容器的資源共享。
Pod的特性
簡單的範例檔mypod.yaml
apiVersion: v1 #版號
kind: Pod #類型
metadata:
name: mypod #pod名字,在Namespace必須是唯一
namespace: default
spec:
containers:
- name: mypod #容器名字
image: nginx #映像檔
ports:
- name: http
containerPort: 80
建立Pod
➜ ~ kubectl create -f mypod.yaml
pod/mypod created
查看Pod
➜ ~ kubectl get pods
NAME READY STATUS RESTARTS AGE
mypod 2/2 Running 0 89s
➜ ~ kubectl get pod mypod
NAME READY STATUS RESTARTS AGE
mypod 2/2 Running 0 116s
刪除Pod
➜ ~ kubectl delete -f mypod.yaml
pod "mypod" deleted
Deployment是Kubernetes為了控制Pod,所提供的一種更新Replication Controller和Pod的設定方式。
簡單的範例檔mynginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
namespace: default
spec:
selector:
matchLabels:
app: my-nginx
replicas: 1 # 期待建立pod的数量
template:
metadata:
labels:
app: my-nginx # 設定label資訊
spec:
containers:
- name: my-nginx
image: nginx #映像檔
imagePullPolicy: Always
ports:
- name: http
containerPort: 80
建立Deployment
➜ ~ kubectl create -f mynginx.yaml
deployment.apps/my-nginx created
查看Deployment
➜ ~ kubectl get pods
NAME READY STATUS RESTARTS AGE
my-nginx-bbd559cf8-h8jxg 2/2 Running 0 2m31s
刪除Deployment
➜ ~ kubectl delete -f mynginx.yaml
deployment.apps "my-nginx" deleted
Kubernetes中的Service可以說是一種抽象概念,它定義了Pod邏輯集合以及存取的策略,Service同Pod的連結是透過Label來完成的。Service可以說是提供一個橋樑,它會替造訪者提供一個固定網址,來讓需求導到對應的後端,讓Kubernetes可以輕鬆存取後端資源。
簡單的範例檔mysvc.yaml
apiVersion: v1
kind: Service
metadata:
name: svc-nginx
namespace: default
spec:
type: ClusterIP
ports:
- name: tcp
port: 80
targetPort: 80
protocol: TCP
selector: //透過Service的Label Selector來連結對應Pod的Label
app: my-nginx
建立Service
➜ ~ kubectl create -f mysvc.yaml
service/svc-nginx created
查看Service
➜ ~ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
svc-nginx ClusterIP 10.229.14.220 <none> 6379/TCP 7s
刪除Service
➜ ~ kubectl delete -f mysvc.yaml
service "svc-nginx" deleted