目录
实验:使用configmap挂载卷给Pod内的nginx容器
1、创建nginx.conf配置文件(必须由nginx镜像里的nginx.conf修改而来,防止出现配置不相似的情况出现,导致访问不了nginx网页)
2、通过nginx.conf文件创建configmap容器(注意nginx.conf文件必须在该目录下)
4、根据configmap创建nginx-deployment.yaml文件
5、运行nginx-deployment.yaml,创建Pod
6、创建Service发布nginx容器服务,创建nginx-service.yaml文件
实验:使用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服务
实验:使用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
使用ConfigMap配置Nginx容器实验

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

被折叠的 条评论
为什么被折叠?



