k8s搭建高可用spring-cloud eureka集群
k8s搭建高可用eureka集群查找镜像docker部署关闭安全认证开启安全认证docker-compose方式部署k8s方式部署使用helm部署集群注册服务,测试注册服务service-a注册服务service-b,调用a提供的接口查找镜像镜像地址: https://hub.docker.com/r/lhstack/eureka根据镜像地址的里面的文档,进行docker,docker-compo
·
k8s搭建高可用spring-cloud eureka集群
查找镜像
镜像地址: https://hub.docker.com/r/lhstack/eureka
根据镜像地址的里面的文档,进行docker,docker-compose,k8s,helm相关部署验证
docker部署
关闭安全认证
docker run --name eureka -p 8761:8761 -e SECURITY_ENABLE=false -e JAVA_OPTS="-Xmx128m -Xms128m" -d lhstack/eureka
开启安全认证
# 关闭之前启动的容器,防止端口冲突
docker rm -f eureka
docker run --name eureka -p 8761:8761 -e SECURITY_USERNAME=admin1 -e SECURITY_PASSWORD=654321 -e JAVA_OPTS="-Xmx128m -Xms128m" -d lhstack/eureka
再次访问则需要输入用户名和密码,输入上面定义的用户名 admin1
,密码 654321
即可
docker-compose方式部署
前提需要安装docker-compose,安装地址: https://www.runoob.com/docker/docker-compose.html,这里只部署带认证方式
#停止之前的容器
docker rm -f eureka
#写入docker-compose脚本
cat > docker-compose.yml<<EOF
version: '3'
services:
eureka-1:
container_name: eureka-1
image: lhstack/eureka
ports:
- 8761:8761
deploy:
resources:
limits:
cpus: '1'
memory: '256M'
logging:
options:
max-size: "1kb"
max-file: "1"
environment:
JAVA_OPTS: "-Xmx128m -Xms128m"
SECURITY_ENABLE: true
SECURITY_USERNAME: admin
SECURITY_PASSWORD: admin
EUREKA_SERVER_URLS: "http://admin:admin@182.168.100.100/eureka,http://admin:admin@182.168.100.101/eureka,http://admin:admin@182.168.100.102/eureka"
networks:
eureka:
ipv4_address: 182.168.100.100
eureka-2:
container_name: eureka-2
image: lhstack/eureka
ports:
- 8762:8761
deploy:
resources:
limits:
cpus: '1'
memory: '256M'
logging:
options:
max-size: "1kb"
max-file: "1"
environment:
JAVA_OPTS: "-Xmx128m -Xms128m"
SECURITY_ENABLE: true
SECURITY_USERNAME: admin
SECURITY_PASSWORD: admin
EUREKA_SERVER_URLS: "http://admin:admin@182.168.100.100/eureka,http://admin:admin@182.168.100.101/eureka,http://admin:admin@182.168.100.102/eureka"
networks:
eureka:
ipv4_address: 182.168.100.101
eureka-3:
container_name: eureka-3
image: lhstack/eureka
ports:
- 8763:8761
deploy:
resources:
limits:
cpus: '1'
memory: '256M'
logging:
options:
max-size: "1kb"
max-file: "1"
environment:
JAVA_OPTS: "-Xmx128m -Xms128m"
SECURITY_ENABLE: true
SECURITY_USERNAME: admin
SECURITY_PASSWORD: admin
EUREKA_SERVER_URLS: "http://admin:admin@182.168.100.100/eureka,http://admin:admin@182.168.100.101/eureka,http://admin:admin@182.168.100.102/eureka"
networks:
eureka:
ipv4_address: 182.168.100.102
networks:
eureka:
driver: bridge
ipam:
driver: default
config:
- subnet: 182.168.0.0/16
EOF
#执行docker-compose
docker-compose up -d
k8s方式部署
前提得安装k8s集群环境,安装方式查找资料即可
# 停掉之前docker-compose启动的eureka
docker-compose down
# 生成statefulset脚本
cat > eureka.yml<<EOF
apiVersion: v1
kind: Namespace
metadata:
name: eureka
---
apiVersion: v1
kind: ConfigMap
metadata:
name: eureka
namespace: eureka
data:
SECURITY_USERNAME: "admin"
SECURITY_PASSWORD: "123456"
EUREKA_SERVER_URLS: "http://admin:123456@eureka-headless.eureka.svc.cluster.local:8761/eureka"
---
apiVersion: v1
kind: Service
metadata:
name: eureka-headless
namespace: eureka
spec:
type: ClusterIP
clusterIP: None
selector:
app: eureka-replica
ports:
- name: http
port: 8761
---
apiVersion: v1
kind: Service
metadata:
name: eureka
namespace: eureka
spec:
type: ClusterIP
ports:
- name: http
port: 8761
selector:
app: eureka
clusterIP: 10.43.87.61
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: eureka-cluster
namespace: eureka
spec:
replicas: 2 #必须和REPLICAS一致
serviceName: eureka-headless
selector:
matchLabels:
app: eureka-replica
template:
metadata:
labels:
app: eureka-replica
spec:
containers:
- name: eureka
image: lhstack/eureka
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 8761
env:
- name: IS_STATEFUL_SET
value: "true"
- name: NAMESPACE
value: "eureka"
- name: SECURITY_ENABLE
value: "true"
- name: SECURITY_USERNAME
valueFrom:
configMapKeyRef:
key: SECURITY_USERNAME
name: eureka
- name: SECURITY_PASSWORD
valueFrom:
configMapKeyRef:
key: SECURITY_PASSWORD
name: eureka
- name: SERVICE_NAME
value: "eureka-headless"
- name: REPLICAS
value: "2"
- name: JAVA_OPTS
value: "-Xmx512m -Xms512m"
resources:
requests:
cpu: 50m
memory: 128Mi
limits:
cpu: 100m
memory: 512Mi
readinessProbe:
httpGet:
path: /actuator/health
port: 8761
initialDelaySeconds: 5
failureThreshold: 5
periodSeconds: 5
successThreshold: 1
timeoutSeconds: 2
livenessProbe:
httpGet:
path: /actuator/health
port: 8761
initialDelaySeconds: 60
failureThreshold: 5
periodSeconds: 120
successThreshold: 1
timeoutSeconds: 2
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: eureka-deploy
namespace: eureka
spec:
replicas: 1
selector:
matchLabels:
app: eureka
template:
metadata:
labels:
app: eureka
spec:
containers:
- name: eureka
image: lhstack/eureka
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 8761
readinessProbe:
httpGet:
path: /actuator/health
port: 8761
initialDelaySeconds: 5
failureThreshold: 5
periodSeconds: 5
successThreshold: 1
timeoutSeconds: 2
livenessProbe:
httpGet:
path: /actuator/health
port: 8761
initialDelaySeconds: 60
failureThreshold: 5
periodSeconds: 120
successThreshold: 1
timeoutSeconds: 2
resources:
requests:
cpu: 50m
memory: 128Mi
limits:
cpu: 100m
memory: 512Mi
env:
- name: SECURITY_ENABLE
value: "true"
- name: SECURITY_USERNAME
valueFrom:
configMapKeyRef:
key: SECURITY_USERNAME
name: eureka
- name: SECURITY_PASSWORD
valueFrom:
configMapKeyRef:
key: SECURITY_PASSWORD
name: eureka
- name: EUREKA_SERVER_URLS
valueFrom:
configMapKeyRef:
key: EUREKA_SERVER_URLS
name: eureka
- name: JAVA_OPTS
value: "-Xmx512m -Xms512m -Deureka.client.fetch-registry=true -Deureka.client.register-with-eureka=true"
EOF
kubectl apply -f eureka.yml
kubectl get pod -n eureka
这里deployment作为eureka集群里面的客户端,用于承载业务服务的注册,保证statefulset的eureka集群的高可用
使用helm部署集群
# 停止之前运行的eureka集群
kubectl delete -f eureka.yml
#拉取helm模板,需要git和helm环境
#git安装 yum install -y git
#helm安装https://github.com/helm/helm/releases
进入helm目录
安装集群
helm install eureka-cluster .
多刷新几下,可以看到负载均衡的过程
修改集群数量
helm upgrade --set replicas=5 eureka-cluster .
可以看到服务在滚动更新,增加新的,停掉老的
注册服务,测试
这里兼容2.x之后的版本哦
注册服务service-a
注册服务service-b,调用a提供的接口
更多推荐
已为社区贡献2条内容
所有评论(0)