• 2. 在 Kubernetes 上安装 Gitlab


    总结:
    所需要的三个yaml文件的下载地址:https://files.cnblogs.com/files/sanduzxcvbnm/k8s-gitlab.zip

    Gitlab官方提供了 Helm 的方式在 Kubernetes 集群中来快速安装,但是在使用的过程中发现 Helm 提供的 Chart 包中有很多其他额外的配置,所以我们这里使用自定义的方式来安装,也就是自己来定义一些资源清单文件。

    Gitlab主要涉及到3个应用:Redis、Postgresql、Gitlab 核心程序,实际上我们只要将这3个应用分别启动起来,然后加上对应的配置就可以很方便的安装 Gitlab 了,我们这里选择使用的镜像不是官方的,而是 Gitlab 容器化中使用非常多的一个第三方镜像:sameersbn/gitlab,基本上和官方保持同步更新,地址:http://www.damagehead.com/docker-gitlab/

    安装

    如果我们已经有可使用的 Redis 或 Postgresql 服务的话,那么直接配置在 Gitlab 环境变量中即可,如果没有的话就单独部署。
    首先部署需要的 Redis 服务,对应的资源清单文件如下:(gitlab-redis.yaml)

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: redis
      namespace: kube-ops
      labels:
        name: redis
    spec:
      replicas: 1 
      selector: 
        matchLabels:
          name: redis
      template:
        metadata:
          name: redis
          labels:
            name: redis
        spec:
          containers:
          - name: redis
            image: sameersbn/redis
            imagePullPolicy: IfNotPresent
            ports:
            - name: redis
              containerPort: 6379
            volumeMounts:
            - mountPath: /var/lib/redis
              name: data
            livenessProbe:
              exec:
                command:
                - redis-cli
                - ping
              initialDelaySeconds: 30
              timeoutSeconds: 5
            readinessProbe:
              exec:
                command:
                - redis-cli
                - ping
              initialDelaySeconds: 5
              timeoutSeconds: 1
          volumes:
          - name: data
            emptyDir: {}
    
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: redis
      namespace: kube-ops
      labels:
        name: redis
    spec:
      ports:
        - name: redis
          port: 6379
          targetPort: redis
      selector:
        name: redis
    

    然后是数据库 Postgresql,对应的资源清单文件如下:(gitlab-postgresql.yaml)

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: postgresql
      namespace: kube-ops
      labels:
        name: postgresql
    spec:
      replicas: 1
      selector:
        matchLabels:
          name: postgresql
      template:
        metadata:
          name: postgresql
          labels:
            name: postgresql
        spec:
          containers:
          - name: postgresql
            image: sameersbn/postgresql:10
            imagePullPolicy: IfNotPresent
            env:
            - name: DB_USER
              value: gitlab
            - name: DB_PASS
              value: passw0rd
            - name: DB_NAME
              value: gitlab_production
            - name: DB_EXTENSION
              value: pg_trgm
            ports:
            - name: postgres
              containerPort: 5432
            volumeMounts:
            - mountPath: /var/lib/postgresql
              name: data
            livenessProbe:
              exec:
                command:
                - pg_isready
                - -h
                - localhost
                - -U
                - postgres
              initialDelaySeconds: 30
              timeoutSeconds: 5
            readinessProbe:
              exec:
                command:
                - pg_isready
                - -h
                - localhost
                - -U
                - postgres
              initialDelaySeconds: 5
              timeoutSeconds: 1
          volumes:
          - name: data
            emptyDir: {}
    
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: postgresql
      namespace: kube-ops
      labels:
        name: postgresql
    spec:
      ports:
        - name: postgres
          port: 5432
          targetPort: postgres
      selector:
        name: postgresql
    

    然后就是我们最核心的 Gitlab 的应用,对应的资源清单文件如下:(gitlab.yaml)

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: gitlab
      namespace: kube-ops
      labels:
        name: gitlab
    spec:
      replicas: 1
      selector:
        matchLabels:
          name: gitlab
      template:
        metadata:
          name: gitlab
          labels:
            name: gitlab
        spec:
          containers:
          - name: gitlab
            image: sameersbn/gitlab:11.8.1
            imagePullPolicy: IfNotPresent
            env:
            - name: TZ
              value: Asia/Shanghai
            - name: GITLAB_TIMEZONE
              value: Beijing
            - name: GITLAB_SECRETS_DB_KEY_BASE
              value: long-and-random-alpha-numeric-string
            - name: GITLAB_SECRETS_SECRET_KEY_BASE
              value: long-and-random-alpha-numeric-string
            - name: GITLAB_SECRETS_OTP_KEY_BASE
              value: long-and-random-alpha-numeric-string
            - name: GITLAB_ROOT_PASSWORD
              value: admin321
            - name: GITLAB_ROOT_EMAIL
              value: 517554016@qq.com
            - name: GITLAB_HOST
              value: git.qikqiak.com
            - name: GITLAB_PORT
              value: "80"
            - name: GITLAB_SSH_PORT
              value: "22"
            - name: GITLAB_NOTIFY_ON_BROKEN_BUILDS
              value: "true"
            - name: GITLAB_NOTIFY_PUSHER
              value: "false"
            - name: GITLAB_BACKUP_SCHEDULE
              value: daily
            - name: GITLAB_BACKUP_TIME
              value: 01:00
            - name: DB_TYPE
              value: postgres
            - name: DB_HOST
              value: postgresql
            - name: DB_PORT
              value: "5432"
            - name: DB_USER
              value: gitlab
            - name: DB_PASS
              value: passw0rd
            - name: DB_NAME
              value: gitlab_production
            - name: REDIS_HOST
              value: redis
            - name: REDIS_PORT
              value: "6379"
            ports:
            - name: http
              containerPort: 80
            - name: ssh
              containerPort: 22
            volumeMounts:
            - mountPath: /home/git/data
              name: data
            livenessProbe:
              httpGet:
                path: /
                port: 80
              initialDelaySeconds: 180
              timeoutSeconds: 5
            readinessProbe:
              httpGet:
                path: /
                port: 80
              initialDelaySeconds: 5
              timeoutSeconds: 1
          volumes:
          - name: data
            emptyDir: {}
    
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: gitlab
      namespace: kube-ops
      labels:
        name: gitlab
    spec:
      ports:
        - name: http
          port: 80
          targetPort: http
        - name: ssh
          port: 22
          targetPort: ssh
      selector:
        name: gitlab
    
    ---
    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      name: gitlab
      namespace: kube-ops
      annotations:
        kubernetes.io/ingress.class: traefik # 注意这个,需要事先安装好traefik,若是使用的nginx-ingress,则需要删除这两行
    spec:
      rules:
      - host: git.qikqiak.com
        http:
          paths: /
          - backend:
              serviceName: gitlab
              servicePort: http
    

    要注意我们这里应用数据没有做数据持久化,只是使用的emptyDir: {}类型的 volume,Pod 挂掉后,对应的数据也就没有了,所以要在正式的环境中使用一定要做数据的持久化,比如添加 PV/PVC 或者 StorageClass。

    要注意的是其中 Redis 和 Postgresql 相关的环境变量配置,另外,我们这里添加了一个 Ingress 对象,来为我们的 Gitlab 配置一个域名git.qikqiak.com,这样应用部署完成后,我们就可以通过该域名来访问了,然后直接部署即可:

    可以事先把gitlab的镜像给拉取下来:docker pull sameersbn/gitlab:11.8.1

    kubectl create -f gitlab-redis.yaml
    kubectl create -f gitlab-postgresql.yaml
    kubectl create -f gitlab.yaml
    

    创建完成后,查看 Pod 的部署状态:

    # kubectl get pods -n kube-ops
    NAME                          READY   STATUS    RESTARTS   AGE
    gitlab-75f4f976f-5nldw        1/1     Running   0          3m28s
    postgresql-6bdfc9f845-4bfh5   1/1     Running   0          140m
    redis-644479576f-7jx9p        1/1     Running   0          141m
    

    可以看到都已经部署成功了,然后我们可以通过 Ingress 中定义的域名git.qikqiak.com(需要做 DNS 解析或者在本地 /etc/hosts 中添加映射)来访问 Portal:
    本机环境:用的是windows系统的主机,然后安装一个vmware ,然后安装俩虚拟机,一个master,一个worker组成的k8s
    需要在本机windows上和workerz主机上添加如下域名解析:192.168.75.11为worker主机ip

    192.168.75.11 git.qikqiak.com
    

    (以下这俩错误是上面实际操作过程中遇到的,上面的代码都已经处理过了,可以直接执行)

    运行常见错误解决:
    1.error: error validating "gitlab-redis.yaml": error validating data: ValidationError(Deployment.spec): missing required field "selector" in io.k8s.api.apps.v1.DeploymentSpec; if you choose to ignore these errors, turn validation off with --validate=false

    报错意思:部署验证错误,在deploymentspec模块中必须指定 selector参数。
    在 Deployment.spec 模块中,除了指定 replicas 副本数量,还需要指定副本标签与 Deployment控制器进行匹配

    解决办法:写全 spec的 selector标签参数,与Pod和deployment相匹配

    2.error: unable to recognize "gitlab-redis.yaml": no matches for kind "Deployment" in version "apps/v1beta1"

    Deployment中使用的apps接口版本不一致。
    查询当前所有的版本:

    # kubectl api-versions
    admissionregistration.k8s.io/v1
    admissionregistration.k8s.io/v1beta1
    apiextensions.k8s.io/v1
    apiextensions.k8s.io/v1beta1
    apiregistration.k8s.io/v1
    apiregistration.k8s.io/v1beta1
    apps/v1
    authentication.k8s.io/v1
    authentication.k8s.io/v1beta1
    authorization.k8s.io/v1
    authorization.k8s.io/v1beta1
    autoscaling/v1
    autoscaling/v2beta1
    autoscaling/v2beta2
    batch/v1
    batch/v1beta1
    certificates.k8s.io/v1beta1
    coordination.k8s.io/v1
    coordination.k8s.io/v1beta1
    crd.projectcalico.org/v1
    discovery.k8s.io/v1beta1
    events.k8s.io/v1beta1
    extensions/v1beta1
    kuboard.cn/v1
    metrics.k8s.io/v1beta1
    networking.k8s.io/v1
    networking.k8s.io/v1beta1
    node.k8s.io/v1beta1
    policy/v1beta1
    rbac.authorization.k8s.io/v1
    rbac.authorization.k8s.io/v1beta1
    scheduling.k8s.io/v1
    scheduling.k8s.io/v1beta1
    storage.k8s.io/v1
    storage.k8s.io/v1beta1
    v1
    

    使用

    使用用户名 root,和部署的时候指定的超级用户密码GITLAB_ROOT_PASSWORD=admin321即可登录进入到首页:

    Gitlab 运行后,我们可以注册为新用户并创建一个项目,还可以做很多的其他系统设置,比如设置语言、设置应用风格样式等等。

    点击Create a project创建一个新的项目,和之前 Github 使用上没有多大的差别:

    创建完成后,我们可以添加本地用户的一个SSH-KEY,这样我们就可以通过 SSH 来拉取或者推送代码了。SSH 公钥通常包含在~/.ssh/id_rsa.pub 文件中,并以ssh-rsa开头。如果没有的话可以使用ssh-keygen命令来生成,id_rsa.pub里面的内容就是我们需要的 SSH 公钥,然后添加到 Gitlab 中。

    由于平时使用的 ssh 默认是 22 端口,现在如果用默认的 22 端口去连接,是没办法和 Gitlab 容器中的 22 端口进行映射的,因为我们只是通过 Service 的 22 端口进行了映射,要想通过节点去进行 ssh 链接就需要在节点上一个端口和容器内部的22端口进行绑定,所以这里我们可以通过 NodePort 去映射 Gitlab 容器内部的22端口,比如我们将环境变量设置为GITLAB_SSH_PORT=30022,将 Gitlab 的 Service 也设置为 NodePort 类型:

    apiVersion: v1
    kind: Service
    metadata:
      name: gitlab
      namespace: kube-ops
      labels:
        name: gitlab
    spec:
      ports:
        - name: http
          port: 80
          targetPort: http
        - name: ssh
          port: 22
          targetPort: ssh
          nodePort: 30022
      type: NodePort
      selector:
        name: gitlab
    

    注意上面 ssh 对应的 nodePort 端口设置为 30022,这样就不会随机生成了,重新更新下 Deployment 和 Service,更新完成后,现在我们在项目上面 Clone 的时候使用 ssh 就会带上端口号了:

    修改后的gitlab.yaml文件内容:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: gitlab
      namespace: kube-ops
      labels:
        name: gitlab
    spec:
      replicas: 1
      selector:
        matchLabels:
          name: gitlab
      template:
        metadata:
          name: gitlab
          labels:
            name: gitlab
        spec:
          containers:
          - name: gitlab
            image: sameersbn/gitlab:11.8.1
            imagePullPolicy: IfNotPresent
            env:
            - name: TZ
              value: Asia/Shanghai
            - name: GITLAB_TIMEZONE
              value: Beijing
            - name: GITLAB_SECRETS_DB_KEY_BASE
              value: long-and-random-alpha-numeric-string
            - name: GITLAB_SECRETS_SECRET_KEY_BASE
              value: long-and-random-alpha-numeric-string
            - name: GITLAB_SECRETS_OTP_KEY_BASE
              value: long-and-random-alpha-numeric-string
            - name: GITLAB_ROOT_PASSWORD
              value: admin321
            - name: GITLAB_ROOT_EMAIL
              value: 517554016@qq.com
            - name: GITLAB_HOST
              value: git.qikqiak.com
            - name: GITLAB_PORT
              value: "80"
            - name: GITLAB_SSH_PORT
              value: "30022" # 修改的这行
            - name: GITLAB_NOTIFY_ON_BROKEN_BUILDS
              value: "true"
            - name: GITLAB_NOTIFY_PUSHER
              value: "false"
            - name: GITLAB_BACKUP_SCHEDULE
              value: daily
            - name: GITLAB_BACKUP_TIME
              value: 01:00
            - name: DB_TYPE
              value: postgres
            - name: DB_HOST
              value: postgresql
            - name: DB_PORT
              value: "5432"
            - name: DB_USER
              value: gitlab
            - name: DB_PASS
              value: passw0rd
            - name: DB_NAME
              value: gitlab_production
            - name: REDIS_HOST
              value: redis
            - name: REDIS_PORT
              value: "6379"
            ports:
            - name: http
              containerPort: 80
            - name: ssh
              containerPort: 22
            volumeMounts:
            - mountPath: /home/git/data
              name: data
            livenessProbe:
              httpGet:
                path: /
                port: 80
              initialDelaySeconds: 180
              timeoutSeconds: 5
            readinessProbe:
              httpGet:
                path: /
                port: 80
              initialDelaySeconds: 5
              timeoutSeconds: 1
          volumes:
          - name: data
            emptyDir: {}
    
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: gitlab
      namespace: kube-ops
      labels:
        name: gitlab
    spec:
      ports:
        - name: http
          port: 80
          targetPort: http
        - name: ssh
          port: 22
          targetPort: ssh
          nodePort: 30022 # 新增的这行
      type: NodePort  # 新增的这行
      selector:
        name: gitlab
    
    ---
    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      name: gitlab
      namespace: kube-ops
      annotations:
        kubernetes.io/ingress.class: traefik # 注意这个,需要事先安装好traefik,若是使用的nginx-ingress,则需要删除这两行
    spec:
      rules:
      - host: git.qikqiak.com
        http:
          paths: /
          - backend:
              serviceName: gitlab
              servicePort: http
    

    然后在执行:kubectl apply -f gitlab.yaml

    现在就可以使用Clone with SSH的地址了,由于上面我们配置了 SSH 公钥,所以就可以直接访问上面的仓库了:

    $ git clone ssh://git@git.qikqiak.com:30022/root/gitlab-demo.git
    Cloning into 'gitlab-demo'...
    warning: You appear to have cloned an empty repository.
    Checking connectivity... done.
    

    然后随便在该项目下面添加一些资源:

    $ echo "# hello world" >  README.md
    $ git add .
    $ git commit -m 'hello world'
    [master (root-commit) 63de7cb] hello world
     1 file changed, 1 insertion(+)
     create mode 100644 README.md
    $ git push origin master
    Counting objects: 3, done.
    Writing objects: 100% (3/3), 224 bytes | 0 bytes/s, done.
    Total 3 (delta 0), reused 0 (delta 0)
    To ssh://git@git.qikqiak.com:30022/root/gitlab-demo.git
     * [new branch]      master -> master
    

    然后刷新浏览器,就可以看到刚刚创建的 Git 仓库中多了一个 README.md 的文件:


    到这里就表明我们的 Gitlab 就成功部署到了 Kubernetes 集群当中了。

    附录:安装nginx-ingress教程

    # vim nginx-deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deployment
      labels:
        app: nginx
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:1.7.9
    
    # vim nginx-service.yaml
    apiVersion: v1
    kind: Service
    metadata:
      name: nginx-service
      labels:
        app: nginx
    spec:
      selector:
        app: nginx
      ports:
      - name: nginx-port
        protocol: TCP
        port: 80
        nodePort: 32600
        targetPort: 80
      type: NodePort
    
    # vim nginx-ingress.yaml
    apiVersion: networking.k8s.io/v1beta1
    kind: Ingress
    metadata:
      name: my-ingress-for-nginx  # Ingress 的名字,仅用于标识
    spec:
      rules:                      # Ingress 中定义 L7 路由规则
      - host: a.demo.kuboard.cn   # 根据 virtual hostname 进行路由(请使用您自己的域名)
        http:
          paths:                  # 按路径进行路由
          - path: /
            backend:
              serviceName: nginx-service  # 指定后端的 Service 为之前创建的 nginx-service
              servicePort: 80
    

    执行命令

    kubectl apply -f nginx-deployment.yaml
    kubectl apply -f nginx-service.yaml
    kubectl apply -f nginx-ingress.yaml
    

    检查执行结果

    kubectl get ingress -o wide
    

    可查看到名称为 my-ingress-for-nginx 的 Ingress。

    从互联网访问

    # 请使用您自己的域名
    curl git.qikqiak.com
    
  • 相关阅读:
    Python004-数据处理示例:以某个数据(字段)为基准从数据中获取不同的字段行数
    VUE-004-禁止修改页面显示项,设置el-input,textarea只读方法
    VUE-003-前端表格数据展示时,设置单元格(el-table-column)保留空格和换行
    cobaltstrike4.0 cracked
    CobaltStrike3.14破解
    CobaltStrike3.12/13 破解
    Windows 10 Manager v2.3.3
    MobaXterm v10.9破解
    Jupyter/JupyterLab安装使用
    WordPress <= 4.6 命令执行漏洞(PHPMailer)复现分析
  • 原文地址:https://www.cnblogs.com/sanduzxcvbnm/p/13852854.html
Copyright © 2020-2023  润新知