# K8s 在 DevOps 中的作用

### DevOps

**Development + Operation ＝ DevOps，Dev: 開發部門，Ops ＝ 維運部門 。**

\[1]在傳統的軟體開發流程，往往是Dev speed > Ops speed 而造成一種gap，很可能在整個軟體發中，因為整體 Release Version 速度慢，以致難以去驗證功能的市場性，造成產能的過度浪費，常常開發出無用的功能。

DevOps 主要有五個階段：

1. Continuous Planning: 指的是透過使用者回饋或者其他部門帶來的需求。
2. Continuous Development: 指的是開發工程師持續的將需求完成，並完成建制。
3. Continuous Testing: 指的是開發好的 Image 自動的進行測試。
4. Continuous Integration: 將測試好的功能發版本並部屬上產品。
5. Continuous Monitoring: 持續的監測產品營運的狀況，持續的提出優化的需求

可參考下圖示例：

![](/files/mffqIUiA9ssDiyanY4u3)

K8s 的出現主要目的是解決Operation 上Continuous Integration、Continuous Monitoring 的問題，一般來說透過Git Repository  工具上的 CI/CD 工具，當工程師Push Code 時，將自動的執行所有的測試，並觸發K8s 工具，使的Code 可以更快速的反應在產品端。

### DevOps Pipeline

\[2]為了推行DevOps加速整個產品的開發週期，會由Dev與Ops 部門一起制訂出開發的Pipeline，盡量讓每一站點都可以自動化，一般來說是會由DevOps 工程師與Dev、QA一起制訂出來。下圖為一般pipeline 會制定的Pipeline，一般會經歷過幾個階段:

1. push code: Dev 工程師將程式碼發布到Repo 上
2. Build: 程式碼將經歷過 code 品質的測試，ex: lint，再來會經過unit test&#x20;
3. MR: Merge Request 這是唯一需要人工的步驟，由Code Reviewer 去審核且合併到Branch 上
4. Deploy: 指的是將build 階段的產物，發布到正式或者測試環境中
5. E2E Test: End to End Test 指的是直接透過終端的操作，整合測試功能
6. Monitoring: 正式發布後，持續的紀錄系統的 Log

下圖為Pipeline 流程圖：

![](/files/rQnXrXOA5gJ3mDY57tmf)

### GitOps

\[3]GitOps 是一個在Git Repository 上實現自動化更新K8s Cluster 的方式，透過Git CI/CD 進行自動化測試之後並把成功build 好的 image 傳入Image Registry 中並Trigger K8s 更新Deployment，如下圖所示。簡單來說GitOps 某種程度上減輕了Ops 許多的工作，讓開發之後自動的進行一系列的Ops，減輕原有Ops部門與Dev部門的溝通成本。

![](/files/aaPDOYxY3XlKF9OplyyD)

#### GitLab GitOps

在GitLab 中要做到GitOps 其實複雜蠻多的，首先要先具備2種Gitlab Runner:

1. Docker gitlab-runner: 使得Repo 可以進行 Unit test 和 build image 等功能。
2. K8s gitlab- runner:  使得Repo可以進行觸發進行dev 的環境部屬，建制Dev、Production 的環境

**Install Docker gitlab-runner**

* Prepare git-lab-runner config

```toml
concurrent = 1
check_interval = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "raspberry pi"
  url = "https://gitlab.com/"
  token = <Token>
  executor = "docker"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]
  [runners.docker]
    tls_verify = false
    image = "docker:latest"
    privileged = true  // MUST
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache", "/certs/client"] // MUST
    shm_size = 0
```

* 需要包含docker in docker 的功能，為了可以 build container image 並上傳於container registry 中

```shell
docker volume create gitlab-runner-config

docker run -d --name gitlab-runner --restart always  \
   -v /var/run/docker.sock:/var/run/docker.sock  \
   -v gitlab-runner-config:/etc/gitlab-runner \
   gitlab/gitlab-runner:latest
```

**Install GitLab Agent**

* Install k3s

```shell
curl -sfL https://get.k3s.io | INSTALL_K3S_CHANNEL=latest sh -
```

* At default branch add `config.yaml` :  內容可為空。

```shell
echo "" > .gitlab/agents/<FOLDER NAME>/config.yaml  
```

* 去Repo 的 infrastructure 新增Agent， 參考<https://www.youtube.com/watch?v=XuBpKtsgGkE>

**Install GitLab Runner**

* prepare helm
* prepare `runner-chart-values.yaml`

```yaml

gitlabUrl: https://gitlab.com/
runnerRegistrationToken: <YOUR TOKEN>
rbac:
    create: true
runners:
    privileged: true
```

* Install Runner

```shell
sudo helm --kubeconfig /etc/rancher/k3s/k3s.yaml \
    install --namespace gitlab-cd gitlab-runner \
    -f runner-chart-values.yaml gitlab/gitlab-runner
```

{% hint style="info" %}
The manipulations of gitlab-runner will not roll back status after running cmds.
{% endhint %}

### 參考資料

\[1] Virmani, Manish. "Understanding DevOps & bridging the gap from continuous integration to continuous delivery." *Fifth international conference on the innovative computing technology (intech 2015)*. IEEE, 2015.\
\[2] Ivanov, Vitalii, and Kari Smolander. "Implementation of a DevOps pipeline for serverless applications." *International conference on product-focused software process improvement*. Springer, Cham, 2018.\
\[3] Beetz, Florian, and Simon Harrer. "GitOps: The Evolution of DevOps?." *IEEE Software* (2021).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xiang753017.gitbook.io/zixiang-blog/k8s/k8s-zai-devops-zhong-de-zuo-yong.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
