Spring Cloud Kubernetes(K8s)的指南

1,956 阅读4分钟

Spring Cloud Kubernetes(K8s)指南

在本教程中,我们将学习如何使用Kubernetes和Spring Cloud来构建一个微服务应用程序,并将其与Spring Boot集成。Spring Cloud是一个Spring模块,为Spring框架提供RAD(快速应用开发)功能。

在Spring Cloud框架的协助下,我们可以轻松创建基于云的分配。

前提条件

  • 你应该在你的机器上本地安装[Minikube]。
  • 了解RESTful APIs。
  • 你应该熟悉设置应用程序集群节点。

教学目的

本教程将教会你有关Spring Boot微服务的一切知识,如何将它们与Kubernetes集成,并将它们部署在Minikube上。

开始学习

在本教程中,我们将建立一个简单的代理应用程序,为客户提供服务。这些客户被提供了一种方法来不时地查询机构服务。

这个项目将帮助我们理解一些基本概念,例如。

  • 使用Spring Cloud K8发现服务。
  • 如何使用Spring Cloud K8 Ribbon进行负载平衡。
  • 使用Spring Cloud K8-Config的ConfigMaps概念。

设置项目

在本节中,我们将使用VirtualBox VM driver ,在我们的开发机器上本地安装,请随意浏览或在给定的链接上搜索K8相关的主题。

让我们开始运行一个单节点的Kubernetes集群,如下所示。

minikube start --vm-driver=virtualbox

这个命令的作用是创建一个虚拟机,在我们的例子中,VirtualBox正在运行一个Minikube集群。

现在我们已经运行了Minikube,让我们把它连接到仪表盘上。

minikube dashboard

Dashboard screenshot

Kubernetes服务发现界面

正如前面所讨论的,这个项目将涵盖K8ServiceDiscovery 接口的实现。需要注意的是,微服务有多个pod运行一个服务。

如果我们有一个Spring Boot应用程序在同一个Kubernetes集群内的pod中运行,我们可以很容易地获取Kubernetes暴露的端点(服务作为一个集合)。

让我们继续,在我们的应用程序中设置这个服务。

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-kubernetes</artifactId>
</dependency>

在上面的XML 文件中,我们已经在客户端应用程序上定义了Spring Cloud starter Kubernetes的依赖项,以实现服务发现。

现在,让我们添加@EnableDiscoveryClient 注解,并使用@Autowired 在我们的控制器中注入ClientController ,如下所示。

@SpringBootApplication
//here is our service discovery
@EnableDiscoveryClient
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
// we then set it in our client controller class
@RestController
public class ClientController {
    @Autowired
    private DiscoveryClient discoveryClient;
}

创建MongoDB服务

在本节中,我们将定义我们的MongoDB服务和一个部署配置文件。首先,我们需要定义部署的秘密用户名和密码。

# application version 1
apiVersion: v1.0.0
# we're creating a secret for our database
kind: Secret
metadata:
  name: db-secret
data:
# specifiy the password and username
  username: johndoe
  password: mypassword

在定义了上述配置后,运行以下命令,将其添加到Kubernetes集群中。

kubectl apply -f secret.yaml

输出。

secret/db-secret created

继续在我们的部署YAML 文件中添加以下配置。

# api version definition
apiVersion: extensions/v1beta1
# we're deploying this application
kind: Deployment
metadata:
# creating MongoDB service
  name: mongo
spec:
# with a single replica
  replicas: 1
  template:
    metadata:
      labels:
        service: mongo
      name: mongodb-service
      # we're describing the object here in details
    spec:
      containers:
      - args:
        ...
        image: mongo:latest
        name: mongo
        env:
          - name: MONGO_ROOT_USERNAME
            valueFrom:
            # these are secret values(username) we defined in the previous section
              secretKeyRef:
                name: db-secret
                key: username
          - name: MONGO_ROOT_PASSWORD
            valueFrom:
              secretKeyRef:
              # this password is retrieved from the previous section
                name: db-secret
                key: password

这个部署配置文件创建了一个mongo: latest image 。它还设置了我们之前创建的用户名和密码,并带有一个默认的管理数据库。

在机构服务上设置MongoDB

让我们首先更新属性,添加数据库凭证,如下图所示。

# we're updating this properties file with the db-secret that created previously
spring.cloud.kubernetes.reload.enabled=true
# setting the secret name
spring.cloud.kubernetes.secrets.name=db-secret
# the db host
spring.data.mongodb.host=mongodb-service
# specifying the port to use
spring.data.mongodb.port=27017
# The database name here is defaulted to admin
spring.data.mongodb.database=admin
# for security, we hide the username and password as they are sensitive
spring.data.mongodb.username=${MONGO_USERNAME}
spring.data.mongodb.password=${MONGO_PASSWORD}

现在我们更好地了解这个应用程序是如何工作的,我们可以克隆完整的代码,在我们的机器上进行本地测试,或者在托管平台上玩玩代码。

部署Spring Cloud应用程序

在本节中,我们将探讨Spring云应用程序的部署。因此,我们在脚本文件中设置了我们的应用程序,如下图所示。每条命令都有一个注释,引导你了解它的作用。


..........
# these commands build the docker images on minikube
# cd into the service
cd travel-agency-service
# running the docker build
docker build -t travel-agency-service .
cd ../client-service
docker build -t client-service .
cd ..

# removing the services
kubectl delete -f travel-agency-service/secret.yaml
kubectl delete -f travel-agency-service/mongo-deployment.yaml
# sets up the secret and mongodb services in the .yaml file
kubectl create -f travel-agency-service/secret.yaml
kubectl create -f travel-agency-service/mongo-deployment.yaml

# setting up the travel-agency-service
kubectl delete -f travel-agency-service/travel-agency-deployment.yaml
kubectl create -f travel-agency-service/travel-agency-deployment.yaml

............
# run checks on pods to check if they are indeed running
kubectl get pods

总结

在本教程中,我们已经看到了如何设置Spring Cloud项目,并使用Spring Boot和Kubernetes来整合一个微服务平台应用,每一个都能提供很好的性能。