secret用来保存小片敏感数据的k8s资源,例如密码,token,或者秘钥。这类数据当然也可以存放在Pod或者镜像中,但是放在Secret中是为了更方便的控制如何使用数据,并减少暴露的风险。
用户可以创建自己的secret,系统也会有自己的secret。
Pod需要先引用才能使用某个secret
方式1:使用kubectl create secret命令
方式2:yaml文件创建Secret
Secret 对象要求这些数据必须是经过 Base64 转码的
[root@k8s-master pod]# echo -n "123456" | base64
MTIzNDU2
[root@k8s-master pod]# echo -n "MTIzNDU2" | base64 -d
123456
vim secret.yaml
###
apiVersion: v1
kind: Secret
metadata:
name: mysql
type: Opaque
data:
password: MTIzNDU2
username: cm9vdA==
database: d29yZHByZXNz
###
创建secret
kubectl apply -f secret.yaml
[root@k8s-master pod]# kubectl get secrets
NAME TYPE DATA AGE
default-token-qplcn kubernetes.io/service-account-token 3 3d4h
mysql Opaque 3 4h50m
[root@k8s-master pod]# kubectl get secrets mysql -o yaml
apiVersion: v1
data:
database: d29yZHByZXNz
password: MTIzNDU2
username: cm9vdA==
kind: Secret
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","data":{"database":"d29yZHByZXNz","password":"MTIzNDU2","username":"cm9vdA=="},"kind":"Secret","metadata":{"annotations":{},"name":"mysql","namespace":"default"},"type":"Opaque"}
creationTimestamp: "2023-12-14T07:29:20Z"
name: mysql
namespace: default
resourceVersion: "133162"
uid: c8beb9d6-8bdd-4576-a8ad-be625defb432
type: Opaque
vim pod.yaml
###
apiVersion: v1
kind: Pod
metadata:
name: mysql
labels:
name: mysql
spec:
containers:
- name: mysql
image: mysql
ports:
- containerPort: 3306
#环境变量
env:
- name: MYSQL_ROOT_PASSWORD #环境变量的名字
valueFrom: #我从哪里来
secretKeyRef: ##从secret中的一个key来
name: mysql ##secret的名字叫什么
key: password ##里面的值
- name: MYSQL_DATABASE
valueFrom:
secretKeyRef:
name: mysql
key: database
###
创建secret
kubectl apply -f pod.yaml
创建上面的pod
[root@k8s-master pod]# kubectl apply -f pod.yaml
pod/mysql created
[root@k8s-master pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
mysql 1/1 Running 0 17s
登录容器观察变量
[root@k8s-master pod]# kubectl exec -it mysql /bin/bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
bash-4.4# mysql -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
...
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wordpress |
+--------------------+
5 rows in set (0.00 sec)
vim mysql.yaml
###
apiVersion: v1
kind: Pod
metadata:
name: pod-mysql
labels:
name: pod-mysql
spec:
containers:
- name: testredis
image: redis
volumeMounts: ##挂载一个卷
- name: foo ###挂载这个卷的名字
mountPath: /opt ###挂载哪
volumes: ##卷的定义
- name: foo ##卷的名字
secret: ##卷是直接引用secret
secretName: mysql ##secret的名字
items: ##定义一个items
- key: username ##引用secret里的值的名字
path: my-group/my-username ##挂载卷的相对路径,尽量别写在etc下
###
创建pod
[root@k8s-master pod]# kubectl apply -f mysql.yaml
pod/pod-mysql created
[root@k8s-master pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
pod-mysql 1/1 Running 0 5s
从volume中读取secret的值
[root@k8s-master pod]# kubectl exec -it pod-mysql /bin/bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@pod-mysql:/data# cd /opt/my-group
root@pod-mysql:/opt/my-group# ls
my-username
root@pod-mysql:/opt/my-group# cat my-username
rootroot@pod-mysql:/opt/my-group#
username被映射到了文件/etc/foo/my-group/my-username而不是/etc/foo/username,而password没有被使用,这种方式每个key的调用需要单独用key像username一样调用
也就是如果修改一个Secret的内容,那么挂载了该Secret的容器中也将会取到更新后的值,但是这个时间间隔是由kubelet的同步时间决定的。
1.设置64加密
[root@k8s-master pod]# echo malong | base64
bWFsb25nCg==
2.将root化成malong
vim secret.yaml
###
apiVersion: v1
kind: Secret
metadata:
name: mysql
type: Opaque
data:
password: MTIzNDU2
username: bWFsb25nCg== 修改为malong加密后的
database: d29yZHByZXNz
###
创建
root@k8s-master pod]# kubectl apply -f secret.yml
secret/mysql configured
连接容器查看
[root@k8s-master pod]# kubectl exec -it pod-mysql /bin/bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@pod-mysql:/data# cd /opt/my-group/
root@pod-mysql:/opt/my-group# cat my-username
malong
ConfigMap与 Secret 类似,用来存储配置文件的kubernetes资源对象,所有的配置内容都存储在etcd中。保存的是不需要加密的、应用所需的配置信息。可以使用 kubectl create configmap 从文件或者目录创建 ConfigMap,也可以直接编写 ConfigMap 对象的 YAML 文件 。
语法:--from-literal
[root@k8s-master ~]# kubectl create configmap centos --from-literal=user=root
configmap/centos created
语法:--from-file=<文件>
[root@k8s-master configMap]# ls
mysql.yaml nginx.conf pod.yaml secret.yml
[root@k8s-master configMap]# kubectl create configmap nginx.yml --from-file=pod.yaml
configmap/nginx.yml created
[root@k8s-master configMap]# kubectl create configmap config --from-file=./
configmap/config created
语法:即将一个目录下的所有配置文件创建为一个ConfigMap,--from-file=<目录>
[root@k8s-master configMap]# kubectl create configmap etc --from-file=/etc
configmap/etc created
创建出来的configMap里面也只有文件,没有目录
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
data:
nginx.level: 1.16.2
nginx.info: | ###用竖杠代替 缩行俩格
这就是我
创建的
configMap
nginx.conf: |
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
yaml文件创建configMap需要注意: data如果是键值对,那么就和在命令行创建configMap键值对相同的操作即可,一个键对应一个值
如果是配置文件,那么配置文件的名字: 加一个竖杠,表示我的内容在以下 然后缩行 俩格
[root@k8s-master configMap]# kubectl apply -f nginx.yaml
configmap/nginx-config created
vim pod-nginx.yaml
###
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
env:
- name: nginx.level ###使用configMap的个别值作为环境变量
valueFrom:
configMapKeyRef:
name: nginx-config
key: nginx.level
###
创建pod
kubectl apply -f pod-nginx.yaml
vim pod-nginx2.yaml
###
apiVersion: v1
kind: Pod
metadata:
name: nginx-2
spec:
containers:
- name: nginx-2
image: nginx
envFrom: ###使用configMap内的所有值作为环境变量
- configMapRef:
name: nginx-config
###
创建pod
[root@k8s-master configMap]# kubectl apply -f pod-nginx2.yaml
pod/nginx-2 created
vim pod-nginx.yaml
###
apiVersion: v1
kind: Pod
metadata:
name: nginx-2
spec:
containers:
- name: nginx-2
image: nginx
volumeMounts: ##挂载一个卷
- name: nginxconf ###挂载这个卷叫什么名字
mountPath: /opt ###挂载在哪
volumes: ###卷的定义
- name: nginxconf ###卷的名字
configMap: ##卷是引用configMap的值
name: nginx-config ###configMap的名字是什么
###
创建pod
kubectl apply -f pod-nginx.yaml
挂载是将该目录下的所有文件都覆盖
修改configMap的配置文件,将端口修改为88
vim pod-nginx.yaml
###
apiVersion: v1
kind: Pod
metadata:
name: nginx-2
spec:
containers:
- name: nginx-2
image: nginx
volumeMounts:
- name: nginxconf
mountPath: /etc/nginx/nginx.conf ##挂在哪
subPath: nginx.conf ###这是告诉他我这是子配置文件,不需要覆盖其他文件,只会代替原来的nginx.conf文件
volumes:
- name: nginxconf
configMap:
name: nginx-config
###
创建pod
kubectl apply -f pod.nginx.yaml
Downward API
用于在容器中获取 POD 的基本信息,kubernetes原生支持Downward API提供了两种方式用于将 POD 的信息注入到容器内部:
1.环境变量:用于单个变量,可以将 POD 信息直接注入容器内部。
2.Volume挂载:将 POD 信息生成为文件,直接挂载到容器内部中去。
1. 使用 fieldRef 可以声明使用:
spec.nodeName - 宿主机名字
status.hostIP - 宿主机 IP
metadata.name - Pod 的名字
metadata.namespace - Pod 的 Namespace
status.podIP - Pod 的 IP
spec.serviceAccountName - Pod 的 Service Account 的名字
metadata.uid - Pod 的 UID
metadata.labels[''] - 指定 的 Label 值
metadata.annotations[''] - 指定 的 Annotation 值
metadata.labels - Pod 的所有 Label
metadata.annotations - Pod 的所有 Annotation
(describe方式看不出来)
[root@k8s-master API]# kubectl get pod nginx -o yaml
apiVersion: v1
kind: Pod
metadata:
annotations:
cni.projectcalico.org/containerID: 20ae0d0b2b7509982df45b5a819fea638bd3e377ca9d712a76693c1c07910002
cni.projectcalico.org/podIP: 10.244.169.157/32
cni.projectcalico.org/podIPs: 10.244.169.157/32
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"labels":{"name":"nginx"},"name":"nginx","namespace":"default"},"spec":{"containers":[{"image":"nginx","name":"nginx"}]}}
creationTimestamp: "2023-12-16T07:20:34Z"
labels:
name: nginx
name: nginx
namespace: default
resourceVersion: "233455"
uid: 93de15ad-7b09-4a07-b928-639d87ff3185
spec:
containers:
- image: nginx
imagePullPolicy: Always
name: nginx
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: kube-api-access-cjkl4
readOnly: true
dnsPolicy: ClusterFirst
[root@k8s-master API]# vim test-env-pod.yml
---
apiVersion: v1
kind: Pod
metadata:
name: test-env-pod
namespace: kube-system
spec:
containers:
- name: test-env-pod
image: daocloud.io/library/nginx
env:
- name: POD_NAME #第一个环境变量的名字
valueFrom: #使用valueFrom方式设置
fieldRef: #关联一个字段metadata.name
fieldPath: metadata.name #这个字段从当前运行的pod详细信息查看
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
注意: POD 的 name 和 namespace 属于元数据,是在 POD 创建之前就已经定下来了的,所以使用 metadata 获取就可以了,但是对于 POD 的 IP 则不一样,因为POD IP 是不固定的,POD 重建了就变了,它属于状态数据,所以使用 status 去获取。
创建POD
[root@k8s-master API]# kubectl apply -f test-env-pod.yml
pod/test-env-pod created
查看
[root@k8s-master API]# kubectl exec -it test-env-pod /bin/bash -n kube-system
root@test-env-pod:/# env | grep POD
POD_NAME=test-env-pod
POD_NAMESPACE=kube-system
POD_IP=10.244.36.120
root@test-env-pod:/#
通过Downward API将 POD 的 Label、等信息通过 Volume 以文件的形式挂载到容器的某个文件中去,然后在容器中打印出该文件的值来验证。
[root@k8s-master API]# vim test-volume-pod.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: test-volume-pod
namespace: kube-system
labels:
k8s-app: test-volume
node-env: test
spec:
containers:
- name: test-volume-pod-container
image: daocloud.io/library/nginx
volumeMounts:
- name: podinfo
mountPath: /etc/podinfo
volumes:
- name: podinfo
downwardAPI:
items:
- path: "labels"
fieldRef:
fieldPath: metadata.labels
创建上面的POD,查看
[root@k8s-master API]# kubectl apply -f test-volume-pod.yaml pod/test-volume-pod created
[root@k8s-master API]# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
test-env-pod 1/1 Running 1 (24m ago) 25m
test-volume-pod 1/1 Running 0 15m
[root@k8s-master API]# kubectl exec -it test-volume-pod /bin/bash -n kube-system
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@test-volume-pod:/# cat /etc/podinfo/labels
k8s-app="test-volume"
node-env="test"root@test-volume-pod:/#
Secret、ConfigMap,以及 Downward API 这三种 Projected Volume 定义的信息,大多还可以通过环境变量的方式出现在容器里。但是,通过环境变量获取这些信息的方式,不具备自动更新的能力。一般情况下,建议使用 Volume 文件的方式获取这些信息。