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

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

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

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

     

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

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

[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:/# 

 二、创建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 2022-01-25 15:41  属于我的梦,明明还在  阅读(139)  评论(0)    收藏  举报