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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM