1:环境
kubernetes | 1.23.3 |
---|---|
ArgoCD | 2.3.3 |
2:ArgoCD介绍
Argo CD is a declarative, GitOps continuous delivery tool for Kubernetes. Argo CD是一个基于Kubernetes的声明式的GitOps工具。 那么,什么是GitOps呢? GitOps是以Git为基础,使用CI/CD来更新运行在云原生环境的应用,它秉承了DevOps的核心理念--“构建它并交付它(you built it you ship it)”。
它的优势总结如下: 1:应用定义、配置和环境信息是声明式的,并且可以进行版本控制; 2:应用部署和生命周期管理是全自动化的,是可审计的,清晰易懂; 3:Argo CD是一个独立的部署工具,支持对多个环境、多个Kubernetes集群上的应用进行统一部署和管理
3:部署ArgoCD
# 安装很简单,不过在实际使用中需要对数据进行持久化。
1:创建一个命名空间存放argocd的Pod
[root@k8s-master ~]# kubectl create ns argocd
namespace/argocd created
2:通过官方命令部署(镜像在国外,需要梯子,或者加速也行)
[root@k8s-master ~]# kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
3:查看部署状态
[root@k8s-master ~]# kubectl get pod,svc -n argocd
NAME READY STATUS RESTARTS AGE
pod/argocd-application-controller-0 1/1 Running 0 60s
pod/argocd-applicationset-controller-79f97597cb-mwzld 1/1 Running 0 62s
pod/argocd-dex-server-6fd8b59f5b-zx76f 1/1 Running 0 62s
pod/argocd-notifications-controller-5549f47758-2rgjk 1/1 Running 0 61s
pod/argocd-redis-79bdbdf78f-xhd8f 1/1 Running 0 61s
pod/argocd-repo-server-5569c7b657-t5ftv 1/1 Running 0 61s
pod/argocd-server-664b7c6878-9tjlh 1/1 Running 0 61s
4:因为需要访问,我们可以通过NodePort或者Ingress暴露 argocd-server
# 我这里使用 NodePort
# 但是我们需要修改一下argocd-server的暴露方式为NodePort
[root@k8s-master argocd]# kubectl edit svc -n argocd argocd-server
......
selector:
app.kubernetes.io/name: argocd-server
sessionAffinity: None
type: NodePort
status:
loadBalancer: {}
[root@k8s-master argocd]# kubectl get svc -n argocd
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
argocd-applicationset-controller ClusterIP 200.1.73.127 <none> 7000/TCP 15m
argocd-dex-server ClusterIP 200.1.77.207 <none> 5556/TCP,5557/TCP,5558/TCP 15m
argocd-metrics ClusterIP 200.1.88.62 <none> 8082/TCP 15m
argocd-notifications-controller-metrics ClusterIP 200.1.185.23 <none> 9001/TCP 15m
argocd-redis ClusterIP 200.1.5.40 <none> 6379/TCP 15m
argocd-repo-server ClusterIP 200.1.249.26 <none> 8081/TCP,8084/TCP 15m
argocd-server NodePort 200.1.15.59 <none> 80:31715/TCP,443:30604/TCP 15m
argocd-server-metrics ClusterIP 200.1.216.113 <none> 8083/TCP 15m
访问节点IP+30604即可
账号:admin
# 获取密码方式如下
[root@k8s-master argocd]# echo $(kubectl get secret -n argocd argocd-initial-admin-secret -o yaml | grep password | awk -F: '{print $2}') | base64 -d
密码:U8g9xqXAPIRz6Ds3
4:使用ArgoCD简单的做一个CD
# 这里我只展示CD并不接触CI
配置完成之后我们就可以创建项目了
准备项目yaml并上传到git仓库
[root@k8s-master flask]# cat demo.yaml
apiVersion: v1
kind: Namespace
metadata:
name: demo
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo
namespace: demo
spec:
replicas: 1
selector:
matchLabels:
app: demo
template:
metadata:
labels:
app: demo
spec:
imagePullSecrets:
- name: harbor
containers:
- name: demo
image: registry.kubernetes-devops.cn/library/nginx:alpine
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: demo
namespace: demo
spec:
type: NodePort
ports:
- name: http
port: 80
targetPort: 80
protocol: TCP
selector:
app: demo
[root@k8s-master flask]# git add .
[root@k8s-master flask]# git commit -m "demo"
[master eddda10] demo
1 file changed, 9 insertions(+), 9 deletions(-)
rename nginx.yaml => demo.yaml (77%)
[root@k8s-master flask]# git push origin master
Username for 'http://10.0.0.10:31179': devops
Password for 'http://devops@10.0.0.10:31179':
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 509 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: . Processing 1 references
remote: Processed 1 references in total
To http://10.0.0.10:31179/devops/flask.git
d31d433..eddda10 master -> master
# 然后我们去ArgoCD去部署这个demo
我们查看一下部署情况,并看看部署之后是否可以访问
[root@k8s-master flask]# kubectl get pod,svc -n demo
NAME READY STATUS RESTARTS AGE
pod/demo-8645cf44c9-2pkv6 1/1 Running 0 84s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/demo NodePort 200.1.73.45 <none> 80:30808/TCP 84s
# 测试访问
[root@k8s-master flask]# curl 10.0.0.10:30808 -I
HTTP/1.1 200 OK
Server: nginx/1.21.5
Date: Sat, 23 Apr 2022 12:46:29 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 28 Dec 2021 18:48:00 GMT
Connection: keep-alive
ETag: "61cb5be0-267"
Accept-Ranges: bytes
这个时候我们去变更一下代码。(变更一下yaml)
[root@k8s-master flask]# cat demo.yaml
apiVersion: v1
kind: Namespace
metadata:
name: demo
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo
namespace: demo
spec:
replicas: 1
selector:
matchLabels:
app: demo
template:
metadata:
labels:
app: demo
spec:
imagePullSecrets:
- name: harbor
containers:
- name: demo
image: registry.kubernetes-devops.cn/library/httpd:latest
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: demo
namespace: demo
spec:
type: NodePort
ports:
- name: http
port: 80
targetPort: 80
protocol: TCP
selector:
app: demo
# 这里更换一个镜像,然后我们提交以下代码并在ArgoCD再次 SYNC一下
[root@k8s-master flask]# git add .
[root@k8s-master flask]# git commit -m "fix httpd"
[master 0d963aa] fix httpd
1 file changed, 1 insertion(+), 1 deletion(-)
[root@k8s-master flask]# git push origin master
Username for 'http://10.0.0.10:31179': devops
Password for 'http://devops@10.0.0.10:31179':
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 280 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: . Processing 1 references
remote: Processed 1 references in total
To http://10.0.0.10:31179/devops/flask.git
eb92f56..0d963aa master -> master
# 我们在增加新服务的时候我们可以看看pod的变化
[root@k8s-master flask]# kubectl get pod -n demo --watch
NAME READY STATUS RESTARTS AGE
demo-8645cf44c9-jf4g5 1/1 Running 0 3m20s
demo-9f6c4b7f5-5mmqp 0/1 Pending 0 0s
demo-9f6c4b7f5-5mmqp 0/1 Pending 0 0s
demo-9f6c4b7f5-5mmqp 0/1 ContainerCreating 0 0s
demo-9f6c4b7f5-5mmqp 0/1 ContainerCreating 0 0s
demo-9f6c4b7f5-5mmqp 1/1 Running 0 2s
demo-8645cf44c9-jf4g5 1/1 Terminating 0 3m28s
demo-8645cf44c9-jf4g5 1/1 Terminating 0 3m28s
demo-8645cf44c9-jf4g5 0/1 Terminating 0 3m29s
demo-8645cf44c9-jf4g5 0/1 Terminating 0 3m29s
demo-8645cf44c9-jf4g5 0/1 Terminating 0 3m29s
# 这里可以看到,更新策略是先启动一个新的然后再删除老的,这里测试一下访问
[root@k8s-master flask]# kubectl get pod,svc -n demo
NAME READY STATUS RESTARTS AGE
pod/demo-9f6c4b7f5-5mmqp 1/1 Running 0 95s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/demo NodePort 200.1.240.228 <none> 80:31086/TCP 5m1s
[root@k8s-master flask]# curl 10.0.0.10:31086
<html><body><h1>It works!</h1></body></html>
# 这里可以看到已经更新了
# 那么如果我们在这个yaml里面再增加一个pod呢?我们来实践一下,再次变更代码
[root@k8s-master flask]# cat demo.yaml
apiVersion: v1
kind: Namespace
metadata:
name: demo
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo
namespace: demo
spec:
replicas: 1
selector:
matchLabels:
app: demo
template:
metadata:
labels:
app: demo
spec:
imagePullSecrets:
- name: harbor
containers:
- name: demo
image: registry.kubernetes-devops.cn/library/httpd:latest
ports:
- containerPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: demos
namespace: demo
spec:
replicas: 1
selector:
matchLabels:
app: demos
template:
metadata:
labels:
app: demos
spec:
imagePullSecrets:
- name: harbor
containers:
- name: demos
image: registry.kubernetes-devops.cn/library/nginx:alpine
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: demo
namespace: demo
spec:
type: NodePort
ports:
- name: http
port: 80
targetPort: 80
protocol: TCP
selector:
app: demo
---
apiVersion: v1
kind: Service
metadata:
name: demos
namespace: demo
spec:
type: NodePort
ports:
- name: http
port: 80
targetPort: 80
protocol: TCP
selector:
app: demos
# 提交代码
[root@k8s-master flask]# git add .
[root@k8s-master flask]# git commit -m "add service"
[master 16ccff1] add service
1 file changed, 38 insertions(+)
[root@k8s-master flask]# git push origin master
Username for 'http://10.0.0.10:31179': devops
Password for 'http://devops@10.0.0.10:31179':
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 318 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: . Processing 1 references
remote: Processed 1 references in total
To http://10.0.0.10:31179/devops/flask.git
0d963aa..16ccff1 master -> master
# 在ArgoCD内再次SYNC,然后观察容器的更新
[root@k8s-master flask]# kubectl get pod -n demo --watch
NAME READY STATUS RESTARTS AGE
demo-9f6c4b7f5-5mmqp 1/1 Running 0 6m43s
demos-7d56f6966c-brsvt 0/1 Pending 0 0s
demos-7d56f6966c-brsvt 0/1 Pending 0 0s
demos-7d56f6966c-brsvt 0/1 ContainerCreating 0 0s
demos-7d56f6966c-brsvt 0/1 ContainerCreating 0 0s
demos-7d56f6966c-brsvt 1/1 Running 0 1s