• K8s ConfigMap 存储 Nginx 配置文件【转】


    ConfigMap 实现 nginx 容器的配置文件管理

    1、在k8s集群拉起一个nginx的pod,通过默认80去访问。

    • 编写nginx的yaml文件。
    [root@k8s-master ~]# cat my-nginx.yaml 
    apiVersion: apps/v1
    kind: Deployment 
    metadata:
      name: my-nginx 
    spec:
      replicas: 1 
      selector:
        matchLabels:
          app: nginx 
      template:
        metadata:
          labels:
            app: nginx 
        spec:
          containers:
          - name: nginx 
            image: nginx:1.18.0
            imagePullPolicy: IfNotPresent  
            ports:
            - containerPort: 80
    • 创建并查看nginx的pod。
    [root@k8s-master ~]# kubectl apply -f my-nginx.yaml 
    deployment.apps/my-nginx created

    [root@k8s-master ~]# kubectl get pod my-nginx-67dfd6c8f9-fn8jf -o wide
    NAME                        READY   STATUS    RESTARTS   AGE   IP             NODE                     NOMINATED NODE   READINESS GATES
    my-nginx-67dfd6c8f9-fn8jf   1/1     Running   0          84s   10.244.1.207   k8s-node-1.example.com   <none>           <none>
    • 测试访问nginx的80端口。
    [root@k8s-master ~]# curl http://10.244.1.207
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>

    2、为nginx的配置文件创建ConfigMap。

    • 编写nginx的配置文件的yaml文件。把默认监听端口修改为8080。
    [root@k8s-master ~]# cat nginx-conf.yaml 
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: nginx-conf
    data:
      default.conf: |-
        server {
          listen     8080;
          listen  [::]:80;
          server_name  localhost;

          location / {
              root   /usr/share/nginx/html;
              index  index.html index.htm;
          }

          error_page   500 502 503 504  /50x.html;
          location = /50x.html {
              root   /usr/share/nginx/html;
          }
        }
    • 创建并查看ConfigMap。
    [root@k8s-master ~]# kubectl apply -f nginx-conf.yaml 
    configmap/nginx-conf created

    [root@k8s-master ~]# kubectl get cm nginx-conf 
    NAME         DATA   AGE
    nginx-conf   1      21s

    [root@k8s-master ~]# kubectl describe cm nginx-conf 
    Name:         nginx-conf
    Namespace:    default
    Labels:       <none>
    Annotations:  <none>

    Data
    ====
    default.conf:
    ----
    server {
      listen       8080;
      listen  [::]:80;
      server_name  localhost;

      location / {
          root   /usr/share/nginx/html;
          index  index.html index.htm;
      }

      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
          root   /usr/share/nginx/html;
      }
    }
    Events:  <none>

    3、在k8s集群拉起一个nginx的pod并加载ConfigMap,通过默认8080去访问。

    • 编写nginx的yaml文件,并加载ConfigMap。
    [root@k8s-master ~]# cat my-nginx-cm.yaml 
    apiVersion: apps/v1
    kind: Deployment 
    metadata:
      name: my-nginx 
    spec:
      replicas: 1 
      selector:
        matchLabels:
          app: nginx 
      template:
        metadata:
          labels:
            app: nginx 
        spec:
          containers:
          - name: nginx 
            image: nginx:1.18.0
            imagePullPolicy: IfNotPresent  
            ports:
            - containerPort: 80
            volumeMounts:
            - name: config-volume 
              mountPath: /etc/nginx/conf.d
          volumes:
          - name: config-volume 
            configMap:
              name: nginx-conf
    • 创建并查看nginx的pod。
    [root@k8s-master ~]# kubectl apply -f my-nginx-cm.yaml 
    deployment.apps/my-nginx created

    [root@k8s-master ~]# kubectl get pod my-nginx-f79db7777-m9l22 -o wide
    NAME                       READY   STATUS    RESTARTS   AGE   IP             NODE                     NOMINATED NODE   READINESS GATES
    my-nginx-f79db7777-m9l22   1/1     Running   0          56s   10.244.1.209   k8s-node-1.example.com   <none>           <none>
    • 测试访问nginx的8080端口。
    [root@k8s-master ~]# curl http://10.244.1.209:8080
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
    • 进入到nginx的pod,查看nginx的配置文件。
    [root@k8s-master ~]# kubectl exec -it my-nginx-f79db7777-m9l22 -- /bin/bash
    root@my-nginx-f79db7777-m9l22:/# cat /etc/nginx/conf.d/default.conf 
    server {
      listen       8080;
      listen  [::]:80;
      server_name  localhost;

      location / {
          root   /usr/share/nginx/html;
          index  index.html index.htm;
      }

      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
          root   /usr/share/nginx/html;
      }
    }root@my-nginx-f79db7777-m9l22:/# 

    转自

    K8s ConfigMap 存储 Nginx 配置文件
    https://mp.weixin.qq.com/s/6aP7xI0bBtf48sOOwYMtYQ

  • 相关阅读:
    JS制作图表
    把图片存入数据库
    .NET各种小问题
    JS各种小问题
    JS操作cookie
    JS处理Json数据
    git与svn的区别
    关于java中的一些循环
    java基础面试(上)
    Spring事务
  • 原文地址:https://www.cnblogs.com/paul8339/p/15424586.html
Copyright © 2020-2023  润新知