K8s ConfigMap 存储 Nginx 配置文件【转】
2021/10/20 7:12:30
本文主要是介绍K8s ConfigMap 存储 Nginx 配置文件【转】,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
ConfigMap 实现 nginx 容器的配置文件管理
1、在k8s集群拉起一个nginx的pod,通过默认80去访问。
- 编写nginx的yaml文件。
[root@k8s-master ~]# cat my-nginx.yaml apiVersion: apps/v1 kind: Deployment metadata: name: my-nginx 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
- 创建并查看nginx的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 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES my-nginx-67dfd6c8f9-fn8jf 1/1 Running 0 84s 10.244.1.207 k8s-node-1.example.com <none> <none>
- 测试访问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 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 NAME DATA AGE nginx-conf 1 21s [root@k8s-master ~]# kubectl describe cm nginx-conf 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 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 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:/#
转自
K8s ConfigMap 存储 Nginx 配置文件
https://mp.weixin.qq.com/s/6aP7xI0bBtf48sOOwYMtYQ
这篇关于K8s ConfigMap 存储 Nginx 配置文件【转】的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-15在Kubernetes (k8s) 中搭建三台 Nginx 服务器怎么实现?-icode9专业技术文章分享
- 2024-11-05基于Kubernetes的自定义AWS云平台搭建指南
- 2024-11-05基于Kubernetes Gateway API的现代流量管理方案
- 2024-11-05在Kubernetes上部署你的第一个应用:Nginx服务器
- 2024-11-05利用拓扑感知路由控制Kubernetes中的流量
- 2024-11-05Kubernetes中的层次命名空间:更灵活的资源管理方案
- 2024-11-055分钟上手 Kubernetes:精简实用的 Kubectl 命令速查宝典!
- 2024-10-30K8s 容器的定向调度与亲和性
- 2024-10-28云原生周刊:K8s未来三大发展方向 丨2024.10.28
- 2024-10-25亚马逊弹性Kubernetes服务(EKS)实战:轻松搭建Kubernetes平台