建立Deployment物件,在分類上Deployment、StatefulSet等都屬於Kubernetes定義的進階物件,apiVersion值跟Basic Type如Pod有區別,以nestjsapp為例
# nestjsapi-deployment.yaml
apiVersion: apps/v1 # Pod是v1,這裡是apps/v1
kind: Deployment
metadata:
name: nestjsapi-deployment
spec:
replicas: 1 # 可以擴充多少identical pod
# 告訴kubernetes這個deployment是要用哪些pod
# 現在只有一個,未來可以多個pod
selector:
matchLabels:
component: api
template: # create pod的blueprint
# 以下copy Pod config內容
metadata:
labels: # pod labels
component: api
spec:
containers:
- name: nestjsapi
image: yirengoo/nestjsapi:v1 # v1 listen to 3000
ports:
- containerPort: 3000
kubectl apply -f nestjsapi-deployment.yaml
kubectl get deployment
kubectl get pod
修改containerPort為5000
# nestjsapi-deployment.yaml
..
template: # create pod的blueprint
# 以下copy Pod config內容
metadata:
labels: # pod labels
component: api
spec:
containers:
- name: nestjsapi
image: yirengoo/nestjsapi:v2 # v2 listen to 5000
ports:
- containerPort: 5000
update Deployment設定檔
kubectl apply -f nestjsapi-deployment.yaml
kubectrl describe pod
port已更新。
修改service
apiVersion: v1
kind: Service
metadata:
name: nestjsapi-service
spec:
type: NodePort
ports:
- port: 5000
targetPort: 5000 # 改為5000
nodePort: 30001
selector:
component: api
update Service設定檔
kubectl apply -f nestjsapi-service.yaml
用postman測試
用新的image以及更改port測試OK