首页 文章 精选 留言 我的

精选列表

搜索[Java实现],共10000篇文章
优秀的个人博客,低调大师

利用Git Webhooks实现jekyll博客自动化部署

Automated Deployment of Jekyll Blog with Git Webhooks and Flask 在我之前的一篇博客中说到购置了一台阿里云的轻量级应用服务器并讲述了如何配置Nginx反向代理转发Disqus请求。在完成所有配置后,我发现这条路子还是走不通,原因在于原本托管在github上的jekyll博客采用git pages自动部署的,使用的是https协议,而Nginx这端如果想配置SSL必须在阿里云上绑定域名并购买相应的证书服务。心想,既然域名,证书,VPS都齐全了,不如直接将博客部署在VPS上。部署的过程比较简单,这里不再赘述,给出一篇参考博客。博客迁移完成后,我意识到更新代码是一个巨繁琐的过程:在本地更新了代码或博客,push到远程分支,ssh登录到服务器,从远程分支pull最新代码,接着jekyll build,最后将生成的_site文件夹拷贝到指定目录。这一套组合拳下来,也是有点繁琐的。这篇博客记录了搭建自动化jekyll博客部署的过程,主要结合的技术有Git Webhooks,轻量级Web框架Flask。这里假设你已经具备基础的Nginx部署知识,可参考反向代理的搭建。 什么是Git Webhooks? Git Webhooks允许服务器接收有关对git仓库执行操作的通知,并允许用户指定服务器在接收到通知时应该执行的操作。一般来说,如果我想通过更改git触发远程操作,webhooks是一个不错的选择。在我们的例子中,我们只对push事件感兴趣(任何时候推送代码到远程仓库)。 创建一个Github Webhook 如果你的网站托管在Github上,进入Repo -> Settings -> Webhooks并点击Add Webhook。 在上图的页面中,你需要配置在某些操作发生时由Github调用的URL。这里我们还需要在服务器上部署一个简单的应用来处理Github的Post请求,下一小节将会讲到。Content type可选json,但这里我对payload并不是很感兴趣,故采用默认的form-urlencoded。另外还可以提供一个密码。其SHA-1哈希值将添加到X-Hub-Signature中。更多细节可以参见Git Webhooks的官方文档。完成配置后点击Add webhook即可。 部署Flask应用 上一小节中,我们为博客repo创建了一个webhook,每次推送新代码到repo时,Github都会给我们的服务器发送一个POST请求,所以我们必须部署一个应用来处理该请求并重新构建jekyll。这里我不想使用一个比较重的web框架,因为我需要的仅仅是来自Github的一个通知。目前也有许多的轻量级Web框架,笔者选了Flask。在/var/www/目录下新建应用文件夹: mkdir autorebuild -p cd autorebuild 在安装flask之前安装虚拟环境virtualenv,进入目录创建新的python环境并激活: pip install virtualenv virtualenv venv source /venv/bin/activate 使用pip安装Flask: pip install flask 现在我们可以创建一个简单的处理webhook配置中指定请求的flask应用: from flask import Flask import subprocess import os app = Flask(__name__) @app.route('/rebuid', methods=['POST']) def autobuildjekyll(): script_path = "/var/www/autorebuild/jekyll_rebuild.sh" subprocess.call([os.path.expanduser(script_path)]) return "Success" if __name__ == "__main__": app.run() 上面的python代码(假设为refresh.py)创建了一个简单的flask应用,其中的一个endpoint(/rebuild)处理Git Webhook发来的POST请求。具体来说,autobuildjekyll函数调用一个简单的shell脚本。jekyll_rebuild.sh脚本从Git仓库中拉取最新的修改,之后重建我们的站点: #!/bin/bash echo "Pulling latest from Git" cd ~/hybridtheory/ && git pull echo "Building Jekyll Site"; jekyll build --source ~/hybridtheory/ --destination /var/www/hytheory.com/ --incremental; echo "Jekyll Site Built"; 这里需要注意的是,千万不要省略shell脚本的头部#!/bin/bash,否则subprocess.call()会报如下错误:OSError: [Errno 8] Exec format error。直接运行python refresh.py即可启动应用,默认绑定端口5000。由于我们采用Nginx作为博客的代理服务器,简单的启动一个flask应用是不够的,我们需要使用Gunicorn来启动flask应用。Gunicorn是一个Python wsgi http server,来源于Ruby的Unicorn项目,能够与各种wsgi web框架写作,比如Flask。首先进入虚拟环境安装Gunicorn: pip install gunicorn 用gunicorn启动我们的flask应用: gunicorn -b 127.0.0.1:8001 refresh:app 这里我们绑定localhost的8001端口,仅能内部访问。refresh为python文件的名字,app为应用名字。最后我们修改Nginx配置,由于Github发送的是https请求,故我们绑定端口443。具体修改过程不再赘述,这里贴出Nginx的具体配置: server { listen 80 default_server; listen [::]:80 default_server; server_name www.hytheory.com; return 301 https://$server_name$request_uri; } server { listen 443 default_server; listen [::]:443 default_server; ssl on; ssl_certificate /etc/nginx/sslcert/1613132_www.hytheory.com.pem; ssl_certificate_key /etc/nginx/sslcert/1613132_www.hytheory.com.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; root /var/www/hytheory.com; # Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html; server_name hytheory.com www.hytheory.com; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; } location /refresh { proxy_pass http://127.0.0.1:8001; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } 至此,所有的配置完成,测试成功。原文链接:https://www.hytheory.com/tutorial/blog/deploy-jekyll-site-automatically-with-git-webhook/ 参考博客 用 Jekyll/Nginx/Let'sEncrypt 搭建一个博客站点 Automate Jekyll with GitHub Webhooks ubuntu(Flask + Gunicorn + Nginx 部署)

优秀的个人博客,低调大师

k8s RBAC 多租户权限控制实现

访问到权限分两部分 1.Authenticating认证授权配置由kube-apiserver管理 这里使用的是 Static Password File方式。apiserver启动yaml里配置basic-auth-file即可,容器 启动apiserver的话要注意这个文件需要是在容器内能访问到的。(可以通过挂载文件,或者在已经挂载的路径里增加配置文件)basic-auth-file文件格式见官方文档https://kubernetes.io/docs/admin/authentication/#static-password-file注意password在第一个,之前没注意被卡了很久。修改了文件以后需要重启apiserver才能生效。这里kubectldelete删除pod有问题,使用docker命令查看apiserver容器其实没有重启。解决方案是使用dockerkill杀掉容器。自动重启。 [root@tensorflow1 ~]# curl -u admin:admin "https://localhost:6443/api/v1/pods" -k { "kind": "Status", "apiVersion": "v1", "metadata": { }, "status": "Failure", "message": "pods is forbidden: User \"system:anonymous\" cannot list pods at the cluster scope", "reason": "Forbidden", "details": { "kind": "pods" }, "code": 403 } 这个就是没有通过认证的报错 2.Authorization授权 k8sRBAC授权规则https://kubernetes.io/docs/admin/authorization/rbac/ 简单来说就是 权限--角色--用户绑定需要配置两个文件一个是role/clusterRole,定义角色以及其权限一个是roleBinding/clusterRoleBinding,定义用户和角色的关系 { "kind": "Status", "apiVersion": "v1", "metadata": { }, "status": "Failure", "message": "pods is forbidden: User \"admin\" cannot list pods at the cluster scope", "reason": "Forbidden", "details": { "kind": "pods" }, "code": 403 } 这个就是没有权限访问,需要配置RBAC 文件如下:方案1:role + roleBinding kind: Role apiVersion: rbac.authorization.k8s.io/v1 metadata: namespace: user1 name: pod-reader rules: - apiGroups: [""] # "" indicates the core API group resources: ["pods"] verbs: ["*"] - apiGroups: [""] # "" indicates the core API group resources: ["namespaces"] verbs: ["*"] kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: user1-rolebinding namespace: user1 subjects: - kind: User name: user1 # Name is case sensitive apiGroup: rbac.authorization.k8s.io roleRef: kind: ClusterRole name: myauth-user apiGroup: rbac.authorization.k8s.io 方案2:clusterRole + roleBinding (推荐) clusterRole定义全局权限,roleBinding将权限限制到namespace。clusterRole仅需要定义一次。 kind: ClusterRole apiVersion: rbac.authorization.k8s.io/v1 metadata: name: myauth-user rules: - apiGroups: [""] # "" indicates the core API group resources: ["pods"] verbs: ["*"] - apiGroups: [""] # "" indicates the core API group resources: ["namespaces"] verbs: ["*"] - apiGroups: [""] # "" indicates the core API group resources: ["resourcequotas"] verbs: ["*"] kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: user2-rolebinding namespace: user2 subjects: - kind: User name: user2 # Name is case sensitive apiGroup: rbac.authorization.k8s.io roleRef: kind: ClusterRole name: myauth-user apiGroup: rbac.authorization.k8s.io kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: user1-rolebinding namespace: user1 subjects: - kind: User name: user1 # Name is case sensitive apiGroup: rbac.authorization.k8s.io roleRef: kind: ClusterRole name: myauth-user apiGroup: rbac.authorization.k8s.io 3.配置 新建静态密码文件 # vi /etc/kubernetes/pki/basic_auth_file admin,admin,1004 jane2,jane,1005 user1,user1,1006 user2,user2,1007 修改apiserver配置修改/etc/kubernetes/manifests/kube-apiserver.yaml在一堆参数配置下面增加 --basic-auth-file=/etc/kubernetes/pki/basic_auth_file eg: - --etcd-servers=https://127.0.0.1:2379 - --etcd-cafile=/etc/kubernetes/pki/etcd/ca.crt - --etcd-certfile=/etc/kubernetes/pki/apiserver-etcd-client.crt - --etcd-keyfile=/etc/kubernetes/pki/apiserver-etcd-client.key - --basic-auth-file=/etc/kubernetes/pki/basic_auth_file 这里将静态文件放在/etc/kubernetes/pki/目录下的原因是,apiserver通过容器启动,这个路径已经挂载容器中了,可以被访问到。放在其他路径需要额外配置挂载路径。apiserver只会启动在master节点上,故仅需要在master节点上配置即可。 启动好以后在master机器上执行kubectlgetall,多半是执行不成功的,因为修改apiserver配置文件后,apiserver自动重启,然后启动失败,所以就无法访问集群了。使用dockerps查看发现k8s_kube-scheduler和k8s_kube-controller-manager重启了,但是apiserver没起来。执行systemctlrestartkubelet,再dockerps就能看到apiserver启动成功了。如果还没启动起来,那再看看上面哪里配置有问题。 应用 使用--username={username}--password={password}参数访问kubectl 参考https://kubernetes-v1-4.github.io/docs/user-guide/kubectl/kubectl_create/ kubectl get all -n user1 --username=user1 --password=user1 kubectl create -f tf1-rc.yaml --username=user1 --password=user1 使用 curl -u {username}:{password} `js"https://{masterIP}:6443/api/v1/" -k访问httprestfulapicurl -u jane:jane2 "https://localhost:6443/api/v1/namespaces/user1/pods" -kcurl -u jane:jane2 "https://localhost:6443/api/v1/namespaces/user1" -k curl -u user1:user1 "https://localhost:6443/api/v1/namespaces/user1" -kcurl -u user1:user1 "https://localhost:6443/api/v1/namespaces/user1/pods" -kcurl -u user1:user1 "https://localhost:6443/api/v1/namespaces/user1/resourcequotas/" -k curl -u user2:user2 "https://localhost:6443/api/v1/namespaces/user2" -kcurl -u user2:user2 "https://localhost:6443/api/v1/namespaces/user2/pods" -kcurl -u user2:user2 "https://localhost:6443/api/v1/namespaces/user2/resourcequotas/" -k

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册
修改密码
忘记密码