到目前為止Service Type都是NodePort,VM外要存取pod都是透過kube-proxy導到NodePort指定的port,production時處理外部的Request由Ingress Service來將request導向Node內部,Node裡需要有相互可以溝通而且沒有對外expose的Service,這種只能Node內可以溝通的Service Type為ClusterIP,建立跟之前docker-compose類似的架構
先以API Server為例,建立ClusterIP yaml設定檔
# nestjsapi-clusterip-service.yaml
apiVersion: v1
kind: Service
metadata:
name: nestjsapi-clusterip-service
spec:
type: ClusterIP
selector:
component: api
ports:
- port: 5000 # port exposed to node network
targetPort: 5000 # link port to container
# nodePort no longer required
先刪除已經建立的deployment, services移除
kubectl get deployment
kubectl delete deployment deploymentName
kubectl get service
kubectl delete service serviceName
確認沒有running的pod
kubectl get pod
套用剛剛建立的cluster ip設定檔
kubectl apply -f nestjsapi-clusterip-service.yaml
建立frontend-deployemnt.yaml
# frontend-deployemnt.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend-deployment
spec:
replicas: 3
selector:
matchLabels:
component: frontend
template:
metadata:
labels:
component: frontend
spec:
containers:
- name: nestjsapi
image: yirengoo/frontend
ports:
- containerPort: 4200
kubectl apply -f frontend-deployemnt.yaml
建立frontend-clusterip-service.yaml
apiVersion: v1
kind: Service
metadata:
name: frontend-clusterip-service
spec:
type: ClusterIP
ports:
- port: 4200
targetPort: 4200
selector:
component: frontend
kubectl apply -f frontend-clusterip-service.yaml
其餘部分明天繼續