一.部署流程介紹 開發人員通過Git上傳asp.net core 項目到Gilab,並編寫好.gitlab-ci.yml , GitLab-Runner 自動拉取代碼,然後進行Build,編譯,單元測試,生成鏡像,並推送到Harbor倉庫,docker 進行測試部署,最後部署部署到K8S. 二.集群 ...
一.部署流程介紹
開發人員通過Git上傳asp.net core 項目到Gilab,並編寫好.gitlab-ci.yml , GitLab-Runner 自動拉取代碼,然後進行Build,編譯,單元測試,生成鏡像,並推送到Harbor倉庫,docker 進行測試部署,最後部署部署到K8S.
二.集群組件介紹
節點 | IP | 組件 |
Deploy | 192.168.0.201 |
Breeze |
Master01 | 192.168.0.202 | Docker+loadbalancer+etc+K8S |
Harbor | 192.168.0.203 | Harbor+DOcker |
Work01 | 192.168.0.204 | Docker+Gitlab |
Work02 | 192.168.0.205 | Docker+Gitlab-Runner |
三.部署組件
1.先部署K8S 環境.
推薦兩種部署方式:
1.https://github.com/wise2c-devops/breeze (可視化Kubernetes集群部署工具,註本篇的部署方式)
2.https://github.com/opsnull/follow-me-install-kubernetes-cluster (手動部署)
2.部署redis
docker-compose安裝推薦
https://www.cnblogs.com/guoyiwen/p/10391885.html
3.部署Gitlab,使用docker-compose方式,創建docker-compose.yml文件
version: '3' services: gitlab: image: 'twang2218/gitlab-ce-zh' container_name: gitlab restart: unless-stopped environment: TZ: 'Asia/Shanghai' GITLAB_OMNIBUS_CONFIG: | external_url 'http://gitlab.evan.com' gitlab_rails['time_zone'] = 'Asia/Shanghai' #以下為啟用郵件相關設置,我用的是QQ郵箱 gitlab_rails['smtp_enable'] = true gitlab_rails['smtp_address'] = 'smtp.qq.com' gitlab_rails['smtp_port'] = 465 gitlab_rails['smtp_user_name'] = '你的郵箱@qq.com' gitlab_rails['smtp_password'] = '郵箱密碼' gitlab_rails['smtp_domain'] = 'smtp.qq.com' gitlab_rails['smtp_authentication'] = 'plain' gitlab_rails['smtp_enable_starttls_auto'] = true gitlab_rails['smtp_tls'] = true gitlab_rails['gitlab_email_from'] = '你的郵箱@qq.com' user["git_user_email"] = '你的郵箱@qq.com' #修改sidekiq的數量,減少記憶體占用,預設為15,也可以不填 sidekiq['concurrency'] = 10 #使用外部redis所需設置,根據剛剛生成的redis設置修改 gitlab_rails['redis_port'] = 6379 gitlab_rails['redis_host'] = '127.0.0.1' gitlab_rails['redis_password'] = '123' gitlab_rails['redis_database'] = '2' #因為22埠被占用,修改映射的埠號時,同時修改在Gitlab項目中的ssh地址加上埠號 gitlab_rails['gitlab_shell_ssh_port'] = 10022 ports: - '10080:80' - '10443:443' - '10022:22' networks: - net_db volumes: - ./config:/etc/gitlab - gitlab-data:/var/opt/gitlab - ./logs:/var/log/gitlab volumes: gitlab-data: external: true networks: net_db: external: true
docker network create net_db
docker volume create gitlab-data
docker-compose up -d
docker-compose logs -f (查看部署狀態)
修改gitlab.rb文件
external_url 'http://你的gitlab地址:10080'
nginx['listen_port'] =80
docker-compose restart
部署成功後Git運行以下命令:
Git global setup(Git全局設置):
git config --global user.name "xxx"
git config --global user.email "xxx.com"
ssh-keygen -t rsa -C "[email protected]" -b 4096
cat ~/.ssh/id_rsa.pub
ssh -T -p 10022 git@localhost (修改成你的地址,測試是否連接成功)
列印: Welcome to GitLab, @xxx! 說明成功了
4.安裝部署Gitlab-Runner(CentOS7)
如果官方的Runner下載不了下載下麵的
鏈接: https://pan.baidu.com/s/1q7ErwW53osI0LRy8SO8Gaw 提取碼: d9n7
1.下載二進位文件
# Linux x86-64 sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
2.賦予它執行許可權:
sudo chmod +x /usr/local/bin/gitlab-runner
3.創建GitLab CI用戶:
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
4.將gitlab ci用戶添加到docker運行用戶的組裡面
sudo usermod -aG root gitlab-runner
5.安裝並作為服務運行:
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start
6.註冊Gitlab-Runner ,shell 方式
sudo gitlab-runner register
官網參考鏈接:https://docs.gitlab.com/runner/register/index.html
7.測試
sudo -u gitlab-runner -H docker info
8.給Gitlab-Runner 執行kubectl許可權,拷貝 /root/.kube 文件夾 到 gitlab-runner 用戶家目錄下,且修改此文件夾屬於 gitlab-runner用戶和組
chown -R gitlab-runner.gitlab-runner .kube
sudo -u gitlab-runner kubectl get cs
5.安裝.NET Core SDK (CentOS7)
在部署Gitlab-Runner的機器上安裝SDK,因為Gitlab CI 要構建代碼和運行ASP.NET Core單元測試.
sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm
sudo yum update
sudo yum install dotnet-sdk-2.2
微軟官方安裝鏈接