Kubernetes 使用configmap挂载卷给Pod内的nginx容器,并且实现nginx的代理服务

使用ConfigMap配置Nginx容器实验
博客围绕使用ConfigMap配置Nginx容器展开两个实验。一是用ConfigMap挂载卷给Pod内的Nginx容器,涵盖创建配置文件、ConfigMap、Deployment和Service等步骤并验证访问;二是将Nginx容器变为代理服务器,通过创建ConfigMap、更新配置、搭建Pod和Service来实现集群内外资源访问,并进行验证。

目录

实验:使用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将nginx容器变为代理服务器,使集群内访问集群外边的资源通过nginx tcp代理。

1、使用nginx-config.yaml文件创建Configmap

2、搭建nginx容器,调用Configmap更新nginx的配置文件(添加入网口的标签):

3、运行 nginx-deployment2.yaml,搭建Pod

4、运行 nginx-service2.yaml,搭建Service服务

5、运行nginx-service2.yaml 和 nginx-service3.yaml, 搭建Service服务

6、验证访问:


实验:使用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
参考资源链接:[Kubernetes全攻略:从入门到高级运维](https://wenku.youkuaiyun.com/doc/7a52o4iz2c?utm_source=wenku_answer2doc_content) 要创建一个Kubernetes集群中的Nginx Pod,并使用ConfigMap中的数据作为环境变量,你需要了解如何在YAML文件中定义PodConfigMap资源以及如何在Pod定义中引用ConfigMap。下面提供了一个具体的操作示例: 1. 首先创建一个ConfigMap,包含你的环境变量数据。例如,创建一个名为`nginx-env`的ConfigMap: ```yaml apiVersion: v1 kind: ConfigMap metadata: name: nginx-env data: ENV: prod ``` 使用kubectl命令来创建ConfigMap: ```bash kubectl apply -f configmap.yaml ``` 2. 接着,创建一个Nginx Pod的YAML文件,并在其中引用ConfigMap以设置环境变量。确保在Pod定义中指定要使用ConfigMap和需要的键: ```yaml apiVersion: v1 kind: Pod metadata: name: nginx-pod spec: containers: - name: nginx image: nginx:latest env: - name: ENV valueFrom: configMapKeyRef: name: nginx-env key: ENV volumeMounts: - name: config-volume mountPath: /etc/nginx/conf.d readOnly: true volumes: - name: config-volume configMap: name: nginx-env ``` 在这个例子中,ConfigMap `nginx-env` 被挂载Nginx容器的`/etc/nginx/conf.d`目录下。我们指定了环境变量`ENV`,它将被设置为ConfigMap中的`prod`值。 3. 使用kubectl创建Pod: ```bash kubectl apply -f pod.yaml ``` 4. 验证Pod是否正确启动,并检查环境变量是否设置正确: ```bash kubectl exec -it nginx-pod -- env ``` 你应该能看到`ENV=prod`环境变量已经设置在Nginx容器中。 通过这些步骤,你将能够将ConfigMap中的数据作为环境变量挂载Kubernetes集群中的Nginx Pod。为了进一步深入学习Kubernetes的集群搭建和管理,建议参考《Kubernetes全攻略:从入门到高级运维》。这本书详细介绍了Kubernetes的核心概念和实践操作,可以帮助你在生产环境中更加高效地管理和扩展容器化应用。 参考资源链接:[Kubernetes全攻略:从入门到高级运维](https://wenku.youkuaiyun.com/doc/7a52o4iz2c?utm_source=wenku_answer2doc_content)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值