安装 ArgoCD

安装:

1
2
$ kubectl create namespace argocd
$ kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml

查看安装是否成功,当argocd命名空间下Pod状态都为Running时表示安装成功。

1
kubectl get pod -A

执行如下命令,将名为argocd-server的Service类型修改为NodePort。

1
2
$ kubectl patch svc argocd-server -n argocd -p '{"spec": {"type": "NodePort"}}'
service/argocd-server patched

查看修改结果。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$ kubectl -n argocd get svc
NAME                                      TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
argocd-applicationset-controller          ClusterIP   10.43.184.106   <none>        7000/TCP,8080/TCP            24h
argocd-dex-server                         ClusterIP   10.43.99.202    <none>        5556/TCP,5557/TCP,5558/TCP   24h
argocd-metrics                            ClusterIP   10.43.47.99     <none>        8082/TCP                     24h
argocd-notifications-controller-metrics   ClusterIP   10.43.58.49     <none>        9001/TCP                     24h
argocd-redis                              ClusterIP   10.43.70.36     <none>        6379/TCP                     24h
argocd-repo-server                        ClusterIP   10.43.40.224    <none>        8081/TCP,8084/TCP            24h
argocd-server                             NodePort    10.43.180.135   <none>        80:31912/TCP,443:32728/TCP   24h
argocd-server-metrics                     ClusterIP   10.43.5.195     <none>        8083/TCP                     24h

通过 argocd-server Service访问Argo CD,直接使用节点IP:端口号访问即可。例如本示例中,端口号为32728

登录用户名为admin,密码可使用如下命令获取。

1
$ kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d;echo

也可以通过下面命令修改密码:

1
$ argocd account update-password --account admin --current-password xxxx --new-password xxxx

安装 Argo CLI

安装Argo CD客户端

1
2
3
4
$ wget https://github.com/argoproj/argo-cd/releases/download/v2.11.0/argocd-linux-amd64
$ cp argocd-linux-amd64 /usr/local/bin/argocd
$ chmod +x /usr/local/bin/argocd
$ argocd version

使用

使用 Argo CLI

登录Argo服务端,用户名为 admin。

1
argocd login 192.168.1.107:32728 --username admin --password J22WcGIGcDnbmOtB

先查看集群:

1
2
3
$ kubectl config get-contexts
CURRENT   NAME      CLUSTER   AUTHINFO   NAMESPACE
*         default   default   default

添加集群:

1
argocd cluster add default --kubeconfig $HOME/.kube/config --name k3s

上面命令中,default 是集群上下文名称,$HOME/.kube/config 是kubectl配置文件所在路径,k3s 是集群在Argo CD中定义的名称。

添加 git 仓库,以公开的 https://github.com/yangchuansheng/argocd-lab 仓库为例,用户名和密码可以为空

1
2
# argocd repo add https://github.com/yangchuansheng/argocd-lab.git --username <username> --password <password>
argocd repo add https://github.com/yangchuansheng/argocd-lab.git

在服务器里访问 github 会超时,可以将该仓库导入自己搭建的 git 服务器,比如 gitness。例如:导入后的地址为 http://192.168.3.180:3000/git/test/argocd-lab.git,修改添加仓库命令为:

1
argocd repo add http://192.168.3.180:3000/git/test/argocd-lab.git

在Argo CD中添加应用。

1
argocd app create nginx --repo http://192.168.3.180:3000/git/test/argocd-lab.git --path dev --dest-server https://192.168.1.107:6443 --dest-namespace test

同步应用:

1
argocd app sync nginx

在 https://192.168.1.107:32728/applications 查看应用状态。

集群中可以查看到部署了一个 nginx 工作负载和一个s ervice。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
$ kubectl get all -n test
NAME                        READY   STATUS    RESTARTS   AGE
pod/test-64b8fff975-c9dp5   1/1     Running   0          6m37s
pod/test-64b8fff975-jztwd   1/1     Running   0          6m37s

NAME                   TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE
service/test-service   ClusterIP   10.43.54.34   <none>        80/TCP    6m37s

NAME                   READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/test   2/2     2            2           6m37s

NAME                              DESIRED   CURRENT   READY   AGE
replicaset.apps/test-64b8fff975   2         2         2       6m37s

使用 Argo Service

创建 Application

https://github.com/yangchuansheng/argocd-lab 项目作为示例,通过 kubectl 创建应用:

1
2
$ kubectl apply -f https://raw.githubusercontent.com/yangchuansheng/argocd-lab/main/application.yaml
application.argoproj.io/myapp-argo-application created

稍等片刻,在 Argo CD 可视化界面中可以看到应用已经创建成功了。

使用Argo Rollouts实现灰度发布

参考: https://support.huaweicloud.com/bestpractice-cce/cce_bestpractice_10007.html

参考文章