Kubernetes 使用configmap挂载卷给Pod内的nginx容器

目录

实验:使用configmap挂载卷给Pod内的nginx容器

1、创建nginx.conf配置文件(必须由nginx镜像里的nginx.conf修改而来,防止出现配置不相似的情况出现,导致访问不了nginx网页)

2、通过nginx.conf文件创建configmap容器(注意nginx.conf文件必须在该目录下)

3、查看创建的configmap和它的详细资料

4、根据configmap创建nginx-deployment.yaml文件

5、运行nginx-deployment.yaml,创建Pod

6、创建Service发布nginx容器服务,创建nginx-service.yaml文件

7、运行nginx-service.yaml,创建Pod

8、验证访问


实验:使用configmap挂载卷给Pod内的nginx容器

1、创建nginx.conf配置文件(必须由nginx镜像里的nginx.conf修改而来,防止出现配置不相似的情况出现,导致访问不了nginx网页)

复制代码
(base) root@sd-cluster-04:/etc/nginx# cat nginx.conf 
# claylpf test

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
(base) root@sd-cluster-04:/etc/nginx# 

2、通过nginx.conf文件创建configmap容器(注意nginx.conf文件必须在该目录下)

复制代码
(base) root@sd-cluster-04:/etc/nginx# kubectl create configmap nginx-config --from-file=nginx.conf -n testns

3、查看创建的configmap和它的详细资料

复制代码
(base) root@sd-cluster-04:/etc/nginx# kubectl get configmap nginx-config -n testns #查看是否成功运行
NAME           DATA   AGE
nginx-config   1      77s


(base) root@sd-cluster-04:/etc/nginx# kubectl describe configmap/nginx-config -n testns  # 查看详细信息
Name:         nginx-config
Namespace:    testns
Labels:       <none>
Annotations:  <none>

Data
====
nginx.conf:
----
# claylpf test

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

Events:  <none>
(base) root@sd-cluster-04:/etc/nginx# 

4、根据configmap创建nginx-deployment.yaml文件

复制代码
(base) root@sd-cluster-04:/etc/nginx# cat nginx-deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: testns
spec:
  replicas: 1
  selector:
    matchLabels:
      app: clay-nginx
  template:
    metadata:
      labels:
        app: clay-nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.24
        ports:
        - containerPort: 80
        volumeMounts:
        - name: nginx-config-volume
          mountPath: /etc/nginx/nginx.conf
          subPath: nginx.conf
      volumes:
        - name: nginx-config-volume
          configMap:
            name: nginx-config
            items:
            - key: nginx.conf
              path: nginx.conf     
(base) root@sd-cluster-04:/etc/nginx# 

代码解释:

复制代码
这是一个 Kubernetes Deployment 的 YAML 文件,用于定义一个部署配置。以下是逐行解释该文件的内容:

1. `apiVersion: apps/v1`: 这一行指定了 Kubernetes API 的版本,以及使用的资源类型是 Deployment。

2. `kind: Deployment`: 指定了资源的种类,这是一个部署 (Deployment)。

3. `metadata:`: 定义资源的元数据,包括名称和命名空间。

   - `name: nginx-deployment`: 部署的名称是 "nginx-deployment"。
   - `namespace: testns`: 部署所属的命名空间是 "testns"。

6. `spec:`: 定义了部署的规格,包括副本数、选择器以及 Pod 模板。

   - `replicas: 1`: 指定了要运行的副本数量,这里是 1 个。
   
   - `selector:`: 选择器用于确定要管理的 Pod 集合。

     - `matchLabels:`: 指定了需要匹配的标签。

       - `app: clay-nginx`: 匹配标签中含有 "app: clay-nginx" 的 Pod。

   - `template:`: 定义了要创建的 Pod 模板。

     - `metadata:`: 定义 Pod 模板的元数据,包括标签。

       - `labels:`: 指定了 Pod 的标签,这里是 "app: clay-nginx"。

     - `spec:`: 定义了 Pod 的规格。

       - `containers:`: 定义了容器列表。

         - `name: nginx`: 定义容器的名称为 "nginx"。
         - `image: nginx:1.24`: 指定容器使用的镜像,这里使用的是 Nginx 版本 1.24。
         - `ports:`: 定义容器的端口配置。

           - `containerPort: 80`: 容器监听的端口是 80。

         - `volumeMounts:`: 定义了卷挂载配置,将卷挂载到容器内。

           - `name: nginx-config-volume`: 指定挂载卷的名称,这个名称将与下面定义的卷匹配。
           - `mountPath: /etc/nginx/nginx.conf`: 指定挂载的路径在容器内的位置。
           - `subPath: nginx.conf`: 指定在卷中的子路径。

       - `volumes:`: 定义了卷的配置。

         - `name: nginx-config-volume`: 定义了一个名为 "nginx-config-volume" 的卷。
         - `configMap:`: 指定卷的类型为 ConfigMap。

           - `name: nginx-config`: 指定 ConfigMap 的名称,这个名称将与集群中的 ConfigMap 匹配。
           - `items:`: 指定要从 ConfigMap 中提取的键值对。

             - `key: nginx.conf`: 指定键的名称。
             - `path: nginx.conf`: 指定将键的值映射到容器内的路径。

这个配置文件描述了一个部署,它将创建一个 Pod,其中运行一个名为 "nginx" 的容器,该容器使用 Nginx 1.24 镜像,并将一个名为 "nginx-config-volume" 的 ConfigMap 挂载到容器内的 `/etc/nginx/nginx.conf` 路径上。这样,Nginx 容器将使用 ConfigMap 中的配置文件来配置其行为。

5、运行nginx-deployment.yaml,创建Pod

复制代码
(base) root@sd-cluster-04:/etc/nginx# kubectl apply -f nginx-deployment.yaml 

(base) root@sd-cluster-04:/etc/nginx# kubectl get pods -o wide -n testns
NAME                                READY   STATUS        RESTARTS   AGE    IP             NODE            NOMINATED NODE   READINESS GATES
es-cluster-0                        0/1     Terminating   0          62d    10.244.5.163   sd-cluster-02   <none>           <none>
mysql-deployment-66c4d975f5-zm4sz   1/1     Running       0          5d1h   10.244.0.212   sd-cluster-04   <none>           <none>
nginx-deployment-67fb5f6db7-9ltlb   1/1     Running       0          21m    10.244.1.254   sd-cluster-05   <none>           <none>
redis-deployment-f7d7dd455-xk7h8    1/1     Running       0          5d     10.244.0.215   sd-cluster-04   <none>           <none>
(base) root@sd-cluster-04:/etc/nginx# 

6、创建Service发布nginx容器服务,创建nginx-service.yaml文件

复制代码
(base) root@sd-cluster-04:/etc/nginx# cat nginx-service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  namespace: testns
  labels:
    app: clay-nginx
spec:
  type: NodePort
  selector:
    app: clay-nginx  
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 31273
(base) root@sd-cluster-04:/etc/nginx# 

逐行解释:

复制代码
这是一个 Kubernetes Service 的 YAML 文件,用于创建一个服务资源。以下是逐行解释该文件的内容:

1. `apiVersion: v1`: 这一行指定了 Kubernetes API 的版本,以及使用的资源类型是 Service。

2. `kind: Service`: 指定了资源的种类,这是一个服务 (Service)。

3. `metadata:`: 定义了服务的元数据,包括名称、命名空间和标签。

   - `name: nginx-service`: 服务的名称是 "nginx-service"。
   - `namespace: testns`: 服务所属的命名空间是 "testns"。

   - `labels:`: 为服务添加标签。

     - `app: clay-nginx`: 将标签 "app: clay-nginx" 添加到服务上。

6. `spec:`: 定义了服务的规格,包括服务类型、选择器和端口配置。

   - `type: NodePort`: 指定了服务的类型为 NodePort,这意味着服务将暴露到每个节点的指定端口上。

   - `selector:`: 选择器用于确定哪些 Pod 属于这个服务。

     - `app: clay-nginx`: 匹配标签中含有 "app: clay-nginx" 的 Pod。

   - `ports:`: 定义了服务监听的端口配置。

     - `protocol: TCP`: 指定了端口的传输协议是 TCP。

     - `port: 80`: 服务监听的端口号是 80。

     - `targetPort: 80`: 服务将流量转发到后端 Pod 的端口号是 80。

     - `nodePort: 31273`: 如果服务类型是 NodePort,这个配置项指定了节点上用于访问服务的端口号。在这个例子中,服务将在节点上的端口 31273 上监听,从而可以通过节点的 IP 地址和该端口访问该服务。

这个配置文件描述了一个服务,它会将流量从节点的端口 31273 转发到匹配标签 "app: clay-nginx" 的后端 Pod 上的端口 80。这种服务类型通常用于在外部网络上访问 Kubernetes 集群中的服务。

7、运行nginx-service.yaml,创建Pod

复制代码
(base) root@sd-cluster-04:/etc/nginx# kubectl apply -f nginx-service.yaml 

(base) root@sd-cluster-04:/etc/nginx# kubectl get service -o wide -n testns
NAME            TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE   SELECTOR
mysql-service   NodePort   10.96.132.123   <none>        3306:30859/TCP   5d    app=mysql
nginx-service   NodePort   10.96.87.157    <none>        80:31273/TCP     65m   app=clay-nginx
redis-service   NodePort   10.96.10.164    <none>        6379:30026/TCP   5d    app=redis
(base) root@sd-cluster-04:/etc/nginx# 

8、验证访问

首先访问容器内的配置文件是否发生更改:

再次访问浏览器,产科nginx的Web服务是否发布

相关推荐
时迁2471 小时前
【k8s】k8s是怎么实现自动扩缩的
云原生·容器·kubernetes·k8s
诡异森林。4 小时前
Docker--Docker网络原理
网络·docker·容器
matrixlzp6 小时前
K8S Service 原理、案例
云原生·容器·kubernetes
angushine7 小时前
让Docker端口映射受Firewall管理而非iptables
运维·docker·容器
SimonLiu0099 小时前
清理HiNas(海纳斯) Docker日志并限制日志大小
java·docker·容器
Sonetto199910 小时前
Nginx 反向代理,啥是“反向代理“啊,为啥叫“反向“代理?而不叫“正向”代理?它能干哈?
运维·前端·nginx
高峰君主11 小时前
Docker容器持久化
docker·容器·eureka
能来帮帮蒟蒻吗11 小时前
Docker安装(Ubuntu22版)
笔记·学习·spring cloud·docker·容器
言之。16 小时前
别学了,打会王者吧
java·python·mysql·容器·spark·php·html5
秦始皇爱找茬19 小时前
docker部署Jenkins工具
docker·容器·jenkins