Gitlab CI-k8s+gitlab+harbor
https://www.qikqiak.com/k8s-book/docs/65.Gitlab%20CI.html
1 简介
从 Gitlab 8.0 开始,Gitlab CI 就已经集成在 Gitlab 中,我们只要在项目中添加一个.gitlab-ci.yml
文件,然后添加一个Runner
,即可进行持续集成。在介绍 Gitlab CI 之前,我们先看看一些 Gitlab CI 的一些相关概念。
1.1 Pipeline
可以参考Jenkins-pipeline(2):开发阶段pipeline:gitlab和Jenkins-pipeline自动集成
一次 Pipeline 其实相当于一次构建任务,里面可以包含很多个流程,如安装依赖、运行测试、编译、部署测试服务器、部署生产服务器等流程。任何提交或者 Merge Request 的合并都可以触发 Pipeline 构建,如下图所示:
+------------------+ +----------------+
| | trigger | |
| Commit / MR +---------->+ Pipeline |
| | | |
+------------------+ +----------------+
1.2 Stages
Stages 表示一个构建阶段,也就是上面提到的一个流程。我们可以在一次 Pipeline 中定义多个 Stages,这些 Stages 会有以下特点:
- 所有 Stages 会按照顺序运行,即当一个 Stage 完成后,下一个 Stage 才会开始
- 只有当所有 Stages 完成后,该构建任务 (Pipeline) 才会成功
- 如果任何一个 Stage 失败,那么后面的 Stages 不会执行,该构建任务 (Pipeline) 失败
Stages 和 Pipeline 的关系如下所示:
| |
| Pipeline |
| |
| +-----------+ +------------+ +------------+ |
| | Stage 1 |---->| Stage 2 |----->| Stage 3 | |
| +-----------+ +------------+ +------------+ |
| |
+--------------------------------------------------------+
1.3 Jobs
Jobs 表示构建工作,表示某个 Stage 里面执行的工作。我们可以在 Stages 里面定义多个 Jobs,这些 Jobs 会有以下特点:
- 相同 Stage 中的 Jobs 会并行执行
- 相同 Stage 中的 Jobs 都执行成功时,该 Stage 才会成功
- 如果任何一个 Job 失败,那么该 Stage 失败,即该构建任务 (Pipeline) 失败
Jobs 和 Stage 的关系如下所示:
| |
| Stage 1 |
| |
| +---------+ +---------+ +---------+ |
| | Job 1 | | Job 2 | | Job 3 | |
| +---------+ +---------+ +---------+ |
| |
+------------------------------------------+
2 Gitlab Runner
如果理解了上面的基本概念之后,可能我们就会发现一个问题,我们的构建任务在什么地方来执行呢,以前用 Jenkins 在 Master 和 Slave 节点都可以用来运行构建任务,而来执行我们的 Gitlab CI 构建任务的就是 Gitlab Runner。
我们知道大多数情况下构建任务都是会占用大量的系统资源的,如果直接让 Gitlab 本身来运行构建任务的话,显然 Gitlab 的性能会大幅度下降的。GitLab CI 最大的作用是管理各个项目的构建状态,因此,运行构建任务这种浪费资源的事情交给一个独立的 Gitlab Runner 来做就会好很多,更重要的是 Gitlab Runner 可以安装到不同的机器上,甚至是我们本机,这样完全就不会影响到 Gitlab 本身了。
安装
安装 Gitlab Runner 非常简单,我们可以完全安装官方文档:https://docs.gitlab.com/runner/install/即可,比如可以直接使用二进制、Docker 等来安装。同样的,我们这里还是将 Gitlab Runner 安装到 Kubernetes 集群中来,让我们的集群来统一管理 Gitlab 相关的服务。
2.1 验证 Kubernetes 集群
执行下面的命令验证 Kubernetes 集群:
# kubectl cluster-info
Kubernetes master is running at https://10.6.76.25:6443
Alertmanager is running at https://10.6.76.25:6443/api/v1/namespaces/kube-system/services/alertmanager:http/proxy
KubeDNS is running at https://10.6.76.25:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
kube-state-metrics is running at https://10.6.76.25:6443/api/v1/namespaces/kube-system/services/kube-state-metrics:http-metrics/proxy
NodeExporter is running at https://10.6.76.25:6443/api/v1/namespaces/kube-system/services/node-exporter:metrics/proxy
Prometheus is running at https://10.6.76.25:6443/api/v1/namespaces/kube-system/services/prometheus:http/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
#cluster-info这个命令会显示当前链接的集群状态和可用的集群服务列表。
2.2 获取 Gitlab CI Register Token
kubernetes(36):持续集成(5)-k8s集群中搭建gitlab
前面我们已经成功安装了 Gitlab,在浏览器中打开gitlab.xxx.com
页面,然后登录后进入到管理页面http://gitlab.xxx.com/admin
,然后点击导航栏中的Runner
,可以看到该页面中有两个总要的参数,一个是 URL,另外一个就是 Register Token,下面的步骤中需要用到这两个参数值。
注意:不要随便泄露 Token
2.3 编写 Gitlab CI Runner 资源清单文件
runner-cm.yaml
同样将 Runner 相关的资源对象都安装到kube-ops
这个 namespace 下面,首先,我们通过 ConfigMap 资源来传递 Runner 镜像所需的环境变量(runner-cm.yaml):
apiVersion: v1
data:
REGISTER_NON_INTERACTIVE: "true"
REGISTER_LOCKED: "false"
METRICS_SERVER: "0.0.0.0:9100"
CI_SERVER_URL: "http://gitlab.kube-ops.svc.cluster.local/ci"
RUNNER_REQUEST_CONCURRENCY: "4"
RUNNER_EXECUTOR: "kubernetes"
KUBERNETES_NAMESPACE: "kube-ops"
KUBERNETES_PRIVILEGED: "true"
KUBERNETES_CPU_LIMIT: "1"
KUBERNETES_CPU_REQUEST: "500m"
KUBERNETES_MEMORY_LIMIT: "1Gi"
KUBERNETES_SERVICE_CPU_LIMIT: "1"
KUBERNETES_SERVICE_MEMORY_LIMIT: "1Gi"
KUBERNETES_HELPER_CPU_LIMIT: "500m"
KUBERNETES_HELPER_MEMORY_LIMIT: "100Mi"
KUBERNETES_PULL_POLICY: "if-not-present"
KUBERNETES_TERMINATIONGRACEPERIODSECONDS: "10"
KUBERNETES_POLL_INTERVAL: "5"
KUBERNETES_POLL_TIMEOUT: "360"
kind: ConfigMap
metadata:
labels:
app: gitlab-ci-runner
name: gitlab-ci-runner-cm
namespace: kube-ops
要注意CI_SERVER_URL
对应的值需要指向我们的 Gitlab 实例的 URL(可以是外网地址,也可以是 Kubernetes 集群内部的 Service DNS 地址,因为 Runner 也是运行在 Kubernetes 集群中的),并加上/ci
( http://gitlab.kube-ops.svc.cluster.local/ci )。此外还添加了一些构建容器运行的资源限制,可以自己根据需要进行更改即可。
注意:在向 ConfigMap 添加新选项后,需要删除 GitLab CI Runner Pod。因为我们是使用 envFrom
来注入上面的这些环境变量而不是直接使用env
的(envFrom 通过将环境变量放置到ConfigMaps
或Secrets
来帮助减小清单文件。
另外如果要添加其他选项的话,我们可以在 Pod 中运行gitlab-ci-multi-runner register --help
命令来查看所有可使用的选项,只需为要配置的标志添加 env 变量即可,如下所示:
gitlab-runner@gitlab-ci-runner-0:/$ gitlab-ci-multi-runner register --help
[...]
--kubernetes-cpu-limit value The CPU allocation given to build containers (default: "1") [$KUBERNETES_CPU_LIMIT]
--kubernetes-memory-limit value The amount of memory allocated to build containers (default: "4Gi") [$KUBERNETES_MEMORY_LIMIT]
--kubernetes-service-cpu-limit value The CPU allocation given to build service containers (default: "1") [$KUBERNETES_SERVICE_CPU_LIMIT]
--kubernetes-service-memory-limit value The amount of memory allocated to build service containers (default: "1Gi") [$KUBERNETES_SERVICE_MEMORY_LIMIT]
--kubernetes-helper-cpu-limit value The CPU allocation given to build helper containers (default: "500m") [$KUBERNETES_HELPER_CPU_LIMIT]
--kubernetes-helper-memory-limit value The amount of memory allocated to build helper containers (default: "3Gi") [$KUBERNETES_HELPER_MEMORY_LIMIT]
--kubernetes-cpu-request value The CPU allocation requested for build containers [$KUBERNETES_CPU_REQUEST]
...
--pre-clone-script value Runner-specific command script executed before code is pulled [$RUNNER_PRE_CLONE_SCRIPT]
[...]
如果定义的 Gitlab 域名并不是通过外网的 DNS 进行解析的,而是通过 /etc/hosts 俩进行映射的,那么我们就需要在 runner 的 Pod 中去添加 git.qikqiak.com 对应的 hosts 了,那么如何添加呢?我们可以想到的是 Pod 的 hostAlias 可以实现这个需求,但是 runner 的 Pod 是自动生成的,没办法直接去定义 hostAlias。这里我们就可以通过上面的--pre-clone-script
参数来指定一段脚本来添加 hosts 信息,也就是在上面的 ConfigMap 中添加环境变量RUNNER_PRE_CLONE_SCRIPT
的值即可:
RUNNER_PRE_CLONE_SCRIPT = "echo 'xx.xx.xxx.xx git.qikqiak.com' >> /etc/hosts"
#这个后面就用到了
runner-scripts-cm.yaml
除了上面的一些环境变量相关的配置外,还需要一个用于注册、运行和取消注册 Gitlab CI Runner 的小脚本。只有当 Pod 正常通过 Kubernetes(TERM信号)终止时,才会触发转轮取消注册。 如果强制终止 Pod(SIGKILL信号),Runner 将不会注销自身。必须手动完成对这种被杀死的 Runner 的清理,配置清单文件如下:(runner-scripts-cm.yaml)
apiVersion: v1
data:
run.sh: |
#!/bin/bash
unregister() {
kill %1
echo "Unregistering runner ${RUNNER_NAME} ..."
/usr/bin/gitlab-ci-multi-runner unregister -t "$(/usr/bin/gitlab-ci-multi-runner list 2>&1 | tail -n1 | awk '{print $4}' | cut -d'=' -f2)" -n ${RUNNER_NAME}
exit $?
}
trap 'unregister' EXIT HUP INT QUIT PIPE TERM
echo "Registering runner ${RUNNER_NAME} ..."
/usr/bin/gitlab-ci-multi-runner register -r ${GITLAB_CI_TOKEN}
sed -i 's/^concurrent.*/concurrent = '"${RUNNER_REQUEST_CONCURRENCY}"'/' /home/gitlab-runner/.gitlab-runner/config.toml
echo "Starting runner ${RUNNER_NAME} ..."
/usr/bin/gitlab-ci-multi-runner run -n ${RUNNER_NAME} &
wait
kind: ConfigMap
metadata:
labels:
app: gitlab-ci-runner
name: gitlab-ci-runner-scripts
namespace: kube-ops
可以看到需要一个 GITLAB_CI_TOKEN,然后我们用 Gitlab CI runner token 来创建一个 Kubernetes secret 对象。将 token 进行 base64 编码:
# echo "vjrZr36yypzdXnBSj6sy" | base64 -w0
dmpyWnIzNnl5cHpkWG5CU2o2c3kK
gitlab-ci-token-secret.yaml
然后使用上面的 token 创建一个 Secret 对象:(gitlab-ci-token-secret.yaml)
apiVersion: v1
kind: Secret
metadata:
name: gitlab-ci-token
namespace: kube-ops
labels:
app: gitlab-ci-runner
data:
GITLAB_CI_TOKEN: dmpyWnIzNnl5cHpkWG5CU2o2c3kK
# kubectl apply -f gitlab-ci-token-secret.yaml
secret/gitlab-ci-token created
runner-statefulset.yaml
然后接下来就可以来编写一个用于真正运行 Runner 的控制器对象,我们这里使用 Statefulset。首先,在开始运行的时候,尝试取消注册所有的同名 Runner,当节点丢失时(即NodeLost
事件),这尤其有用。然后再尝试重新注册自己并开始运行。在正常停止 Pod 的时候,Runner 将会运行unregister
命令来尝试取消自己,所以 Gitlab 就不能再使用这个 Runner 了,这个是通过 Kubernetes Pod 生命周期中的hooks
来完成的。
另外通过使用envFrom
来指定Secrets
和ConfigMaps
来用作环境变量,对应的资源清单文件如下:
(runner-statefulset.yaml)
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: gitlab-ci-runner
namespace: kube-ops
labels:
app: gitlab-ci-runner
spec:
updateStrategy:
type: RollingUpdate
replicas: 2
serviceName: gitlab-ci-runner
template:
metadata:
labels:
app: gitlab-ci-runner
spec:
volumes:
- name: gitlab-ci-runner-scripts
projected:
sources:
- configMap:
name: gitlab-ci-runner-scripts
items:
- key: run.sh
path: run.sh
mode: 0755
serviceAccountName: gitlab-ci
securityContext:
runAsNonRoot: true
runAsUser: 999
supplementalGroups: [999]
containers:
- image: gitlab/gitlab-runner:latest
name: gitlab-ci-runner
command:
- /scripts/run.sh
envFrom:
- configMapRef:
name: gitlab-ci-runner-cm
- secretRef:
name: gitlab-ci-token
env:
- name: RUNNER_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
ports:
- containerPort: 9100
name: http-metrics
protocol: TCP
volumeMounts:
- name: gitlab-ci-runner-scripts
mountPath: "/scripts"
readOnly: true
restartPolicy: Always
runner-rbac.yaml
可以看到上面我们使用了一个名为 gitlab-ci 的 serviceAccount,新建一个 rbac 资源清单文件:(runner-rbac.yaml)
apiVersion: v1
kind: ServiceAccount
metadata:
name: gitlab-ci
namespace: kube-ops
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: gitlab-ci
namespace: kube-ops
rules:
- apiGroups: [""]
resources: ["*"]
verbs: ["*"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: gitlab-ci
namespace: kube-ops
subjects:
- kind: ServiceAccount
name: gitlab-ci
namespace: kube-ops
roleRef:
kind: Role
name: gitlab-ci
apiGroup: rbac.authorization.k8s.io
2.4 创建 Runner 资源对象
# ls gitlab-ci-token-secret.yaml runner-*
gitlab-ci-token-secret.yaml runner-cm.yaml runner-rbac.yaml runner-statefulset.yaml runner-scripts-cm.yaml
kubectl apply -f runner-cm.yaml
kubectl apply -f runner-scripts-cm.yaml
kubectl apply -f gitlab-ci-token-secret.yaml
kubectl apply -f runner-rbac.yaml
kubectl apply -f runner-statefulset.yaml
创建完成后,可以通过查看 Pod 状态判断 Runner 是否运行成功:
# kubectl -n kube-ops get pod| grep gitlab-ci
gitlab-ci-runner-0 1/1 Running 0 4m35s
gitlab-ci-runner-1 1/1 Running 0 116s
以看到已经成功运行了两个(具体取决于StatefulSet
清单中的副本数) Runner 实例,然后切换到 Gitlab Admin 页面下面的 Runner 页面:
当然我们也可以根据需要更改 Runner 的一些配置,比如添加 tag 标签等。
3 Gitlab CI
准备测试代码
下载代码上传到gitlab
测试项目cnych/presentation-gitlab-k8s 我fork了一下
接下来使用 Gitlab CI 所用到的代码库可以从 Github 上获得:https://github.com/wangxu01/gitlab-ci-k8s-demo ,可以在 Gitlab 上新建一个项目导入该仓库,当然也可以新建一个空白的仓库,然后将 Github 上面的项目 Clone 到本地后,更改远程仓库地址即可:
[root@k8s-master test]# git clone https://github.com/wangxu01/gitlab-ci-k8s-demo.git
正克隆到 'gitlab-ci-k8s-demo'...
remote: Enumerating objects: 2036, done.
remote: Total 2036 (delta 0), reused 0 (delta 0), pack-reused 2036
接收对象中: 100% (2036/2036), 3.29 MiB | 767.00 KiB/s, done.
处理 delta 中: 100% (787/787), done.
[root@k8s-master test]# cd gitlab-ci-k8s-demo/
[root@k8s-master gitlab-ci-k8s-demo]# ls
Dockerfile Gopkg.lock Gopkg.toml LICENSE main.go Makefile manifests README.md vendor VERSION
[root@k8s-master gitlab-ci-k8s-demo]# git remote set-url origin ssh://git@gitlab.wangxu.com:30022/root/presentation-gitlab-k8s.git
[root@k8s-master gitlab-ci-k8s-demo]# git push -u origin master
Counting objects: 2036, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (1133/1133), done.
Writing objects: 100% (2036/2036), 3.29 MiB | 0 bytes/s, done.
Total 2036 (delta 787), reused 2036 (delta 787)
remote: Resolving deltas: 100% (787/787), done.
remote:
remote: The private project root/presentation-gitlab-k8s was successfully created.
remote:
remote: To configure the remote, run:
remote: git remote add origin git@gitlab.wangxu.com:root/presentation-gitlab-k8s.git
remote:
remote: To view the project, visit:
remote: http://gitlab.wangxu.com/root/presentation-gitlab-k8s
remote:
To ssh://git@gitlab.wangxu.com:30022/root/presentation-gitlab-k8s.git
* [new branch] master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。
[root@k8s-master gitlab-ci-k8s-demo]#
自动触发部署
当我们把仓库推送到 Gitlab 以后,应该可以看到 Gitlab CI 开始执行构建任务了:
此时 Runner Pod 所在的 namespace 下面也会出现两个新的 Pod:
# kubectl get pods -n kube-ops
……
runner-aczwwb3k-project-2-concurrent-0chkmk 0/2 ContainerCreating 0 2m14s
runner-aczwwb3k-project-2-concurrent-1nbfbv 0/2 ContainerCreating 0 2m13s
错误1-gitlab域名
这两个新的 Pod 就是用来执行具体的 Job 任务的,这里同时出现两个证明第一步是并行执行的两个任务,从上面的 Pipeline 中也可以看到是 test 和 test2 这两个 Job。我们可以看到在执行 image_build 任务的时候出现了错误:
可以点击查看这个 Job 失败详细信息:
Running with gitlab-runner 12.4.0 (1564076b)
on gitlab-ci-runner-1 DQisyaVT
Using Kubernetes namespace: kube-ops
Using Kubernetes executor with image golang:1.10.3-stretch ...
Waiting for pod kube-ops/runner-dqisyavt-project-2-concurrent-07lr98 to be running, status is Pending
Running on runner-dqisyavt-project-2-concurrent-07lr98 via gitlab-ci-runner-1...
Fetching changes with git depth set to 50...
Initialized empty Git repository in /builds/root/presentation-gitlab-k8s/.git/
Created fresh repository.
fatal: unable to access 'http://gitlab-ci-token:[MASKED]@gitlab.wangxu.com/root/presentation-gitlab-k8s.git/': Could not resolve host: gitlab.wangxu.com
ERROR: Job failed: command terminated with exit code 1
这是因为我的gitlab域名没有在 run pod进行解析
# cat runner-cm.yaml
apiVersion: v1
data:
REGISTER_NON_INTERACTIVE: "true"
REGISTER_LOCKED: "false"
METRICS_SERVER: "0.0.0.0:9100"
CI_SERVER_URL: "http://gitlab.kube-ops.svc.cluster.local/ci"
RUNNER_REQUEST_CONCURRENCY: "4"
RUNNER_EXECUTOR: "kubernetes"
KUBERNETES_NAMESPACE: "kube-ops"
KUBERNETES_PRIVILEGED: "true"
KUBERNETES_CPU_LIMIT: "1"
KUBERNETES_CPU_REQUEST: "500m"
KUBERNETES_MEMORY_LIMIT: "1Gi"
KUBERNETES_SERVICE_CPU_LIMIT: "1"
KUBERNETES_SERVICE_MEMORY_LIMIT: "1Gi"
KUBERNETES_HELPER_CPU_LIMIT: "500m"
KUBERNETES_HELPER_MEMORY_LIMIT: "100Mi"
KUBERNETES_PULL_POLICY: "if-not-present"
KUBERNETES_TERMINATIONGRACEPERIODSECONDS: "10"
KUBERNETES_POLL_INTERVAL: "5"
KUBERNETES_POLL_TIMEOUT: "360"
RUNNER_PRE_CLONE_SCRIPT : "echo '10.6.76.23 gitlab.wangxu.com' >> /etc/hosts"
RUNNER_PRE_CLONE_SCRIPT : "echo '10.6.76.24 gitlab.wangxu.com' >> /etc/hosts"
kind: ConfigMap
metadata:
labels:
app: gitlab-ci-runner
name: gitlab-ci-runner-cm
namespace: kube-ops
#重启runner
kubectl apply -f runner-cm.yaml
kubectl delete -f runner-statefulset.yaml
kubectl apply -f runner-statefulset.yaml
错误2-代码配置文件
下载代码通过了,但在执行 image_build 任务的时候出现了错误:
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Error response from daemon: Get http://registry.qikqiak.com/v1/users/: dial tcp: lookup registry.qikqiak.com on 10.96.0.10:53: no such host
这里调用的还是原git的地址
我们把原项目的registry.qikqiak.com 改成自己的gitlab.wangxu.com 再提交一下
[root@k8s-master gitlab-ci-k8s-demo]# grep -r registry.qikqiak.com *
manifests/deployment.yaml: image: registry.qikqiak.com/gitdemo/gitlab-k8s:__VERSION__
[root@k8s-master gitlab-ci-k8s-demo]# vim manifests/deployment.yaml
[root@k8s-master gitlab-ci-k8s-demo]# grep -r image: *
manifests/deployment.yaml: image: harbor.wangxu.com/gitdemo/gitlab-k8s:__VERSION__
vendor/golang.org/x/crypto/openpgp/packet/userattribute_test.go: t.Errorf("Error decoding JPEG image:%v", err)
[root@k8s-master gitlab-ci-k8s-demo]# git add .
[root@k8s-master gitlab-ci-k8s-demo]# git commit -m 'change image'
[master 532d6d0] change image
1 file changed, 1 insertion(+), 1 deletion(-)
(reverse-i-search)`': ^C
[root@k8s-master gitlab-ci-k8s-demo]# git push origin master
Counting objects: 7, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 357 bytes | 0 bytes/s, done.
Total 4 (delta 3), reused 0 (delta 0)
To ssh://git@gitlab.wangxu.com:30022/root/presentation-gitlab-k8s.git
3fec16d..532d6d0 master -> master
[root@k8s-master gitlab-ci-k8s-demo]#
我们也没有在 Gitlab 中开启 Container Registry,所以环境变量中并没有这些值,我们这里使用 Harbor 来作为我们的镜像仓库,这里我们只需要把 Harbor 相关的配置以参数的形式配置到环境中就可以了。
定位到项目 -> 设置 -> CI/CD,展开Environment variables
栏目,配置镜像仓库相关的参数值:
配置上后,我们在上面失败的 Job 任务上点击“重试”,在重试过后依然可以看到会出现下面的错误信息:
$ docker login -u "${CI_REGISTRY_USER}" -p "${CI_REGISTRY_PASSWORD}" registry.qikqiak.com
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Error response from daemon: Get http://registry.qikqiak.com/v1/users/: dial tcp: lookup registry.qikqiak.com on 10.96.0.10:53: no such host
ERROR: Job failed: command terminated with exit code 1
原地址还是没有改过来,这个其实是配置在.gitlab-ci.yml
文件
[root@k8s-master gitlab-ci-k8s-demo]# sed -i 's/git.qikqiak.com/gitlab.wangxu.com/g' .gitlab-ci.yml
[root@k8s-master gitlab-ci-k8s-demo]# sed -i 's/registry.qikqiak.com/harbor.wangxu.com/g' .gitlab-ci.yml
[root@k8s-master gitlab-ci-k8s-demo]# git add .
[root@k8s-master gitlab-ci-k8s-demo]# git commit -m 'change 5'
[master dce9e4c] change 5
1 file changed, 5 insertions(+), 5 deletions(-)
[root@k8s-master gitlab-ci-k8s-demo]# git push origin master
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 319 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To ssh://git@gitlab.wangxu.com:30022/root/presentation-gitlab-k8s.git
acbed8f..dce9e4c master -> master
[root@k8s-master gitlab-ci-k8s-demo]#
错误3-harbor域名
$ docker login -u "${CI_REGISTRY_USER}" -p "${CI_REGISTRY_PASSWORD}" harbor.wangxu.com
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Error response from daemon: Get http://harbor.wangxu.com/v1/users/: dial tcp: lookup harbor.wangxu.com on 10.96.0.10:53: no such host
还是域名的问题
#vim runner-cm.yaml
...
RUNNER_PRE_CLONE_SCRIPT : "echo '10.6.76.23 gitlab.wangxu.com harbor.wangxu.com' >> /etc/hosts"
RUNNER_PRE_CLONE_SCRIPT : "echo '10.6.76.24 gitlab.wangxu.com harbor.wangxu.com' >> /etc/hosts"
...
kubectl apply -f runner-cm.yaml
kubectl delete -f runner-statefulset.yaml
kubectl apply -f runner-statefulset.yaml
错误4-配置环境变量
然后又失败了
$ docker login -u "${CI_REGISTRY_USER}" -p "${CI_REGISTRY_PASSWORD}" harbor.wangxu.com
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Error response from daemon: Get https://harbor.wangxu.com/v2/: unauthorized: authentication required
ERROR: Job failed: command terminated with exit code 1
Harbor密码写错了 选中受保护
编译完成
终于过去了
Running with gitlab-runner 12.4.0 (1564076b)
on gitlab-ci-runner-0 ztHmfWd9
Using Kubernetes namespace: kube-ops
Using Kubernetes executor with image docker:latest ...
Waiting for pod kube-ops/runner-zthmfwd9-project-3-concurrent-06mlgk to be running, status is Pending
Running on runner-zthmfwd9-project-3-concurrent-06mlgk via gitlab-ci-runner-0...
$ echo '10.6.76.24 gitlab.wangxu.com harbor.wangxu.com' >> /etc/hosts
Fetching changes with git depth set to 50...
Initialized empty Git repository in /builds/root/presentation-gitlab-k8s/.git/
Created fresh repository.
From http://gitlab.wangxu.com/root/presentation-gitlab-k8s
* [new branch] master -> origin/master
Checking out d4bfe70b as master...
Skipping Git submodules setup
Downloading artifacts for compile (126)...
Downloading artifacts from coordinator... ok id=126 responseStatus=200 OK token=4yKS-1uT
$ mkdir -p "/go/src/gitlab.wangxu.com/${CI_PROJECT_NAMESPACE}"
$ ln -sf "${CI_PROJECT_DIR}" "/go/src/gitlab.wangxu.com/${CI_PROJECT_PATH}"
$ cd "/go/src/gitlab.wangxu.com/${CI_PROJECT_PATH}/"
$ docker info
Client:
Debug Mode: false
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 17.03.2-ce
Storage Driver: overlay
Backing Filesystem: xfs
Supports d_type: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Log:
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 4ab9917febca54791c5f071a9d1f404867857fcc
runc version: 54296cf40ad8143b62dbcaa1d90e520a2136ddfe
init version: 949e6fa
Security Options:
seccomp
Profile: default
Kernel Version: 5.2.10-1.el7.elrepo.x86_64
Operating System: Alpine Linux v3.5 (containerized)
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 7.783GiB
Name: runner-zthmfwd9-project-3-concurrent-06mlgk
ID: S4QT:6C5I:BKXI:3HX3:FTRQ:ZGO2:2AUP:X5G5:YR2I:MHUV:XMYE:XTWU
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Experimental: false
Insecure Registries:
harbor.wangxu.com
127.0.0.0/8
Live Restore Enabled: false
WARNING: overlay: the backing xfs filesystem is formatted without d_type support, which leads to incorrect behavior.
Reformat the filesystem with ftype=1 to enable d_type support.
Running without d_type support will not be supported in future releases.
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
$ docker login -u "${CI_REGISTRY_USER}" -p "${CI_REGISTRY_PASSWORD}" "${CI_REGISTRY}"
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
$ docker build -t "${CI_REGISTRY_IMAGE}:latest" .
Sending build context to Docker daemon 28.63MB
Step 1/4 : FROM busybox:1.28.4-glibc
1.28.4-glibc: Pulling from library/busybox
5497949500d2: Pulling fs layer
5497949500d2: Verifying Checksum
5497949500d2: Download complete
5497949500d2: Pull complete
Digest: sha256:bda689514be526d9557ad442312e5d541757c453c50b8cf2ae68597c291385a1
Status: Downloaded newer image for busybox:1.28.4-glibc
---> 56cc512116c8
Step 2/4 : COPY app /bin/app
---> b97b889363e3
Removing intermediate container 197636a53e2a
Step 3/4 : RUN chmod +x /bin/app
---> Running in 0a69653107b2
---> d1ee3ebf5944
Removing intermediate container 0a69653107b2
Step 4/4 : CMD /bin/app
---> Running in 74ff2064d7c8
---> a3f04c58f4bd
Removing intermediate container 74ff2064d7c8
Successfully built a3f04c58f4bd
$ docker tag "${CI_REGISTRY_IMAGE}:latest" "${CI_REGISTRY}/library/${CI_REGISTRY_IMAGE}:${CI_COMMIT_REF_NAME}"
$ test ! -z "${CI_COMMIT_TAG}" && docker push "${CI_REGISTRY_IMAGE}:latest"
$ docker push "${CI_REGISTRY}/library/${CI_REGISTRY_IMAGE}:${CI_COMMIT_REF_NAME}"
The push refers to a repository [harbor.wangxu.com/library/gitlab-ci-k8s-demo]
b3ca74fc4d32: Preparing
e49dd1e534d9: Preparing
e49dd1e534d9: Pushed
b3ca74fc4d32: Pushed
master: digest: sha256:37e5a5d0935f69926e9fa66bc7b7488272c07cdbd7d26666f38e4ef24bd209dc size: 739
Job succeeded
可以看到镜像传到harbor上了
配置关联的k8s集群
然后我们又失败了
在最后的阶段deploy_review
可以看到失败了,这是因为在最后的部署阶段我们使用kubectl
工具操作集群的时候并没有关联上任何集群。
我们在 Gitlab CI 中部署阶段使用到的镜像是cnych/kubectl
,该镜像的Dockerfile
文件可以在仓库https://github.com/cnych/docker-kubectl中获取
:
我们也fork一下 https://github.com/wangxu01/docker-kubectl
cnych/kubectl
我们下载
再上传一个
到
dockerHub wangxu01/kubelet
谢谢作者
#kubelet Dockerfile
FROM alpine:3.8
MAINTAINER cnych <icnych@gmail.com>
ENV KUBE_LATEST_VERSION="v1.13.4"
RUN apk add --update ca-certificates \
&& apk add --update -t deps curl \
&& apk add --update gettext \
&& apk add --update git \
&& curl -L https://storage.googleapis.com/kubernetes-release/release/${KUBE_LATEST_VERSION}/bin/linux/amd64/kubectl -o /usr/local/bin/kubectl \
&& chmod +x /usr/local/bin/kubectl \
&& apk del --purge deps \
&& rm /var/cache/apk/*
ENTRYPOINT ["kubectl"]
CMD ["--help"]
我们知道kubectl
在使用的时候默认会读取当前用户目录下面的~/.kube/config
文件来链接集群,当然我们可以把连接集群的信息直接内置到上面的这个镜像中去,这样就可以直接操作集群了,但是也有一个不好的地方就是不方便操作,假如要切换一个集群还得重新制作一个镜像。所以一般我们这里直接在 Gitlab 上配置集成 Kubernetes 集群。
在项目页面点击Add Kubernetes Cluster
-> Add existing cluster
:
1.Kubernetes cluster name 可以随便填
2.API URL 是你的集群的apiserver
的地址, 一般可以通过输入kubectl cluster-info
获取,Kubernetes master 地址就是需要的
# kubectl cluster-info | grep 'Kubernetes master' | awk '/http/ {print $NF}'
https://10.6.76.25:6443
3.CA证书、Token、项目命名空间
对于我们这个项目准备部署在一个名为gitlab
的 namespace 下面,所以首先我们需要到目标集群中创建一个 namespace:
kubectl create ns gitlab
由于我们在部署阶段需要去创建、删除一些资源对象,所以我们也需要对象的 RBAC 权限,这里为了简单,我们直接新建一个 ServiceAccount,绑定上一个cluster-admin
的权限:(gitlab-sa.yaml)
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: gitlab
namespace: gitlab
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: gitlab
namespace: gitlab
subjects:
- kind: ServiceAccount
name: gitlab
namespace: gitlab
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
然后创建上面的 ServiceAccount 对象:
$ kubectl apply -f sa.yaml
serviceaccount "gitlab" created
clusterrolebinding.rbac.authorization.k8s.io "gitlab" created
可以通过上面创建的 ServiceAccount 获取 CA 证书和 Token:
然后根据上面的Secret找到CA证书
$ kubectl get secret gitlab-token-f9zp7 -n gitlab -o json | jq -r '.data["ca.crt"]' | base64 -d
xxxxxCA证书内容xxxxx
当然要找到对应的 Token 也很简单
$ kubectl get secret gitlab-token-f9zp7 -n gitlab -o json | jq -r '.data.token' | base64 -d
xxxxxxtoken值xxxx
[root@k8s-master gitlab]# kubectl get serviceaccount gitlab -n gitlab -o json | jq -r '.secrets[0].name'
gitlab-token-lx7kn
[root@k8s-master gitlab]# kubectl get secret gitlab-token-lx7kn -n gitlab -o json | jq -r '.data["ca.crt"]' | base64 -d
-----BEGIN CERTIFICATE-----
MIICyDCCAbCgAwIBAgIBADANBgkqhkiG9w0BAQsFADAVMRMwEQYDVQQDEwprdWJl
cm5ldGVzMB4XDTE5MDgyOTAxMjAzMVoXDTI5MDgyNjAxMjAzMVowFTETMBEGA1UE
AxMKa3ViZXJuZXRlczCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAOff
fS97EVsrYu0WEJIjCYJ02rhrkGZEqXGF7M1O4jW+xqew+raT1DU+I3U+8KmntGgP
S88IWOeAsvu60d0i9I8W2X3wxwa+a0BrDOx+iUEawTl4X7lllJyN14CkLEc0L2WG
xybbvGfVYZXOuOL/v5xuo9O58VTmEXzXoMD9pk8jb4Qe0NzJKSmWdu5Mj4J0czSo
0tcq3F0kGDvYMbNZhYGbxxoImuPZU6ynp+RBAMMZX5PL2KWhTi5Yw5sf/siq/hN4
fi9O14eoGbIU460PC9yWYTNd6C3UGF2+MCiIg9uuTgZP/Uxhg3rYrnT+vys5D6z6
7QrFrLXh8U91wCUVPf0CAwEAAaMjMCEwDgYDVR0PAQH/BAQDAgKkMA8GA1UdEwEB
/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAMTe4kAdIm0ZoZVqheDUGhjM1EPb
y0+837wtcdsOYjA7KjgioUuP+AvOkQkyXTPI9SDASmg062hMDPKeZ7KLt1mCLfvc
iQLcHpcYKnEoEOOAij1q3KKal454pOg6Bt21632ufO5ELqCfJJKKzbP53u02QDmv
Xlxe2paJqxAZL5wP7zkLzMiHc+YU4W1x0ppx1wlhMJfH2jxLra2T2EsSePvcLjOO
EsjyeDdpF7MqxS2R34Q38fe7/AeyJxyLOhrkRa5RMS8XWd/5CDxAkDB/ooNEUmUq
hqmrvEKPY9Z+BeulnHhpUOUHMNSSfrxjuAt2sbbv0UzK+7hePlBor+61kyc=
-----END CERTIFICATE-----
[root@k8s-master gitlab]# kubectl get secret gitlab-token-lx7kn -n gitlab -o json | jq -r '.data.token' | base64 -d
eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJnaXRsYWIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlY3JldC5uYW1lIjoiZ2l0bGFiLXRva2VuLWx4N2tuIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImdpdGxhYiIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6IjFhYWRjNzljLTQ2MGYtNDMwNy05MmI1LTNkOTgwMmIyOTExZiIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDpnaXRsYWI6Z2l0bGFiIn0.04Kfa1xY0_g4xUE_CqMNvtAzJGlO7CICv1iicP7WzMiTEjKa6c031duYBP85-jeRdrHxnEX2VaUrqJIS5eT-GJajBMrUD6D55qa_N5OBrUdTwxe-NJZigYKL3xE1fpZKKoJg5ggGd239BJduUvp10Xo_V0hhwhgsGmHDzMyhtZEzWmty3DjiNFBNedxQwV2fNoOy0Z8UYq-A38hGpQl_zPt_TVdsMSgw2X8KIvgWetsOspfAl6DL0URUmjBSXhKqEl1g4YNGin-cZGyDWeUbjg3LpoXQzediyN5tmI2pwmW-tgRZqnK7vLZQQ1zQ7fmD27l8orMNmMhQmt6zVdLJng
is blocked: Requests to the local network are not allowed
接着就保存成功了
.gitlab-ci.yml
现在 Gitlab CI 的环境都准备好了,我们可以来看下用于描述 Gitlab CI 的.gitlab-ci.yml
文件。
一个 Job 在.gitlab-ci.yml
文件中一般如下定义:
# 运行golang测试用例
test:
stage: test
script:
- go test ./...
上面这个 Job 会在 test 这个 Stage 阶段运行。
为了指定运行的 Stage 阶段,可以在.gitlab-ci.yml
文件中放置任意一个简单的列表:
# 所有 Stage
stages:
- test
- build
- release
- deploy
你可以指定用于在全局或者每个作业上执行命令的镜像:
# 对于未指定镜像的作业,会使用下面的镜像
image: golang:1.10.3-stretch
# 或者对于特定的job使用指定的镜像
test:
stage: test
image: python:3
script:
- echo Something in the test step in a python:3 image
对于.gitlab-ci.yml
文件的的其他部分,请查看如下文档介绍:https://docs.gitlab.com/ce/ci/yaml/README.html。
在我们当前的项目中定义了 4 个构建阶段:test、build、release、review、deploy,完整的.gitlab-ci.yml
文件如下:
image:
name: golang:1.10.3-stretch
entrypoint: ["/bin/sh", "-c"]
# The problem is that to be able to use go get, one needs to put
# the repository in the $GOPATH. So for example if your gitlab domain
# is mydomainperso.com, and that your repository is repos/projectname, and
# the default GOPATH being /go, then you'd need to have your
# repository in /go/src/mydomainperso.com/repos/projectname
# Thus, making a symbolic link corrects this.
before_script:
- mkdir -p "/go/src/gitlab.wangxu.com/${CI_PROJECT_NAMESPACE}"
- ln -sf "${CI_PROJECT_DIR}" "/go/src/gitlab.wangxu.com/${CI_PROJECT_PATH}"
- cd "/go/src/gitlab.wangxu.com/${CI_PROJECT_PATH}/"
stages:
- test
- build
- release
- review
- deploy
test:
stage: test
script:
- make test
test2:
stage: test
script:
- sleep 3
- echo "We did it! Something else runs in parallel!"
compile:
stage: build
script:
# Add here all the dependencies, or use glide/govendor/...
# to get them automatically.
- make build
artifacts:
paths:
- app
image_build:
stage: release
image: docker:latest
variables:
DOCKER_DRIVER: overlay
DOCKER_HOST: tcp://localhost:2375
services:
- name: docker:17.03-dind
command: ["--insecure-registry=harbor.wangxu.com"]
script:
- docker info
- docker login -u "${CI_REGISTRY_USER}" -p "${CI_REGISTRY_PASSWORD}" "${CI_REGISTRY}"
- docker build -t "${CI_REGISTRY_IMAGE}:latest" .
- docker tag "${CI_REGISTRY_IMAGE}:latest" "${CI_REGISTRY}/library/${CI_REGISTRY_IMAGE}:${CI_COMMIT_REF_NAME}"
- test ! -z "${CI_COMMIT_TAG}" && docker push "${CI_REGISTRY_IMAGE}:latest"
- docker push "${CI_REGISTRY}/library/${CI_REGISTRY_IMAGE}:${CI_COMMIT_REF_NAME}"
deploy_review:
image: cnych/kubectl
stage: review
only:
- branches
except:
- tags
environment:
name: dev
url: https://dev-gitlab-k8s-demo.wangxu.com
on_stop: stop_review
script:
- kubectl version
- cd manifests/
- sed -i "s/__CI_ENVIRONMENT_SLUG__/${CI_ENVIRONMENT_SLUG}/" deployment.yaml ingress.yaml service.yaml
- sed -i "s/__VERSION__/${CI_COMMIT_REF_NAME}/" deployment.yaml ingress.yaml service.yaml
- |
if kubectl apply -f deployment.yaml | grep -q unchanged; then
echo "=> Patching deployment to force image update."
kubectl patch -f deployment.yaml -p "{\"spec\":{\"template\":{\"metadata\":{\"annotations\":{\"ci-last-updated\":\"$(date +'%s')\"}}}}}"
else
echo "=> Deployment apply has changed the object, no need to force image update."
fi
- kubectl apply -f service.yaml || true
- kubectl apply -f ingress.yaml
- kubectl rollout status -f deployment.yaml
- kubectl get all,ing -l ref=${CI_ENVIRONMENT_SLUG}
stop_review:
image: cnych/kubectl
stage: review
variables:
GIT_STRATEGY: none
when: manual
only:
- branches
except:
- master
- tags
environment:
name: dev
action: stop
script:
- kubectl version
- kubectl delete ing -l ref=${CI_ENVIRONMENT_SLUG}
- kubectl delete all -l ref=${CI_ENVIRONMENT_SLUG}
deploy_live:
image: cnych/kubectl
stage: deploy
environment:
name: live
url: https://live-gitlab-k8s-demo.wangxu.com
only:
- tags
when: manual
script:
- kubectl version
- cd manifests/
- sed -i "s/__CI_ENVIRONMENT_SLUG__/${CI_ENVIRONMENT_SLUG}/" deployment.yaml ingress.yaml service.yaml
- sed -i "s/__VERSION__/${CI_COMMIT_REF_NAME}/" deployment.yaml ingress.yaml service.yaml
- kubectl apply -f deployment.yaml
- kubectl apply -f service.yaml
- kubectl apply -f ingress.yaml
- kubectl rollout status -f deployment.yaml
- kubectl get all,ing -l ref=${CI_ENVIRONMENT_SLUG}
上面的.gitlab-ci.yml
文件中还有一些特殊的属性,如限制运行的的when
和only
参数,例如only: ["tags"]
表示只为创建的标签运行,更多的信息,我可以通过查看 Gitlab CI YAML 文件查看:https://docs.gitlab.com/ce/ci/yaml/README.html
由于我们在.gitlab-ci.yml
文件中将应用的镜像构建完成后推送到了我们的私有仓库,而 Kubernetes 资源清单文件中使用的私有镜像,所以我们需要配置一个imagePullSecret
,否则在 Kubernetes 集群中是无法拉取我们的私有镜像的:(替换下面相关信息为自己的)
# kubectl create secret docker-registry myregistry --docker-server=harbor.wangxu.com --docker-username=admin --docker-password=Harbor12345 --docker-email=314144952@qq.com -n gitlab
secret/myregistry created
在下面的 Deployment 的资源清单文件中会使用到创建的myregistry
。
创建k8s资源清单文件
接下来为应用创建 Kubernetes 资源清单文件,添加到代码仓库中。首先创建 Deployment 资源:(deployment.yaml)
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: gitlab-k8s-demo-__CI_ENVIRONMENT_SLUG__
namespace: gitlab
labels:
app: gitlab-k8s-demo
ref: __CI_ENVIRONMENT_SLUG__
track: stable
spec:
replicas: 2
selector:
matchLabels:
app: gitlab-k8s-demo
ref: __CI_ENVIRONMENT_SLUG__
template:
metadata:
labels:
app: gitlab-k8s-demo
ref: __CI_ENVIRONMENT_SLUG__
track: stable
spec:
imagePullSecrets:
- name: myregistry
containers:
- name: app
image: harbor.wangxu.com/library/gitlab-ci-k8s-demo:__VERSION__
#注意是harbor地址,原作者github有不一致的地方,注意看日志调整
imagePullPolicy: Always
ports:
- name: http-metrics
protocol: TCP
containerPort: 8000
livenessProbe:
httpGet:
path: /health
port: 8000
initialDelaySeconds: 3
timeoutSeconds: 2
readinessProbe:
httpGet:
path: /health
port: 8000
initialDelaySeconds: 3
timeoutSeconds: 2
#注意用上面创建的 myregistry 替换 imagePullSecrets。
这是一个基本的 Deployment 资源清单的描述,像__CI_ENVIRONMENT_SLUG__
和__VERSION__
这样的占位符用于区分不同的环境,__CI_ENVIRONMENT_SLUG__
将由 dev 或 live(环境名称)和__VERSION__
替换为镜像标签。
为了能够连接到部署的 Pod,还需要 Service。对应的 Service 资源清单如下(service.yaml):
---
apiVersion: v1
kind: Service
metadata:
name: gitlab-k8s-demo-__CI_ENVIRONMENT_SLUG__
namespace: gitlab
labels:
app: gitlab-k8s-demo
ref: __CI_ENVIRONMENT_SLUG__
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "8000"
prometheus.io/scheme: "http"
prometheus.io/path: "/metrics"
spec:
type: ClusterIP
ports:
- name: http-metrics
port: 8000
protocol: TCP
selector:
app: gitlab-k8s-demo
ref: __CI_ENVIRONMENT_SLUG__
我们的应用程序运行8000端口上,端口名为http-metrics
,如果你还记得前面我们监控的课程中应该还记得我们使用prometheus-operator
为 Prometheus 创建了自动发现
的配置,所以我们在annotations
里面配置上上面的这几个注释后,Prometheus 就可以自动获取我们应用的监控指标数据了。
现在 Service 创建成功了,但是外部用户还不能访问到我们的应用,当然我们可以把 Service 设置成 NodePort 类型,另外一个常见的方式当然就是使用 Ingress 了,我们可以通过 Ingress 来将应用暴露给外面用户使用,对应的资源清单文件如下:(ingress.yaml)
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: gitlab-k8s-demo-__CI_ENVIRONMENT_SLUG__
namespace: gitlab
labels:
app: gitlab-k8s-demo
ref: __CI_ENVIRONMENT_SLUG__
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: __CI_ENVIRONMENT_SLUG__-gitlab-k8s-demo.wangxu.com
http:
paths:
- path: /
backend:
serviceName: gitlab-k8s-demo-__CI_ENVIRONMENT_SLUG__
servicePort: 8000
当然如果想配置 https 访问的话我们可以自己用 CA 证书创建一个 tls 密钥,也可以使用cert-manager
来自动为我们的应用程序添加 https。
当然要通过上面的域名进行访问,还需要进行 DNS 解析的,__CI_ENVIRONMENT_SLUG__-gitlab-k8s-demo.qikqiak.com
其中__CI_ENVIRONMENT_SLUG__
值为 live 或 dev,所以需要创建dev-gitlab-k8s-demo.wangxu.com
和 live-gitlab-k8s-demo.wangxu.com
两个域名的解析。
我们可以使用 DNS 解析服务商的 API 来自动创建域名解析,也可以使用 Kubernetes incubator 孵化的项目 external-dns operator 来进行操作。
触发gitlab-CI
所需要的资源清单和.gitlab-ci.yml
文件已经准备好了,我们可以小小的添加一个文件去触发下 Gitlab CI 构建:
touch test1
git add .
git commit -m"Testing the GitLab CI functionality #1"
git push origin master
还是失败了,我的node节点没有配置harbor的域名
Warning Failed 1s (x2 over 16s) kubelet, k8s-node-2 Failed to pull image "harbor.wangxu.com/library/gitlab-ci-k8s-demo:master": rpc error: code = Unknown desc = Error response from daemon: Get https://harbor.wangxu.com/v2/: x509: certificate signed by unknown authority
Warning Failed 1s (x2 over 16s) kubelet, k8s-node-2 Error: ErrImagePull
[root@k8s-master gitlab-ci-k8s-demo]#
[root@k8s-node-1 ~]# tail /etc/hosts
10.6.76.23 gitlab.wangxu.com harbor.wangxu.com
10.6.76.24 gitlab.wangxu.com harbor.wangxu.com
[root@k8s-node-1 ~]# vim /etc/docker/daemon.json
[root@k8s-node-1 ~]# systemctl restart docker
[root@k8s-node-1 ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["http://hub-mirror.c.163.com"],
"insecure-registries": ["harbor.wangxu.com"]
}
[root@k8s-node-1 ~]#
真是被自己坑死了,再重新构建
整个 Pipeline 构建成功后,我们可以在项目的环境菜单下面看到多了一个环境:
#解析 ingress域名
10.6.76.23 dev-gitlab-k8s-demo.wangxu.com
10.6.76.24 dev-gitlab-k8s-demo.wangxu.com
瑕疵和没有解决的
终止操作无效啊。
按照设定
- kubectl delete ing -l ref=${CI_ENVIRONMENT_SLUG}
- kubectl delete all -l ref=${CI_ENVIRONMENT_SLUG}
但是无效,调试几次还是不行,不过也不影响实现了 gitlac-CI部署。有缘看到的,看是否遇到