【K8S实战系列-nignx-2】k8s中configmap挂载配置nginx.conf

【K8S实战系统-nignx-2】k8s中configmap挂载配置nginx.conf

1. ConfigMap是什么


ConfigMap作用是存储不加密的数据到etcd中,让Pod以变量或数据卷Volume挂载到容器中

2. 创建ConfigMap

2.1 准备nginx.conf 配置文件

user  nginx;
worker_processes  auto;

error_log  /nas/nas-nginx/logs/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  4096;
}

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  /nas/nginx/logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    include /nas/nginx/conf.d/*.conf;
}

2.2 创建名nginx-cm的configmap

kubectl create configmap nginx-cm --from-file=nginx.conf

2.3 然后查看详细信息

kubectl describe cm nginx-cm

3 Volume数据卷形式挂载

vim nginx-deployment.yaml


apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: "registry-vpc.cn-hangzhou.aliyuncs.com/cncx-product/nginx:v2021"
          imagePullPolicy: IfNotPresent
          ports:
          - containerPort: 80
          volumeMounts:
          - name: nginx-config
            mountPath: /etc/nginx/nginx.conf
            subPath: nginx.conf
          - name: pvc-nas-nginx
            mountPath: "/nas/nas-nginx"
      volumes:
        - name: nginx-config
          configMap:
            name: nginx-cm
            items:
            - key: nginx.conf
              path: nginx.conf
        - name: pvc-nas-nginx
          persistentVolumeClaim:
            claimName: nas-nginx-claim
      imagePullSecrets:
        - name: images-secret

这里我不仅使用configmap来获取nginx.conf的配置文件;

也挂载了nas的数据盘,来存放其他配置文件,以及静态资源的存储;

4. 创建nginx pod

kubectl apply -f nginx-deployment.yaml


5. 查看nginx deploy

[root@server40 nginx]# kubectl get deploy -o wide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                                                           SELECTOR
nginx   2/2     2            2           56s   nginx        registry-vpc.cn-hangzhou.aliyuncs.com/cncx-product/nginx:v2021   app=nginx

6.进入容器内部,查看目录具体信息

kubectl exec -it 容器名 /bin/bash 

7 通过nginx 服务测试是否成功

创建服务:nginx-deployment.yaml

vim nginx-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx
 	labels:
    name: nginx-svc
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
    nodePort: 30080
  - port: 443
    targetPort: 443
    protocol: TCP
    nodePort: 30443
  selector:
    app: nginx

8. 通过浏览器查看效果。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值