day11 : argo gitops服务以及ingress (上)

花了好几天终於完成了所有的基础设施,接着就可以开始部署服务以及使用了,对於k8s来说要部署服务需要的其实只要yaml就可以了,最简单能够让外头使用的服务会需要几个yaml(deployment、service)基於istio还需要(virtualservice、gateway);而在我个人的认知上,布署服务的层面分为两个阶段,第一阶段是yaml管理,在这个阶段会将服务所需要的资源做成相对应的yaml并且管理好,第二阶段是operator,在这个阶段透过kubernetes的API通知k8s有resources需要新增异动,对於服务的布署,不太可能透过人为的方式用kubectl的指令或是api打给k8s,所以会仰赖cd流程去自动化的apply yaml的新增异动,那麽要如何自动的配置yaml就是今天的目标,

yaml的配置方法有很多种,新手入门的人可以简单的用shell的方式,优点是利用变数的方式填空很容易统一掌控服务的yaml,缺点就是不容易扩充而且有新功能的时候很容易叠着加上去,进阶一点的用法可以使用kustomize方式,优点是透过这样的方式扩充容易,而且是基於k8s而生具有较完整的支援,缺点就是如果没有管理好的情况,很可能让懂得使用的开发人员做出超出控制的yaml。

先来一段基础版

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-kubernetes
spec:
  replicas: 3
  selector:
    matchLabels:
      app: hello-kubernetes
  template:
    metadata:
      labels:
        app: hello-kubernetes
    spec:
      containers:
      - name: hello-kubernetes
        image: paulbouwer/hello-kubernetes:1.10
        ports:
        - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: hello-kubernetes
spec:
  type: ClusterIP
  ports:
  - port: 80
    targetPort: 8080
  selector:
    app: hello-kubernetes
---
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: hello-kubernetes
spec:
  selector:
    istio: ingressgateway
  servers:
    - port:
        number: 80
        name: http
        protocol: HTTP
      hosts:
        - hello-world.yuhlin.com.tw
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: hello-kubernetes
spec:
  hosts:
    - hello-world.yuhlin.com.tw
  gateways:
    - hello-kubernetes
  http:
    - route:
        - destination:
            port:
              number: 80
            host: hello-kubernetes

另外因为我的示例中并没有使用lb转送服务,所以我用nodeport的方式解释会需要用到下述指令

export INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].nodePort}')
export SECURE_INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="https")].nodePort}')
export TCP_INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="tcp")].nodePort}')

在简易设定好上面的指令後,设定ip和hostname到/etc/hosts,那麽观察一下浏览器,确实有成功的浏览到了呢,那麽前几天做的prometheus、loki等等有需要对外expose的服务也都可以依这个方式对外,而不是kubectl port-forward罗
https://ithelp.ithome.com.tw/upload/images/20210911/20139661qlMtRgCe4r.png

那麽就来进阶的用kustomize玩耍yaml吧,应用kustomize会需要先有一个base目录放置yaml的雏形
https://ithelp.ithome.com.tw/upload/images/20210911/20139661wGwE1dlZO7.png
像是图例中有基本的deployment、hpa、service
针对istio使用的vs、gw
对於openshift使用的route
kustomization.yaml中则是定义会运用到哪些base yaml。

准备好base後,就可以在另一个目录撰写kustomization.yaml说明服务需要依据base做哪些变动

namePrefix: hello-world
bases:
  - ../base
patches:
  - deployment.yaml
  - service.yaml
  - hpa.yaml
  - vs.yaml
  - gw.yaml

images:
- name: image-name
  newName: my-registry/yuhlin/hello-world
  newTag: 20210911v0

我以deployment和hpa为范例
https://ithelp.ithome.com.tw/upload/images/20210911/20139661LR2L8VPh7o.png
https://ithelp.ithome.com.tw/upload/images/20210911/20139661aSA6xASjLK.png
其实这个做法看起来跟填空几乎没有两样,但是可以轻易的多加入更多参数(env、volume等等...),接着将其他yaml 想要加入的参数写好,这样我在这个目录中的服务就可以再根据base和这个目录下的patches去调整要变动的项目罗。


<<:  (Day11) 物件参考特性

>>:  musl libc 简介与其 porting(一)

Day26 Cookie 的使用-1

cookie的使用方法: 这边我们用setcookie() 添加COOKIE值 setcookie(...

Day 10 学习线上服务思考用户的数位防身术-国内篇

昨天分享介绍国外线上服务思考用户的数位防身术设计方式,今天就回到国内来看看目前国内线上服务实作,分析...

第11章:控制背景运行服务介绍

前言 在上一章节中,介绍了有关於Linux作业系统上的process程序管理,以及取得主机上的CPU...

[Day 27] Bevy 游戏引擎 (Part 1)

昨天大概讲完 Rocket 的运用了 所以接下来就来介绍其他东西吧 这次要讲的是 Game Engi...

Day 20 - 研习计画之网站上线以及功能延伸开发篇

转眼间到11月网站也准备要上线了,回想当时很佩服研习生们有坚持住将该有的功能开发出来,另外昨日提到的...