k8s搭建高可用spring-cloud eureka集群

k8s搭建高可用spring-cloud eureka集群

  • 查找镜像
  • docker部署
    • 关闭安全认证
    • 开启安全认证
  • docker-compose方式部署
  • k8s方式部署
  • 使用helm部署集群
  • 注册服务,测试
    • 注册服务service-a
    • 注册服务service-b,调用a提供的接口

查找镜像

镜像地址: https://hub.docker.com/r/lhstack/eureka
k8s搭建高可用spring-cloud eureka集群_第1张图片
根据镜像地址的里面的文档,进行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

k8s搭建高可用spring-cloud eureka集群_第2张图片
k8s搭建高可用spring-cloud eureka集群_第3张图片

开启安全认证

# 关闭之前启动的容器,防止端口冲突
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即可
k8s搭建高可用spring-cloud eureka集群_第4张图片

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:[email protected]/eureka,http://admin:[email protected]/eureka,http://admin:[email protected]/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:[email protected]/eureka,http://admin:[email protected]/eureka,http://admin:[email protected]/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:[email protected]/eureka,http://admin:[email protected]/eureka,http://admin:[email protected]/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搭建高可用spring-cloud eureka集群_第5张图片
k8s搭建高可用spring-cloud eureka集群_第6张图片
k8s搭建高可用spring-cloud eureka集群_第7张图片
k8s搭建高可用spring-cloud eureka集群_第8张图片

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:[email protected]: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

k8s搭建高可用spring-cloud eureka集群_第9张图片
在这里插入图片描述
这里deployment作为eureka集群里面的客户端,用于承载业务服务的注册,保证statefulset的eureka集群的高可用
k8s搭建高可用spring-cloud eureka集群_第10张图片
k8s搭建高可用spring-cloud eureka集群_第11张图片
k8s搭建高可用spring-cloud eureka集群_第12张图片

使用helm部署集群

# 停止之前运行的eureka集群
kubectl delete -f eureka.yml
#拉取helm模板,需要git和helm环境
#git安装 yum install -y git
#helm安装https://github.com/helm/helm/releases

进入helm目录
k8s搭建高可用spring-cloud eureka集群_第13张图片
安装集群

helm install eureka-cluster .

k8s搭建高可用spring-cloud eureka集群_第14张图片
k8s搭建高可用spring-cloud eureka集群_第15张图片
多刷新几下,可以看到负载均衡的过程
k8s搭建高可用spring-cloud eureka集群_第16张图片
k8s搭建高可用spring-cloud eureka集群_第17张图片
k8s搭建高可用spring-cloud eureka集群_第18张图片
修改集群数量

helm upgrade --set replicas=5 eureka-cluster .

可以看到服务在滚动更新,增加新的,停掉老的
k8s搭建高可用spring-cloud eureka集群_第19张图片
k8s搭建高可用spring-cloud eureka集群_第20张图片

注册服务,测试

这里兼容2.x之后的版本哦

注册服务service-a

k8s搭建高可用spring-cloud eureka集群_第21张图片
k8s搭建高可用spring-cloud eureka集群_第22张图片

k8s搭建高可用spring-cloud eureka集群_第23张图片
k8s搭建高可用spring-cloud eureka集群_第24张图片

注册服务service-b,调用a提供的接口

k8s搭建高可用spring-cloud eureka集群_第25张图片
k8s搭建高可用spring-cloud eureka集群_第26张图片
k8s搭建高可用spring-cloud eureka集群_第27张图片
在这里插入图片描述
k8s搭建高可用spring-cloud eureka集群_第28张图片

k8s搭建高可用spring-cloud eureka集群_第29张图片

你可能感兴趣的:(k8s,eureka,docker,spring,cloud,k8s)