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

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

开发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
    @Slf4j
    public 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 builder
    WORKDIR application
    ARG JAR_FILE=target/*.jar
    # 提取构建镜像
    RUN java -Djarmode=layertools -jar ${JAR_FILE} extract
    # 正式构建镜像
    FROM openjdk:8u212-jdk-stretch
    WORKDIR application
    COPY --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: v1
    kind: Service
    metadata:
    name: helloworld
    namespace: kubernetesclient
    spec:
    type: NodePort
    ports:
    - port: 8080
    nodePort: 30100
    selector:
    name: helloworld
    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
    namespace: kubernetesclient
    name: helloworld
    spec:
    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/

    你可能感兴趣的文章
    Network Dissection:Quantifying Interpretability of Deep Visual Representations(深层视觉表征的量化解释)
    查看>>
    Network Sniffer and Connection Analyzer
    查看>>
    NetworkX系列教程(11)-graph和其他数据格式转换
    查看>>
    Networkx读取军械调查-ITN综合传输网络?/读取GML文件
    查看>>
    Net与Flex入门
    查看>>
    net包之IPConn
    查看>>
    NFinal学习笔记 02—NFinalBuild
    查看>>
    NFS共享文件系统搭建
    查看>>
    nfs复习
    查看>>
    NFS网络文件系统
    查看>>
    nft文件传输_利用remoting实现文件传输-.NET教程,远程及网络应用
    查看>>
    ng 指令的自定义、使用
    查看>>
    nginx + etcd 动态负载均衡实践(二)—— 组件安装
    查看>>
    nginx + etcd 动态负载均衡实践(四)—— 基于confd实现
    查看>>
    Nginx + Spring Boot 实现负载均衡
    查看>>
    Nginx + uWSGI + Flask + Vhost
    查看>>
    Nginx - Header详解
    查看>>
    Nginx Location配置总结
    查看>>
    Nginx upstream性能优化
    查看>>
    Nginx 中解决跨域问题
    查看>>