開發人員經過Git上傳asp.net core 項目到Gilab,並編寫好.gitlab-ci.yml , GitLab-Runner 自動拉取代碼,而後進行Build,編譯,單元測試,生成鏡像,並推送到Harbor倉庫,docker 進行測試部署,最後部署部署到K8S.html
節點 | IP | 組件 |
Deploy | 192.168.0.201 | Breezelinux |
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.https://github.com/wise2c-devops/breeze (可視化Kubernetes集羣部署工具,注本篇的部署方式)
2.https://github.com/opsnull/follow-me-install-kubernetes-cluster (手動部署)
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 "xxx@qq.com" -b 4096nginx
cat ~/.ssh/id_rsa.pub
ssh -T -p 10022 git@localhost (修改爲你的地址,測試是否鏈接成功)
打印: Welcome to GitLab, @xxx! 說明成功了
若是官方的Runner下載不了下載下面的
連接: https://pan.baidu.com/s/1q7ErwW53osI0LRy8SO8Gaw 提取碼: d9n7
Gitlab-Runner服務器上安裝Git
yum install -y git
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
sudo usermod -aG docker 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
在部署Gitlab-Runner的機器上安裝SDK,由於Gitlab CI 要構建代碼和運行ASP.NET Core單元測試.git
sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm
sudo yum update
sudo yum install dotnet-sdk-2.2
微軟官方安裝連接