ArgoCD实践之基于配置清单创建Application

打印 上一主题 下一主题

主题 852|帖子 852|积分 2556

1. 什么是Application


  • ArgoCD的两个核心概念为Application和Project,他们可分别基于Application CRD和AppProject CRD创建;
  • 核心组件: Application Controller以及相关的一组CRD
  • 从本质来说,Application包含两个部分:
  • 一组在kubernetes上部署和运行某个应用的资源配置文件
  • 这组资源相关的source和destination
  • Source: 定义从何处获取资源配置文件,包RepoURL和配置文件所在的目录
  • Destination定义这组资源配置文件中定义的对象应该创建并运行于何处,其中的Cluster可以是ArgoCD所在集群之外的其他集群。
1.0 什么是基础不可变设施

GitOps当中是这样定义的。应用都需要运行在多台机器上,它们被组织成不同的环境,例如开发环境、测试环境和生产环境等等。需要将相同的应用部署到不同的机器上。通常需要系统管理员确保所有的机器都处于相同的状态。接着所有的修改、补丁、升级需要在所有的机器中进行。随着时间的推移,很难再确保所有的机器处于相同的状态,同时越来越容易出错。这就是传统的可变架构中经常出现的问题。这时我们有了不可变架构,它将整个机器环境打包成一个单一的不可变单元,而不是传统方式仅仅打包应用。这个单元包含了之前所说的整个环境栈和应用所有的修改、补丁和升级,这就解决了前面的问题。 —— 摘自 InfoQ 的《关于不可变架构以及为什么需要不可变架构》作者 百占辉
1.1 Application核心组件


  • ArgoCD Application还存在两个非常重要的属性: Sync Status和Health Status
Synced:一致
OutOfSync:不一致

  • Health Status: Application的健康状态,是各资源的健康状态的聚合信息
Healthy:健康
Degraded:降级
Missing:缺失,即在GitRepo中存在资源定义,但并未完成部署

2. ArgoCD Application的创建

ArgoCD可以基于WEB-UI的方式来进行应用的发布,也可以基于Configuration List的方式去部署应用。
2.1 查看ArgoCD支持的API-Resources
  1. kubectl api-resources --api-group=argoproj.io
  2. NAME              SHORTNAMES         APIVERSION             NAMESPACED   KIND
  3. applications      app,apps           argoproj.io/v1alpha1   true         Application
  4. applicationsets   appset,appsets     argoproj.io/v1alpha1   true         ApplicationSet
  5. appprojects       appproj,appprojs   argoproj.io/v1alpha1   true         AppProject
复制代码
2.2 查看ArgoCD的字段属性

explain可以分级查看字段属性
  1. [root@c-k-m1-10 argocd]# kubectl explain application
  2. KIND:     Application
  3. VERSION:  argoproj.io/v1alpha1
  4. DESCRIPTION:
  5.      Application is a definition of Application resource.
  6. FIELDS:
  7.    apiVersion        <string>
  8.      APIVersion defines the versioned schema of this representation of an
  9.      object. Servers should convert recognized schemas to the latest internal
  10.      value, and may reject unrecognized values. More info:
  11.      https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
  12.    kind        <string>
  13.      Kind is a string value representing the REST resource this object
  14.      represents. Servers may infer this from the endpoint the client submits
  15.      requests to. Cannot be updated. In CamelCase. More info:
  16.      https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
  17.    metadata        <Object> -required-
  18.      Standard object's metadata. More info:
  19.      https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
  20.    operation        <Object>
  21.      Operation contains information about a requested or running operation
  22.    spec        <Object> -required-
  23.      ApplicationSpec represents desired application state. Contains link to
  24.      repository with application definition and additional parameters link
  25.      definition revision.
  26.    status        <Object>
  27.      ApplicationStatus contains status information for the application
复制代码
2.3 准备Git源

GitOps中定义以特定Repository(配置仓库)为应用程序部署和管理的唯一可信源,该Repository负责定义Application的期望状态。本次测试使用gitee作为唯一的可信源。支持更多的配置管理工具例如helm、kustomize、jsonnet等;本次使用kubernetes原生的配置清单包含如下一个namespace一个裸Pod以及一个Service。
  1. kind: Namespace
  2. apiVersion: v1
  3. metadata:
  4.   name: hello
  5.   
  6. apiVersion: v1
  7. kind: Service
  8. metadata:
  9.   name: hello-svc
  10.   namespace: hello
  11. spec:
  12.   type: NodePort
  13.   selector:   
  14.     app: hello
  15.   ports:
  16.   - name: http         # 端口名称
  17.     protocol: TCP      # 协议类型,目前支持TCP、UDP、SCTP默认为TCP
  18.     port: 80           # Service的端口号
  19.     targetPort: 8080   # 后端目标进程的端口号
  20.     nodePort:
  21.        
  22. apiVersion: v1
  23. kind: Pod
  24. metadata:
  25.   name: hello
  26.   namespace: hello
  27.   labels:
  28.      app: hello
  29. spec:
  30.   containers:
  31.   - name: hello
  32.     image: lihuahaitang/helloworld:v1
  33.     imagePullPolicy: IfNotPresent
复制代码
2.4 编辑资源配置清单;
  1. [root@c-k-m1-10 argocd]# cat application-hello.yaml
  2. apiVersion: argoproj.io/v1alpha1   # 定义的API版本,可通过API-Resources查看
  3. kind: Application  # 定义的资源类型
  4. metadata:
  5.   name: hello  # 名称
  6.   namespace: argocd   # argocd所在的名称空间
  7. spec:
  8.   project: default   # 指明所属的项目是default
  9.   source:     # 配置仓库及相关的配置访问的方法
  10.     repoURL: https://gitee.com/good-news/apps.git   # 资源配置清单的Git的仓库源地址
  11.     targetRevision: HEAD                  # 期望基于哪个修订版本来部署
  12.     path: kubernetes    # Git仓库的子目录路径
  13.   destination:       # 应用程序要部署到的目标位置
  14.     server: https://kubernetes.default.svc     # 目标kubernetes集群的API-Server访问入口,这里为本地集群
  15.     namespace: hello          # 目标应用要部署的名称空间
  16.   syncPolicy:                 # 同步策略,如果不写默认就是Manual为手动同步
  17.     automated: null                # 为自动同步策略
复制代码
2.5 查看应用状态

这里的应用状态为未同步,因为我们未指定同步策略为自动。默认为手动同步;
  1. [root@c-k-m1-10 argocd]# argocd app list
  2. WARN[0000] Failed to invoke grpc call. Use flag --grpc-web in grpc calls. To avoid this warning message, use flag --grpc-web.
  3. NAME          CLUSTER                         NAMESPACE  PROJECT  STATUS  HEALTH  SYNCPOLICY  CONDITIONS  REPO                                  PATH        TARGET
  4. argocd/hello  https://kubernetes.default.svc  hello      default                  <none>      <none>      https://gitee.com/good-news/apps.git  kubernetes  HEAD
复制代码
2.6 手动执行同步策略
  1. [root@c-k-m1-10 argocd]# argocd app sync hello
  2. WARN[0000] Failed to invoke grpc call. Use flag --grpc-web in grpc calls. To avoid this warning message, use flag --grpc-web.
  3. TIMESTAMP                  GROUP        KIND   NAMESPACE                  NAME    STATUS   HEALTH        HOOK  MESSAGE
  4. 2023-03-25T22:00:35+08:00            Service     default                 hello   Unknown  Healthy              
  5. 2023-03-25T22:00:37+08:00            Service     default                 hello   Unknown  Healthy              ignored (requires pruning)
  6. 2023-03-25T22:00:37+08:00          Namespace       hello                 hello   Running   Synced              namespace/hello created
  7. 2023-03-25T22:00:37+08:00            Service       hello             hello-svc   Running   Synced              service/hello-svc created
  8. 2023-03-25T22:00:37+08:00                Pod       hello                 hello   Running   Synced              pod/hello created
  9. 2023-03-25T22:00:37+08:00            Service     default                 hello  OutOfSync  Healthy                  ignored (requires pruning)
  10. 2023-03-25T22:00:37+08:00            Service       hello             hello-svc  OutOfSync  Healthy                  service/hello-svc created
  11. 2023-03-25T22:00:37+08:00                Pod       hello                 hello    Synced   Progressing              pod/hello created
  12. 2023-03-25T22:00:37+08:00          Namespace                             hello    Synced                           
  13. Name:               argocd/hello
  14. Project:            default
  15. Server:             https://kubernetes.default.svc
  16. Namespace:          hello
  17. URL:                https://argocd.k8s.local/applications/hello
  18. Repo:               https://gitee.com/good-news/apps.git
  19. Target:             HEAD
  20. Path:               kubernetes
  21. SyncWindow:         Sync Allowed
  22. Sync Policy:        <none>
  23. Sync Status:        OutOfSync from HEAD (c916463)
  24. Health Status:      Healthy
  25. Operation:          Sync
  26. Sync Revision:      c916463463c2244ae78ba442a0de764b743a493b
  27. Phase:              Succeeded
  28. Start:              2023-03-25 22:00:34 +0800 CST
  29. Finished:           2023-03-25 22:00:37 +0800 CST
  30. Duration:           3s
  31. Message:            successfully synced (all tasks run)
  32. GROUP  KIND       NAMESPACE  NAME       STATUS     HEALTH   HOOK  MESSAGE
  33.        Service    default    hello      OutOfSync  Healthy        ignored (requires pruning)
  34.        Namespace  hello      hello      Running    Synced         namespace/hello created
  35.        Service    hello      hello-svc  OutOfSync  Healthy        service/hello-svc created
  36.        Pod        hello      hello      Synced     Healthy        pod/hello created
复制代码
2.7 查看名称空间的Pod以及Service
  1. [root@c-k-m1-10 argocd]# kubectl get po,svc -n hello
  2. NAME        READY   STATUS    RESTARTS   AGE
  3. pod/hello   1/1     Running   0          5m22s
  4. NAME                TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
  5. service/hello-svc   NodePort   xx.xx.xx.xx   <none>        80:32618/TCP   5m22s
复制代码
2.8 WEBUI查看应用状态


2.9 尝试访问应用
  1. sh-3.2# curl -I http://xx.xx.xx.xx32618/
  2. HTTP/1.1 200 OK
  3. Date: Sat, 25 Mar 2023 14:07:57 GMT
  4. Connection: keep-alive
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

小秦哥

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表