在上一篇中我們已經把服務部署完成,接下來我們要進一步來設定對外IP。
建立地區静態IP
建立完成後,在loadBalancerIP
帶入靜態IPYOUR-IP
# service_hs_exter.yaml
apiVersion: v1
kind: Service
metadata:
name: hs-exter
namespace: dev-ithome
spec:
type: LoadBalancer
loadBalancerIP: "YOUR-IP"
ports:
- name: http
port: 8080
targetPort: 8080
protocol: TCP
selector:
app: hs
kubectl -n dev-ithome get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
gs ClusterIP 10.229.4.49 <none> 8081/TCP 15m
hs-exter LoadBalancer 10.229.2.25 x.x.x.x 80:32754/TCP 2m13s
完成後就可以,透過外網IP連到服務
使用Ingress静態IP,需要先建立二個資源:
執行下列指令,建立全域静態IP
gcloud compute addresses create test-static-ip --global
查看建立的IP
gcloud compute addresses describe test-static-ip --global
# service_ingress_hs.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: hs-ingress
namespace: dev-ithome
annotations:
kubernetes.io/ingress.global-static-ip-name: test-static-ip
spec:
rules:
- host: xxx.xxx.xxx
http:
paths:
- backend:
serviceName: hs-backend
servicePort: 80
---
apiVersion: v1
kind: Service
metadata:
name: hs-backend
namespace: dev-ithome
spec:
type: NodePort
ports:
- name: http
port: 80
targetPort: 8080
protocol: TCP
selector:
app: hs