BHOL405 - k8s应用开发部署 - 使用Secrets保存和使用密钥信息
在这几节实验中,我们将完成我们的 Hello Boathouse 应用的k8s部署过程,其中会涉及到一下k8s中的对象
- Pod
- Deployment
- Service
- Secret
- Namespace
这些内容基本上覆盖了我们k8s集群进行应用开发部署的主要对象。
01 - 实验准备
使用以下命令清理之前的部署:
kubectl delete -f kube-deploy/hello-boathouse-lb-service.yamlkubectl delete -f kube-deploy/hello-boathouse-nodeport-service.yamlkubectl delete -f kube-deploy/hello-boathouse-deployment.yaml
02 - 使用 Secrets 保存密钥信息
使用 secrets 我们可以将密钥信息保存在k8s集群中,并给到不同的pod使用。
使用 vscode 创建两个文件:
- hello-boathouse-secrets.yaml
- hello-boathouse-secrets-volumes.yaml
hello-boathouse-secrets.yaml
apiVersion: v1kind: Secretmetadata:name: db-secretstype: Opaquedata:username: cm9vdA==password: cGFzc3dvcmQ=
hello-boathouse-secrets-volumes.yaml
apiVersion: apps/v1kind: Deploymentmetadata:name: hello-boathouse-deploymentspec:replicas: 3selector:matchLabels:app: hello-boathousetemplate:metadata:labels:app: hello-boathousespec:containers:- name: hello-boathouseimage: registry.cn-hangzhou.aliyuncs.com/boathouse216/hello-boathouse:v1ports:- name: nodejs-portcontainerPort: 3000volumeMounts:- name: cred-volumemountPath: /etc/credsreadOnly: truevolumes:- name: cred-volumesecret:secretName: db-secrets
使用以下命令提交部署
## 提交部署kubectl apply -f kube-deploy/hello-boathouse-secretskubectl apply -f kube-deploy/hello-boathouse-secrets-volumes.yaml## 检查部署结果kubectl get secretskubectl get pods
使用以下命令进入其中一个pod查看 /etc/creds 内容
## 进入其中一个podkubectl exec {pod id} -it -- /bin/bashcd /etc/credscat usernamecat password
