(一)、Argo CD 概念介绍与基础安装

一、 Argocd

1.1、前言

Argo CD 是一个为 Kubernetes 而生的,遵循声明式 GitOps 理念的持续部署(CD)工具,它的配置和使用非常简单,并且自带一个简单易用的 Dashboard 页面,并且支持多种配置管理/模板工具(例如 Kustomize、Helm、Ksonnet、Jsonnet、plain-YAML)。
Argo CD 被实现为一个 Kubernetes 控制器,它持续监控正在运行的应用程序并将当前的实时状态与所需的目标状态(例如 Git 仓库中的配置)进行比较,在 Git 仓库更改时自动同步和部署应用程序。

1.1.1、架构图

Argo CD 在 CI/CD 流程中的位置如下图所示,Argo CD 的主要职责是 CD(Continuous Delivery,持续交付),将应用部署到 Kubernetes 等环境中,而 CI(Continuous Integration,持续集成)主要是交给 Jenkins,Gitlab CI 等工具来完成。

图片

这里简单介绍一下 GitOps 的概念,GitOps 这个词出现于 2017 年,是由 Weaveworks 公司根据多年云计算基础设施和应用程序管理经验而提出的一个概念,它是一种进行 Kubernetes 集群管理和应用程序交付的方式,GitOps 使用 Git 作为声明性基础设施和应用程序的单一事实来源。

GitOps 的核心思想是拥有一个 Git 仓库,包含目标环境中当前所需基础设施的声明性描述,以及使目标环境与 Git 仓库中描述的状态相匹配的自动化过程,Argo CD 就是一个遵循了 GitOps 理念的持续部署(CD)工具。

在 GitOps 模式的 CI/CD 流水线包含以下几个流程:

1.将应用的 Git 仓库分为 Application Deployment file 和 Docker file 两个库。 Docker file 用于存放应用的核心代码以及 Docker build file,后续将会直接打包成 Docker image;Application Deployment file 可以 Kustomize、Helm、Ksconnet、Jsonnet 等多种 Kubernetes 包管理工具来定义;以 Helm 为例,Chart 中所使用到的 Image 由 Docker file Code 打包完成后提供。
2.使用 Jenkins 或 Gitlab 等 CI 工具进行自动化构建打包,并将 Docker image push 到 Harbor 镜像仓库。
3.使用 Argo CD 部署应用。Argo CD 可以独立于集群之外,并且支持管理多个 Kubernetes 集群。在 Argo CD 上配置好应用部署的相关信息后 Argo CD 便可以正常工作,Argo CD 会自动和代码仓库 Application deployment file 的内容进行校验,当代码仓库中应用属性等信息发生变化时,Argo CD 会自动同步更新 Kubernetes 集群中的应用;应用启动时,会从 Harbor 镜像仓库拉取 Docker image。

Argo CD:一个持续交付工具,用于自动化部署应用到Kubernetes集群。

Argo Rollouts:用于渐进式交付(如金丝雀发布、蓝绿部署)的工具。

ArgoCD 控制方式

  • ArgoCD Web UI (网页窗口)
  • ArgoCD CLI (命令行)

以上两种我们都要安装。

1.2、在K8s中安装ArgoCD

1.2.1、下载yaml文件

https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml

[root@k8s-master argo-cd]# pwd
/root/argo-cd

[root@k8s-master argo-cd]# wget https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml

# 如果下载不下来,可以尝试使用一下的命令
export https_proxy=http://<IP>:<PORT>
export http_proxy=http://<IP>:<PORT>

unset http_proxy
unset https_proxy

1.2.2、Argo应用部署到K8s

# 创建namespace
kubectl create namespace argocd

# 应用k8s 文件
kubectl apply -n argocd -f install.yaml
customresourcedefinition.apiextensions.k8s.io/applications.argoproj.io created
customresourcedefinition.apiextensions.k8s.io/applicationsets.argoproj.io created
.........
networkpolicy.networking.k8s.io/argocd-repo-server-network-policy created
networkpolicy.networking.k8s.io/argocd-server-network-policy created

1.2.3、暴露 Argo CD

1.2.3.1、方法一:通过 NodePort 暴露 Argo CD

默认情况下,argocd-server 的 Service 类型为 ClusterIP,只能在集群内部访问。
若需要在集群外部访问 Argo CD,可以将其 Service 类型修改为 NodePort

图片

执行:

# NodePort暴露
kubectl patch svc argocd-server -n argocd -p '{"spec": {"type": "NodePort"}}'

图片

浏览器访问:https://192.168.6.90:30714

图片

1.2.3.2、方法二:配置 Ingess 域名访问

1.2.3.2.1、生成自签名证书
# 创建证书目录
mkdir /root/argo-cd/certs && cd /root/argo-cd/certs 

# 自签名证书
[root@k8s-master1 certs]# openssl req -x509 -newkey rsa:4096 -nodes -days 365 \
    -keyout privkey.pem -out fullchain.pem \
    -subj "/CN=argocd.zhukang.com"
Generating a 4096 bit RSA private key
..................++
.........................................................................................++
writing new private key to 'privkey.pem'
-----


[root@k8s-master1 certs]# ll
总用量 8
-rw-r--r-- 1 root root 1814 9月  27 22:07 fullchain.pem
-rw-r--r-- 1 root root 3272 9月  27 22:07 privkey.pem
1.2.3.2.2、创建 Secret‌
[root@k8s-master1 certs]#  kubectl create secret tls argocd-tls-secret -n argocd \
     --cert=/root/argo-cd/certs/fullchain.pem \
     --key=/root/argo-cd/certs/privkey.pem
secret/argocd-tls-secret created

    
# 查看 Secret 是否创建成功
kubectl get secret argocd-tls-secret -n argocd

# 检查证书内容
kubectl describe secret argocd-tls-secret -n argocd

图片

1.2.3.2.3、编写 Ingress 文件
[root@k8s-master1 argo-cd]# pwd
/root/argo-cd

[root@k8s-master1 argo-cd]# vim ingress.yaml 
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: argocd-ingress
  namespace: argocd
  annotations:
    nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"  # 后端使用 HTTPS
    nginx.ingress.kubernetes.io/ssl-passthrough: "true"     # 透传 HTTPS 流量
spec:
  ingressClassName: nginx       # 填写ingressclass
  rules:
  - host: argocd.zhukang.com   # 替换为你的域名
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: argocd-server  # 必须与 Service 名称完全一致
            port:
              number: 443       # 指向 Service 的 HTTPS 端口
  tls:                          # 配置 TLS 证书(可选,若需 HTTPS)
  - hosts:
    - argocd.zhukang.com
    secretName: argocd-tls-secret  # 证书 Secret 名称(需提前创建)

    
kubectl apply -f ingress.yaml

验证

kubectl  get ing -n argocd

图片

如果是服务器环境,在DNS服务商处添加记录,将域名 argocd.zhukang.net 解析到Ingress控制器的IP(如 Nginx Ingress 的 LoadBalancer IP 或节点 IP)。

当然我是本地调试安装的,我直接在etc/hosts文件下解析域名

# C:\Windows\System32\drivers\etc\hosts
192.168.6.90 argocd.zhukang.com

1.2.4、获取密码

执行命令,返回的一个字符串就是登陆密码
账号为:admin

[root@k8s-master1 argo-cd]# kubectl get secret -n argocd argocd-initial-admin-secret -o jsonpath={.data.password} | base64 --decode
BDLecmh5Yh17320a

浏览器访问:https://argocd.zhukang.com

图片

图片

图片

1.3、ArgoCD Cli 安装

# 下载argocd client
wget https://github.com/argoproj/argo-cd/releases/download/v2.12.7/argocd-linux-amd64
# 权限
chmod u+x argocd-linux-amd64
# 移动可执行文件
mv ./argocd-linux-amd64 /usr/local/bin/argocd
# 验证 
argocd version
-->
argocd: v2.12.7+4d70c51
  BuildDate: 2024-11-05T15:52:38Z
  GitCommit: 4d70c51e64e534ffe656c45317037b2bcdaa69f9
  GitTreeState: clean
  GoVersion: go1.22.8
  Compiler: gc
  Platform: linux/amd64
FATA[0000] Argo CD server address unspecified    

# 这里有个报错是因为没有指定 Argo CD server 的地址。
[root@k8s-master1 argo-cd]# argocd login argocd.zhukang.com --username admin --password BDLecmh5Yh17320a --insecure
WARN[0000] Failed to invoke grpc call. Use flag --grpc-web in grpc calls. To avoid this warning message, use flag --grpc-web. 
'admin:login' logged in successfully
Context 'argocd.zhukang.com' updated

警告的原因是 ArgoCD CLI 默认使用 gRPC 通信,但是你现在访问的 argocd.zhukang.com 是通过 Ingress/NodePort 暴露的 HTTP(S) 接口,它 只支持 gRPC-Web,而不是原生 gRPC。

解决方案:加上参数 --grpc-web

argocd login argocd.zhukang.com --username admin --password BDLecmh5Yh17320a --insecure --grpc-web

查看 Argocd 集群列表

argocd cluster list

图片

再次查看版本

argocd  version

图片

posted @ 2025-09-27 22:54  kennyy  阅读(58)  评论(0)    收藏  举报