k8s部署应用精简示例(nginx)模板

创建一个namespace

kind: Namespace
apiVersion: v1
metadata:
    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:  
 4    name: nginx-deploy
 5    namespace: nginx-test  
 6 spec:
 7    replicas: 
 8    selector:
 9      matchLabels:
10        app: nginx-php
11    template:
12      metadata:
13        labels:
14          app: nginx-php
15      spec:
16        containers:
17          - image: 'nginx:1.21.1'
18            imagePullPolicy: IfNotPresent
19            name: nginx-server
20            ports:
21              - containerPort: 80
22                name: http
23                protocol: TCP
24            resources:
25              limits:
26                cpu: 500m
27                memory: 1Gi
28              requests:
29                cpu: 200m
30                memory: 500m
31            volumeMounts:
32              - mountPath: /var/www/html
33                name: php-html
34              - mountPath: /etc/nginx/conf.d
35                name: nginx-config
36        volumes:
37          - hostPath:
38              path: /data/k8s-data/nginx/html
39              type: Directory
40            name: php-html
41          - configMap:
42              items:
43                - key: default
44                  path: default.conf
45              name: nginx-config
46            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

 

posted @ 2022-11-27 15:05  风吹过的绿洲  阅读(197)  评论(0编辑  收藏  举报