20230111-构建极简轻量级基于K8S的DevOps平台

一、基于K8S的DevOps平台资源及环境规划

1.资源不足的情况下,K8S集群只开两台master节点;
⇒由于etcd集群部署在三台master上,所有为了保持etcd最小运行,必须至少开两台master;
⇒虽然生产环境下master不运行业务运行的POD,资源不足的情况下直接让master节点跑业务POD。

2.考虑gitlab对资源要求过高,选用轻量级的gogs。

主机名 环境功能 IP OS/应用版本 开关机状态
k8s-master01 K8S集群
--master
172.26.37.121 OS:AlmaLinux release 8.6
K8S Version:v1.23.8
资源:2C4G
开机
k8s-master02 K8S集群
--master
172.26.37.122 OS:AlmaLinux release 8.6
K8S Version:v1.23.8
资源:2C4G
开机
k8s-master03 K8S集群
--master
172.26.37.123 OS:AlmaLinux release 8.6
K8S Version:v1.23.8
资源:2C4G
一般关机
k8s-node01 K8S集群
--node
172.26.37.124 OS:AlmaLinux release 8.6
K8S Version:v1.23.8
资源:2C4G
一般关机
k8s-node02 K8S集群
--node
172.26.37.125 OS:AlmaLinux release 8.6
K8S Version:v1.23.8
资源:2C4G
一般关机
k8s-master-lb K8S集群
--master-LB
172.26.37.126 - -
docker(harbor) docker环境
--harbor环境
172.26.37.129 OS:AlmaLinux release 8.6
gocker Version:20.10.9
harbor Version:v2.3.2
资源:2C2G
开机
gogs(git) gogs环境
--git
172.26.37.127 OS:AlmaLinux release 8.6
gogs Version:0.12.9
资源:2C2G
开机
jenkins jenkins环境 172.26.37.128 OS:AlmaLinux release 8.6
K8S Version:v1.23.8
资源:2C2G
开机

二、基于K8S的DevOps平台设计

1)用户在Gogs中创建对应的项目;

2)在Harbor中创建对应的镜像项目;

3)Jenkins上配置Kuberneters集群(含测试及生产);

4)Jenkins创建对应的流水线(任务,job);

  流水线(任务,job)包括:通过Dockerfile生成镜像、将镜像推送(Push)到私有Harbor(或者其它的镜像仓库)、将更新的镜像部署到Kubernetes环境。

5)用户将推送(Push)代码到Gogs;

  1. 如果有配置钩子,推送(Push)代码会自动触发Jenkins流水线(任务,job);如没有配置钩子,需要手动触发Jenkins流水线(任务,job);


三、基于K8S的DevOps平台建设

一)Harbor环境部署

1.Docker部署

安装yum-utils包(包含 yum-config-manager应用)
# yum install -y yum-utils device-mapper-persistent-data lvm2

安装docker-ce.repo
# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

开启nightly repositories
# yum-config-manager --enable docker-ce-nightly

开启test repositories
# yum-config-manager --enable docker-ce-test

安装指定版本的docker-ce,确认docker-ce-cli也部署了
# yum list docker-ce --showduplicates | sort -r
docker-ce.x86_64            3:23.0.0~rc.1-1.el8                 docker-ce-test  
docker-ce.x86_64            3:23.0.0~beta.1-1.el8               docker-ce-test  
docker-ce.x86_64            3:22.06.0~beta.0-1.el8              docker-ce-test  
docker-ce.x86_64            3:20.10.9-3.el8                     docker-ce-test  
docker-ce.x86_64            3:20.10.9-3.el8                     docker-ce-stable
docker-ce.x86_64            3:20.10.8-3.el8                     docker-ce-test  

# yum install docker-ce-20.10.9

启动docker服务并设置自启动
# systemctl start docker.service
# systemctl enable docker.service
验证docker安装
# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:aa0cc8055b82dc2509bed2e19b275c8f463506616377219d9642221ab53cf9fe
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

2.配置Docker加速器

登录aliyun镜像加速器,获取镜像加速器地址信息

# tee /etc/docker/daemon.json <<-'EOF'
> {
>   "registry-mirrors": ["https://lg384hnd.mirror.aliyuncs.com"]
> }
> EOF
{
  "registry-mirrors": ["https://lg384hnd.mirror.aliyuncs.com"]
}
重新加载docker服务
# systemctl daemon-reload
# systemctl restart docker
查看加速器配置情况
# docker info

3.安装 Harbor

Harbor在物理机上部署是非常难的,而为了简化Harbor的应用,Harbor官方直接把Harbor做成了在容器中运行的应用,而且这个容器在Harbor中依赖类似redis、mysql、pgsql等很多存储系统,所以它需要编排很多容器协同起来工作,因此VMWare Harbor在部署和使用时,需要借助于Docker的单机编排工具( Docker compose)来实现。

Compose 是一个用于定义和运行多容器 Docker 应用程序的工具。使用 Compose,您可以使用 YAML 文件来配置应用程序的服务。然后,使用单个命令,从配置创建并启动所有服务。
安装docker-compose

# curl -L "https://github.com/docker/compose/releases/download/v2.15.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# cd /usr/local/bin/
# ll docker-compose
# chmod +x docker-compose
# docker-compose --version
docker-compose version 2.15.1, build d4451659
# ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

安装 harbor

# wget -c https://github.com/goharbor/harbor/releases/download/v2.3.2/harbor-offline-installer-v2.3.2.tgz
# tar xf  harbor-offline-installer-v2.3.2.tgz
# cd harbor
# docker load -i harbor.v2.3.2.tar.gz
Loaded image: goharbor/redis-photon:v2.3.2
Loaded image: goharbor/nginx-photon:v2.3.2
Loaded image: goharbor/harbor-portal:v2.3.2
Loaded image: goharbor/trivy-adapter-photon:v2.3.2
Loaded image: goharbor/chartmuseum-photon:v2.3.2
Loaded image: goharbor/notary-signer-photon:v2.3.2
Loaded image: goharbor/harbor-core:v2.3.2
Loaded image: goharbor/harbor-log:v2.3.2
Loaded image: goharbor/harbor-registryctl:v2.3.2
Loaded image: goharbor/harbor-exporter:v2.3.2
Loaded image: goharbor/notary-server-photon:v2.3.2
Loaded image: goharbor/prepare:v2.3.2
Loaded image: goharbor/harbor-db:v2.3.2
Loaded image: goharbor/harbor-jobservice:v2.3.2
Loaded image: goharbor/registry-photon:v2.3.

# ls
common.sh  harbor.v2.3.2.tar.gz  harbor.yml.tmpl  install.sh  LICENSE  prepare

设置主机名
# echo "172.26.37.126 docker" >> /etc/hosts
# ping docker 
PING docker (172.26.37.126) 56(84) bytes of data.
64 bytes from docker (172.26.37.126): icmp_seq=1 ttl=64 time=0.231 ms
64 bytes from docker (172.26.37.126): icmp_seq=2 ttl=64 time=0.069 ms

修改harbor配置文件
# cp harbor.yml.tmpl harbor.yml
# vi harbor.yml
# diff harbor.yml.tmpl harbor.yml
5c5
< hostname: reg.mydomain.com
---
> hostname: docker     // 添加主机名
13,18c13,18
< https:               // 注释掉证书,不使用证书就需要注释
<   # https port for harbor, default is 443    
<   port: 443     // 注释
<   # The path of cert and key files for nginx
<   certificate: /your/certificate/path     // 注释
<   private_key: /your/private/key/path     // 注释
---
> #https:
> #  # https port for harbor, default is 443
> #  port: 443
> #  # The path of cert and key files for nginx
> #  certificate: /your/certificate/path
> #  private_key: /your/private/key/path

注意data目录:data_volume: /data
注意默认密码:harbor_admin_password: Harbor12345
创建目录
# mkdir -p /data /var/log/harbor

安装前检测
# ./prepare
prepare base dir is set to /root/harbor
WARNING:root:WARNING: HTTP protocol is insecure. Harbor will deprecate http protocol in the future. Please make sure to upgrade to https
Generated configuration file: /config/portal/nginx.conf
Generated configuration file: /config/log/logrotate.conf
Generated configuration file: /config/log/rsyslog_docker.conf
Generated configuration file: /config/nginx/nginx.conf
Generated configuration file: /config/core/env
Generated configuration file: /config/core/app.conf
Generated configuration file: /config/registry/config.yml
Generated configuration file: /config/registryctl/env
Generated configuration file: /config/registryctl/config.yml
Generated configuration file: /config/db/env
Generated configuration file: /config/jobservice/env
Generated configuration file: /config/jobservice/config.yml
Generated and saved secret to file: /data/secret/keys/secretkey
Successfully called func: create_root_cert
Generated configuration file: /compose_location/docker-compose.yml
Clean up the input dir

安装
./install.sh
[Step 0]: checking if docker is installed ...
[Step 1]: checking docker-compose is installed ...
[Step 2]: loading Harbor images ...
[Step 3]: preparing environment ...
[Step 4]: preparing harbor configs ...
[Step 5]: starting Harbor ...
Creating network "harbor_harbor" with the default driver
Creating harbor-log ... done
Creating registry      ... done
Creating harbor-db     ... done
Creating registryctl   ... done
Creating redis         ... done
Creating harbor-portal ... done
Creating harbor-core   ... done
Creating harbor-jobservice ... done
Creating nginx             ... done
✔----Harbor has been installed and started successfully.----

# docker ps
CONTAINER ID   IMAGE                                COMMAND                  CREATED         STATUS                   PORTS                                   NAMES
3fecf91546bc   goharbor/harbor-jobservice:v2.3.2    "/harbor/entrypoint.鈥   3 minutes ago   Up 3 minutes (healthy)                                           harbor-jobservice
49d49cbac2c6   goharbor/nginx-photon:v2.3.2         "nginx -g 'daemon of鈥   3 minutes ago   Up 3 minutes (healthy)   0.0.0.0:80->8080/tcp, :::80->8080/tcp   nginx
568310d7122b   goharbor/harbor-core:v2.3.2          "/harbor/entrypoint.鈥   3 minutes ago   Up 3 minutes (healthy)                                           harbor-core
c2adc20a3d2d   goharbor/harbor-portal:v2.3.2        "nginx -g 'daemon of鈥   3 minutes ago   Up 3 minutes (healthy)                                           harbor-portal
38d7f57f58e8   goharbor/harbor-db:v2.3.2            "/docker-entrypoint.鈥   3 minutes ago   Up 3 minutes (healthy)                                           harbor-db
605bbb3e7f25   goharbor/redis-photon:v2.3.2         "redis-server /etc/r鈥   3 minutes ago   Up 3 minutes (healthy)                                           redis
1229c30f7581   goharbor/registry-photon:v2.3.2      "/home/harbor/entryp鈥   3 minutes ago   Up 3 minutes (healthy)                                           registry
0f481db488ac   goharbor/harbor-registryctl:v2.3.2   "/home/harbor/start.鈥   3 minutes ago   Up 3 minutes (healthy)                                           registryctl
f06f459592c6   goharbor/harbor-log:v2.3.2           "/bin/sh -c /usr/loc鈥   3 minutes ago   Up 3 minutes (healthy)   127.0.0.1:1514->10514/tcp               harbor-log

# ll common docker-compose.yml
-rw-r--r--. 1 root root 5996 Jan 12 20:07 docker-compose.yml
common:
total 0
drwxr-xr-x. 11 root root 133 Jan 12 20:05 config
# ss -antl
State              Recv-Q              Send-Q                           Local Address:Port                           Peer Address:Port             Process             
LISTEN             0                   128                                  127.0.0.1:1514                                0.0.0.0:*                                    
LISTEN             0                   128                                    0.0.0.0:80                                  0.0.0.0:*                                    
LISTEN             0                   128                                    0.0.0.0:22                                  0.0.0.0:*                                    
LISTEN             0                   128                                       [::]:80                                     [::]:*                                    
LISTEN             0                   128                                       [::]:22                                     [::]:*  

4.Web页面操作Harbor

1)使用系统账号登录

User:admin

Passwd:略

2)用户管理:创建用户

略

3)创建项目


harbor.png

5.Push Harbor仓库测试

配置Docker insecure-registries

修改docker启动insecure-registry地址
# cp -p /usr/lib/systemd/system/docker.service /usr/lib/systemd/system/docker.service.20230112
# vi /usr/lib/systemd/system/docker.service 
# diff  /usr/lib/systemd/system/docker.service /usr/lib/systemd/system/docker.service.20230112
13c13
< ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --insecure-registry=172.26.37.126:80
---
> ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
重启docker
# systemctl daemon-reload
# systemctl restart docker

docker登录Harbor测试

# docker login 172.26.37.126:80
Username: admin
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

docker push镜像到Harbor

查看镜像
# docker image ls
REPOSITORY                      TAG       IMAGE ID       CREATED         SIZE
hello-world                     latest    feb5d9fea6a5   15 months ago   13.3kB

tag镜像
# docker tag hello-world:latest 172.26.37.126:80/cmdb/hello-world:latest

查看镜像
# docker image ls
REPOSITORY                      TAG       IMAGE ID       CREATED         SIZE
hello-world                     latest    feb5d9fea6a5   15 months ago   13.3kB
172.26.37.126:80/cmdb/hello-world       latest    feb5d9fea6a5   15 months ago   13.3kB

push镜像
# docker push 172.26.37.126:80/cmdb/hello-world
Using default tag: latest
The push refers to repository [172.26.37.126:80/cmdb/hello-world]
e07ee1baac5f: Pushed 
latest: digest: sha256:f54a58bc1aac5ea1a25d796ae155dc228b3f0e11d046ae276b39c4bf2f13d8c4 size: 525

Harbor仓库中查看push的镜像


harbor2.png

6.Harbor开机自启动配置

确认重启命令

# cd harbor/
# pwd
/root/harbor
停止harbor
# docker-compose stop
启动harbor
# docker-compose start
Starting log         ... done
Starting registry    ... done
Starting registryctl ... done
Starting postgresql  ... done
Starting portal      ... done
Starting redis       ... done
Starting core        ... done
Starting jobservice  ... done
Starting proxy       ... done
查看harbor启动状况,并登录web页面确认
# docker ps

创建自启动相关文件

新建启动脚本
# vim harbor_enable.sh 
插入以下内容
# !/bin/bash
cd /root/harbor
docker-compose start

启动脚本赋予可执行权限
# chmod +x harbor_enable.sh 
# ll harbor_enable.sh 
-rwxr-xr-x. 1 root root 52 Jan 13 01:28 harbor_enable.sh

修改自启动文件
# vim /etc/rc.local
插入如下内容
/bin/bash /root/harbor/harbor_enable.sh
# ll /etc/rc.local 
lrwxrwxrwx. 1 root root 13 Apr 19  2022 /etc/rc.local -> rc.d/rc.local
# ll /etc/rc.d/rc.local 
-rw-r--r--. 1 root root 514 Jan 13 01:30 /etc/rc.d/rc.local
# chmod +x /etc/rc.local 
# ll /etc/rc.d/rc.local 
-rwxr-xr-x. 1 root root 514 Jan 13 01:30 /etc/rc.d/rc.local

重启服务器并查看启动后Harbor状况

重启服务器
# reboot
查看容器状况
# docker ps
登录Harbor web页面确认

7.问题处理

1)docker服务重启导致harbor-core无法正常完成启动
docker服务重启后,harbor-core状态为(health: starting)
# docker ps 
CONTAINER ID   IMAGE                             COMMAND                  CREATED       STATUS                             PORTS                                   NAMES
49d49cbac2c6   goharbor/nginx-photon:v2.3.2      "nginx -g 'daemon of鈥   4 hours ago   Up 2 minutes (healthy)             0.0.0.0:80->8080/tcp, :::80->8080/tcp   nginx
568310d7122b   goharbor/harbor-core:v2.3.2       "/harbor/entrypoint.鈥   4 hours ago   Up 16 seconds (health: starting)                                           harbor-core
c2adc20a3d2d   goharbor/harbor-portal:v2.3.2     "nginx -g 'daemon of鈥   4 hours ago   Up 2 minutes (healthy)                                                     harbor-portal
38d7f57f58e8   goharbor/harbor-db:v2.3.2         "/docker-entrypoint.鈥   4 hours ago   Up 2 minutes (healthy)                                                     harbor-db
1229c30f7581   goharbor/registry-photon:v2.3.2   "/home/harbor/entryp鈥   4 hours ago   Up 2 minutes (healthy)                                                     registry
f06f459592c6   goharbor/harbor-log:v2.3.2        "/bin/sh -c /usr/loc鈥   4 hours ago   Up 2 minutes (healthy)             127.0.0.1:1514->10514/tcp               harbor-log
进入harbor目录
# cd ~/harbor
使用docker-compose来重启容器
# docker-compose up -d
查看各个容器状况
# docker ps 
CONTAINER ID   IMAGE                                COMMAND                  CREATED       STATUS                        PORTS                                   NAMES
3fecf91546bc   goharbor/harbor-jobservice:v2.3.2    "/harbor/entrypoint.鈥   4 hours ago   Up About a minute (healthy)                                           harbor-jobservice
49d49cbac2c6   goharbor/nginx-photon:v2.3.2         "nginx -g 'daemon of鈥   4 hours ago   Up 4 minutes (healthy)        0.0.0.0:80->8080/tcp, :::80->8080/tcp   nginx
568310d7122b   goharbor/harbor-core:v2.3.2          "/harbor/entrypoint.鈥   4 hours ago   Up About a minute (healthy)                                           harbor-core
c2adc20a3d2d   goharbor/harbor-portal:v2.3.2        "nginx -g 'daemon of鈥   4 hours ago   Up 4 minutes (healthy)                                                harbor-portal
38d7f57f58e8   goharbor/harbor-db:v2.3.2            "/docker-entrypoint.鈥   4 hours ago   Up 4 minutes (healthy)                                                harbor-db
605bbb3e7f25   goharbor/redis-photon:v2.3.2         "redis-server /etc/r鈥   4 hours ago   Up About a minute (healthy)                                           redis
1229c30f7581   goharbor/registry-photon:v2.3.2      "/home/harbor/entryp鈥   4 hours ago   Up 4 minutes (healthy)                                                registry
0f481db488ac   goharbor/harbor-registryctl:v2.3.2   "/home/harbor/start.鈥   4 hours ago   Up About a minute (healthy)                                           registryctl
f06f459592c6   goharbor/harbor-log:v2.3.2           "/bin/sh -c /usr/loc鈥   4 hours ago   Up 4 minutes (healthy)        127.0.0.1:1514->10514/tcp               harbor-log

参考URL:

https://github.com/goharbor/harbor/releases/
https://github.com/goharbor/harbor
https://goharbor.io/docs/2.7.0/install-config/
https://docs.docker.com/compose/
https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
https://github.com/docker/compose/releases
https://blog.csdn.net/qq_40213055/article/details/122997250

二)Gogs环境部署

1.Git安装

git安装
# dnf install -y git

2.数据库安装(MySQL8)

依赖包安装

# dnf -y install wget  cmake gcc gcc-c++ ncurses  ncurses-devel  libaio-devel  openssl openssl-devel perl

创建mysql用户

# groupadd mysql
# useradd mysql -d /var/lib/mysql/ -g mysql
# tail -1  /etc/passwd
mysql:x:1000:1000::/var/lib/mysql/:/bin/bash

获取mysql-8.0.31安装包

# wget -c https://dev.mysql.com/get/mysql-8.0.31-1.el8.x86_64.rpm-bundle.tar
# tar -xvf mysql-8.0.31-1.el8.x86_64.rpm-bundle.tar

安装mysql

# rpm -ivh mysql-community-common-8.0.31-1.el8.x86_64.rpm 
# rpm -ivh mysql-community-client-plugins-8.0.31-1.el8.x86_64.rpm 
# rpm -ivh mysql-community-libs-8.0.31-1.el8.x86_64.rpm 
# rpm -ivh mysql-community-client-8.0.31-1.el8.x86_64.rpm 
# rpm -ivh mysql-community-icu-data-files-8.0.31-1.el8.x86_64.rpm 
# rpm -ivh mysql-community-server-8.0.31-1.el8.x86_64.rpm 

初始化mysql数据库

# mysqld --initialize --console

查看数据库初始化密码

# cat /var/log/mysqld.log  | grep localhost
2023-01-13T19:01:34.497431Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: yl)TpkEQj9B6

启动mysql服务

# chmod 777 /var/lib/mysql/auto.cnf
# chown -R mysql:mysql /var/lib/mysql/
# systemctl start mysqld
# systemctl status mysqld
# systemctl enable mysqld

确认数据库版本

# mysqladmin --version
mysqladmin  Ver 8.0.31 for Linux on x86_64 (MySQL Community Server - GPL)

确认mysql版本

# mysqladmin --version
mysqladmin  Ver 8.0.31 for Linux on x86_64 (MySQL Community Server - GPL)

修改mysql数据库root密码

# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.31

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.03 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> 

开启开启mysql的远程访问

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> 

3.安装Gogs服务

创建git用户

# groupadd git
# useradd git -d /home/git/ -g git
# tail -1  /etc/passwd
git:x:1001:1001::/home/git/:/bin/bash

下载Gogs安装程序

# wget https://dl.gogs.io/0.12.9/gogs_0.12.9_linux_amd64.tar.gz
# tar xvf gogs_0.12.9_linux_amd64.tar.gz
# ll gogs
total 44560
-rwxrwxr-x 1 mysql mysql 45608848 Jun  8  2022 gogs
-rw-rw-r-- 1 mysql mysql     1054 Jun  8  2022 LICENSE
-rw-rw-r-- 1 mysql mysql     7021 Jun  8  2022 README.md
-rw-rw-r-- 1 mysql mysql     5372 Jun  8  2022 README_ZH.md
drwxrwxr-x 7 mysql mysql      112 Jun  8  2022 scripts

创建gogs数据库并导入数据

# cd gogs/
# mysql -uroot -p < scripts/mysql.sql
Enter password: 

确认gogs数据库

# mysql -uroot -p
Enter password: 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| gogs               |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)
mysql> use gogs;
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> exit

创建git用户

# groupadd git
# useradd git -d /home/git/ -g git
# tail -1  /etc/passwd
git:x:1001:1001::/home/git/:/bin/bash

将gogs安装包所有者改为git

# chown -R git:git ~/gogs
# mv ~/gogs /home/git/

配置gogs自启动文件

# cp /home/git/gogs/scripts/systemd/gogs.service /lib/systemd/system/
# systemctl start gogs
# systemctl enable gogs
# systemctl status gogs

4.首次登录Gogs,并配置

首次登录Gogs:http://172.26.37.127:3000/ ,并配置

1)配置数据库信息



2)配置应用基本设置



3)配置可选设置

4)配置管理员用户

5)确认配置文件

# cat /home/git/gogs/custom/conf/app.ini

6)登录gogs

5.在Gogs中创建项目并利用

1)创建CMDB仓库



2)first commit

$ git clone http://172.26.37.127:3000/luorf/CMDB.git
$ touch README.md
$ git init
$ git add README.md
$ git commit -m "first commit"
$ git remote add origin http://172.26.37.127:3000/luorf/CMDB.git
$ git push -u origin master

参考URL

https://gpu.xuandashi.com/31361.html
https://dev.mysql.com/downloads/mysql/
https://www.jianshu.com/p/c25fba32b4f0
https://dev.mysql.com/doc/refman/8.0/en/server-option-variable-reference.html
https://blog.csdn.net/u013618714/article/details/126179117
https://gogs.io/
https://dl.gogs.io/
https://gogs.io/docs
https://blog.51cto.com/u_8406447/5769472

五)Jenkins+Gogs自动远程部署django项目

1.Jenkins安装或确认必要插件

jenkins安装或确认必要插件gitlab、Publish Over SSH。
Dashboard--Manage Jenkins--Plugin Manager


2.Publish Over SSH配置

jenkins配置SSH连接django服务部署的对象服务器
Dashboard--Manage Jenkins--Configure System,找到 Publish over SSH中SSH Server。输入目标服务器IP、登录用户、登录用户密码等,最后Test Configuration


3.Jenkins中创建Job

Dashboard--New Item--输入Item name--选择Freestyle project--点击OK

配置Job:

Source Code Management :Configure--General--Source Code Management--Git--输入Repository URL--选择登录gogs的用户Credentials--选择Branch----Save;


      Build Triggers:选择Trigger builds remotely--Authentication Token;选择Build when a change is pushed to Gogs
      Build Environment :选择Send files or execute commands over SSH before the build starts--设置SSH Server、Transfers

注意Transfers Set Source files、Remote directory、Exec command,其中Exec Command中项目执行sh脚本参考如下:

#!/bin/sh
#项目工作目录
workspace="/root/local_cmdb"
#uwsgi日志输出目录
log_path="/root/local_cmdb/local_cmdb/uwsgi.log"
#设置pip source
pip_source="https://pypi.tuna.tsinghua.edu.cn/simple"
#安装项目需要python、python-devel、mysql-devel
yum install -y python39 gcc python39-devel mysql-devel
if [ $? -ne 0 ]; then
    echo " python etc install failed"  `date +%Y%m%d%H%M%S` >> $log_path
fi
#配置Python、pip环境
ln -s /usr/bin/python3.9 /usr/bin/python
ln -s /usr/bin/pip3.9 /usr/bin/pip
#安装项目需要nginx
yum install nginx -y
if [ $? -ne 0 ]; then
    echo " nginx install failed" `date +%Y%m%d%H%M%S` >> $log_path
fi
#设置nginx自启动
systemctl enable nginx
systemctl start nginx
#安装uwsgi以及django依赖包
pip install uwsgi -i $pip_source 
pip install -r $workspace/requirements.txt -i $pip_source
if [ $? -ne 0 ]; then
    echo "uwsgi etc install failed" `date +%Y%m%d%H%M%S` >> $log_path
fi
#python $workspace/manage.py runserver

#nginx config
cp -pf /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak`date +%Y%m%d%H%M%S`
cat /dev/null /etc/nginx/nginx.conf
cp -pf $workspace/config/nginx.conf /etc/nginx/nginx.conf
nginx_procnum = `ps -C nginx --no-header|wc -l`
if [$nginx_procnum -eq 0]
then 
    echo "start nginx" `date +%Y%m%d%H%M%S` >> $log_path
    systemctl start nginx
else
    echo "restart nginx"  `date +%Y%m%d%H%M%S` >> $log_path
    systemctl restart nginx.service
fi
systemctl status nginx.service


###uwsgi config
cp -pf $workspace/config/server_uwsgi.service /etc/systemd/system/server_uwsgi.service
systemctl enable /etc/systemd/system/server_uwsgi.service
systemctl daemon-reload
uwsgi_procnum = `ps -C uwsgi --no-header|wc -l`
if [$uwsgi_procnum -eq 0]
then 
    echo "start uwsgi"  `date +%Y%m%d%H%M%S` >> $log_path
    systemctl start server_uwsgi
else
    echo "restart uwsgi"  `date +%Y%m%d%H%M%S` >> $log_path
    systemctl restart server_uwsgi
fi
systemctl status server_uwsgi.service

4.gogs配置web钩子

选择我的仓库--仓库设置--管理Web钩子--添加Web钩子--选择Gogs--填写推送地址、选择数据格式、密钥文本可免、设置希望触发Web钩子的事件、选择激活--点击添加Web钩子。参考20230129-Jenkins+Gogs自动打包项目

5.测试

开发环境提交代码

下载项目源码

# git clone http://172.26.37.127:3000/luorf/CMDB2.git
# cd CMDB2

修改项目安装脚本文件

# vi local_cmdb.sh

提交修改

# git add .
# git commit -m 'local_cmdb.sh.11'
# git push origin master

gogs自动推送确认:


jenkins脚本执行确认:


远程服务器nginx、uwsgi服务启动正常,django web访问及利用正常

# systemctl status nginx.service
# systemctl status server_uwsgi.service 
# netstat -ant |grep 80
tcp        0      0 127.0.0.1:8000          0.0.0.0:*               LISTEN     
# netstat -ant |grep 81
tcp        0      0 0.0.0.0:81              0.0.0.0:*               LISTEN 
# curl -I http://172.26.37.161:81/admin

参考URL

https://blog.csdn.net/questions1234/article/details/120291055
https://blog.csdn.net/gbfeng123/article/details/120025234

未完待续。。。。

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容