《Windows Azure Platform 系列文章目錄》
我們在創建AKS的時候,默認會創建1個具有公網的負載均衡器,這個負載均衡器的作用是:
1.提供Virtual Network內部的AKS Node,具有往Internet訪問的流量。通過Node的私有IP地址,通過Outbound Rule,轉化爲公網IP訪問。
2.通過把K8S Service暴露爲Load Balancer,外部的用戶可以訪問到部署在AKS上的服務
請注意,Azure AKS默認創建的公網IP地址,不能被用戶重複使用。
如果用戶需要使用公網IP地址,只能在負載均衡器上新建新的公網IP地址。
1.我們新建一個AKS集羣,默認會創建1個負載均衡器
2.我們在這個負載均衡器上,新建一個公網IP地址,如下圖:
下面這個lbfront就是我手動增加的ip地址 創建完畢後 會顯示這個公網ip是52.130.254.118
3.我們準備2個yaml file,分別如下:
publiclb-nginx80,設置負載均衡器端口爲80。必須在下面指定loadBalancerIP的公網IP地址
apiVersion: v1
kind: Service
metadata:
name: nginx80
spec:
type: LoadBalancer
loadBalancerIP: 52.130.254.118
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
run: testapp01
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
run: testapp01
name: testapp01
spec:
replicas: 1
selector:
matchLabels:
run: testapp01
template:
metadata:
labels:
run: testapp01
spec:
containers:
- image: nginx
name: nginx
publiclb-nginx81,設置負載均衡器端口爲81。必須在下面指定loadBalancerIP的公網IP地址
apiVersion: v1
kind: Service
metadata:
name: nginx81
spec:
type: LoadBalancer
loadBalancerIP: 52.130.254.118
ports:
- port: 81
protocol: TCP
targetPort: 80
selector:
run: testapp01
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
run: testapp01
name: testapp01
spec:
replicas: 1
selector:
matchLabels:
run: testapp01
template:
metadata:
labels:
run: testapp01
spec:
containers:
- image: nginx
name: nginx
4.我們通過kubectl apply,分別執行這2個yaml file。步驟略。
可以看到nginx 80和nginx81這2個服務都起來了
5.我們分別訪問這2個服務地址:http://52.130.254.118/ 和 http://52.130.254.118:81。
可以訪問到這2個Nginx的服務。如下圖: