1. 为什么需要图床

1. 自己的搭建的图床地址不会更改, 长期可用, 服务保证, 第3方服务, 随时因为各种异常封禁的.

2. Hugo写markdown过程中需要使用图片. 使用Hugo相对地址比较麻烦.

3. 七牛,又拍云之类需要备案和认证, 时间和精力成本比较高.

4. 而且自己搭建后, 凡是可使用markdown的地方都可使用, 富文本也可用, 公众号之类文章也可以使用, 综上图床必须的.

2. 图床原理, 需要搭建什么的图床.

1. 原理的话

  1. 就是服务器上的一个一个图片文件, 通过nginx apeache类服务器访问这些图片.

2. 搭建什么的图床.

  1. Chevereto + nginx可网页上传, 稍微有点难度, 有需求再上.
  2. Nginx 这个目前觉得最简单, 就用这个了.

3. 在Kubernetes下nginx pod创建图床.

1. vim nginx-deployment.yaml, 使用到pvc, 命名空间命名的是nginx-space

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
  namespace: nginx-space
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-nginx
  template:
    metadata:
      namespace: nginx-space
      labels:
        app: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx:1.16.0
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: www-actiger-com
          ####### img就是存放图片的
        - mountPath: /usr/share/nginx/img
          name: img-actiger-com
        - mountPath: /etc/nginx/
          name: nginx-config
      volumes:
      - name: www-actiger-com
        persistentVolumeClaim:
          claimName: pvc-www
      - name: img-actiger-com
        persistentVolumeClaim:
          claimName: pvc-img
      - name: nginx-config
        persistentVolumeClaim:
          claimName: pvc-nginxconfig
      nodeSelector:
        kubernetes.io/hostname: cc6.com
---
apiVersion: v1
kind: Service
metadata:
  name: my-nginx
  namespace: nginx-space
  labels:
    run: my-nginx
spec:
  ports:
  - port: 80
    protocol: TCP
  selector:
    app: my-nginx

2. vim pv-pvc-img.yaml

kind: PersistentVolume
metadata:
  name: pv-img
  namespace: nginx-space
spec:
  capacity:
    storage: 500Mi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/root/img"

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-img
  namespace: nginx-space
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: ""
  resources:
    requests:
      storage: 500Mi

3. vim etc/nginx/conf.d/default.conf, nginx的配置文件, autoindex on自动生成目录了.

## For img
server {
    listen       80;
    server_name  img.actiger.com;

    location / {
        root   /usr/share/nginx/img;
        index  index.html index.htm;
        autoindex on;
        autoindex_localtime on;
        autoindex_exact_size off;
    }
    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/img;
    }
}

4. vim my-nginx.yml, 增加img.actiger.com地址ingress nginx转发的service上.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  namespace: nginx-space
  name: my-nginx
spec:
  tls:
  - hosts:
    - www.actiger.com
    secretName: ingress-secret
  - hosts:
    - actiger.com
    secretName: ingress-top-secret
  rules:
  - host: img.actiger.com
    http:
      paths:
      - backend:
          serviceName: my-nginx
          servicePort: 80
  - host: www.actiger.com
    http:
      paths:
      - backend:
          serviceName: my-nginx
          servicePort: 80
  - host: actiger.com
    http:
      paths:
      - backend:
          serviceName: my-nginx
          servicePort: 80

使用kubectl apply -f 上面的yaml文件, 即可.

4. 上传文件在服务器的/root/img目录下, 即可在见面上访问到. 右键链接复制地址, 贴到markdown中就可以随意用了.

img.actiger.com/blog/

如图

CopyLinkAddress