Gitlab
官方提供了 Helm 的方式在 Kubernetes 集羣中來快速安裝,可是在使用的過程當中發現 Helm 提供的 Chart 包中有不少其餘額外的配置,因此咱們這裏使用自定義的方式來安裝,也就是本身來定義一些資源清單文件。node
Gitlab
主要涉及到3個應用:Redis、Postgresql、Gitlab 核心程序,實際上咱們只要將這3個應用分別啓動起來,而後加上對應的配置就能夠很方便的安裝 Gitlab 了,咱們這裏選擇使用的鏡像不是官方的,而是 Gitlab 容器化中使用很是多的一個第三方鏡像:sameersbn/gitlab
,基本上和官方保持同步更新,地址:http://www.damagehead.com/docker-gitlab/git
若是咱們已經有可以使用的 Redis 或 Postgresql 服務的話,那麼直接配置在 Gitlab 環境變量中便可,若是沒有的話就單獨部署。redis
建立一個用於存儲密碼的secret文件:sql
建立username和password文件:
$ echo -n "admin" > ./username $ echo -n "1f2d1e2e67df" > ./password
用kubectl生成secret對象:
$ kubectl create secret generic db-user-pass --from-file=./username --from-file=./password secret "db-user-pass" created
建立PVC和storageclass作持久化:docker
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: gitlab-redis-pvc namespace: kube-ops annotations: volume.beta.kubernetes.io/storage-class: "gitlab-storageclass" spec: accessModes: - ReadWriteMany resources: requests: storage: 1Gi
部署須要的 Redis 服務,對應的資源清單文件以下:(gitlab-redis.yaml)shell
apiVersion: apps/v1beta1 kind: Deployment metadata: name: redis namespace: kube-ops labels: name: redis spec: 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 persistentVolumeClaim: claimName: gitlab-redis-pvc --- apiVersion: v1 kind: Service metadata: name: redis namespace: kube-ops labels: name: redis spec: ports: - name: redis port: 6379 targetPort: redis selector: name: redis
vim gitlab-postgresql-pvc.yaml:數據庫
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: gitlab-postgresql-pvc namespace: kube-ops annotations: volume.beta.kubernetes.io/storage-class: "gitlab-storageclass" spec: accessModes: - ReadWriteMany resources: requests: storage: 1Gi
而後是數據庫 Postgresql,對應的資源清單文件以下:(gitlab-postgresql.yaml)vim
apiVersion: apps/v1beta1 kind: Deployment metadata: name: postgresql namespace: kube-ops labels: name: postgresql spec: template: metadata: name: postgresql labels: name: postgresql spec: containers: - name: postgresql image: sameersbn/postgresql 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 persistentVolumeClaim: claimName: gitlab-postgresql-pvc --- apiVersion: v1 kind: Service metadata: name: postgresql namespace: kube-ops labels: name: postgresql spec: ports: - name: postgres port: 5432 targetPort: postgres selector: name: postgresql
vim gitlab-pvc.yamlapi
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: gitlab-pvc namespace: kube-ops annotations: volume.beta.kubernetes.io/storage-class: "gitlab-storageclass" spec: accessModes: - ReadWriteMany resources: requests: storage: 1Gi
而後就是咱們最核心的 Gitlab 的應用,對應的資源清單文件以下:(gitlab.yaml)瀏覽器
apiVersion: apps/v1beta1 kind: Deployment metadata: name: gitlab namespace: kube-ops labels: name: gitlab spec: template: metadata: name: gitlab labels: name: gitlab spec: containers: - name: gitlab image: sameersbn/gitlab:12.1.6 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 valueFrom: secretKeyRef: name: git-user-pass key: password # value: admin321 - name: GITLAB_ROOT_EMAIL value: 1369472116@qq.com - name: GITLAB_HOST value: gitlab.fuyuteng.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 persistentVolumeClaim: claimName: gitlab-pvc --- 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 spec: rules: - host: gitlab.fuyuteng.com http: paths: - backend: serviceName: gitlab servicePort: http
咱們這裏應用數據都作數據持久化,還有敏感數據用了secret引入到環境變量,
好比添加 PV/PVC 或者 StorageClass。
要注意的是其中 Redis 和 Postgresql 相關的環境變量配置,另外,咱們這裏添加了一個 Ingress 對象,來爲咱們的 Gitlab 配置一個域名git.qikqiak.com
,這樣應用部署完成後,咱們就能夠經過該域名來訪問了,而後直接部署便可:
$ kubectl create -f gitlab-redis.yaml gitlab-postgresql.yaml gitlab.yaml
建立完成後,查看 Pod 的部署狀態:
$ kubectl get pods -n kube-ops NAME READY STATUS RESTARTS AGE gitlab-7d855554cb-twh7c 1/1 Running 0 10m postgresql-8566bb959c-2tnvr 1/1 Running 0 17h redis-8446f57bdf-4v62p 1/1 Running 0 17h
能夠看到都已經部署成功了,而後咱們能夠經過 Ingress 中定義的域名git.qikqiak.com
(須要作 DNS 解析或者在本地 /etc/hosts 中添加映射)來訪問 Portal:
gitlab portal
使用用戶名 root,和部署的時候指定的超級用戶密碼GITLAB_ROOT_PASSWORD=admin321
便可登陸進入到首頁:
gitlab homepage
Gitlab 運行後,咱們能夠註冊爲新用戶並建立一個項目,還能夠作不少的其餘系統設置,好比設置語言、設置應用風格樣式等等。
點擊Create a project
建立一個新的項目,和以前 Github 使用上沒有多大的差異:
create gitlab project
建立完成後,咱們能夠添加本地用戶的一個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 ssh
如今就可使用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 的文件: