k8s部署nginx挂载configmap

一、配置configmap文件

1  配置nginx.conf

user  nginx;
    worker_processes  1;

    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;


    events {
        worker_connections  1024;
    }


    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;

        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';

        access_log  /var/log/nginx/access.log  main;

        sendfile        on;
        #tcp_nopush     on;

        keepalive_timeout  65;

        #gzip  on;

        include /etc/nginx/conf.d/*.conf;
    }

2  应用configmap

kubectl create configmap nginx-configmap --from-file=./nginx.conf -n demo

3  查看configmap

# kubectl get configmap nginx-configmap -n demo -o yaml
apiVersion: v1
data:
  nginx.conf: |+
    user  nginx;
        worker_processes  1;

        error_log  /var/log/nginx/error.log warn;
        pid        /var/run/nginx.pid;


        events {
            worker_connections  1024;
        }


        http {
            include       /etc/nginx/mime.types;
            default_type  application/octet-stream;

            log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                              '$status $body_bytes_sent "$http_referer" '
                              '"$http_user_agent" "$http_x_forwarded_for"';

            access_log  /var/log/nginx/access.log  main;

            sendfile        on;
            #tcp_nopush     on;

            keepalive_timeout  65;

            #gzip  on;

            include /etc/nginx/conf.d/*.conf;
        }

kind: ConfigMap
metadata:
  creationTimestamp: "2022-01-09T13:35:53Z"
  managedFields:
  - apiVersion: v1
    fieldsType: FieldsV1
    fieldsV1:
      f:data:
        .: {}
        f:nginx.conf: {}
    manager: kubectl
    operation: Update
    time: "2022-01-09T13:35:53Z"
  name: nginx-configmap
  namespace: demo
  resourceVersion: "4097244"
  selfLink: /api/v1/namespaces/demo/configmaps/nginx-configmap
  uid: 9d7b0963-1e46-4198-aa95-7f6fd3222c75

二、创建nginx的deployment引用configmap

1  编辑deployment并引用

编辑nginx-apply-configmap.yaml文件

kind: Deployment
apiVersion: apps/v1
metadata:
  name: demo-nginx
  namespace: demo
  labels:
    app: demo-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: demo-nginx
  template:
    metadata:
      labels:
        app: demo-nginx
    spec:
      volumes:
        - name: host-time
          hostPath:
            path: /etc/localtime
            type: ''
        - name: volume-nginx-configmap
          configMap:
            name: nginx-configmap
            items:
            - key: nginx.conf
              path: nginx.conf
      containers:
        - name: demo-nginx
          image: nginx
          ports:
            - name: tcp-80
              containerPort: 80
              protocol: TCP
          resources:
            limits:
              cpu: 500m
              memory: 256Mi
            requests:
              cpu: 500m
              memory: 128Mi
          volumeMounts:
            - name: host-time
              readOnly: true
              mountPath: /etc/localtime
            - name: volume-nginx-configmap
              mountPath: /etc/nginx/nginx.conf
              subPath: nginx.conf
          imagePullPolicy: IfNotPresent
      tolerations:
      - key: node-type
        operator: Equal
        value: dev
        effect: NoSchedule

应用nginx-apply-configmap.yaml:

kubectl apply -f nginx-apply-configmap.yaml

查看pod运行情况:

# kubectl get pod -n demo | grep demo-nginx
demo-nginx-7bb8f9fb46-k4blv      1/1     Running     0          104s

进入pod查看挂载情况:

# kubectl  exec -it demo-nginx-7bb8f9fb46-k4blv -n demo -- /bin/bash
root@demo-nginx-7bb8f9fb46-k4blv:/# 
root@demo-nginx-7bb8f9fb46-k4blv:/# 
root@demo-nginx-7bb8f9fb46-k4blv:/# cat /etc/nginx/nginx.conf 
user  nginx;
    worker_processes  1;

    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;


    events {
        worker_connections  1024;
    }


    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;

        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';

        access_log  /var/log/nginx/access.log  main;

        sendfile        on;
        #tcp_nopush     on;

        keepalive_timeout  65;

        #gzip  on;

        include /etc/nginx/conf.d/*.conf;
    }

root@demo-nginx-7bb8f9fb46-k4blv:/# 

k8s中的ConfigMap是一种用于存储配置数据的资源对象,可以在容器中使用这些配置数据。PV(Persistent Volume)是一种持久化存储资源,用于将存储卷与Pod进行绑定。下面是关于k8s中的nginx ConfigMap和PV的介绍和演示: 1. 创建nginx ConfigMap: ```shell kubectl create configmap nginx-config --from-file=/etc/config/ ``` 这将创建一个名为nginx-config的ConfigMap,并将/etc/config/目录下的所有文件作为配置数据。 2. 查看ConfigMap信息: ```shell kubectl get configmap nginx-config ``` 这将显示名为nginx-config的ConfigMap的详细信息。 3. 在Pod中使用ConfigMap: 可以在Pod的配置文件中使用ConfigMap来注入配置数据。例如,在Pod的配置文件中添加以下内容: ```yaml apiVersion: v1 kind: Pod metadata: name: nginx-pod spec: containers: - name: nginx-container image: nginx volumeMounts: - name: config-volume mountPath: /etc/config volumes: - name: config-volume configMap: name: nginx-config ``` 这将在Pod中创建一个名为config-volume的卷,并将nginx-config ConfigMap的配置数据挂载到Pod的/etc/config目录下。 4. 创建PV: ```shell kubectl apply -f pv.yaml ``` 其中pv.yaml是一个包含PV定义的YAML文件。 5. 查看PV信息: ```shell kubectl get pv ``` 这将显示所有PV的详细信息。 6. 将PV与Pod绑定: 可以在Pod的配置文件中使用PV来绑定持久化存储。例如,在Pod的配置文件中添加以下内容: ```yaml apiVersion: v1 kind: Pod metadata: name: nginx-pod spec: containers: - name: nginx-container image: nginx volumeMounts: - name: data-volume mountPath: /data volumes: - name: data-volume persistentVolumeClaim: claimName: my-pvc ``` 这将在Pod中创建一个名为data-volume的卷,并将名为my-pvc的PersistentVolumeClaim与之绑定。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值