一.GitOps概述
1.什么是gitops?
GitOps是DevOps的一种特有的持续交付方式,GitOps核心思想是将应用系统的声明性基础架构和应用程序放在Git版本库中。
将Git作为交付流水线的核心,每个开发人员都可以提交拉取请求(Pull Request)并使用Git来加速和简化应用程序部署和运维任务,通过使用Git,开发人员可以更高效地将注意力集中在创新功能而不是运维相关任务上,例如系统安装,配置,迁移等。
在GitOps中可以自由地为流水线(Pipeline)的不同部分选择最佳工具,可以从开源生态系统重选择一组工具,也可以从闭源中选择一组工具,或者根据使用情况,甚至可以将它们组合在一起。
2.整体的架构图
二.部署之环境准备
1.主机角色规划
2.部署gitlab
#后续更新插入链接
3.部署harbor
#后续更新插入链接
3.部署k8s集群相关组件
3.0部署k8s集群
#后续更新插入链接
3.1部署metalLB
#后续更新插入链接
3.2部署ingress-nginx
#后续更新插入链接
三.部署ArgoCD默认启用tls环境基于NodePort暴露
1.Argo概述
Argo CD是Kubernetes的一个声明性GitOps持续交付工具。
官方链接:
https://argo-cd.readthedocs.io/en/stable/
github地址:
https://github.com/argoproj/argo-cd
2. 为什么选择Argo CD
应用程序定义、配置和环境应该是声明性的和版本控制的。
应用程序部署和生命周期管理应该是自动化的、可审计的、易于理解的。
Argo CD被实现为Kubernetes控制器,它持续监控运行中的应用程序,并将当前的实时状态与所需的目标状态(如Git仓库中指定的)进行比较。活动状态偏离目标状态的已部署应用程序被视为OutOfSync。
Argo CD报告并可视化差异,同时提供自动或手动将实时状态同步回所需目标状态的设施。在Git仓库中对所需目标状态所做的任何修改都可以自动应用并反映在指定的目标环境中。
3.Argo CD和K8S版本对应关系
如上图所示,咱们课堂环境基于K8S 1.23.17部署,则考虑的般般范围有限,目前能兼容的版本是Argo 2.7版本。
推荐阅读:
https://argo-cd.readthedocs.io/en/release-2.7/operator-manual/installation/ #supported-versions
https://argo-cd.readthedocs.io/en/release-2.9/operator-manual/installation/ #tested-versions4.部署Argo CD
#我选择第一种正常部署,没有选择高可用部署,我的虚拟机配置不是太高!
4.1 创建名称空间
[root@master231 ~]# kubectl create namespace argocd
namespace/argocd created
[root@master231 ~]#
4.2 下载资源清单
[root@master231 ~]# wget https://raw.githubusercontent.com/argoproj/argo-cd/v2.7.17/manifests/ha/install.yaml
4.3 部署应用
[root@master231 ~]# kubectl apply -n argocd -f install.yaml
4.4 检查pod的运行状态
[root@master231 ~]# kubectl get pods -n argocd -o wide
NAME READY STATUS RESTARTS AGE
argocd-application-controller-0 1/1 Running 0 4h47m
argocd-applicationset-controller-7b6f65b969-rt72x 1/1 Running 0 4h47m
argocd-dex-server-5589648d46-nm6s8 1/1 Running 0 4h47m
argocd-notifications-controller-575c44bfb5-ncqrg 1/1 Running 0 4h47m
argocd-redis-594b788d76-bc5h8 1/1 Running 0 4h47m
argocd-repo-server-757dc59b79-tdt4m 1/1 Running 0 4h47m
argocd-server-7c8f64b6fc-hc76n 1/1 Running 0 4h47m
4.5 修改svc的类型
[root@master231 ~]# kubectl -n argocd get svc argocd-server
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
argocd-server ClusterIP 10.200.228.98 <none> 80/TCP,443/TCP 12m
[root@master231 ~]#
[root@master231 ~]#
[root@master231 ~]# kubectl -n argocd edit svc argocd-server
...
ports:
...
type: NodePort
...
[root@master231 ~]# kubectl -n argocd get svc argocd-server
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
argocd-server NodePort 10.200.206.212 <none> 80:31988/TCP,443:31823/TCP 12m
[root@master231 ~]#
4.6登录测试
1.查看ArgoCD默认的密码
[root@master231 ~]# kubectl -n argocd get secrets argocd-initial-admin-secret -o jsonpath={.data.password} | base64 -d | more
pzMRCHAGnO39GVJC
[root@master231 ~]#
2.登录ArgoCD
如上图所示,使用默认的用户名admin进行登录,登录成功后就看到下图的情况。
四.基于ingress-nginx暴漏服务
1.修改资源清单(禁用TLS证书)
---
17589 apiVersion: apps/v1
17590 kind: Deployment
17591 metadata:
17592 labels:
.......
17596 name: argocd-server
17597 spec:
17598 selector:
17599 matchLabels:
17600 app.kubernetes.io/name: argocd-server
17601 template:
17602 metadata:
17603 labels:
17604 app.kubernetes.io/name: argocd-server
17605 spec:
17606 affinity:
17607 podAntiAffinity:
......
17621 containers:
17622 - args:
17623 - /usr/local/bin/argocd-server
#添加"--insecure"启动参数,不然就会老是跳转(307)到https,无法访问
17624 - --insecure
17625 env:
17626 - name: ARGOCD_SERVER_INSECURE
2. 创建资源并查看pod列表
root@master231:~# kubectl get pod
NAME READY STATUS RESTARTS AGE
argocd-application-controller-0 1/1 Running 0 3h58m
argocd-applicationset-controller-7b6f65b969-rt72x 1/1 Running 0 3h58m
argocd-dex-server-5589648d46-nm6s8 1/1 Running 0 3h58m
argocd-notifications-controller-575c44bfb5-ncqrg 1/1 Running 0 3h58m
argocd-redis-594b788d76-bc5h8 1/1 Running 0 3h58m
argocd-repo-server-757dc59b79-tdt4m 1/1 Running 0 3h58m
argocd-server-7c8f64b6fc-hc76n 1/1 Running 0 3h58m
3.创建ingress资源清单3.1编写ingress资源清单
root@master231:~# cat ingress-argocd.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-argocd
namespace: default
spec:
ingressClassName: nginx
rules:
- host: argocd.cqx.com
http:
paths:
- backend:
service:
name: argocd-server
port:
number: 80
path: /
pathType: Prefix
3.2创建资源
root@master231:~# kubectl apply -f ingress-argocd.yaml3.3查看ingress详细信息
root@master231:~# kubectl describe -f ingress-argocd.yaml
Name: ingress-argocd
Labels: <none>
Namespace: argocd
Address: 10.0.0.150
Default backend: default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
Host Path Backends
---- ---- --------
argocd.cqx.com
/ argocd-server:80 (10.100.2.199:8080) #argocd-server pod ip地址
Annotations: <none>
Events: <none>
3.4 基于ingress-nginx访问argocd服务(宿主机要做hosts解析)
#查看ingress中EXTERNAL-IP地址
root@master231:~# kubectl -n ingress-nginx get svc myingress-ingress-nginx-controller
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
myingress-ingress-nginx-controller LoadBalancer 10.200.44.37 10.0.0.150 80:30777/TCP,443:31572/TCP 8d
#查看argocd-server的svc
root@master231:~# kubectl -n argocd get svc argocd-server
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
argocd-server ClusterIP 10.200.105.90 <none> 80/TCP,443/TCP 4h12m
#查看argocd的登录密码
root@master231:~# kubectl get secrets argocd-initial-admin-secret -o jsonpath={.data.password} | base64 -d | more
sJhvr9eKYWHjrxiT
五.ArgoCD客户端工具修改初始化密码
1.查看默认的初始化密码
[root@master231 ~]# kubectl get svc -n argocd argocd-server
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
argocd-server ClusterIP 10.200.105.90 <none> 80/TCP,443/TCP 2m33s
[root@master231 ~]#
[root@master231 ~]# kubectl -n argocd get secrets argocd-initial-admin-secret -o jsonpath={.data.password} | base64 -d | more
fpMTrVf54cxI4dL6
[root@master231 ~]#
2.添加svc的IP地址到host文件解析
[root@master231 ~]# tail -1 /etc/hosts
10.200.248.216 argocd.yinzhengjie.com
[root@master231 ~]#3.登录argocd
[root@master231 ~]# argocd login --plaintext argocd.cqx.com
Username: admin
Password: # 注意哈,此处输入上面我们的密码即可。
'admin:login' logged in successfully
Context 'argocd.cqx.com' updated
[root@master231 ~]#4.退出登录
[root@master231 ~]# argocd logout argocd.cqx.com
Logged out from 'argocd.cqx.com'
[root@master231 ~]#
5.基于命令行免交互登录
[root@master231 ~]# argocd login --plaintext argocd.cqx.com --username admin --password fpMTrVf54cxI4dL6
'admin:login' logged in successfully
Context 'argocd.cqx.com' updated
[root@master231 ~]#
6.此时浏览器退出登录,需要重新输入账号密码进行登录
6.1 先登录
[root@master231 ~]# argocd login --plaintext argocd.cqx.com --username admin --password fpMTrVf54cxI4dL6
'admin:login' logged in successfully
Context 'argocd.cqx.com' updated
[root@master231 ~]#
6.2 后修改密码
[root@master231 ~]# argocd account update-password --account admin --current-password fpMTrVf54cxI4dL6 --new-password ******#$
Password updated
Context 'argocd.cqx.com' updated
[root@master231 ~]#
6.3 使用新密码登录 如下图所示。
相关参数说明:
--server:
表示指定Argo服务器的地址。
--plaintext:
表示Argo服务器禁用tls加密。