k8s部署應用精簡示例(nginx)模板

創建一個namespace

1 kind: Namespace
2 apiVersion: v1
3 metadata:
4   name: nginx-test

創建一個configmap

 1 apiVersion: v1
 2 kind: ConfigMap
 3 metadata:
 4   name: nginx-config
 5   namespace: nginx-test
 6 data:
 7  default: |
 8     server {
 9         listen       80;
10         server_name  localhost;
11         location / {
12             root   /var/www/html;
13             index  index.html index.htm index.php;
14         }
15         error_page   500 502 503 504  /50x.html;
16         location = /50x.html {
17             root   /var/www/html;
18         }
19         location ~ \.php$ {
20             root           /var/www/html;
21             fastcgi_pass   127.0.0.1:9000;
22             fastcgi_index  index.php;
23             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
24             include        fastcgi_params;
25         }
26         location ~ /\.ht {
27             deny  all;
28         }
29     }

構建deployment配置文件

 1 apiVersion: apps/v1
 2 kind: Deployment
 3 metadata: 
6
name: nginx-deploy 7 namespace: nginx-test
9
spec:
11 replicas: 1
13 selector: 14 matchLabels: 15 app: nginx-php
21 template: 22 metadata:
26 labels: 27 app: nginx-php 28 spec: 29 containers: 30 - image: 'nginx:1.21.1' 31 imagePullPolicy: IfNotPresent 32 name: nginx-server 33 ports: 34 - containerPort: 80 35 name: http 36 protocol: TCP 37 resources: 38 limits: 39 cpu: 500m 40 memory: 1Gi 41 requests: 42 cpu: 200m 43 memory: 500m
46 volumeMounts: 47 - mountPath: /var/www/html 48 name: php-html 49 - mountPath: /etc/nginx/conf.d 50 name: nginx-config
56 volumes: 57 - hostPath: 58 path: /data/k8s-data/nginx/html 59 type: Directory 60 name: php-html 61 - configMap:63 items: 64 - key: default 65 path: default.conf 66 name: nginx-config 67 name: nginx-config

綁定一個service

 1 apiVersion: v1
 2 kind: Service
 3 metadata:
 4   name: nginx-deploy
 5   namespace: nginx-test
 6 spec:
 7   type: NodePort
 8   ports:
 9     - name: http
10       port: 80
11       protocol: TCP
12       targetPort: 80
13       nodePort: 30080
14   selector:
15     app: nginx-php

 創建一個PVC

 1 apiVersion: v1
 2 kind: PersistentVolumeClaim
 3 metadata:
 4   name: nfs-pvc001
 5   namespace: nginx-test
 6 spec:
 7   accessModes:
 8     - ReadWriteOnce
 9   resources:
10     requests:
11       storage: 1Gi
12   storageClassName: nfs
13   selector:
14     matchLabels:
15       pv: nfs-pv001
 1 apiVersion: v1
 2 kind: PersistentVolume
 3 metadata:
 4   name: nfs-pv001
 5   namespace: nginx-test
 6   labels:
 7     pv: nfs-pv001
 8 spec:
 9   capacity:
10     storage: 1Gi
11   accessModes:
12     - ReadWriteOnce
13   persistentVolumeReclaimPolicy: Recycle
14   storageClassName: nfs
15   nfs:
16     path: /nfs/data/pv001
17     server: 192.168.122.100

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章