微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

[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。

[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去访问。

[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>
[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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐