介绍如何在Gitlab项目中添加K8S集群,以便使用K8S集群部署gitlab-runner帮我们运行gitlab的CI/CD。

参考官方文档:https://docs.gitlab.com/ee/user/project/clusters/add_remove_clusters.html#add-existing-cluster

1.登入gitlab后,进入自己的项目主页,菜单栏 Operations => Kubernetes => Add Kubernetes cluster,选择页签 Add existing cluster。



2.只需要获取响应的值填录到该表单即可。Kubernetes cluster name集群名称随意填,Project namespace可不填。

2.1 获取API URL

运行以下命令得到输出值:

kubectl cluster-info | grep 'Kubernetes master' | awk '/http/ {print $NF}'

2.2 获取CA Certificate

运行以下命令得到输出值:

kubectl get secrets # 获取一个类似default-token-xxxxx的名称,填写在下面这个命令<secret name>
kubectl get secret <secret name> -o jsonpath="{['data']['ca\.crt']}" | base64 --decode

2.3 获取Token

创建文件gitlab-admin-service-account.yaml:

vim gitlab-admin-service-account.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
name: gitlab
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: gitlab-admin
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: gitlab
namespace: kube-system kubectl apply -f gitlab-admin-service-account.yaml
kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep gitlab | awk '{print $1}')

添加完成之后,可以在集群中安装你想用的插件了,例如gitlab-runner。

官方操作步骤:

Add existing cluster

If you have an existing Kubernetes cluster, you can add it to a project, group, or instance.

NOTE: Note: Kubernetes integration is not supported for arm64 clusters. See the issue Helm Tiller fails to install on arm64 cluster for details.

Existing Kubernetes cluster

To add a Kubernetes cluster to your project, group, or instance:

  1. Navigate to your:

    1. Project's {cloud-gear} Operations > Kubernetes page, for a project-level cluster.
    2. Group's {cloud-gear} Kubernetes page, for a group-level cluster.
    3. Admin Area > {cloud-gear} Kubernetes page, for an instance-level cluster.
  2. Click Add Kubernetes cluster.

  3. Click the Add existing cluster tab and fill in the details:

    1. Kubernetes cluster name (required) - The name you wish to give the cluster.

    2. Environment scope (required) - The associated environment to this cluster.

    3. API URL (required) - It's the URL that GitLab uses to access the Kubernetes API. Kubernetes exposes several APIs, we want the "base" URL that is common to all of them. For example, https://kubernetes.example.com rather than https://kubernetes.example.com/api/v1.

      Get the API URL by running this command:

      kubectl cluster-info | grep 'Kubernetes master' | awk '/http/ {print $NF}'
    4. CA certificate (required) - A valid Kubernetes certificate is needed to authenticate to the cluster. We will use the certificate created by default.

      1. List the secrets with kubectl get secrets, and one should be named similar to default-token-xxxxx. Copy that token name for use below.

      2. Get the certificate by running this command:

        kubectl get secret <secret name> -o jsonpath="{['data']['ca\.crt']}" | base64 --decode

        NOTE: Note: If the command returns the entire certificate chain, you need copy the root ca certificate at the bottom of the chain.

    5. Token - GitLab authenticates against Kubernetes using service tokens, which are scoped to a particular namespace. The token used should belong to a service account with cluster-admin privileges. To create this service account:

      1. Create a file called gitlab-admin-service-account.yaml with contents:

        apiVersion: v1
        kind: ServiceAccount
        metadata:
        name: gitlab
        namespace: kube-system
        ---
        apiVersion: rbac.authorization.k8s.io/v1beta1
        kind: ClusterRoleBinding
        metadata:
        name: gitlab-admin
        roleRef:
        apiGroup: rbac.authorization.k8s.io
        kind: ClusterRole
        name: cluster-admin
        subjects:
        - kind: ServiceAccount
        name: gitlab
        namespace: kube-system
      2. Apply the service account and cluster role binding to your cluster:

        kubectl apply -f gitlab-admin-service-account.yaml

        You will need the container.clusterRoleBindings.create permission to create cluster-level roles. If you do not have this permission, you can alternatively enable Basic Authentication and then run the kubectl apply command as an admin:

        kubectl apply -f gitlab-admin-service-account.yaml --username=admin --password=<password>

        NOTE: Note: Basic Authentication can be turned on and the password credentials can be obtained using the Google Cloud Console.

        Output:

        serviceaccount "gitlab" created
        clusterrolebinding "gitlab-admin" created
      3. Retrieve the token for the gitlab service account:

        kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep gitlab | awk '{print $1}')

        Copy the <authentication_token> value from the output:

        Name:         gitlab-token-b5zv4
        Namespace: kube-system
        Labels: <none>
        Annotations: kubernetes.io/service-account.name=gitlab
        kubernetes.io/service-account.uid=bcfe66ac-39be-11e8-97e8-026dce96b6e8 Type: kubernetes.io/service-account-token Data
        ====
        ca.crt: 1025 bytes
        namespace: 11 bytes
        token: <authentication_token>

      NOTE: Note: For GKE clusters, you will need the container.clusterRoleBindings.create permission to create a cluster role binding. You can follow the Google Cloud documentation to grant access.

    6. GitLab-managed cluster - Leave this checked if you want GitLab to manage namespaces and service accounts for this cluster. See the Managed clusters section for more information.

    7. Project namespace (optional) - You don't have to fill it in; by leaving it blank, GitLab will create one for you. Also:

      • Each project should have a unique namespace.
      • The project namespace is not necessarily the namespace of the secret, if you're using a secret with broader permissions, like the secret from default.
      • You should not use default as the project namespace.
      • If you or someone created a secret specifically for the project, usually with limited permissions, the secret's namespace and project namespace may be the same.
  4. Finally, click the Create Kubernetes cluster button.

After a couple of minutes, your cluster will be ready to go. You can now proceed to install some pre-defined applications.

Gitlab添加K8S集群的更多相关文章

  1. 使用RKE快速部署k8s集群

    一.环境准备 1.1环境信息 IP地址 角色 部署软件 10.10.100.5 K8s Master Etcd.Control 10.10.100.17 K8s Worker1 Worker 10.1 ...

  2. rancher导入k8s集群后添加监控无数据

    1.日志报错 rancher导入k8s集群后添加监控无数据,rancher日志报错: k8s.io/kube-state-metrics/pkg/collectors/builder.go:: Fai ...

  3. k8s集群节点更换ip 或者 k8s集群添加新节点

    1.需求情景:机房网络调整,突然要回收我k8s集群上一台node节点机器的ip,并调予新的ip到这台机器上,所以有了k8s集群节点更换ip一说:同时,k8s集群节点更换ip也相当于k8s集群添加新节点 ...

  4. kubernetes系列03—kubeadm安装部署K8S集群

    本文收录在容器技术学习系列文章总目录 1.kubernetes安装介绍 1.1 K8S架构图 1.2 K8S搭建安装示意图 1.3 安装kubernetes方法 1.3.1 方法1:使用kubeadm ...

  5. rancher2.X搭建k8s集群平台

    一, 新版特性 Rancher 1.6支持多种容器编排框架,包括Kubernetes.Mesos.Docker Swarm,默认的基础编排引擎是Cattle,Cattle极简的操作体验受到了大量开源社 ...

  6. 关于使用rancher部署k8s集群的一些小问题的解决

    问题一: 在rancher的ui上,不能创建k8s的master节点的高可用集群.创建k8s集群,添加节点的时候,可以添加多个master,但是多个master又没有高可用,只要其中一个出问题了,那么 ...

  7. jenkins流水线部署springboot应用到k8s集群(k3s+jenkins+gitee+maven+docker)(2)

    前言:上篇已介绍了jenkins在k3s环境部署,本篇继续上篇讲述流水线构建部署流程 1.从gitlab上拉取代码步骤 在jenkins中,新建一个凭证:Manage Jenkins -> Ma ...

  8. China Azure中部署Kubernetes(K8S)集群

    目前China Azure还不支持容器服务(ACS),使用名称"az acs create --orchestrator-type Kubernetes -g zymtest -n kube ...

  9. k8s重要概念及部署k8s集群(一)--技术流ken

    重要概念 1. cluster cluster是 计算.存储和网络资源的集合,k8s利用这些资源运行各种基于容器的应用. 2.master master是cluster的大脑,他的主要职责是调度,即决 ...

随机推荐

  1. 4-8 CS后台项目练习-2

    8. 类别管理--添加类别--持久层 8.1. 配置 续前日,无新增 8.2. 规划需要执行的SQL语句 续前日,无新增 8.3. 接口与抽象方法 此前需要执行的SQL语句大致是: select id ...

  2. IDea折叠模块快捷键-*04

    如何折叠IntelliJ IDEA代码片段 IntelliJ IDEA 快捷键说明大全(转载) idea 字体_IDEA 新手实用插件分享:让你的 IDEA 逼格瞬间提升

  3. 基于ABP实现DDD--实体创建和更新

      本文主要介绍了通过构造函数和领域服务创建实体2种方式,后者多用于在创建实体时需要其它业务规则检测的场景.最后介绍了在应用服务层中如何进行实体的更新操作. 一.通过构造函数创建实体 假如Issue的 ...

  4. Linux系统上传公钥不生效问题

    Authentication refused: bad ownership or modes for file /home/yanbo.xu/.ssh/authorized_keys 原因: sshd ...

  5. 自定义bean对象实现序列化接口

    上一个word count的案例中,我们为了理解mapreduce的流程,写了上面的代码.现在我们要把一个实体类序列化.比如现在有这么一个文件,里面的数据格式是这样的: 第一列是时间戳,第二列是手机号 ...

  6. centos/windows服务器,Mysql数据库表结构损坏-已解决

    [问题原因]服务器突然断电 [故障报告]数据库表结构损坏 [解决思路]进入强制恢复模式,备份库表及数据重建 故障发现 周末公司断电,周一启动数据库就直接报错了 查看日志 上面标记的log,明确表示是非 ...

  7. Excel 运算符(四):引用运算符

    引用运算符用于将单元格区域合并运算,包括:冒号.,逗号. 空格. : 运算 :运算符用于定义一个连续的数据区域,例如"A1:B3",表示从 A1 到 B3 的 6 个单元格. 并集 ...

  8. rcu使用遇到问题汇总

    1.3.10内核,在项目中遇到一种情况,我们根据sk指针hash到一个cpu上,然后访问该cpu对应分配的一个数据区. 然后系统会偶尔crash掉,crash掉有两种情况,一种是cred的rcu回收时 ...

  9. [NOI P模拟赛] 传统艺能(子序列自动机、矩阵乘法,线段树)

    (2:00)OID:"完了,蓝屏了!"(代码全消失) 众人欢呼 OID:开机,"原题测试--" (30min later)OID 开始传统艺能: " ...

  10. 随机视频API

    首先打开服务器创建一个html文件也可以不创建 代码如下 点击查看代码 <!DOCTYPE html> <html lang="zh-CN"> <he ...