K8S+GitLab-自动化分布式部署ASP.NET Core(一) 部署环境
一.部署流程介绍
开发人员通过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
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 "xxx@qq.com" -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
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
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
微软官方安装链接