使用 tekton 做 CI/CD
? k8s ?    2020-03-31 14:04:14    2895    0    3
gua_l   ? k8s ?

项目源码:

https://github.com/tektoncd/pipeline

The Tekton Pipelines project provides k8s-style resources for declaring CI/CD-style pipelines.
Tekton管道项目为声明CI/ cd风格的管道提供了k8风格的资源。


 

 安装

Install Tekton Pipelines

kubectl apply --filename https://storage.googleapis.com/tekton-releases/pipeline/latest/release.yaml

Install Tekton CLI (tkn)

curl -LO https://github.com/tektoncd/cli/releases/download/v0.7.1/tkn_0.7.1_Linux_x86_64.tar.gz

# Change destination directory as needed
tar xvzf tkn_0.7.1_Linux_x86_64.tar.gz -C ~/bin
​

Install Tekton dashboard 仪表盘

kubectl apply --filename https://github.com/tektoncd/dashboard/releases/download/v0.5.1/tekton-dashboard-release.yaml


更改镜像源

默认镜像源安装时的问题解决:

其中:https://storage.googleapis.com/tekton-releases/pipeline/latest/release.yaml

https://github.com/tektoncd/dashboard/releases/download/v0.5.1/tekton-dashboard-release.yaml

两个yaml 中创建资源时,pull 镜像用的镜像库是国外的,gcr.io 需要替换成国内的镜像源:gcr.azk8s.cn

如果使用原来的gcr.io , 资源创建成功后在启动的过程中,pod状态一直是 imagepullbackoff , 查看pod 内部,是无法pull 镜像所致。

get pods --namespace tekton-pipelines --watch
get pods --namespace describe pod <pod-id>​

解决方法:

#将资源删除
kubectl delete--filename https://storage.googleapis.com/tekton-releases/pipeline/latest/release.yaml
kubectl delete --filename https://github.com/tektoncd/dashboard/releases/download/v0.5.1/tekton-dashboard-release.yaml

# 下载yaml 文件
# 注意如果因为我们下面要替换的国内源不是即时更新的,有一个同步时间差。
# 如果下载latest release 可能会遇到下载到昨天发布的最新版本,而使用国内源时会出现找不到最新镜像ID. 所以在更新之前可以在官网看一下最新版本是不是昨天才更新的版本,如果是建议选上一个版本
# 如果latest 是几天之前的,则没有问题。
# 官网release : https://github.com/tektoncd/pipeline/releases
# https://storage.googleapis.com/tekton-releases/pipeline/previous/vx.xx.x/release.yaml
wget https://storage.googleapis.com/tekton-releases/pipeline/latest/release.yaml
wget https://github.com/tektoncd/dashboard/releases/download/v0.5.1/tekton-dashboard-release.yaml

#修改yaml 里面的镜像库
vi release.yaml
:%s/gcr.io/gcr.azk8s.cn/g
:wq
vi tekton-dashboard-release.yaml
:%s/gcr.io/gcr.azk8s.cn/g
:wq

#重新创建资源
kubectl apply -f release.yaml
kubectl apply -f tekton-dashboard-release.yaml

#查看pods 运行状态
kubectl get pods --namespace tekton-pipelines --watch
NAME                                           READY   STATUS              RESTARTS   AGE
tekton-dashboard-7bb59dd55-x4q4q               0/1     ContainerCreating   0          10s
tekton-pipelines-controller-6f9dc7b8f9-99bd9   1/1     Running             0          10m
tekton-pipelines-webhook-69f557c7ff-pqml8      1/1     Running             0          10m
tekton-dashboard-7bb59dd55-x4q4q               0/1     Running             0          45s
tekton-dashboard-7bb59dd55-x4q4q               1/1     Running             0          50s

 

HelloWork TaskRun

 

准备好 Resource

task :  task-hello-world.yaml

taskrun :  taskrun-hello-world.yaml

kustomization : 配置以上资源

 

实例:

root@vm1:~/k8s/tekton/helloworld# ll
total 20
drwxr-xr-x 2 root root 4096 Apr 15 11:25 ./
drwxr-xr-x 3 root root 4096 Apr 15 11:23 ../
-rw-r--r-- 1 root root   67 Apr 15 11:25 kustomization.yaml
-rw-r--r-- 1 root root  196 Apr  2 13:23 task-hello-world.yaml
-rw-r--r-- 1 root root  134 Apr  2 13:23 taskrun-hello-world.yaml


root@vm1:~/k8s/tekton/helloworld# cat task-hello-world.yaml
apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
  name: echo-hello-world
spec:
  steps:
    - name: echo
      image: ubuntu
      command:
        - echo
      args:
        - "hello world"


root@vm1:~/k8s/tekton/helloworld# cat taskrun-hello-world.yaml 
apiVersion: tekton.dev/v1alpha1
kind: TaskRun
metadata:
  name: echo-hello-world-task-run
spec:
  taskRef:
    name: echo-hello-world

    
root@vm1:~/k8s/tekton/helloworld# cat kustomization.yaml
resources:
  - task-hello-world.yaml
  - taskrun-hello-world.yaml

root@vm1:~/k8s/tekton/helloworld# kubectl apply -k .

 


 

常用检查命令

 tkn taskrun describe <echo-hello-world-task-run>​
tkn taskrun logs  <echo-hello-world-task-run>



常见debug 命令

 

kubectl get pods --watch

kubectl descibe pod xxxxx

kubectl logs <podname> 查看所有steps

kubectl logs <podname> --container/-c <step-name>


#持续观察pod状态的变化
kubectl get pods --watch
#当状态为error时,查看这个pod 的log
kubectl descibe pod <podname>

#在上面的describe 的详情页面找到发生Error的步骤:如step-build-and-push, 查看此阶段的log
kubectl logs <podname> --container step-build-and-push
#这个log的内容其实可以在执行的节点上找到对应的容器,为vm2上容器log
docker container logs fc7d1c46d52c --tail 10



应用一个 TaskRun

  • 创建一个 secret
  • 创建各种资源:  git , image,  serviceaccout ,  task
  1. git:  指 代码库的资源 ,比如gitlab
  2. image:  指定 docker build 的image 需要推送的镜像库registry
  3. serviceAccount: 指定镜像库的认证信息,引用上面创建的secret
  4. task: 指定 任务 的过程 ,比如使用 git 类型作为输入,image 类型作为输出,执行step 的步骤
  • 创建taskrun : 用来引用上面的创建的task 和其他各种资源。

       

kubectl create secret docker-registry regcred \
                    --docker-server=registry.gitlab.systems.com \
                    --docker-username=xxxx \
                    --docker-password=xxxx \
                    --docker-email=xxxxxx@china.com.cn
​


# 例子参见 官网
kubectl apply -f pipelineresource-ci-git-repo.yaml
kubectl apply -f pipelineresource-cointainer-registry.yaml
kubectl apply -f tekton-docker-cred-service.yaml
kubectl apply -f task-build-image.yaml
kubectl apply -f taskrun-build-docker-image.yaml

应用一个 pipelineRun

  • 创建一个 secret
  • 创建各种资源:  git , image,  serviceaccout ,  task,clusterrole,clusterrolebinding ,pipeline
  1. git:  指 代码库的资源 ,比如gitlab
  2. image:  指定 docker build 的image 需要推送的镜像库registry
  3. serviceAccount: 指定镜像库的认证信息,引用上面创建的secret
  4. clusterrole:创建一个集群角色
  5. clusterrolebinding:将集群角色和serviceaccout 绑定在一起
  6. task: 指定 任务 的过程 ,比如使用 git 类型作为输入,image 类型作为输出,执行step 的步骤
  7. task:  指定部署任务。
  8. pipeline:  对各个task 编排,传参
  • 创建pipelinerun : 用来引用上面的创建的pipeline 和其他资源一起应用。

 

 
kubectl create clusterrole tekton-role \
               --verb=get,list,watch,create,update,patch,delete \
               --resource=deployments,deployments.apps,jobs​
kubectl create clusterrolebinding tekton-binding \
             --clusterrole=tekton-role \
             --serviceaccount=default:docker-cred-service​​

 

 # 例子参见 官网
 # https://github.com/tektoncd/pipeline/blob/master/docs/pipelineruns.md
kubectl apply -f pipelineresource-ci-git-repo.yaml
kubectl apply -f pipelineresource-cointainer-registry.yaml
kubectl apply -f tekton-docker-cred-service.yaml
kubectl apply -f task-build-image.yaml
kubectl apply -f task-deploy-using-kubectl.yaml 
kubectl apply -f pipeline.yaml
kubectl apply -f pipelinerun.yaml 

​


 

 部署 tekon 仪表盘

 

 下载 dashboard 的yaml

https://github.com/tektoncd/dashboard/tree/b7fdb418af94a29c0f99f8ae4b569848a4488d31#install-dashboard

 

  • 修改 yaml 文件中的镜像库为国内镜像

gcr.azk8s.cn/tekton-releases/github.com/tektoncd/dashboard/cmd/dashboard

  • 修改service 的类型为NodePort

在yaml 文件的tekton-dashboard的定义下,spec 下加上type: NodePort

  •        应用yaml , 安装dashboard
kubectl apply -f dashboard.yaml ​
  • 查看服务的nodeport , 30910
kubectl get svc --all-namespaces
tekton-pipelines   tekton-dashboard              NodePort    10.111.144.179   <none>        9097:30910/TCP              31s
  • 浏览器访问,使用集群节点ip加nodeport 30910

      比如http://vm1:30910

 

 

 

 

 


Pre: common-password 设置密码复杂度

Next: 新装 ubuntu 18/20 之后的root 密码、 网络、ssh 设置 、软件源

2895
Sign in to leave a comment.
No Leanote account? Sign up now.
3 comments
Table of content