今天我們一樣要用Service去提供Pod的服務,但是我們宣告為NodePort的方式而非Cluster-IP。
在開始之前一樣先附上我的叢集節點:
從圖中可以清楚看到,k8s-node01、k8s-node02的加入規則是透過kubernetes加入的;edge、raspberrypi是透過KubeEdge加入的,且他們是邊緣節點。
和前幾天提到的一樣,NodePort和Cluster-IP最大的不同是NodePort可以對叢集外提供服務,在Service上也一樣。首先我們先在Kubernetes下做測試:
# k8s-nginx-deploy-nodeport.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: k8s-nginx-deploy
namespace: default
labels:
app: k8s-nginx-deploy
spec:
replicas: 3
selector:
matchLabels:
app: k8s-nginx-deploy
template:
metadata:
labels:
app: k8s-nginx-deploy
spec:
containers:
- image: nginx:latest
name: nginx
ports:
- containerPort: 80
nodeSelector:
name: k8s-node02
---
apiVersion: v1
kind: Service
metadata:
name: k8s-nginx-service
spec:
type: NodePort
selector:
app: k8s-nginx-deploy
ports:
- name: http
port: 7777
targetPort: 80
protocol: TCP
kubectl apply -f k8s-nginx-deploy-nodeport.yaml
檢查一下部署狀態
我們可以用幾種方法存取到NGINX:
# edge-nginx-deploy-nodeport.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: kubeedge-nginx-deploy
namespace: default
labels:
app: kubeedge-nginx-deploy
spec:
replicas: 3
selector:
matchLabels:
app: kubeedge-nginx-deploy
template:
metadata:
labels:
app: kubeedge-nginx-deploy
spec:
containers:
- image: nginx:latest
name: nginx
ports:
- containerPort: 80
nodeSelector:
name: edge
---
apiVersion: v1
kind: Service
metadata:
name: kubeedge-nginx-service
spec:
type: NodePort
selector:
app: kubeedge-nginx-deploy
ports:
- name: http
port: 6666
targetPort: 80
protocol: TCP
kubectl apply -f edge-nginx-deploy-nodeport.yaml
使用上面的YAML檔在edge上佈署NGINX服務。
確定service與Pod正確佈署後連線看看: