﻿# 在 VMWare Tanzu Community Edition 架設 Argo CD 服務的安裝筆記

昨天有人推薦我 [Argo CD](https://argoproj.github.io/cd/) 這套 Kubernetes 的持續部署神器，今天我在 [Tanzu](https://github.com/vmware-tanzu/community-edition) 嘗試架設 [Argo CD](https://argoproj.github.io/cd/) 起來，確實非常容易上手，相當不錯！這套 Argo CD 除了可以輕鬆的幫你實現 GitOps 流程，其 Web UI 的視覺化設計是個亮點，他可以幫助你更清晰的看懂應用程式的部署狀態，我覺得介面非常友善。這篇文章我特別記錄一下安裝與設定步驟。

## 架設一套 Tanzu 的 Unmanaged Clusters 非受控叢集

> 詳細安裝過程請參見 [用 Docker 架設 VMware Tanzu Community Edition 的安裝筆記](/post/2022/03/13/Install-VMware-Tanzu-Community-Edition-on-Docker-Notes) 文章。

1.  建立 Unmanaged Clusters 非受控叢集
    
    Windows
    
    ```bash
    tanzu uc create beepboop --cni=calico
    ```
    
    Linux
    
    ```bash
    tanzu uc create beepboop
    ```
    
2.  切換 KUBECONFIG 過去
    
    ```bash
    kubectl config use-context kind-beepboop
    ```
    

### 安裝 NGINX Ingress controller using Helm

1.  安裝 Helm
    
    Windows
    
    ```powershell
    # Install
    choco install kubernetes-helm -y
    
    # Upgrade
    choco upgrade kubernetes-helm -y
    ```
    
    Ubuntu Linux
    
    ```bash
    sudo snap install helm --classic
    ```
    
2.  安裝 `ingress-nginx` 套件
    
    加入 `ingress-nginx` Repo
    
    ```bash
    helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
    ```
    
    安裝 `ingress-nginx` Release
    
    ```bash
    helm install ingress-nginx ingress-nginx/ingress-nginx --set "controller.service.type=ClusterIP"
    
    kubectl get services
    kubectl get pods
    kubectl port-forward svc/ingress-nginx-controller 80:80
    ```
    
    > 因為 TCE 預設跑在 Docker 底下，不支援 `LoadBalancer` 服務類型，所以我直接用 `ClusterIP` 並搭配 [Port Forwarding](https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/) 進行連線！
    

### 安裝 Argo CD 服務

1.  建立 `argocd` 命名空間
    
    ```bash
    kubectl create namespace argocd
    ```
    
2.  建立服務資源
    
    ```bash
    kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
    ```
    
3.  建立 Ingress 連接 Argo CD 服務
    
    PowerShell
    
    ```powershell
    @'
    apiVersion:  networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: ingress-argocd
      namespace: argocd
      annotations:
        kubernetes.io/ingress.class: "nginx"
        nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
    spec:
      rules:
      - host: "argocd.localhost"
        http:
          paths:
          - path: /
            pathType: ImplementationSpecific
            backend:
              service:
                name: argocd-server
                port:
                  number: 443
    '@ | Out-File 'ingress-argocd.yaml' -Encoding UTF8
    ```
    
    Bash
    
    ```bash
    cat > ingress-argocd.yaml <<EOF
    apiVersion:  networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: ingress-argocd
      namespace: argocd
      annotations:
        kubernetes.io/ingress.class: "nginx"
        nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
    spec:
      rules:
      - host: "argocd.localhost"
        http:
          paths:
          - path: /
            pathType: ImplementationSpecific
            backend:
              service:
                name: argocd-server
                port:
                  number: 443
    EOF
    ```
    
    套用 YAML 進行安裝
    
    ```bash
    kubectl apply -f ingress-argocd.yaml
    kubectl get ingress
    ```
    
4.  取得 Argo CD 預設密碼
    
    ```bash
    kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d
    ```
    
    > 若用 Windows 請先用 `choco install base64 -y` 安裝 [Base64 encoder/decoder](https://community.chocolatey.org/packages/base64) 命令列工具！
    
5.  連接 Argo CD 服務
    
    http://argocd.localhost/
    
    > 預設帳號為 `admin`，而預設密碼請透過上個步驟取得。
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/a766953f4fd73261d250-159120350-5e263402-bb42-4538-9582-c103f806c380.png)
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/0d406e06b4d2699ea041-159120353-5398597c-03d5-4a52-b1aa-43460f2e9523.png)
    
6.  透過 Argo CD 部署第一個應用程式
    
    Settings > Repositories > Connect repo using HTTPS
    
    ```
    Project: default
    Repository: https://github.com/doggy8088/vote-argo.git
    ```
    
    Applications > NEW APP
    
    ```
    Application Name: vote-argo
    Project: default
    SYNC POLICY: Manual
    勾選 AUTO-CREATE NAMESPACE 核取項目
    
    SOURCE
    
    Repository URL: https://github.com/doggy8088/vote-argo.git
    Revision: master
    Path: manifests
    
    DESTINATION
    
    Cluster URL: https://kubernetes.default.svc
    
    Destination: in-cluster
    Namespace: vote-argo
    ```
    
    按下 CREATE 即可建立應用程式！
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/ab789fffb654c3bd4bcb-159120537-d28e207a-7ea7-455d-b139-e92806f0a22d.png)
    
    目前應用程式的狀態是 `OutOfSync`，這代表我們的 Git Repo 與 Argo CD 要部署的目標叢集之間是不一致的。
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/b01dee04b375b068ebb4-159120604-6e751e2f-5edc-48e7-b0d0-be88b6abc241.png)
    
7.  按下 **SYNC** 按鈕進行同步
    
    按下 **Sync** 之後會先讓你確定設定，再按下 **SYNCHORIZE** 按鈕就會開始同步 Git 與 Kubernetes 之間的狀態，這意味著應用程式將會開始部署！
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/ed0163eb335a08550861-159120657-93e572b0-c218-48d7-8b8b-b5d1638bc541.png)
    
    同步進行中可以看到完整且即時的狀態
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/084ebadfc79de7e4d0c7-159120690-edc96054-00be-4dca-a662-318876e8716a.png)
    
    部署完成就可以看到相當視覺化的部署結果！ 👍
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/3a779684f699083045da-159120721-963fcc83-d34d-4588-bb82-3394ea58b4aa.png)
    
8.  連接 `vote-argo` 服務
    
    http://vote-argo.localhost/
    
    如果一切都很順利的運作，應該會在瀏覽器看到以下畫面：
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/7024871f044b9aa2458e-159120780-11c162d4-f69b-4ff0-86bd-6f3ada4a20a0.png)
    
9.  啟用 `AUTO-SYNC` 特性並透過修改 Git Repo 中的內容觸發 Argo CD 自動部署
    
    點擊進 `vote-argo` 應用程式，點擊 **APP DETAILS** 按鈕，點擊 **ENABLE AUTO-SYNC** 按鈕即可啟用自動同步！
    
    修改 [https://github.com/doggy8088/vote-argo/blob/master/manifests/deployment.yml](https://github.com/doggy8088/vote-argo/blob/master/manifests/deployment.yml) 內容，調整 `replicas` 數量。
    
    接著 Argo CD 會每 3 分鐘自動檢查一次 Repo 是否有新的 Commit 出現，如果有 **不同步** (`OutOfSync`) 的狀態發生，就會自動進行同步，將變更同步到 Kubernetes 叢集中。
    

### 相關連結

-   https://github.com/doggy8088/vote-argo
-   [Ingress | Kubernetes](https://kubernetes.io/docs/concepts/services-networking/ingress/)
    -   [實現 Angular, Docker 與 Kubernetes 持續部署 - YouTube](https://www.youtube.com/watch?v=qS2A2QfyjVw)
    -   [How to enable default-http-backend in Kubernetes in order to make Ingress work? - Stack Overflow](https://stackoverflow.com/a/68266778/910074)
    -   [kubernetes - How do I make an ingress forward to an ssl port(443) if https traffic - Stack Overflow](https://stackoverflow.com/a/56203533/910074)
    -   [Installation with Helm | NGINX Ingress Controller](https://docs.nginx.com/nginx-ingress-controller/installation/installation-with-helm/)
-   Tanzu
    -   [Configure External Access to a Service in a Tanzu Kubernetes Cluster](https://docs.vmware.com/en/VMware-Cloud-Director/10.2/VMware-Cloud-Director-Tenant-Portal-Guide/GUID-57842ED7-4D1E-4210-9E8F-DDB0B6145BC4.html)
    -   [Quick Tip - Install Metallb as service load balancer with Tanzu Community Edition (TCE)](https://williamlam.com/2021/10/quick-tip-install-metallb-as-service-load-balancer-with-tanzu-community-edition-tce.html)
    -   [Tanzu Kubernetes Ingress Example Using Nginx](https://docs.vmware.com/en/VMware-vSphere/7.0/vmware-vsphere-with-tanzu/GUID-457B1569-DFDC-4849-959C-72EDA72030AD.html)
-   [Azure DevOps with GitOps : AKS and ArgoCD | LinkedIn](https://www.linkedin.com/pulse/azure-devops-gitops-aks-argocd-ajay-vikram-singh/)
-   Azure Sales Demo Kit
    -   [ArgoCD | Azure Sales Demo Kit](https://web.archive.org/web/20220808012910/https://azure.github.io/demokit/docs/kubernetes-devops/argocd/)
-   [Tanzu Kubernetes Ingress Example Using Nginx](https://docs.vmware.com/en/VMware-vSphere/7.0/vmware-vsphere-with-tanzu/GUID-457B1569-DFDC-4849-959C-72EDA72030AD.html)
-   [Quick Tip - Install Metallb as service load balancer with Tanzu Community Edition (TCE)](https://williamlam.com/2021/10/quick-tip-install-metallb-as-service-load-balancer-with-tanzu-community-edition-tce.html)
