K8S 部署 SpringBoot 项目

首先准备 SpringBoot 测试镜像,略。

模板文件

准备一个模板文件 testspringboot-rc.yaml

apiVersion: v1
kind: ReplicationController
metadata:
  name: testspringboot  #必选,资源名称
spec:
  # 节点数,设置为多个可以实现负载均衡效果
  replicas: 1
  selector:
    app: testspringboot
  template:
    metadata:
      labels:
        app: testspringboot
    spec:
      containers:
      - name: demo
        #镜像名
        image: testspringboot
        #本地有镜像就不会去仓库拉取
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080

配置说明:
kind:资源类型,详情见附A
metadata: 元数据,用于备注资源信息
spec: specification of the resource content 指定该资源的内容
selector: 选择器,将选择具有label标签的资源作为管理范围
template: 资源模版定义
containerPort: 容器暴露给 K8S 的端口

部署到 K8S 集群

执行命令 kubectl create -f testspringboot-rc.yaml
或者在 Web UI (dashboard) 执行

暴露对外端口

    1. 创建 SVC 模板文件 testspringboot-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: testsvc
spec:
  type: NodePort
  ports:
  - port: 8080
    targetPort: 8080
    # 节点暴露给外部的端口(范围必须为30000-32767)
    nodePort: 30001
  selector:
    app: testspringboot
    1. 创建 Service
kubectl create -f testspringboot-svc.yaml
    1. 访问测试


      K8S 集群任意IP

附A:资源类型

类别 名称
资源对象 Pod、ReplicaSet、ReplicationController、Deployment、StatefulSet、DaemonSet、Job、CronJob、HorizontalPodAutoscaling
配置对象 Node、Namespace、Service、Secret、ConfigMap、Ingress、Label、ThirdPartyResource、 ServiceAccount
存储对象 Volume、Persistent Volume
策略对象 SecurityContext、ResourceQuota、LimitRange
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容