大家好,我是喬克,一個愛折騰的運維工程,一個睡覺都被自己醜醒的雲原生愛好者。
作者:喬克
公衆号:運維開發故事
不論什麼樣的應用,基本都有配置檔案,在企業中,大部分會用到配置中心,比如apollo、nacos等,也有一些公司直接使用Kubernetes自帶的配置管理,主要有:
- Secret
- ConfigMap
Secret
如果把配置資訊儲存在Secret中,其會被加密存放到Etcd中,Pod可以通過以下兩種種方式使用它:
- 通過環境變量的方式
- 通過挂載的方式
- 指定拉取鏡像的Secret
一般情況下,通過Secret儲存的配置資訊都是敏感資訊,比如資料庫的賬号密碼、認證服務的賬号密碼等,且Secret不宜過大,因為如果使用大的Secret,則将大量占用API Server和kubelet的記憶體。
建立Secret
建立Secret的方式主要有兩種:
- 使用YAML檔案建立
- 使用kubectl指令建立
使用YAML檔案建立
使用YAML檔案建立,就要熟悉Secret的配置詳情,可以通過
kubectl explain secret
去檢視。其主要字段有apiVersion,data,kind,metadata,type。
比如建立一個簡單的Secret如下:
apiVersion: v1
kind: Secret
metadata:
name: my-secret-volume
type: Opaque
data:
user: cm9vdA==
password: UEBzc1cwcmQ=
其中apiVersion、kind和metadata是常用字段,這裡就不贅述了。type表示secret的類型,主要有以下幾種:
- Qpaque:可以定義任意資料
- kubernetes.io/service-account-token:配置ServiceAccount Token
- kubernetes.io/dockercfg:配置docker認證檔案
- kubernetes.io/dockerconfigjson:配置docker認證檔案
- kubernetes.io/basic-auth:配置基礎認證
- kubernetes.io/ssh-auth:配置ssh認證
- kubernetes.io/tls:配置TLS證書
- bootstrap.kubernetes.io/token:配置bootstrap token
如果在建立Secret的時候沒有指定類型,預設使用Qpaque類型。另外data的資料的值是需要base64轉碼。
使用kubectl指令建立
在使用kubectl建立的時候,如果不熟悉子指令資訊,可以通過kubectl explain secret檢視。
我們使用以下指令建立一個Secret:
$ kubectl create secret generic secret-auth-test --from-literal=username=joker --from-literal=password=123
建立完成後,可以看到username和password的值被自動加密了,如下:
$ kubectl get secrets secret-auth-test -oyaml
apiVersion: v1
data:
password: MTIz
username: am9rZXI=
kind: Secret
metadata:
creationTimestamp: "2022-07-25T07:44:18Z"
name: secret-auth-test
namespace: default
resourceVersion: "652834"
uid: ff1b756a-6b38-4b68-a47c-c51988729b68
type: Opaque
除了直接在指令行輸入資料,還可以從檔案建立,如下:
$ echo -n 'admin' > ./username.txt
$ echo -n '1f2d1e2e67df' > ./password.txt
然後通過--from-file引入檔案,如下:
$ kubectl create secret generic db-user-pass \
--from-file=./username.txt \
--from-file=./password.txt
建立後的secret值都是加密的,如果要擷取明文資訊,通過以下指令即可:
$ kubectl get secret db-user-pass -o jsonpath='{.data.password}' | base64 --decode
預設情況下,secret是使用base64加密的,是以解密可以直接使用base64解密。
使用Secret
Secret隻是一個靜态資源,最終,我們是想使用它,在實際中,主要通過以下方式使用:
- 通過環境變量的方式
- 通過挂載的方式
- 指定拉取鏡像的Secret
我們在上面建立了
secret-auth-test
的Secret,下面分别使用以上三種方式進行使用。
通過環境變量使用Secret
在Pod的對象中,有spec.containers.env.valueFrom.secretKeyRef字段,該字段可以用來引用Secret字段,如下:
apiVersion: v1
kind: Pod
metadata:
name: secret-env-pod
spec:
containers:
- name: mycontainer
image: redis
env:
- name: SECRET_USERNAME
valueFrom:
secretKeyRef:
name: secret-auth-test
key: username
- name: SECRET_PASSWORD
valueFrom:
secretKeyRef:
name: secret-auth-test
key: password
這樣就會把Secret裡的資訊注入到容器環境變量裡,應用可以直接通過讀取環境變量來使用。
通過挂載的方式使用Secret
可以使用挂載的方式,将Secret以檔案的形式挂載到容器中,如下:
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: mypod
image: redis
volumeMounts:
- name: foo
mountPath: "/etc/foo"
readOnly: true
volumes:
- name: foo
secret:
secretName: secret-auth-test
這樣就會把資料挂載到/etc/foo這個目錄裡,如下:
$ kubectl exec -it mypod -- /bin/sh
# ls -l /etc/foo
total 0
lrwxrwxrwx 1 root root 15 Jul 25 08:30 password -> ..data/password
lrwxrwxrwx 1 root root 15 Jul 25 08:30 username -> ..data/username
如果Secret裡有多個鍵值,還可以隻挂載某一個資料,如下:
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: mypod
image: redis
volumeMounts:
- name: foo
mountPath: "/etc/foo"
readOnly: true
volumes:
- name: foo
secret:
secretName: secret-auth-test
items:
- key: username
path: my-group/my-username
上面指定volumes.secret.items.path用來指定username的子目錄,如下:
$ kubectl exec -it mypod-password -- /bin/bash
root@mypod-password:/data# cat /etc/foo/my-group/my-username
joker
除此之外,還可以指定權限,如下:
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: mypod
image: redis
volumeMounts:
- name: foo
mountPath: "/etc/foo"
volumes:
- name: foo
secret:
secretName: secret-auth-test
defaultMode: 0400
然後可以看到被挂載的Secret的權限如下:
$ kubectl exec -it mypod-permision -- /bin/bash
root@mypod-permision:/etc/foo# ls -l
total 0
lrwxrwxrwx 1 root root 15 Jul 25 08:38 password -> ..data/password
lrwxrwxrwx 1 root root 15 Jul 25 08:38 username -> ..data/username
root@mypod-permision:/etc/foo# ls ..data/password -l
-r-------- 1 root root 3 Jul 25 08:38 ..data/password
注意:我們進/etc/foo目錄直接使用ls -l檢視到的權限是777,但是仔細的人可以發現其實質是一個連結檔案,我們真正要看的權限是被連結的檔案,也就是上面的..data/password。
在拉取鏡像的時候使用Secret
我們在前面列舉了很多YAML檔案,都沒有配置imagePullSecret,主要是那些鏡像都是Dockerhub官方的鏡像,對外是公開的。
然而,在實際的生産中,不會将自己公司的鏡像對外公開,這非常的不安全。如果鏡像倉庫加密了,在下載下傳鏡像的時候要docker login,在Kubernetes中,也免不了該操作。
為此,Kubernetes提供了imagePullSecret字段,該字段用來指定拉取鏡像的Secret,這個Secret會儲存鏡像倉庫的認證資訊。
(1)首先建立鏡像認證資訊的Secret
kubectl create secret \
docker-registry pull-registry-secret \
--docker-server=registry.test.cn \
--docker-username=ops \
--docker-password=ops123123 \
(2)在Pod中使用
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
imagePullSecrets:
- name: pull-registry-secret
containers:
- name: mypod
image: redis
volumeMounts:
- name: foo
mountPath: "/etc/foo"
volumes:
- name: foo
secret:
secretName: secret-auth-test
defaultMode: 0400
這樣就可以拉取私有倉庫裡的鏡像了。
總結
綜上,我們可以通過Secret保管其他系統的敏感資訊(比如資料庫的使用者名和密碼),并以Mount的方式将Secret挂載到Container中,然後通過通路目錄中檔案的方式擷取該敏感資訊。當Pod被API Server建立時,API Server不會校驗該Pod引用的Secret是否存在。一旦這個Pod被排程,則kubelet将試着擷取Secret的值。如果Secret不存在或暫時無法連接配接到API Server,則kubelet按一定的時間間隔定期重試擷取該Secret,并發送一個Event來解釋Pod沒有啟動的原因。一旦Secret被Pod擷取,則kubelet将建立并挂載包含Secret的Volume。隻有所有Volume都挂載成功,Pod中的Container才會被啟動。在kubelet啟動Pod中的Container後,Container中和Secret相關的Volume将不會被改變,即使Secret本身被修改。為了使用更新後的Secret,必須删除舊Pod,并重新建立一個新Pod。
ConfigMap
ConfigMap和Serect類似,不同之處在于ConfigMap儲存的資料資訊是不需要加密的,比如一些應用的配置資訊,其他的用法和Secret一樣。
建立ConfigMap
同樣,我們可以使用兩種方式來建立ConfigMap:
- 通過指令行方式,也就是kubectl create configmap;
- 通過YAML檔案方式;
通過指令建立ConfigMap
如果不熟悉ConfigMap對象的字段,可以通過kubectl explain configmap來檢視,如果想檢視建立configmap的示例,可以通過kubectl create configmap -h檢視,如下:
Examples:
# Create a new config map named my-config based on folder bar
kubectl create configmap my-config --from-file=path/to/bar
# Create a new config map named my-config with specified keys instead of file basenames on disk
kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt
# Create a new config map named my-config with key1=config1 and key2=config2
kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2
# Create a new config map named my-config from the key=value pairs in the file
kubectl create configmap my-config --from-file=path/to/bar
# Create a new config map named my-config from an env file
kubectl create configmap my-config --from-env-file=path/to/foo.env --from-env-file=path/to/bar.env
從上面可以看出,建立ConfigMap可以從給定一個目錄來建立。例如,我們定義了如下一些配置檔案:
$ mkdir configmap-demo
$ cd configmap-demo
$ ll
total 8
-rw-r--r-- 1 root root 25 Sep 6 17:07 mysqld.conf
-rw-r--r-- 1 root root 25 Sep 6 17:07 redis.conf
$ cat mysqld.conf
host=127.0.0.1
port=3306
$ cat redis.conf
host=127.0.0.1
port=6379
然後使用一下指令來進行建立:
$ kubectl create configmap my-configmap --from-file=../configmap-demo/
然後通過一下指令檢視建立完的configmap:
$ kubectl get cm
NAME DATA AGE
kube-root-ca.crt 1 21d
my-configmap 2 9s
$ kubectl describe cm my-configmap
Name: my-configmap
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
mysqld.conf:
----
host=127.0.0.1
port=3306
redis.conf:
----
host=127.0.0.1
port=6379
BinaryData
====
Events: <none>
我們可以看到兩個key對應的是檔案的名字,value對應的是檔案的内容。如果要看鍵值的話可以通過如下指令檢視:
$ kubectl get configmap my-configmap -o yaml
apiVersion: v1
data:
mysqld.conf: |
host=127.0.0.1
port=3306
redis.conf: |
host=127.0.0.1
port=6379
kind: ConfigMap
metadata:
creationTimestamp: "2022-07-25T09:20:43Z"
name: my-configmap
namespace: default
resourceVersion: "667706"
uid: 46cb52e9-0936-4934-9628-ac20efcfd893
當然,我們還可以通過檔案來建立一個configmap,比如我們定義一個如下的配置檔案:
$ cat nginx.conf
user nobody;
worker_processes 1;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip on;
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;
}
}
}
然後通過如下指令建立一個nginx的configmap:
$ kubectl create configmap nginx-configmap --from-file=nginx.conf
檢視建立後的資訊:
$ kubectl get configmap nginx-configmap -o yaml
apiVersion: v1
data:
nginx.conf: |
user nobody;
worker_processes 1;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip on;
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;
}
}
}
kind: ConfigMap
metadata:
creationTimestamp: "2022-07-25T09:24:29Z"
name: nginx-configmap
namespace: default
resourceVersion: "668283"
uid: a025da28-6817-4605-8daf-375b676282c1
注:在一條指令中--from-file可以指定多次。
另外,通過幫助文檔我們可以看到我們還可以直接使用字元串進行建立,通過--from-literal參數傳遞配置資訊,同樣的,這個參數可以使用多次,格式如下:
$ kubectl create configmap my-cm-daemo --from-literal=db.host=localhost --from-literal=db.port=3306
通過YAML建立ConfigMap
通過YAML檔案建立就比較簡單,我們可以參考上面輸出的yaml資訊,比如定義如下一個YAML檔案:
apiVersion: v1
kind: ConfigMap
metadata:
name: my-cm-daemon2
labels:
app: cm-daemon
data:
redis.conf: |
host=127.0.0.1
port=6379
然後建立即可。
使用ConfigMap
ConfigMap中的配置資料可以通過如下方式進行使用:
- 設定環境變量值
- 在資料卷中建立config檔案
通過環境變量使用ConfigMap
我們直接通過在pod.spec.containers.env.valueFrom.configMapKeyRef中引用ConfigMap即可,如下:
apiVersion: v1
kind: Pod
metadata:
name: env-configmap
labels:
app: env-configmap-mysql
spec:
containers:
- name: test-configmap
image: busybox
command:
- "/bin/sh"
- "-c"
- "env"
env:
- name: DB_HOST
valueFrom:
configMapKeyRef:
name: my-cm-daemo
key: db.host
- name: DB_PORT
valueFrom:
configMapKeyRef:
name: my-cm-daemo
key: db.port
envFrom:
- configMapRef:
name: my-cm-daemo
建立後,可以通過日志檢視環境變量輸出,如下:
$ kubectl logs env-configmap | grep DB
DB_PORT=3306
DB_HOST=localhost
通過資料卷使用ConfigMap
基本原理和Secret一樣。
在這裡,通過指定pod.spec.volumes.configMap.name來指定ConfigMap,然後挂載到容器裡,如下:
apiVersion: v1
kind: Pod
metadata:
name: volume-configmap-test
spec:
containers:
- name: volume-configmap-test
image: busybox
command: [ "/bin/sh", "-c", "cat /etc/config/redis.conf" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: my-configmap
我們可以通過日志檢視ConfigMap是否挂載進去了。
$ kubectl logs volume-configmap-test
host=127.0.0.1
port=6379
apiVersion: v1
kind: Pod
metadata:
name: volume-path-configmap
spec:
containers:
- name: volume-path-configmap-test
image: busybox
command: [ "/bin/sh","-c","cat /etc/config/path/to/msyqld.conf" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: my-configmap
items:
- key: mysqld.conf
path: path/to/msyqld.conf