博客
关于我
Kubernetes官方java客户端之四:内部应用
阅读量:412 次
发布时间:2019-03-06

本文共 4097 字,大约阅读时间需要 13 分钟。

开发Kubernetes环境内的Spring Boot应用

概述

本文将介绍如何开发一个完全部署在Kubernetes环境中的Spring Boot应用,该应用将使用Kubernetes官方Java客户端对Kubernetes API进行操作。

环境准备

为了顺利完成本文的实战,我们需要具备以下基础知识和工具:

  • Spring Boot的基本知识
  • Docker镜像制作
  • Kubernetes环境的探针技术
  • 项目结构

    项目的代码结构如下:

    kubernetesclient/├── pom.xml└── helloworld/    ├── DemoApplication.java    └── Dockerfile

    代码编写

    1. 创建Spring Boot应用

    以下是应用的主要代码:

    package com.bolingcavalry.demo;import com.google.gson.Gson;import io.kubernetes.client.openapi.ApiClient;import io.kubernetes.client.openapi.Configuration;import io.kubernetes.client.openapi.apis.CoreV1Api;import io.kubernetes.client.openapi.models.V1PodList;import io.kubernetes.client.util.Config;import lombok.extern.slf4j.Slf4j;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;import java.util.stream.Collectors;@SpringBootApplication@RestController@Slf4jpublic class DemoApplication {    public static void main(String[] args) {        SpringApplication.run(DemoApplication.class, args);    }    @RequestMapping(value = "/hello")    public List
    hello() throws Exception { ApiClient client = Config.defaultClient(); Configuration.setDefaultApiClient(client); CoreV1Api api = new CoreV1Api(); V1PodList v1PodList = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null); log.info("pod info \n{}", new Gson().toJson(v1PodList)); return v1PodList .getItems() .stream() .map(value -> value.getMetadata().getNamespace() + ":" + value.getMetadata().getName()) .collect(Collectors.toList()); }}

    2. Docker镜像构建

    以下是用于制作Docker镜像的Dockerfile:

    # 基础镜像FROM openjdk:8u212-jdk-stretch AS builderWORKDIR applicationARG JAR_FILE=target/*.jar# 提取构建镜像RUN java -Djarmode=layertools -jar ${JAR_FILE} extract# 正式构建镜像FROM openjdk:8u212-jdk-stretchWORKDIR applicationCOPY --from=builder application/dependencies/ .COPY --from=builder application/spring-boot-loader/ .COPY --from=builder application/snapshot-dependencies/ .COPY --from=builder application/application/ .ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]

    镜像构建与部署

    1. 在本地编译并构建镜像

    执行以下命令:

    mvn clean package -U -DskipTests

    2. 制作Docker镜像

    确保已安装Docker,执行以下命令:

    docker build -t 192.168.50.43:5888/common/helloworld:1.0-SNAPSHOT .

    3. 推送镜像到Harbor

    将镜像推送到Harbor仓库:

    docker push 192.168.50.43:5888/common/helloworld:1.0-SNAPSHOT

    Kubernetes部署

    1. 创建Deployment和Service

    以下是Kubernetes配置文件:

    apiVersion: v1kind: Servicemetadata:  name: helloworld  namespace: kubernetesclientspec:  type: NodePort  ports:  - port: 8080    nodePort: 30100  selector:    name: helloworldapiVersion: extensions/v1beta1kind: Deploymentmetadata:  namespace: kubernetesclient  name: helloworldspec:  replicas: 1  template:    metadata:      labels:        name: helloworld    spec:      serviceAccountName: kubernates-client-service-account      containers:        - name: helloworld          image: 192.168.50.43:5888/common/helloworld:1.0-SNAPSHOT          tty: true          livenessProbe:            httpGet:              path: /actuator/health/liveness              port: 8080            initialDelaySeconds: 5            failureThreshold: 10            timeoutSeconds: 10            periodSeconds: 5          readinessProbe:            httpGet:              path: /actuator/health/readiness              port: 8080            initialDelaySeconds: 5            timeoutSeconds: 10            periodSeconds: 5          ports:            - containerPort: 8080          resources:            requests:              memory: 512Mi              cpu: 100m            limits:              memory: 1Gi              cpu: 1000m

    2. 部署应用

    执行以下命令:

    kubectl apply -f helloworld.yaml

    测试与验证

    1. 查看Pod信息

    运行以下命令查看Pod信息:

    kubectl get pods

    2. 访问应用

    通过浏览器访问:

    http://192.168.50.135:30100/hello

    总结

    通过本文的实践,我们成功地将一个Spring Boot应用部署到了Kubernetes环境中,并使用Kubernetes官方Java客户端对API进行操作。这为后续学习Kubernetes客户端的更深入功能打下了基础。

    转载地址:http://uqtkz.baihongyu.com/

    你可能感兴趣的文章
    Redis五种核心数据结构的基本使用与应用场景
    查看>>
    Redis五种数据结构简介
    查看>>
    PHPCMS多文件上传和上传数量限制
    查看>>
    phpEnv的PHP集成环境
    查看>>
    PHPExcel一些基本设置总结
    查看>>
    phpexcel中文手册
    查看>>
    PHPExcel导入导出 若在thinkPHP3.2中使用(无论实例还是静态调用(如new classname或classname::function)都必须加反斜杠,因3.2就命名空间,如/c...
    查看>>
    phpize及其用法
    查看>>
    phpMailer发送邮件
    查看>>
    PHPMailer发送邮件
    查看>>
    phpmailer发送邮件,可以带附件
    查看>>
    phpmailer的用法
    查看>>
    phpMQTT
    查看>>
    phpmyadmin 安装
    查看>>
    phpmyadmin导出数据库出现Fatal error: Cannot 'break' 2 levels in D:\phpstudy\WWW\phpMyAdmin
    查看>>
    phpmyadmin数据库建表及插入
    查看>>
    phpnow配置
    查看>>
    phprpc简单使用
    查看>>
    phpspider中当爬虫获取数据时如何去掉广告
    查看>>
    phpstorm 2016.3.3 激活
    查看>>