Kubernetes自动化部署Laravel
Kubernetes自动化部署Laravel
上一篇跟大家分享了制作Laravel镜像,今天跟大家分享Kubernetes自动化部署Laravel,同样原理还是使用Gitea+Drone进行自动化构建推送镜像到镜像仓库,然后拉取最新镜像进行平滑重启服务,相比较Go自动化部署,我们在打包Laravel镜像的时候.env文件是不打包在镜像里,而是将配置信息放在ConfigMap来完成,同时创建了一个存储卷来永久保存public\uploads的文件。
一、准备Git仓库
我直接在Gitea上创建一个名字为projectLaravel仓库,然后Drone激活连接到这个仓库,然后本地准备了一份Laravel代码,放到我们刚刚创建的这个仓库,然后在项目目录下,我们创建一个文件名为.gitignore的文件,我们将不上传到仓库的信息放到这个文件,详细如下
/node_modules /public/hot /public/storage /storage/*.key /vendor .env .env.backup .phpunit.result.cache docker-compose.override.yml Homestead.json Homestead.yaml npm-debug.log yarn-error.log /.idea /.vscode
二、准备部署服务配置
我在服务器路径/home/k8s/project下创建了projectLaravel目录用于存放部署配置信息,我们先创建configmap.yaml,这个文件用于存放Laravel项目.env所有配置,因为我们在打包镜像的时候.env文件是不打包到镜像,所以需要用到这个配置来存放.env文件所有的配置信息,详细如下
kind: ConfigMap apiVersion: v1 metadata: name: project-laravel-config data: .env: | APP_NAME=测试项目 APP_ENV=production APP_KEY=base64:wJHVxWqHwlhJHDi8RLRAXesLuKDXnO0GZsVLhM3hUyA=
然后我们先执行以下命令,初始化生效
cd /home/k8s/project/projectLaravel kubectl apply -f configmap.yaml
然后我们在/home/k8s/project/projectLaravel目录下创建laravel-deployment.yaml文件,用于部署服务,详细如下
#设置持久卷 apiVersion: v1 kind: PersistentVolumeClaim metadata: name: drone-laravel-pv-claim spec: accessModes: - ReadWriteOnce resources: requests: storage: 5Gi --- #部署Laravel服务 apiVersion: apps/v1 kind: Deployment metadata: name: drone-laravel-deployment spec: selector: matchLabels: app: drone-laravel replicas: 2 strategy: type: RollingUpdate rollingUpdate: maxSurge: 1 maxUnavailable: 1 template: metadata: labels: app: drone-laravel spec: containers: - name: drone-laravel image: 【镜像地址,需要替换】 imagePullPolicy: Always ports: - containerPort: 80 volumeMounts: - name: env #ENV配置 mountPath: /var/www/html/.env subPath: .env - name: uploads #上传数据 mountPath: /var/www/html/public/uploads volumes: - name: env configMap: name: project-laravel-config defaultMode: 420 - name: uploads persistentVolumeClaim: claimName: drone-laravel-pv-claim --- #暴露对外端口 apiVersion: v1 kind: Service metadata: name: drone-laravel-service labels: app: drone-laravel spec: type: NodePort selector: app: drone-laravel ports: - port: 80 targetPort: 80 nodePort: 30088
大家根据实际情况,将镜像地址替换成自己的laravel镜像地址。
三、编写打包镜像脚本
我们在项目目录下创建一个default.conf文件,用于Nginx代理配置,详细如下
server { listen 80; server_name localhost; #charset koi8-r; location / { root /var/www/html/public; index index.php index.html index.htm; # 如果没有以下4行,laravel将只能访问首页,其他页面都是404 try_files $uri $uri/ /index.php?$query_string; if (!-e $request_filename){ rewrite ^/(.*) /index.php last; } # 如果没有以上4行,laravel将只能访问首页,其他页面都是404 } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # # location ~ \.php$ { # proxy_pass http://127.0.0.1; # } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root /var/www/html/public; index index.php index.html; # 坑在这里,需将原有的127.0.0.1:9000替换成phpfpm:9000 fastcgi_pass 127.0.0.1:9000; # 坑在这里,需将原有的127.0.0.1:9000替换成phpfpm:9000 fastcgi_index index.php; # 下面这行也改了 中间的$document_root fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
然后我们同样在项目目录中创建Dockerfile文件,用于构建我们的Laravel镜像
FROM evan886/alpine_nginx_php7.4:v3.14 #复制项目代码至运行目录 ADD default.conf /etc/nginx/conf.d/ ADD .env.example /var/www/html/.env ADD . /var/www/html/ #设置权限 RUN chown www-data:www-data -R /var/www/html/storage RUN chown www-data:www-data -R /var/www/html/bootstrap #初始化密钥 RUN cd /var/www/html && php artisan key:generate
四、构建自动化部署脚本
根据Drone的规则,我们在项目目录下创建一个文件名为.drone.yml,用于代码提交后触发自动化构建部署,详细如下
kind: pipeline type: kubernetes name: project-laravel #构建步骤 steps: #安装Laravel扩展 - name: backend pull: if-not-exists image: laradock/workspace:latest-7.3 commands: - cp .env.example .env - composer install --prefer-dist - php artisan key:generate # - php artisan migrate volumes: - name: cache path: /cache #推送镜像至镜像仓库 - name: publish image: plugins/docker mirrors: #镜像仓库加速地址,不需要加速,可以直接删除 from_secret: registry_mirrors settings: purge: false registry: #镜像仓库域名 from_secret: registry repo: #镜像仓库详细地址 from_secret: repo use_cache: true tags: - latest username: #镜像仓库账户 from_secret: registry_user_name password: #镜像仓库密码 from_secret: registry_password volumes: - name: cache path: /cache #部署服务 - name: ssh commands image: appleboy/drone-ssh settings: host: from_secret: ssh_ip port: 22 username: from_secret: ssh_user_name password: from_secret: ssh_password script: - cd /home/k8s/project/projectLaravel - kubectl apply -f laravel-deployment.yaml - kubectl rollout restart deployment drone-laravel-deployment #通知到telegram - name: notification image: appleboy/drone-telegram settings: token: from_secret: telegram_token to: from_secret: telegram_user_id message: > {{#success build.status}} {{repo.name}} 第{{build.number}} 次自动部署成功 耗时 {{since build.started}} 提交分支 {{commit.branch}} 详情 {{ build.link }} {{else}} {{repo.name}} 第{{build.number}} 次自动部署失败 耗时{{since build.started}} 提交分支 {{commit.branch}} 详情 {{ build.link }} {{/success}} volumes: - name: cache host: path: /home/droneCache
五、配置Drone密钥信息
由于我们为了保证重要信息不泄露出去,我在.drone.yml文件所用到的重要信息都放到Drone仓库管理面板上,详细如下
六、实验自动化部署
完成以上操作,我们提交代码到远程仓库,触发构建,出现如下效果
我的电报收到通知信息
浏览访问部署服务IP:30088,出现如下效果
七、检验.env效果
我们修改Laravel项目routes\api.php文件,新增一条路由,代码如下
<?php use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; /* |-------------------------------------------------------------------------- | API Routes |-------------------------------------------------------------------------- | | Here is where you can register API routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | is assigned the "api" middleware group. Enjoy building your API! | */ Route::middleware('auth:sanctum')->get('/user', function (Request $request) { return $request->user(); }); Route::get('/home', function (Request $request) { echo "自动化部署Laravel项目".$_ENV['APP_NAME']; });
然后我们再次提交代码后,等待自动化部署构建完成后,浏览器访问部署服务IP:30088/api/home,出现如下效果
这样我们就成功的使用Gitea+Drone自动化部署Laravel,以下我贴出我的Laravel项目结构,以及新增的几个配置文件
0条评论