spring cloud kubernetes 学习记录(2):使用yaml配置

在上篇文章spring cloud kubernetes 学习记录(1):hello world 中,建立了hello-world项目,并用命令得方式在kubernetes中进行启动。
但命令得方式,还是太过于简单,这次,我们对 hello-world 项目进行 kubernetes 得yaml得配置。
首先,我们需要对上次生成得 deployment 与 service 进行清除,使用以下命令:

kubectl delete deployment hello-world
kubectl delete service hello-world

在清除之后,使用 get 命令进行查看,是否存在,不存在则为清除成功。
Docker镜像没有改动,所以不需要改动,下面开始编写yaml配置文件。

ymal

创建 hello-world.ymal 文件后,写入以下内容:

kind: Service
apiVersion: v1
metadata:
  name: hello-world
spec:
  selector:
    app: hello-world
  ports:
  - protocol: TCP
    port: 8080
    nodePort: 30001
  type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world
spec:
  selector:
    matchLabels:
      app: hello-world
  replicas: 3
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
        - name: hello-world
          image: hello_world:latest
          imagePullPolicy: Never
          ports:
            - containerPort: 8080

该配置文件内容,分为两个部分,--- 以上得为定义 service , --- 以下得为定义 deployment。

service

1、metadata.name:定义了 service 得名称。
2、selector.app: 定义了pod得名称
3、ports.protocol && ports.port :定义了pod得端口
4、ports.nodePort:定义了对外得端口,type 为 NodePort 需要在 30000-32767 中。
5、spec.type:定义了 service 得网络类型,NodePort 表示可以被外访问。

deployment

1、metadata.name:定义了 deployment 得名称。
2、spec.selector:定义了管理得pods,这里定义了匹配得app 为 hello-world (matchLabels.app: hello-world)
3、spec.replicas:定义了pod得服务数量,这里为开启3个pod服务
4、template:定义实际得pod
5、metadata.labels.app:定义pod得名称
6、spec.containers:定义pod得容器
7 、image :定义容器得镜像名称与版本
8、imagePullPolicy:定义拉取image得方式,这里为从本地拉取
9、ports.containerPort:定义pod打开得端口

yaml 格式参考: 使用YAML创建一个 Kubernetes Depolyment

定义好之后,执行以下命令进行部署

kubectl create -f hello-world.yaml

执行后,查看下部署得状态

deployment.png
pods.png
service.png

可以看到,pod 开启了3个,进行了负载均衡,并且service得端口为30001,不再是之前得随机数。

我们用上文得接口进行访问测试一下。


/.png

/services/.png

接口访问成功,yaml配置部署,就已经完成了。

kubernetes 负载均衡

这是扩展学习,上面我们开启了3个hello-world得pod,下面我们使用 kubernetes 得命令对 hello-world进行动态缩减与扩充。

使用下面得命令,将hello-world得服务缩减为0个:

kubectl scale --replicas=0 deployment/hello-world

执行完成之后,查看一下 deployment 和 pod 得状态:

deployment.png
pods.png

可以看到,hello-world得pod已经全部被关闭,尝试调用接口:


/.png

接口也无法调用,接下来我们将pod设置为1个:

kubectl scale --replicas=1 deployment/hello-world

等待一会,查看pod状态:


pod.png

可以看到启动了一个pod,尝试调用接口:


/.png

接口调用成功。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、基础架构 1.1 Master Master节点上面主要由四个模块组成:APIServer、scheduler...
    阿斯蒂芬2阅读 10,962评论 0 44
  • 一、 K8s 是什么? Kubernetes(k8s)是自动化容器操作的开源平台,这些操作包括部署,调度和节点集群...
    loveroot阅读 6,668评论 1 21
  • Kubernetes是Google开源的容器集群管理系统,其提供应用部署、维护、 扩展机制等功能,利用Kubern...
    devabel阅读 6,411评论 0 13
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,253评论 19 139
  • 读经日记第44篇 2018年9月24 星期一 天气:多云, 系统读经25周一天 共176天 读经...
    群策群辉阅读 198评论 0 0