Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

5.38. Spring boot with Kubernetes

首先你需要構建 docker 鏡像,並且 push 到 registry 參考這裡

5.38.1. Kubernetes 編排腳本

創建密鑰

		
kubectl create secret docker-registry docker-hub \
--docker-server=https://index.docker.io/v1/ \
--docker-username=netkiller \
--docker-password=passw0rd \
--docker-email=netkiller@msn.com
		
		

查看是否創建成功

		
iMac:spring neo$ kubectl get secret
NAME                  TYPE                                  DATA   AGE
default-token-fhfn8   kubernetes.io/service-account-token   3      2d23h
docker-hub            kubernetes.io/dockerconfigjson        1      15s		
		
		

springboot.yml 編排腳本

		
apiVersion: v1
kind: Service
metadata:
  name: springboot
  namespace: default
  labels:
    app: springboot
spec:
  type: NodePort
  ports:
  - port: 8888
    nodePort: 30000
  selector:
    app: springboot
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: springboot
spec:
  replicas: 3
  selector:
    matchLabels:
      app: springboot
  template:
    metadata:
      labels:
        app: springboot
    spec:
      containers:
      - name: springboot
        image: netkiller/config:latest
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8888
      imagePullSecrets:
        - name: docker-hub
		
		

5.38.2. 部署鏡像

		
iMac:spring neo$ kubectl create -f springboot.yml 
deployment.apps/springboot created

iMac:spring neo$ kubectl expose deployment springboot --type="LoadBalancer"
service/springboot exposed

iMac:spring neo$ minikube service list
|----------------------|---------------------------|--------------|---------------------------|
|      NAMESPACE       |           NAME            | TARGET PORT  |            URL            |
|----------------------|---------------------------|--------------|---------------------------|
| default              | kubernetes                | No node port |
| default              | springboot                |         8888 | http://192.168.64.2:30000 |
| kube-system          | kube-dns                  | No node port |
| kube-system          | registry                  | No node port |
| kubernetes-dashboard | dashboard-metrics-scraper | No node port |
| kubernetes-dashboard | kubernetes-dashboard      | No node port |
|----------------------|---------------------------|--------------|---------------------------|

iMac:spring neo$ minikube service springboot --url
http://192.168.64.2:30000
		
		

http://192.168.64.2:30000 是訪問地址,Kubernetes 會負載均衡到後面的三個 pod 上。

		
iMac:config neo$ curl -k https://config:s3cr3t@192.168.64.2:30000/netkiller-dev.json
{"sms":{"gateway":{"url":"https://sms.netkiller.cn/v1","username":"netkiller","password":"123456"}}}		
		
		

刪除服務

		
iMac:spring neo$ kubectl delete -f springboot.yml 
service "springboot" deleted
deployment.apps "springboot" deleted