前言

安裝Argocd
-
準備一個k8s叢集,然後從官網獲取yaml部署清單執行部署即可;
# 建立名稱空間
kubectl create namespace argocd
# 部署argocd
wget
https:
/
/raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install
.yaml
kubectl apply -n argocd -f install.yaml
# 檢視狀態
kubectl get pod -n argocd -w
# 調整下svc為nodeport預設,訪問ui頁面用的
kubectl -n argocd edit svc argocd-server
-
訪問頁面,輸入地址後會自動跳轉https;
# 獲取ui頁面的登陸密碼,管理員為 admin
kubectl -n argocd
get
secret argocd-initial-admin-secret -o jsonpath=
"{.data.password}"
| base64 -d
# 線上獲取bcrpyt加密後的密碼值: https://www.bejson.com/encrypt/bcrpyt_encode/
準備一個密碼,自行到上面的地址加密即可
# 將加密後的密文替換為下面的 admin.password 的值
kubectl -n argocd patch secret argocd-secret \
-p '{
"stringData"
: {
"admin.password"
:
"$2a$10$dVCUtDIFah893qSLMMIReeyNa8vHx1112/kLYTbglAQMpbzBR5dbK"
,
"admin.passwordMtime"
:
"'$(date +%FT%T%Z)'"
}}'
準備一個helm Chart
# 準備一個主目錄存放資原始檔
mkdir
gitops &&
cd
gitops/ && git init
mkdir
-p {helm,argocd} &&
cd
helm/
# 新增一個helm chart
# tree -L 2
└── app1
├── Chart.yaml
├── templates
│ ├── deployment.yaml
│ └── service.yaml
└── values.yaml
apiVersion:
v2
appVersion:"1.0"
name:
app1
description:
app1
for
kubernetes
type:
application
version:0.1
.
0
global:
replicas:2
image:
repository:harbor.example.cn/public/nginx
#repository: nginx
tag:stable-alpine
apiVersion:apps/v1
kind:Deployment
metadata:
name:app
namespace:
{{
.Release.Namespace
}}
spec:
replicas:
{{
.Values.global.replicas
}}
selector:
matchLabels:
demo:app1
template:
metadata:
labels:
demo:app1
spec:
containers:
-name:nginx
# 這裡引入變數,層級呼叫,.Values表宣告作用
image:
{{
.Values.image.repository
}}
:{{.Values.image.tag}}
ports:
-containerPort:80
name:http
apiVersion:v1
kind:Service
metadata:
name:app1-svc
namespace:
{{
.Release.Namespace
}}
labels:
demo:app1
spec:
type:ClusterIP
selector:
demo:app1
ports:
-port:80
name:http
targetPort:http
protocol:TCP
定義argocd清單
-
argocd有自己的宣告式寫法,這裡定義一個用來呼叫和管理應用部署的資源清單;
# 進入argocd的存放目錄,在下面建立4個目錄來區分不同的叢集環境
cd
gitops/argocd &&
mkdir
{dev,
test
,pre,prod}
# 定義清單檔案
cd
argocd /
tree -L 2
.
├── dev
│ └── Application.yaml
├── pre
│ └── Application.yaml
├── prod
│ └── Application.yaml
└──
test
└── Application.yaml
apiVersion:argoproj.io/v1alpha1
kind:Application
metadata:
name:demo-argocd
namespace:argocd
spec:
project:default# 所屬專案,預設即可
source:
repoURL:https://git.example.com/gitops/gitops1.git# helm所在的git倉庫地址(後面需要推送的git倉庫)
# git倉庫的分支版本
targetRevision:HEAD
# helm檔案在git倉庫的相對路徑
path:helm/app1
# 這裡定義傳遞給helm執行的引數
# 型別values.yaml
helm:
parameters:
-name:image.repository
value:harbor.example.cn/public/nginx
-name:image.tag
value:stable-alpine
-name:global.replicas
value:"2"
destination:
# 部署到當前k8s叢集的地址
server:https://kubernetes.default.svc
# 部署目標名稱空間
namespace:demo
syncPolicy:
automated:
prune:true
selfHeal:true
allowEmpty:false
syncOptions:
-Validate=false
-CreateNamespace=true
-PrunePropagationPolicy=foreground
-PruneLast=true
retry:
limit:5
backoff:
factor:2
maxDuration:1m
-
推送Git倉庫
# 整體目錄檔案如下
gitops
# tree -L 3
.
├── argocd
│ ├── dev
│ │ └── Application.yaml
│ ├── pre
│ │ └── Application.yaml
│ ├── prod
│ │ └── Application.yaml
│ └── test
│ └── Application.yaml
└── helm
└── app1
├── Chart.yaml
├── templates
└── values.yaml
# 推送git倉庫
git
init
git
add
..
git ...
整合argocd
-
登陸argocd頁面,新增一個git倉庫地址,這裡的大概流程是透過同步git上的argocd資源清單來引用helm的部署檔案來實現部署和更新;


-
填寫倉庫地址資訊;


-
接著建立一個應用,定義git倉庫配置;





-
此時資源是沒有開啟自動同步的,需要先手動同步一下;



-
檢視應用的部署狀態和Git同步狀態;

-
成功部署後,將 demo-production 調整為自動同步;


版本更新
-
demo-production :與dev/Application.yaml 檔案同步,用來更新應用的版本和配置的;
-
demo-argocd :定義服務的部署,讀取的是git倉庫下的helm資源清單;

# vim argocd/dev/Application.yaml
---
apiVersion:argoproj.io/v1alpha1
kind:Application
metadata:
name:demo-argocd
namespace:argocd
spec:
project:default
source:
repoURL:https://git.example.com/gitops/gitops1.git
targetRevision:HEAD
path:helm/app1
helm:
parameters:
-name:image.repository
value:harbor.example.cn/public/nginx
-name:image.tag
value:v1.0.0# 修改映象的版本
-name:global.replicas
value:"1"# 修改下副本數
destination:
server:https://kubernetes.default.svc
namespace:demo
syncPolicy:
automated:
prune:true
selfHeal:true
allowEmpty:false
syncOptions:
-Validate=false
-CreateNamespace=true
-PrunePropagationPolicy=foreground
-PruneLast=true
retry:
limit:5
backoff:
factor:2
maxDuration:1m



馬哥教育
三月IT充電計劃

