posts - 359,comments - 0,views - 19万

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@k8s-master ~]# cat my-nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
  namespace: test
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

 启动并查看pod状态

1
2
3
4
5
6
[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 -n test
NAME                        READY   STATUS    RESTARTS   AGE   IP             NODE                     NOMINATED NODE   READINESS GATES
my-nginx-67dfd6c8f9-fn8jf   1/1     Running   0          84s   10.244.1.207  

 测试访问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
  namespace: test
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。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@k8s-master ~]# kubectl apply -f nginx-conf.yaml
configmap/nginx-conf created
 
[root@k8s-master ~]# kubectl get cm nginx-conf -n test
NAME         DATA   AGE
nginx-conf   1      21s
 
[root@k8s-master ~]# kubectl describe cm nginx-conf  -n test
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。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@k8s-master ~]# cat my-nginx-cm.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
  namespace: test
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

1
2
3
4
5
6
[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 -n test
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端口。

1
2
3
4
5
6
[root@k8s-master ~]# curl http://10.244.1.209:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>

 进入到nginx的pod,查看nginx的配置文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[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:/#

 二、创建configmap 资源 (方式二)

vim index.html           
<h1> this is hello,waorld!</h1>

创建资源

kubectl create configmap  index-config   --from-file=index.html
//configmap 资源 的删除方式
kubectl delete configmap index-config

 

部署实验

复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
  namespace: test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: web
        image: harbocto.boe.com.cn/public/nginx
        imagePullPolicy: Always
        ports:
        - containerPort: 80
        resources:
          requests:
            cpu: 200m
            memory: 200Mi
            ephemeral-storage: 1Gi
          limits:
            cpu: 2000
            memory: 2Gi
            ephemeral-storage: 5Gi
        volumeMounts:
        - mountPath: /etc/nginx/nginx.conf
          name: nginxconf
          subPath: nginx.conf
      volumes:
      - name: nginxconf
        configMap:
          name: nginxconf
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginxconf
  namespace: test
data:
  nginx.conf: |
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        client_max_body_size 50m;
        sendfile        on;
        keepalive_timeout  65;
        server {
            listen       80;
            server_name  localhost;
            root   /usr/share/nginx/html;
            location / {
               index  index.html index.htm;
            }
        }
    }
---
apiVersion: v1
kind: Service
metadata:
  name: web
  namespace: test
  labels:
    name: web
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    nodePort: 30500
  selector:
    app: web
复制代码
kubectl create -f nginx.yml



posted on   属于我的梦,明明还在  阅读(130)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示