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:/# 

你可能感兴趣的:(kubernetes,nginx,linux,运维,k8s,服务器)