制作 Gitbook 文档镜像,运行在 K8S 上


Gitbook 构建静态 HTML,可以运行在 Nginx 上。

制作 Nginx + 文档 的镜像,运行在 K8S 上,完美~

步骤

1. 构建 Gitbook 静态文档 {#gitbookBuild}

在 gitbook 文档库根目录构建 HTML 文档。

~/gitbook/k8s$ gitbook build .
1

在该目录下会生成 _book 目录。

2. 制作 Gitbook 镜像 {#dockerfile}

编写 dockerfile,将 _book 目录内容拷贝至镜像的 /usr/share/nginx/html 目录。

alpine 标签的基础镜像是 Mini Linux 镜像,包含 Nginx 只有 8MB。

FROM nginx:alpine
WORKDIR /usr/share/nginx/html
ADD _book/ /usr/share/nginx/html
EXPOSE 80
1
2
3
4
  • 构建镜像
docker build -t book:0.01 .
1
  • 运行容器(测试)
docker run -p 80:80 book:0.01
1

然后通过 docker tag ,将其推送至镜像仓库。

docker tag book:0.01 <YOUR Docker Registry>:0.01
docker push $_
1
2

3. 创建 K8S 资源 {#Deploy}

  • Deployment
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  labels:
    k8s-app: gitbook
    qcloud-app: gitbook
  name: gitbook
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s-app: gitbook
      qcloud-app: gitbook
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        k8s-app: gitbook
        qcloud-app: gitbook
    spec:
      containers:
      - image: <YOUR GITBOOK IMAGES>  # gitbook 镜像地址
        imagePullPolicy: IfNotPresent
        name: gitbook
        resources:
          limits:
            cpu: 500m
            memory: 1Gi
          requests:
            cpu: 250m
            memory: 256Mi
        securityContext:
          privileged: false
          procMount: Default
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
      ```

- Service


```yaml
apiVersion: v1
kind: Service
metadata:
  name: gitbook
spec:
  externalTrafficPolicy: Cluster
  ports:
  - name: tcp-80-80
    nodePort: 30226
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    k8s-app: gitbook
    qcloud-app: gitbook
  sessionAffinity: None
  type: LoadBalancer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  • Ingress
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: query-ip
spec:
  rules:
  - host: dhcp.cn
    http:
      paths:
      - backend:
          serviceName: gitbook
          servicePort: 80
        path: /doc
status:
  loadBalancer:
    ingress:
    - ip: <YOUR LoadBalancer IP>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

该文档地址访问成功~