ArgoCD實踐之基於配置清單建立Application

梨花海棠發表於2023-03-26

1. 什麼是Application

1.0 什麼是基礎不可變設施

GitOps當中是這樣定義的。應用都需要執行在多臺機器上,它們被組織成不同的環境,例如開發環境、測試環境和生產環境等等。需要將相同的應用部署到不同的機器上。通常需要系統管理員確保所有的機器都處於相同的狀態。接著所有的修改、補丁、升級需要在所有的機器中進行。隨著時間的推移,很難再確保所有的機器處於相同的狀態,同時越來越容易出錯。這就是傳統的可變架構中經常出現的問題。這時我們有了不可變架構,它將整個機器環境打包成一個單一的不可變單元,而不是傳統方式僅僅打包應用。這個單元包含了之前所說的整個環境棧和應用所有的修改、補丁和升級,這就解決了前面的問題。 —— 摘自 InfoQ 的《關於不可變架構以及為什麼需要不可變架構》作者 百佔輝

1.1 Application核心元件

Synced:一致
OutOfSync:不一致

Healthy:健康
Degraded:降級
Missing:缺失,即在GitRepo中存在資源定義,但並未完成部署
image

2. ArgoCD Application的建立

ArgoCD可以基於WEB-UI的方式來進行應用的釋出,也可以基於Configuration List的方式去部署應用。

2.1 檢視ArgoCD支援的API-Resources

kubectl api-resources --api-group=argoproj.io
NAME              SHORTNAMES         APIVERSION             NAMESPACED   KIND
applications      app,apps           argoproj.io/v1alpha1   true         Application
applicationsets   appset,appsets     argoproj.io/v1alpha1   true         ApplicationSet
appprojects       appproj,appprojs   argoproj.io/v1alpha1   true         AppProject

2.2 檢視ArgoCD的欄位屬性

explain可以分級檢視欄位屬性

[root@c-k-m1-10 argocd]# kubectl explain application
KIND:     Application
VERSION:  argoproj.io/v1alpha1

DESCRIPTION:
     Application is a definition of Application resource.

FIELDS:
   apiVersion	<string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

   kind	<string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

   metadata	<Object> -required-
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

   operation	<Object>
     Operation contains information about a requested or running operation

   spec	<Object> -required-
     ApplicationSpec represents desired application state. Contains link to
     repository with application definition and additional parameters link
     definition revision.

   status	<Object>
     ApplicationStatus contains status information for the application

2.3 準Git源

GitOps中定義以特定Repository(配置倉庫)為應用程式部署和管理的唯一可信源,該Repository負責定義Application的期望狀態。本次測試使用gitee作為唯一的可信源。支援更多的配置管理工具例如helm、kustomize、jsonnet等;本次使用kubernetes原生的配置清單包含如下一個namespace一個裸Pod以及一個Service。

kind: Namespace
apiVersion: v1
metadata:
  name: hello
  
apiVersion: v1
kind: Service
metadata:
  name: hello-svc
  namespace: hello
spec:
  type: NodePort
  selector:    
    app: hello
  ports:
  - name: http         # 埠名稱
    protocol: TCP      # 協議型別,目前支援TCP、UDP、SCTP預設為TCP
    port: 80           # Service的埠號
    targetPort: 8080   # 後端目標程式的埠號
    nodePort:
	
apiVersion: v1
kind: Pod
metadata:
  name: hello
  namespace: hello
  labels:
     app: hello
spec:
  containers:
  - name: hello
    image: lihuahaitang/helloworld:v1
    imagePullPolicy: IfNotPresent

2.4 編輯資源配置清單;

[root@c-k-m1-10 argocd]# cat application-hello.yaml
apiVersion: argoproj.io/v1alpha1   # 定義的API版本,可透過API-Resources檢視
kind: Application  # 定義的資源型別
metadata:
  name: hello  # 名稱
  namespace: argocd   # argocd所在的名稱空間
spec:
  project: default   # 指明所屬的專案是default
  source:     # 配置倉庫及相關的配置訪問的方法
    repoURL: https://gitee.com/good-news/apps.git   # 資源配置清單的Git的倉庫源地址
    targetRevision: HEAD                  # 期望基於哪個修訂版本來部署 
    path: kubernetes    # Git倉庫的子目錄路徑
  destination:       # 應用程式要部署到的目標位置
    server: https://kubernetes.default.svc     # 目標kubernetes叢集的API-Server訪問入口,這裡為本地叢集
    namespace: hello          # 目標應用要部署的名稱空間
  syncPolicy:                 # 同步策略,如果不寫預設就是Manual為手動同步
    automated: null                # 為自動同步策略

2.5 檢視應用狀態

這裡的應用狀態為未同步,因為我們未指定同步策略為自動。預設為手動同步;

[root@c-k-m1-10 argocd]# argocd app list
WARN[0000] Failed to invoke grpc call. Use flag --grpc-web in grpc calls. To avoid this warning message, use flag --grpc-web. 
NAME          CLUSTER                         NAMESPACE  PROJECT  STATUS  HEALTH  SYNCPOLICY  CONDITIONS  REPO                                  PATH        TARGET
argocd/hello  https://kubernetes.default.svc  hello      default                  <none>      <none>      https://gitee.com/good-news/apps.git  kubernetes  HEAD

2.6 手動執行同步策略

[root@c-k-m1-10 argocd]# argocd app sync hello
WARN[0000] Failed to invoke grpc call. Use flag --grpc-web in grpc calls. To avoid this warning message, use flag --grpc-web. 
TIMESTAMP                  GROUP        KIND   NAMESPACE                  NAME    STATUS   HEALTH        HOOK  MESSAGE
2023-03-25T22:00:35+08:00            Service     default                 hello   Unknown  Healthy              
2023-03-25T22:00:37+08:00            Service     default                 hello   Unknown  Healthy              ignored (requires pruning)
2023-03-25T22:00:37+08:00          Namespace       hello                 hello   Running   Synced              namespace/hello created
2023-03-25T22:00:37+08:00            Service       hello             hello-svc   Running   Synced              service/hello-svc created
2023-03-25T22:00:37+08:00                Pod       hello                 hello   Running   Synced              pod/hello created
2023-03-25T22:00:37+08:00            Service     default                 hello  OutOfSync  Healthy                  ignored (requires pruning)
2023-03-25T22:00:37+08:00            Service       hello             hello-svc  OutOfSync  Healthy                  service/hello-svc created
2023-03-25T22:00:37+08:00                Pod       hello                 hello    Synced   Progressing              pod/hello created
2023-03-25T22:00:37+08:00          Namespace                             hello    Synced                            

Name:               argocd/hello
Project:            default
Server:             https://kubernetes.default.svc
Namespace:          hello
URL:                https://argocd.k8s.local/applications/hello
Repo:               https://gitee.com/good-news/apps.git
Target:             HEAD
Path:               kubernetes
SyncWindow:         Sync Allowed
Sync Policy:        <none>
Sync Status:        OutOfSync from HEAD (c916463)
Health Status:      Healthy

Operation:          Sync
Sync Revision:      c916463463c2244ae78ba442a0de764b743a493b
Phase:              Succeeded
Start:              2023-03-25 22:00:34 +0800 CST
Finished:           2023-03-25 22:00:37 +0800 CST
Duration:           3s
Message:            successfully synced (all tasks run)

GROUP  KIND       NAMESPACE  NAME       STATUS     HEALTH   HOOK  MESSAGE
       Service    default    hello      OutOfSync  Healthy        ignored (requires pruning)
       Namespace  hello      hello      Running    Synced         namespace/hello created
       Service    hello      hello-svc  OutOfSync  Healthy        service/hello-svc created
       Pod        hello      hello      Synced     Healthy        pod/hello created

2.7 檢視名稱空間的Pod以及Service

[root@c-k-m1-10 argocd]# kubectl get po,svc -n hello 
NAME        READY   STATUS    RESTARTS   AGE
pod/hello   1/1     Running   0          5m22s

NAME                TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
service/hello-svc   NodePort   xx.xx.xx.xx   <none>        80:32618/TCP   5m22s

2.8 WEBUI檢視應用狀態

image

2.9 嘗試訪問應用

sh-3.2# curl -I http://xx.xx.xx.xx32618/
HTTP/1.1 200 OK
Date: Sat, 25 Mar 2023 14:07:57 GMT
Connection: keep-alive

相關文章