k8s用congfigMap文件搭建nginx

创建configmap文件替换nginx的nginx.conf文件

废话不多说,上代码
首先是deployment文件:
PS:经过几天的学习呢,我现在已经可以默写yaml文件了

deploy文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  namespace: default
  labels:
    app: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3
  template:
    metadata:
      name: nginx-tem
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        imagePullPolicy: IfNotPresent
        name: nginx-pod
        volumeMounts:
          - name: nginx-configmap
            mountPath: /etc/nginx/nginx.conf
            subPath: nginx.conf
        ports:
        - containerPort: 80
          name: nginx-port
      volumes:
        - name: nginx-configmap
          configMap:
            name: nginx-configmap
            items:
            - key: nginx.conf
              path: nginx.conf

configmap文件

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-configmap
data:
  nginx.conf: |
    user  nginx;
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        server {
            listen       80;
            server_name  localhost;
            location / {
                root   html;
                index  index.html index.htm;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }

测试

我们更改一下configmap文件,进入pod看一下是否同步修改了

kubectl edit cm nginx-configmap
  configmap/nginx-configmap edited

通过命令呢,我们把进程数改为4
注意:POD需要重启,否则需要等待同步一会,为了方便我们这里直接重启啦
在这里插入图片描述
我们重启一下pod(自己家里的虚拟机,没多少应用hh)
在这里插入图片描述
通过命令进入容器

kubectl exec -it nginx-768cc7f8f6-cknnj  -- /bin/bash

在这里插入图片描述
可以看到我们的进程数已经变成了我们修改的4,代表我们试验成功

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐