使用Spring Boot Operator将Spring Boot部署到Kubernetes
mvn com.google.cloud.tools:jib-maven-plugin:build \
-Djib.to.auth.username=${{ secrets.MY_USERNAME }} \
-Djib.to.auth.password=${{ secrets.MY_PASSWORD }} \
-Djib.container.jvmFlags=--add-opens,java.base/sun.nio.ch=ALL-UNNAMED \
-Djib.from.image=freemanliu/oprenjre:11.0.5 \
-Dimage=registry.cn-shanghai.aliyuncs.com/qingmuio/operator-demo/operator-demo:v1.0.0
kubectl apply -f https://raw.githubusercontent.com/goudai/spring-boot-operator/master/manifests/deployment.yaml
namespace/spring-boot-operator-system created
customresourcedefinition.apiextensions.k8s.io/springbootapplications.springboot.qingmu.io created
role.rbac.authorization.k8s.io/spring-boot-operator-leader-election-role created
clusterrole.rbac.authorization.k8s.io/spring-boot-operator-manager-role created
clusterrole.rbac.authorization.k8s.io/spring-boot-operator-proxy-role created
clusterrole.rbac.authorization.k8s.io/spring-boot-operator-metrics-reader created
rolebinding.rbac.authorization.k8s.io/spring-boot-operator-leader-election-rolebinding created
clusterrolebinding.rbac.authorization.k8s.io/spring-boot-operator-manager-rolebinding created
clusterrolebinding.rbac.authorization.k8s.io/spring-boot-operator-proxy-rolebinding created
service/spring-boot-operator-controller-manager-metrics-service created
deployment.apps/spring-boot-operator-controller-manager created
kubectl get po -n spring-boot-operator-system
NAME READY STATUS RESTARTS AGE
spring-boot-operator-controller-manager-7f498596bb-wcwtn 2/2 Running 0 2m15s
# Demo.yaml
apiVersion: springboot.qingmu.io/v1alpha1
kind: SpringBootApplication
metadata:
name: operator-demo
spec:
springBoot:
version: v1.0.0
# image: registry.cn-shanghai.aliyuncs.com/qingmuio/operator-demo/operator-demo:v1.0.0
kubectl apply -f Demo.yaml
springbootapplication.springboot.qingmu.io/operator-demo created
~# kubectl get po | grep operator-demo
operator-demo-7574f4789c-mg58m 1/1 Running 0 76s
operator-demo-7574f4789c-ssr8v 1/1 Running 0 76s
operator-demo-7574f4789c-sznww 1/1 Running 0 76s
kubectl exec -it operator-demo-7574f4789c-mg58m bash
bash-5.0# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 02:06 ? 00:00:00 /pause
root 6 0 26 02:06 ? 00:00:09 java --add-opens java.base/sun.nio.ch=ALL-UNNAMED -cp /app/resources:/app/classes:/app/libs/* io.qingmu.operator.operatordemo.Oper...
root 38 0 0 02:07 pts/0 00:00:00 bash
root 44 38 0 02:07 pts/0 00:00:00 ps -ef
~# kubectl get svc | grep operator-demo
operator-demo ClusterIP 10.101.128.6
8080/TCP 2m52s
root@server1:~# curl -i http://10.101.128.6:8080
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 9
Date: Wed, 08 Apr 2020 08:45:46 GMT
hello !!!
# Demo.yaml
apiVersion: springboot.qingmu.io/v1alpha1
kind: SpringBootApplication
metadata:
name: operator-demo
spec:
springBoot:
version: v1.0.0
replicas: 1
root@server1:~# kubectl apply -f Demo.yaml
springbootapplication.springboot.qingmu.io/operator-demo configured
~# kubectl get po | grep operator-demo
operator-demo-7574f4789c-sznww 1/1 Running 0 8m29s
~# kubectl delete -f Demo.yaml
springbootapplication.springboot.qingmu.io "operator-demo" deleted
kubectl get po | grep operator-demo
kubectl create \
secret docker-registry aliyun-registry-secret \
--docker-server=registry-vpc.cn-hangzhou.aliyuncs.com \
--docker-username=*** \
--docker-password=*** \
--docker-email=***
apiVersion: springboot.qingmu.io/v1alpha1
kind: SpringBootApplication
metadata:
name: 你的应用的名称
spec:
springBoot:
version: v1.0.0
replicas: 1
image: 你的image地址
imagePullSecrets:
- 上面创建的secret
apiVersion: springboot.qingmu.io/v1alpha1
kind: SpringBootApplication
metadata:
name: operator-demo
namespace: default
spec:
springBoot:
# image 可以不设置,如果不设置默认使用 IMAGE_REPOSITORY+/+mate.name+:+spec.springBoot.version
# registry.cn-shanghai.aliyuncs.com/qingmuio + / + operator-demo + : + v1.0.0
image: registry.cn-shanghai.aliyuncs.com/qingmuio/operator-demo:v1.0.0
clusterIp: ""
version: v1.0.0
replicas: 1
resource:
cpu:
request: 50m
limit: ""
memory:
request: 1Gi
limit: 1Gi
path:
liveness: /actuator/health
readiness: /actuator/health
hostLog: /var/applog
shutdown: /spring/shutdown
imagePullSecrets:
- aliyun-docker-registry-secret
env:
- name: EUREKA_SERVERS
value: http://eureka1:8761/eureka/,http://eureka2:8761/eureka/,http://eureka3:8761/eureka/
nodeAffinity:
key: "failure-domain.beta.kubernetes.io/zone"
operator: "In"
values:
- "cn-i"
- "cn-h"
- "cn-g"
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
shutdown:
enabled: true
@RestController
public class ShutdownController {
@Autowired
private ShutdownEndpoint shutdownEndpoint;
@GetMapping("/spring/shutdown")
public Map<String, String> shutdown(HttpServletRequest request) {
return shutdownEndpoint.shutdown();
}
}
cn-i区(node-i1,node-i02)
cn-h区(node-g1,node-g02)
cn-g区(node-h1,node-h02)
kubectl label node node-i1 failure-domain.beta.kubernetes.io/zone=cn-i
kubectl label node node-i2 failure-domain.beta.kubernetes.io/zone=cn-i
kubectl label node node-h1 failure-domain.beta.kubernetes.io/zone=cn-i
kubectl label node node-ih2 failure-domain.beta.kubernetes.io/zone=cn-i
spec:
springBoot:
nodeAffinity: #可以不设置 节点亲和 这里演示的是尽量将pod分散到 i h g 三个可用区,默认设置了pod反亲和
key: "failure-domain.beta.kubernetes.io/zone"
operator: "In"
values:
- "cn-i"
- "cn-h"
- "cn-g"
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
control-plane: controller-manager
name: spring-boot-operator-controller-manager
namespace: spring-boot-operator-system
.....
#注意:一下配置针对通用全局的Spring Boot默认配置,对crd的Spring Boot生效,这里不配置也可以在部署的yaml中指定
# 私有仓库的地址,比如我的最终打包的镜像地址是 registry.cn-shanghai.aliyuncs.com/qingmuio/operator-demo/operator-demo:v1.0.0
# 那么配置的值是 registry.cn-shanghai.aliyuncs.com/qingmuio/operator-demo
# 配置这个值之后,我们我们如果在发布的yaml中不写image,那么使用的image就是 IMAGE_REPOSITORY+"/"+mate.name+spec.springBoot.version
- name: IMAGE_REPOSITORY
value: registry.cn-shanghai.aliyuncs.com/qingmuio
# 请求CPU限制
- name: REQUEST_CPU
value: 50m
# 限制最大能用最大CPU Java应用可以不用限制,限制不合理会导致启动异常缓慢
- name: LIMIT_CPU
value: ""
# 请求内存大小
- name: REQUEST_MEMORY
value: 500Mi
# 限制最大内存大小 一般和request一样大即可
- name: LIMIT_MEMORY
value: 500Mi
# 就绪检查Path,Spring Boot Actuator 默认Path
- name: READINESS_PATH
value: /actuator/health
# 就绪存活Path,Spring Boot Actuator 默认Path
- name: LIVENESS_PATH
value: /actuator/health
# 就绪存活Path,优雅停机Path
- name: SHUTDOWN_PATH
value: /spring/shutdown
# 复制级 即副本数
- name: REPLICAS
value: "3"
# 将日志外挂到主机磁盘Path,默认两者相同
- name: HOST_LOG_PATH
value: /var/applog
# 用于pull镜像的secrets
- name: IMAGE_PULL_SECRETS
value: ""
# 用于pull镜像的secrets
- name: SPRING_BOOT_DEFAULT_PORT
value: "8080"
# Node亲和,比如我可以设置Pod尽量分散在不同可用区cn-i,cn-g,cn-h区
- name: NODE_AFFINITY_KEY
value: ""
- name: NODE_AFFINITY_OPERATOR
value: ""
- name: NODE_AFFINITY_VALUES
value: ""
# 全局的环境变量,会追加到每个Spring Boot的每个Pod中,格式 k=v;k1=v2,
# 如 EUREKA_SERVERS=http://eureka1:8761/eureka/,http://eureka2:8761/eureka/,http://eureka3:8761/eureka/;k=v
- name: SPRING_BOOT_ENV
value: ""
image: registry.cn-shanghai.aliyuncs.com/qingmuio/spring-boot-operator-controller:latest
.....
apiVersion: springboot.qingmu.io/v1alpha1
kind: SpringBootApplication
metadata:
name: 你的应用的名称
spec:
springBoot:
version: v1.0.0
环境变量名 | 是否可以空 | 默认值 | 说明 |
---|---|---|---|
IMAGE_REPOSITORY | true | “” | 私有仓库的地址 |
REQUEST_CPU | true | 50m | 请求CPU限制 |
LIMIT_CPU | true | “” | 限制最大能用最大CPU Java应用可以不用限制,限制不合理会导致启动异常缓慢 |
REQUEST_MEMORY | true | 2Gi | 请求内存大小 |
LIMIT_MEMORY | true | 2Gi | 限制最大内存大小 一般和request一样大即可 |
READINESS_PATH | true | /actuator/health | 就绪检查Path,Spring Boot Actuator 默认Path |
LIVENESS_PATH | true | /actuator/health | 存活检查Path,Spring Boot Actuator 默认Path |
SHUTDOWN_PATH | true | /spring/shutdown | 就绪存活Path,优雅停机Path |
REPLICAS | true | 3 | 副本数 |
HOST_LOG_PATH | true | /var/applog | 将日志外挂到主机磁盘Path,默认两者相同 |
IMAGE_PULL_SECRETS | true | 无 | 用于pull镜像的secrets |
SPRING_BOOT_DEFAULT_PORT | true | 8080 | 用于pull镜像的secrets |
NODE_AFFINITY_KEY | true | “” | Node亲和key,比如我可以设置Pod尽量分散在不同可用区cn-i,cn-g,cn-h区 |
NODE_AFFINITY_OPERATOR | true | “” | Node亲和操作符 |
NODE_AFFINITY_VALUES | true | “” | Node亲和value |
SPRING_BOOT_ENV | true | “” | 全局的环境变量,会追加到每个Spring Boot的每个Pod中,格式 k=v;k1=v2 |
Spring Boot Operator GitHub仓库:https://github.com/goudai/spring-boot-operator
https://github.com/goudai/spring-boot-operator
https://qingmu.io/2018/08/07/How-to-run-springcloud-in-docker/
如有文章对你有帮助,
“在看”和转发是对我最大的支持!
关注Java开发宝典
每天学习Java技术
点赞是最大的支持