在這一步如果之前的發佈操作有不清楚的請查看博客
kubernetes 運行springboot 並且暴露端口
一:查看k8s的deployment
kubectl get deployment
# 如下顯示
NAME READY UP-TO-DATE AVAILABLE AGE
springboot 1/1 1 1 5h11m
springboot01 3/3 3 3 4h41m
二:使用 expose 將資源暴露爲新的Kubernetes Service
kubectl expose deployment springboot01 --port=9000 --target-port=9000
三:使用svc命令查看服務
kubectl get svc
# 顯示如下
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 28h
springboot01 ClusterIP 10.96.46.178 <none> 9000/TCP 21s
# 其中 springboot01 就是 剛剛新增上去的服務
四:修改服務內容
kubectl edit svc springboot01
# 展示如下
apiVersion: v1
kind: Service
metadata:
creationTimestamp: "2019-11-13T15:55:51Z"
labels:
run: springboot01
name: springboot01
namespace: default
resourceVersion: "148732"
selfLink: /api/v1/namespaces/default/services/springboot01
uid: 240076e7-d339-4a81-bf14-234f3ce96b20
spec:
clusterIP: 10.96.46.178
externalTrafficPolicy: Cluster
ports:
- nodePort: 30761
port: 9000
protocol: TCP
targetPort: 9000
selector:
run: springboot01
sessionAffinity: None
type: NodePort // 將這個type 由 ClusterIP 修改爲 NodePort 保存退出
status:
loadBalancer: {}
五:再次查看
kubectl get svc
# 展示
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 28h
springboot01 NodePort 10.96.46.178 <none> 9000:30761/TCP 4m
# 這裏的 springboot01 的 30761 端口就是暴露給外界的
六:訪問
# 進行訪問 master 的地址 或者pod 的也行
curl 192.168.233.141:30761
到此暴露端口成功