【K8S实战系列-nignx-2】k8s中configmap挂载配置nginx.conf

  • Post author:
  • Post category:其他


【K8S实战系统-nignx-2】k8s中configmap挂载配置nginx.conf

1. ConfigMap是什么


ConfigMap作用是存储不加密的数据到etcd中,让Pod以变量或数据卷Volume挂载到容器中

2. 创建ConfigMap

2.1 准备nginx.conf 配置文件

user  nginx;
worker_processes  auto;

error_log  /nas/nas-nginx/logs/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  4096;
}

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  /nas/nginx/logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    include /nas/nginx/conf.d/*.conf;
}

2.2 创建名nginx-cm的configmap

kubectl create configmap nginx-cm --from-file=nginx.conf

2.3 然后查看详细信息

kubectl describe cm nginx-cm

3 Volume数据卷形式挂载

vim nginx-deployment.yaml


apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: "registry-vpc.cn-hangzhou.aliyuncs.com/cncx-product/nginx:v2021"
          imagePullPolicy: IfNotPresent
          ports:
          - containerPort: 80
          volumeMounts:
          - name: nginx-config
            mountPath: /etc/nginx/nginx.conf
            subPath: nginx.conf
          - name: pvc-nas-nginx
            mountPath: "/nas/nas-nginx"
      volumes:
        - name: nginx-config
          configMap:
            name: nginx-cm
            items:
            - key: nginx.conf
              path: nginx.conf
        - name: pvc-nas-nginx
          persistentVolumeClaim:
            claimName: nas-nginx-claim
      imagePullSecrets:
        - name: images-secret

这里我不仅使用configmap来获取nginx.conf的配置文件;

也挂载了nas的数据盘,来存放其他配置文件,以及静态资源的存储;

4. 创建nginx pod

kubectl apply -f nginx-deployment.yaml


5. 查看nginx deploy

[root@server40 nginx]# kubectl get deploy -o wide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                                                           SELECTOR
nginx   2/2     2            2           56s   nginx        registry-vpc.cn-hangzhou.aliyuncs.com/cncx-product/nginx:v2021   app=nginx

6.进入容器内部,查看目录具体信息

kubectl exec -it 容器名 /bin/bash 

7 通过nginx 服务测试是否成功

创建服务:nginx-deployment.yaml

vim nginx-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx
 	labels:
    name: nginx-svc
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
    nodePort: 30080
  - port: 443
    targetPort: 443
    protocol: TCP
    nodePort: 30443
  selector:
    app: nginx

8. 通过浏览器查看效果。



版权声明:本文为shgh_2004原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。