Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

24.3. Spring boot with Kubernetes

首先你需要构建 docker 镜像,并且 push 到 registry 参考这里

24.3.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
		
			

24.3.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