标签:Kubernetes的Secret k8s kubernetes Secret
Kubernetes的Secret#Opaque类型的数据是一个map类型,要求value是base64编码格式:
##创建admin账户
echo -n "admin" | base64
YWRtaW4=
echo -n "1f2d1e2e67df" | base64
MWYyZDFlMmU2N2Rm
#创建secret.yaml
cat >> secrets.yml << EOF
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
password: MWYyZDFlMmU2N2Rm
username: YWRtaW4=
#创建secret
kubectl create -f secrets.yml
#查看secret运行状态
kubectl get secret --all-namespaces
NAMESPACE NAME TYPE DATA AGE
default mysecret Opaque 2 10m
#使用secret
##创建好secret之后,有两种方式来使用它:
(1)以Volume方式
(2)以环境变量方式
#创建nginx.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
name: wtf
name: mypod
spec:
volumes:
- name: secrets
secret:
secretName: mysecret
containers:
- image: nginx:1.7.9
name: nginx
volumeMounts:
- name: secrets
mountPath: "/etc/secrets"
readOnly: true
ports:
- name: cp
containerPort: 5432
hostPort: 5432
##说明:这样就可以通过文件的方式挂载到容器内,在/etc/secrets目录下回
生成这个文件。
#查看Pod运行状态
kubectl get pod
NAME READY STATUS RESTARTS AGE
mypod 1/1 Running 0 12s
#进入容器内部查看/etc/secrets目录下文件
kubectl exec -it mypod /bin/bash
root@mypod:/# cd /etc/secrets
root@mypod:/etc/secrets# ls -l
total 0
lrwxrwxrwx 1 root root 15 Jun 1 07:25 password -> ..data/password
lrwxrwxrwx 1 root root 15 Jun 1 07:25 username -> ..data/username
cat username
admin
cat password
1f2d1e2e67df
#创建wordpress.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: wordpress-deployment
spec:
replicas: 2
template:
metadata:
labels:
app: wordpress
spec:
containers:
- name: "wordpress"
image: "wordpress:latest"
ports:
- containerPort: 80
env:
- name: WORDPRESS_DB_USER
valueFrom:
secretKeyRef:
name: mysecret
key: username
- name: WORDPRESS_DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: password
#查看Pod运行状态
kubectl get po
NAME READY STATUS RESTARTS AGE
wordpress-deployment-6b569fbb7d-8qcpg 1/1 Running 0 2m
wordpress-deployment-6b569fbb7d-xwwkg 1/1 Running 0 2m
##说明:进入容器通过env命令,你将可以看到这两个环境变量被注入到容器内。
#进入容器内部查看环境变量
kubectl exec -it wordpress-deployment-694f4c79b4-cpsxw /bin/bash
root@wordpress-deployment-694f4c79b4-cpsxw:/var/www/html# env
WORDPRESS_DB_USER=admin
WORDPRESS_DB_PASSWORD=1f2d1e2e67df
#可以直接用kubectl命令来创建用于docker registry认证的secret:
kubectl create secret docker-registry myregistrykey --docker-server=DOCKER_REGISTRY_SERVER --docker-username=DOCKER_USER --docker-password=DOCKER_PASSWORD --docker-email=DOCKER_EMAIL
secret "myregistrykey" created
#也可以直接读取~/.docker/config.json的内容来创建:
cat ~/.docker/config.json | base64
cat > myregistrykey.yaml << EOF
apiVersion: v1
kind: Secret
metadata:
name: myregistrykey
data:
.dockerconfigjson: UmVhbGx5IHJlYWxseSByZWVlZWVlZWVlZWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGx5eXl5eXl5eXl5eXl5eXl5eXl5eSBsbGxsbGxsbGxsbGxsbG9vb29vb29vb29vb29vb29vb29vb29vb29vb25ubm5ubm5ubm5ubm5ubm5ubm5ubm5ubmdnZ2dnZ2dnZ2dnZ2dnZ2dnZ2cgYXV0aCBrZXlzCg==
type: kubernetes.io/dockerconfigjson
EOF
#创建
kubectl create -f myregistrykey.yaml
#在创建Pod的时候,通过imagePullSecrets来引用刚创建的myregistrykey:
apiVersion: v1
kind: Pod
metadata:
name: foo
spec:
containers:
- name: foo
image: janedoe/awesomeapp:v1
imagePullSecrets:
- name: myregistrykey
#Service Account用来访问Kubernetes API,由Kubernetes自动创建,并且会
自动挂载到Pod的/run/secrets/kubernetes.io/serviceaccount目录中。
kubectl run nginx --image nginx
deployment "nginx" created
kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-3137573019-md1u2 1/1 Running 0 13s
kubectl exec nginx-3137573019-md1u2 ls /run/secrets/kubernetes.io/serviceaccount
ca.crt
namespace
token
标签:Kubernetes的Secret k8s kubernetes Secret
原文地址:http://blog.51cto.com/wutengfei/2123231