這邊教大家最簡單且方便的架設方式,安裝 Google 在 vscode 上開發的 cloud code
之後再安裝 skaffold
目前 k8s 中該元件的版本號,必須要依照 server 上 k8s 的版本來進行設定。
用來擺放描述性資料的地方
用來決定此設定檔的類型
用來描述物件被生成的細節
container 名稱
container 的 image
container 的對外 port
apiVersion: apps/v1
kind: Pod
metadata:
name: my-pod
labels:
app: my-Pod
spec:
containers:
- name: my-Pod
image: demoImage
ports:
- containerPort: 3000
這邊要用到 volumes 的觀念來進行掛載
要掛載在 container 中的名稱
選擇要掛載在 container 中的哪個 path 上
有時候我們要掛載的目錄中可能包含了多個子目錄,而這些子目錄恰巧又分別被多個不同的 container 使用,此時就可以透過 subPath 的方式來簡化 volume 的設定
從外部掛載進來的名稱
spec:
containers:
- name: my-Pod
image: demoImage
ports:
- containerPort: 3000
volumeMounts:
- name: nginx-secret
mountPath: /etc/nginx/ssl
- name: pod-config
mountPath: /etc/nginx/conf.d
readOnly: true
volumes:
- name: pod-config
configMap:
name: pod-config
- name: nginx-secret
secret:
secretName: nginx-secret
設定 Pod 的數量
選擇 Pod 的條件
定義 Pod 的資訊,上述提到的 Pod 寫法都會擺在這
定義 Pod 的描述資訊
定義 Pod 內的 container 資訊
apiVersion: v1
kind: ReplicationController
metadata:
name: demoReplicationController
spec:
replicas: 2
selector:
app: my-Pod
template:
metadata:
labels:
app: my-Pod
spec:
containers:
- name: my-pod
image: demoImage
ports:
- containerPort: 3000
k8s 為了確保在 rollout(滾動更新) 時可以按照想要的方式進行更新,只要是 Deployment 在 spec 中都會有 strategy 的設定值
設定 rollout type,有 Recreate 以及 RollingUpdate 兩種,default 為 RollingUpdate
在 rollout 的過程中最多可以比原本設定的 Pod 數量多出多少
在 rollout 的過程中,可以允許多少個 Pod 無法使用,假如 MaxSurge 設定非 0,maxUnavailable 也不能設定非 0
apiVersion: apps/v1
kind: Deployment
metadata:
name: demoDeployment
spec:
replicas: 2
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
selector:
matchLabels:
app: my-deployment
template:
# . . . pod settings
指定 service 建立完的 clusterIP 中哪個 port 要對應到 targetPort
可以指定 Node 中哪個 Port 要對應到 targetPort ,沒設定的話 k8s 會隨機挑選一個 Port
指定要相對應 Pod 的 Port,targetPort 會跟 port 相同
使用 TCP 或 UDP, default 是 TCP
apiVersion: v1
kind: Service
metadata:
name: my-service
labels:
app: my-service
spec:
ports:
- protocol: TCP
port: 3000
targetPort: 3000
selector:
app: my-Pod
要提供給網站用的 tls secret 名稱
網站的 domain name
經由哪個 path 來連接到 service
欲連接到的 service 名稱
欲連接到的 service 對外port
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: my-ingress
spec:
tls:
- secretName: nginx-secret
rules:
- host: mysite.com.tw
http:
paths:
- path: /
backend:
serviceName: my-service
servicePort: 3000
點擊 ingress-nginx 中 deploy 頁面進行初始化安裝
這邊有一個一定要安裝的描述檔,不論是架在本地端或是雲端都需要
ingress-nginx 會自動幫我們建立好的 ingress 進行統整的動作
統整過後的 ingress 都會擺在 nginx.conf 內
kubectl exec -n ingress-nginx -ti podName -- /bin/sh
由於 Health check 是要檢查 Pod 的健康狀態,因此 Health check 會寫在 Pod 或 Deployment 的設定檔內
livenessProbe 與 readinessProbe 的設定方式一樣
設定 health check 要造訪的路徑
設定要造訪的 scheme,預設為 HTTP 也可設定為 HTTPS
設定要造訪的 port
設定 service 剛啟動時要間隔多久再啟動 health check
每多久訪問一次,預設為 10s
設定訪問幾次而且都成功就代表 service 成功運行,預設為 1 次
代表 service 回傳不如預期時,在 kubelet 放棄此 container 之前會在嘗試的次數,預設為3次
# . . . container settings
livenessProbe:
httpGet:
path: /healthz
scheme: HTTPS
port: 8080
initialDelaySeconds: 3
periodSeconds: 60
successThreshold: 3
failureThreshold: 5
readinessProbe:
httpGet:
path: /healthz
scheme: HTTPS
port: 8080
initialDelaySeconds: 3
periodSeconds: 60
successThreshold: 3
failureThreshold: 5
# . . . container settings
apiVersion: batch/v1beta1
kind: Cronjob
spec:
schedules: 需輸入符合 cron 格式的字串
jobTemplate:
spec:
template:
spec:
containers:
- name: cornjob 名稱
image: 排程映像檔
command: [‘node’,’index.js’] -> node index.js
restartPolicy: 重啟設定(always, onFailure, Never)
謝謝大家