目录

一、实验环境

二、实验描述

三、实验1:步骤

1.使用configmap投射到nginx.conf配置文件到pod里

1.1需要准备nginx.conf配置文件

1.2将nginx.conf内容存放到configmap里(通过文件的方式,,这样简单一点)

1.3 启动ngnix的pod,使用configmap里的nginx.conf配置文件

2.验证

四、实验2:步骤

1. 修改nginx.conf配置文件,添加https的支持配置

2. 重新生成支持https配置的configmap,存放nginx.conf

3. 查看https-nginx-1里的具体内容是否有nginx.conf的内容

4. 将证书的内容生成secret

5. 启动pod使用configmap和secret里的内容

验证: 用浏览器访问宿主机的30443端口


一、实验环境

4台linux虚拟机,并已经搭建好k8s环境

二、实验描述

实验1:启动nginx里的pod,使用configmap投射nginx.conf配置文件到pod里。

实验2:使用secret投射https的证书到pod里,让pod支持https的访问

三、实验1:步骤

1.使用configmap投射到nginx.conf配置文件到pod里

1.1需要准备nginx.conf配置文件

[root@master secrect]# vim nginx.conf
worker_processes 4;
events {worker_connections 2048;
}
http {include  mime.types;default_type application/octet-stream;sendfile  on;keepalive_timeout 65s;server {listen 80;server_name localhost;location / {root  html;index index.html index.htm;}error_page  500 502 503 504 /50x.html;location = /50x.html {root html;}}
}

1.2将nginx.conf内容存放到configmap里(通过文件的方式,,这样简单一点)

[root@master secrect]# kubectl create configmap sc-nginx-1 --from-file=nginx.conf

然后查看configmap是否启动成功

[root@master secrect]# kubectl get configmap
NAME                   DATA   AGE
example-redis-config   1      14h
game-config            2      13h
kube-root-ca.crt       1      5d14h
sc-nginx-1             1      18s

同时查看sc-nginx-1里的具体内容是否有nginx.conf的内容

[root@master secrect]# kubectl describe configmap sc-nginx-1
Name:         sc-nginx-1
Namespace:    default
Labels:       <none>
Annotations:  <none>Data
====
nginx.conf:
----
worker_processes 4;
events {worker_connections 2048;
}
http {include  mime.types;default_type application/octet-stream;sendfile  on;keepalive_timeout 65s;server {listen 80;server_name localhost;location / {root  html;index index.html index.htm;}error_page  500 502 503 504 /50x.html;location = /50x.html {root html;}}
}BinaryData
====Events:  <none>

1.3 启动ngnix的pod,使用configmap里的nginx.conf配置文件

创建一个启动pod的配置文件

[root@master secrect]# vim nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: sanchuang-nginx
spec:replicas: 3selector:matchLabels:app: sanchuang-nginxtemplate:metadata:labels:app: sanchuang-nginxspec:containers:- name: nginximage: "nginx:latest"imagePullPolicy: IfNotPresentports:- containerPort: 80volumeMounts:- name: sanchuang-nginx-configmountPath: /etc/nginx/nginx.confsubPath: nginx.confvolumes:- name: sanchuang-nginx-configconfigMap:name: sc-nginx-1    #这里的名字要与上面创建的configmap名字一致items:- key: nginx.confpath: nginx.conf

启动这个配置文件

[root@master secrect]# kubectl apply -f nginx.yaml

查看pod是否启动起来了

[root@master secrect]# kubectl get pod -o wide
NAME                              READY   STATUS    RESTARTS   AGE   IP            NODE    NOMINATED NODE   READINESS GATES
sanchuang-nginx-77cdd449c-5l5wv   1/1     Running   0          18m   10.244.3.42   node3   <none>           <none>
sanchuang-nginx-77cdd449c-v58dp   1/1     Running   0          18m   10.244.1.41   node1   <none>           <none>
sanchuang-nginx-77cdd449c-xs2rx   1/1     Running   0          18m   10.244.2.24   node2   <none>           <none>

2.验证

查找启动的容器的node, 然后在node节点上nginx的信息,最上面的的docker是刚刚最新启动的,将container id记录下来,然后用docker top + container id来查看是否跟nginx.conf里的设定的的一样

[root@node1 ~]# docker ps
CONTAINER ID   IMAGE                                               COMMAND                   CREATED          STATUS          PORTS                               NAMES
3851fc4a2715   080ed0ed8312                                        "/docker-entrypoint.…"   19 minutes ago   Up 19 minutes                                       k8s_nginx_sanchuang-nginx-77cdd449c-v58dp_default_568e22d3-916e-408c-96f7-49884f3b9597_0
48782208ab6b   registry.aliyuncs.com/google_containers/pause:3.6   "/pause"                  19 minutes ago   Up 19 minutes                                       k8s_POD_sanchuang-nginx-77cdd449c-v58dp_default_568e22d3-916e-408c-96f7-49884f3b9597_0
0aae485e6d94   a4ca41631cc7                                        "/coredns -conf /etc…"   52 minutes ago   Up 52 minutes                                       k8s_coredns_coredns-6d8c4cb4d-z65vk_kube-system_a657dc0e-f82e-4641-9db8-f29755ff6393_0
49d320996d28   817bbe3f2e51                                        "/metrics-server --c…"   52 minutes ago   Up 52 minutes                                       k8s_metrics-server_metrics-server-784768bd4b-6lfzz_kube-system_1fe97741-f8e3-4dab-a768-b7de08105488_0
40ea4fffe8fa   registry.aliyuncs.com/google_containers/pause:3.6   "/pause"                  52 minutes ago   Up 52 minutes                                       k8s_POD_coredns-6d8c4cb4d-z65vk_kube-system_a657dc0e-f82e-4641-9db8-f29755ff6393_0
3ef0bcb0d964   registry.aliyuncs.com/google_containers/pause:3.6   "/pause"                  52 minutes ago   Up 52 minutes                                       k8s_POD_metrics-server-784768bd4b-6lfzz_kube-system_1fe97741-f8e3-4dab-a768-b7de08105488_0
4aa2c741b9e8   kubernetesui/dashboard                              "/dashboard --insecu…"   58 minutes ago   Up 58 minutes                                       k8s_kubernetes-dashboard_kubernetes-dashboard-546cbc58cd-jrrjr_kubernetes-dashboard_fe6404c1-ee7e-4fc7-9577-907d3ae8eeae_2
85b0cd9c8304   registry.aliyuncs.com/google_containers/pause:3.6   "/pause"                  58 minutes ago   Up 58 minutes                                       k8s_POD_kubernetes-dashboard-546cbc58cd-jrrjr_kubernetes-dashboard_fe6404c1-ee7e-4fc7-9577-907d3ae8eeae_15
13877b33b24b   8b675dda11bb                                        "/opt/bin/flanneld -…"   58 minutes ago   Up 58 minutes                                       k8s_kube-flannel_kube-flannel-ds-bcjbs_kube-flannel_b630dead-ad4c-4e9c-8b5d-c0b1925a4787_6
1ac9c4e275e1   f21c8d21558c                                        "/usr/local/bin/kube…"   58 minutes ago   Up 58 minutes                                       k8s_kube-proxy_kube-proxy-4vwfg_kube-system_9c2cdb9f-210d-464e-816a-12ee16dbe281_6
3dfbf7f2a608   registry.aliyuncs.com/google_containers/pause:3.6   "/pause"                  58 minutes ago   Up 58 minutes                                       k8s_POD_kube-flannel-ds-bcjbs_kube-flannel_b630dead-ad4c-4e9c-8b5d-c0b1925a4787_6
74686b9e8c36   registry.aliyuncs.com/google_containers/pause:3.6   "/pause"                  58 minutes ago   Up 58 minutes                                       k8s_POD_kube-proxy-4vwfg_kube-system_9c2cdb9f-210d-464e-816a-12ee16dbe281_6
89276130acfc   wordpress:latest                                    "docker-entrypoint.s…"   7 days ago       Up 58 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp   my_workdpress-wordpress-1
14114c317d64   mariadb:10.6.4-focal                                "docker-entrypoint.s…"   7 days ago       Up 58 minutes   3306/tcp, 33060/tcp                 my_workdpress-db-1
[root@node1 ~]# docker top 3851fc4a2715
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                17643               17622               0                   10:22               ?                   00:00:00            nginx: master process nginx -g daemon off;
101                 17681               17643               0                   10:22               ?                   00:00:00            nginx: worker process
101                 17682               17643               0                   10:22               ?                   00:00:00            nginx: worker process
101                 17683               17643               0                   10:22               ?                   00:00:00            nginx: worker process
101                 17684               17643               0                   10:22               ?                   00:00:00            nginx: worker process

可以看到一个docker容器里有4个work,说明实验成功。

还有一种方法,进入pod查看nginx.conf配置文件里的内容

[root@master secrect]# kubectl get pod -o wide
NAME                              READY   STATUS    RESTARTS   AGE   IP            NOD
sanchuang-nginx-77cdd449c-5l5wv   1/1     Running   0          18m   10.244.3.42   nod
sanchuang-nginx-77cdd449c-v58dp   1/1     Running   0          18m   10.244.1.41   nod
sanchuang-nginx-77cdd449c-xs2rx   1/1     Running   0          18m   10.244.2.24   nod
[root@master secrect]# kubectl exec -it sanchuang-nginx-77cdd449c-5l5wv -- bash
root@sanchuang-nginx-77cdd449c-5l5wv:/# cat /etc/nginx/nginx.conf
worker_processes 4;
events {worker_connections 2048;
}
http {include  mime.types;default_type application/octet-stream;sendfile  on;keepalive_timeout 65s;server {listen 80;server_name localhost;location / {root  html;index index.html index.htm;}error_page  500 502 503 504 /50x.html;location = /50x.html {root html;}}
}

四、实验2:步骤

描述:使用secret投射https的证书到pod里,让pod支持https的访问

1. 修改nginx.conf配置文件,添加https的支持配置

[root@master secrect]# vim nginx.conf
worker_processes 4;
events {worker_connections 2048;
}
http {include  mime.types;default_type application/octet-stream;sendfile  on;keepalive_timeout 65s;server {listen 80;server_name localhost;location / {root  html;index index.html index.htm;}error_page  500 502 503 504 /50x.html;location = /50x.html {root html;}}server {listen     443 sslp;server_name  localhost;ssl_certificate     /etc/nginx/conf.d/tls.crt;   #证书的位置,使用绝对路径ssl-certificate_key /etc/nginx/conf.d/tls.key;        ssl_session_cache      share:SSL:1m;ssl_session_timeout    5m;ssl_ciphers   HIGH:!aNULL:!MD5;ssl_prefer_server_ciphers   on;location / {root    html;index   index.html   index.htm;}}
}

2. 重新生成支持https配置的configmap,存放nginx.conf

https-nginx-1是configmap的名字

[root@master secrect]# kubectl create configmap https-nginx-1 --from-file=nginx.conf
configmap/https-nginx-1 created
[root@master secrect]# kubectl get cm
NAME                   DATA   AGE
example-redis-config   1      19h
game-config            2      19h
https-nginx-1          1      16s
kube-root-ca.crt       1      5d19h
sc-nginx-1             1      5h26m

3. 查看https-nginx-1里的具体内容是否有nginx.conf的内容

[root@master secrect]# kubectl describe configmap https-nginx-1
Name:         https-nginx-1
Namespace:    default
Labels:       <none>
Annotations:  <none>Data
====
nginx.conf:
----
worker_processes 4;
events {worker_connections 2048;
}
http {include  mime.types;default_type application/octet-stream;sendfile  on;keepalive_timeout 65s;server {listen 80;server_name localhost;location / {root  html;index index.html index.htm;}error_page  500 502 503 504 /50x.html;location = /50x.html {root html;}}server {listen     443 sslp;server_name  localhost;ssl_certificate     /etc/nginx/conf.d/tls.crt;    ssl-certificate_key /etc/nginx/conf.d/tls.key;        ssl_session_cache      share:SSL:1m;ssl_session_timeout    5m;ssl_ciphers   HIGH:!aNULL:!MD5;ssl_prefer_server_ciphers   on;location / {root    html;index   index.html   index.htm;}}
}BinaryData
====Events:  <none>

4. 将证书的内容生成secret

确保证书文件在同目录下,8905404_sanchuangedu.cn.pem就和8905404_sanchuangedu.cn.key是证书文件

证书是需要去购买或者免费试用的,可以到阿里云或者腾讯云、华为云等平台去购买或者免费申请试用

[root@master secrect]# ls
8905404_sanchuangedu.cn.key  backup      nginx.yaml       secret.yaml
8905404_sanchuangedu.cn.pem  nginx.conf  secret-pod.yaml
[root@master secrect]# kubectl create secret tls https-secret --key 8905404_sanchuangedu.cn.key --cert 8905404_sanchuangedu.cn.pem
secret/https-secret created
[root@master secrect]# kubectl get secret
NAME                  TYPE                                  DATA   AGE
default-token-w2nj9   kubernetes.io/service-account-token   3      5d19h
https-secret          kubernetes.io/tls                     2      12s
test-secret           Opaque                                2      19h

查看https-secret里的内容

[root@master secrect]# kubectl describe secret https-secret
Name:         https-secret
Namespace:    default
Labels:       <none>
Annotations:  <none>Type:  kubernetes.io/tlsData
====
tls.key:  1679 bytes
tls.crt:  3834 bytes

5. 启动pod使用configmap和secret里的内容

[root@master secrect]# kubectl apply -f nginx.yaml
deployment.apps/sanchuang-nginx-3 created

创建一个service把它发布出去

[root@master secrect]# vim service.yaml
apiVersion: v1
kind: Service
metadata:name: my-https-nginx
spec:type: NodePortports:- name: httpport: 80targetPort: 80nodePort: 30080protocol: TCP- name: httpsport: 443targetPort: 443nodePort: 30443protocol: TCPselector:app: sanchuang-nginx-3

验证: 用浏览器访问宿主机的30443端口

k8s——configmap-secret-nginx实验相关推荐

  1. K8S configmap详解:从文件创建、从文件夹创建及以volume、env环境变量的方式在pod中使用

    K8S configmap详解:从文件创建.从文件夹创建及以volume.env环境变量的方式在pod中使用 ConfigMap是用来存储配置文件的kubernetes资源对象,所有的配置内容都存储在 ...

  2. .NET Core 使用 K8S ConfigMap的正确姿势

    背景 ASP.NET Core默认的配置文件定义在 appsetings.json和 appsettings.{Environment}.json文件中.这里面有一个问题就是,在使用容器部署时,每次修 ...

  3. k8s 配置 Secret 集成Harbor

    本篇主要 记录一下 在 k8s 中如果想要 从 harbor拉取镜像 该怎么操作,以及介绍了一下 k8s 中 Secret 是什么 1.Secret 是什么 1.1 Secret 概述 Secret ...

  4. 实战:k8s中网络策略实验(成功测试-博客输出)-20211005

    目录 文章目录 目录 写在前面 基础知识 实验环境 原课件内容 1.案例1:拒绝其他命名空间Pod访问 2.案例2:同一个命名空间下应用之间限制访问 3.案例3:只允许指定命名空间中的应用访问 总结 ...

  5. k8s --使用secret

    为什么要使用secret 官网说明 以前一些数据库密码可能会写在配置文件中springboot工程中一般是application.yaml.有的会直接写明文,有的加密之后把密文写在配置文件中. k8s ...

  6. k8s 配置存储之 Configmap secret

    文章目录 configmap 示例 更新 Secret Pod 用使用 Secret三种方式 使用 kubectl 创建 Secret 手动创建 Secret data stringData 从生成器 ...

  7. k8s资源secret和ConfigMap的相关详解

    secret 一.secret相关简介 二.secret资源的使用 三.Secret实践k8s连接Harbor 四.ConfigMap相关介绍 五.ConfigMap资源的使用 一.secret相关简 ...

  8. 笔记-Ubuntu部署K8s,启动nginx服务

    部署K8s,我切到了root用户进行部署 su root 启动Docker systemctl enable docker systemctl status docker systemctl star ...

  9. k8s往secret里导入证书_K8S之Secret

    简介 secret顾名思义,用于存储一些敏感的需要加密的数据.这些数据可能是要保存在pod的定义文件或者docker的镜像中.把这些数据通过加密的方式存放到secrets对象中,可以降低信息泄露的风险 ...

  10. k8s学习-Secret(创建、使用、更新、删除等)

    目录 概念 模板 secret的类型 实战 创建 使用kubectl 使用yaml文件创建 使用 环境变量 挂载 imagePullSecret 更新 删除 注意项 参考 概念 Secret 是一个主 ...

最新文章

  1. 错误:AttributeError: module 'enum' has no attribute 'IntFlag'
  2. GitChat · 大数据 | 一步一步学习大数据:Hadoop 生态系统与场景
  3. Hadoop 07_MapReduce运行的三种方式
  4. C++实现剔除不能识别的非ASCIII、非中文字符
  5. .NET Framework 2.0 组件和非托管代码与交互操作详解(转)
  6. 文字输入限制_从拼音输入法的兴起看汉字文化圈的衰落
  7. JQuery Mobile中特有事件和方法
  8. centos7恢复mysql数据库_centos7 mysql数据库的安装与使用
  9. linux 叫号程序,linux socket编程有关问题,上面是小弟我做的一个简单的模拟银行排队叫号系统...
  10. python批量转换图片格式_利用Python批量把PDF文件文件转换成图片格式
  11. matlab光滑曲线链接,在Matlab中使用光滑曲线连接点
  12. 玉溪第一座智能变电站,造国际一流智能配电网,机器人来运维
  13. 相机下载_SonyPMCARE, 反向工程索尼PlayMemories相机应用
  14. Arduino_Core_STM32---pinMode()实现分析
  15. virtual reality(虚拟现实)
  16. 自动驾驶路径规划——A*(Astar)算法
  17. ubuntu独立显卡驱动
  18. 飞思卡尔MC9S12G64串口发送接收驱动
  19. 详谈室内定位技术方案
  20. asp.net 文件下载的五种方式

热门文章

  1. 如何得到在指定视图下沿折线或曲线的展开剖面
  2. Facebook账号被锁定怎么找回
  3. 搭建wsl2,windows子系统
  4. 麦块盒子一直说没安装java_我的世界盒子麦块
  5. MS7210,数字RGB转HDMI,P=P替代IT66121FN,HDMI_Tx
  6. VSCode 快捷键、快捷指令
  7. 输入一长串命令后,快速回到命令开头
  8. 使用Stream流, 获取所有的“张”姓学员和“宋”姓学员, 并把获取到的信息存储到新集合中. 然后遍历集合并打印
  9. 华为SmartAX MA5620配置当交换机使用
  10. 带翠友们浅谈一下翡翠的价格问题?