k8s之Secret

Create Generic Secret

  • 创建Generic Secret
root@master:~# kubectl create secret generic mysql-secret --from-literal=root.pass='lili' --from-literal=wp.user='wpuser' --from-literal=wp.pass='WPp@ss' --from-literal=db.name='wpdb' --dry-run=client  -oyaml
apiVersion: v1
data:
  db.name: d3BkYg==
  root.pass: bGlsaQ==
  wp.pass: V1BwQHNz
  wp.user: d3B1c2Vy
kind: Secret
metadata:
  creationTimestamp: null
  name: mysql-secret

root@master:~# kubectl create secret generic mysql-secret --from-literal=root.pass='lili' --from-literal=wp.user='wpuser' --from-literal=wp.pass='WPp@ss' --from-literal=db.name='wpdb'
secret/mysql-secret created

root@master:~# kubectl get secret
NAME           TYPE     DATA   AGE
mysql-secret   Opaque   4      4s
  • Pod基于环境变量引用Secret
root@master:~# cat mysql-secret.yaml
apiVersion: v1
kind: Pod
metadata:
  name: mysql
spec:
  containers:
  - name: mysql
    image: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/mysql:5.7
    env:
    - name: MYSQL_ROOT_PASSWORD                             # mysql镜像支持的环境变量
      valueFrom:
        secretKeyRef:                                       # 引用secret的键的值作为密码
          name: mysql-secret
          key: root.pass                                    # 以root.pass作为键,引用它的值
          optional: false

root@master:~# kubectl apply -f mysql-secret.yaml
pod/mysql created

root@master:~# kubectl get pod -owide
NAME    READY   STATUS    RESTARTS   AGE   IP            NODE           NOMINATED NODE   READINESS GATES
mysql   1/1     Running   0          12s   10.244.1.26   jgswy-node02   <none>           <none>
  • 验证
root@master:~# kubectl exec -it mysql -- /bin/bash

bash-4.2# printenv
HOSTNAME=mysql
TERM=xterm
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT=tcp://10.96.0.1:443
MYSQL_VERSION=5.7.44-1.el7
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_HOST=10.96.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
MYSQL_SHELL_VERSION=8.0.35-1.el7
SHLVL=1
HOME=/root
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_SERVICE_PORT_HTTPS=443
MYSQL_MAJOR=5.7
GOSU_VERSION=1.16
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
MYSQL_ROOT_PASSWORD=lili                                        #注入的环境变量
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
_=/usr/bin/printenv

bash-4.2# mysql -uroot -plili  -h127.0.0.1
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.44 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Create a TLS secret

  • 准备证书
root@jmaster:/data/learning-k8s/examples/configmaps_and_secrets# ls
certs.d                   configmaps-volume-demo.yaml  nginx-ssl-conf.d     secret-nginx-certs.yaml  secrets-volume-demo.yaml
configmap-nginx-cfg.yaml  downwardapi-demo.yaml        projected-demo.yaml  secrets-demo.yaml
configmaps-env-demo.yaml  nginx-conf.d                 secret-mysql.yaml    secrets-env-demo.yaml

root@master:/data/learning-k8s/examples/configmaps_and_secrets# ls certs.d/
nginx.crt  nginx.key
  • 创建seceret
root@master:/data/learning-k8s/examples/configmaps_and_secrets# kubectl create secret tls  nginx-tls --cert=./certs.d/nginx.crt --key=./certs.d/nginx.key -oyaml
apiVersion: v1
data:
  tls.crt: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURsVENDQW4yZ0F3SUJBZ0lVR2Zya05FeGZiS2N5Yy9LYkpLUXJ5MzRTcHI0d0RRWUpLb1pJaHZjTkFRRUwKQlFBd1dqRUxNQWtHQTFVRUJoTUNRMDR4RURBT0JnTlZCQWdNQjBKbGFXcHBibWN4RURBT0JnTlZCQWNNQjBKbAphV3BwYm1jeER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Tk00NHVPUUN6YnpaeTkwVCt4aEkvMUgrCnhwQlpSK3NSRzJOTjE4d1VCUW9wQVFLQmdFdnNHWDdiRytmUTJ3Z3Iwa2NZd0NML2ZvQXdPaGR2elZpaEltcUkKNmlxOFh1ejhUOWZibWNYbHFoTVVyZnpvNU5JZmdpUlBGL0RCSmwwUENWbXQ0RGxTVkpxamxJa0xDdnhqZmhiSQo3blBQZEI3YjlyTzQ5dEVvZHRzMWlJYWUzUXBwRys5L09pd055aXdRZ0VNVTV4Mzc4Yzk4dmJqWHVBVWVrT3c0CmNZeXRBb0dBTEtNazBwMUUrZHJtVzVuS3VMa2dyNDYzbmtlenBld2Y4eVJ2UHZQcUo4UkdobTBGblphOW1mQ0YKd1JCMDh1MkZ6YTRHSldEY0p2VG9kV3NtSmpCVHRMQ0wzaFlCSmNNQXNVNEdEbXBCZDdQMVBQQ1ZCb0JMeVJjbApyK2VTOFJzZTRwTERjOWdpQk9ZSGp4Q2hsVktSdVhESmh2V2k0azZVNGY5bStBNmp4STg9Ci0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==
kind: Secret
metadata:
  creationTimestamp: "2024-09-29T07:34:46Z"
  name: nginx-tls
  namespace: default
  resourceVersion: "3323132"
  uid: 9f5a998d-1d30-4746-9dfc-6ac16483f237
type: kubernetes.io/tls

root@master:/data/learning-k8s/examples/configmaps_and_secrets# kubectl get secret
NAME           TYPE                DATA   AGE
mysql-secret   Opaque              4      5h18m
nginx-tls      kubernetes.io/tls   2      74s
  • Pod通过卷挂载证书
root@master:~# cat nginx-tls.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-tls
spec:
  containers:
  - name: nginx
    image: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx
    volumeMounts:
    - name: certs
      mountPath: /etc/nginx/certs.d/
  volumes:
  - name: certs
    secret:
      secretName: nginx-tls
      optional: false

root@master:~# kubectl apply -f nginx-tls.yaml -oyaml


root@master:~# kubectl get pod -owide
NAME        READY   STATUS    RESTARTS   AGE    IP            NODE           NOMINATED NODE   READINESS GATES
mysql       1/1     Running   0          122m   10.244.1.26   jgswy-node02   <none>           <none>
nginx-tls   1/1     Running   0          14s    10.244.1.64   jgswy-node02   <none>           <none>
  • 验证
root@master:~# kubectl exec -it nginx-tls -- /bin/sh
# pwd
/etc/nginx/certs.d
# ls
tls.crt  tls.key

Create docker-registry secret

  • 创建docker-registry secret

当启动的Pod需要到私有镜像仓库中下载镜像的时候,需要提供仓库的账号密码等信息
可以通过配置imagePullSecrets来指定认证加载的secret,kubelet下载镜像的时候会自动加载secret中的信息。

root@master:~# kubectl create secret docker-registry dockerhub-secret --docker-username=‘admin’ --docker-password='P@ssw0rd' --docker-email='666163.com' -oyaml
apiVersion: v1
data:
  .dockerconfigjson: eyJhdXRocyI6eyJodHRwczovL2luZGV4LmRvY2tlci5pby92MS8iOnsidXNlcm5hbWUiOiLigJhhZG1pbuKAmSIsInBhc3N3b3JkIjoiUEBzc3cwcmQiLCJlbWFpbCI6IjY2NjE2My5jb20iLCJhdXRoIjoiNG9DWVlXUnRhVzdpZ0prNlVFQnpjM2N3Y21RPSJ9fX0=
kind: Secret
metadata:
  creationTimestamp: "2024-09-29T08:56:15Z"
  name: dockerhub-secret
  namespace: default
  resourceVersion: "3332988"
  uid: ef39c4ad-ce3e-4c8d-a1ee-300d7d374633
type: kubernetes.io/dockerconfigjson

root@master:~# kubectl get secret
NAME               TYPE                             DATA   AGE
dockerhub-secret   kubernetes.io/dockerconfigjson   1      6s
mysql-secret       Opaque                           4      6h38m
nginx-tls          kubernetes.io/tls                2      81m
root@master:~# echo "eyJhdXRocyI6eyJodHRwczovL2luZGV4LmRvY2tlci5pby92MS8iOnsidXNlcm5hbWUiOiLigJhhZG1pbuKAmSIsInBhc3N3b3JkIjoiUEBzc3cwcmQiLCJlbWFpbCI6IjY2NjE2My5jb20iLCJhdXRoIjoiNG9DWVlXUnRhVzdpZ0prNlVFQnpjM2N3Y21RPSJ9fX0=" | base64 -d | jq .
{
  "auths": {
    "https://index.docker.io/v1/": {
      "username": "‘admin’",
      "password": "P@ssw0rd",
      "email": "666163.com",
      "auth": "4oCYYWRtaW7igJk6UEBzc3cwcmQ="
    }
  }
}

配置带tls证书的Nginx虚拟主机

  • 准备nginx配置文件以及证书
root@master:s# ls nginx-conf.d/
myserver.conf  myserver-gzip.cfg  myserver-status.cfg
root@master:# ls nginx-ssl-conf.d/
myserver.conf  myserver-gzip.cfg  myserver-status.cfg


root@jgswy-master:/data/learning-k8s/examples/configmaps_and_secrets# cat nginx-conf.d/myserver.conf
server {
    listen 8080;
    server_name www.ik8s.io;

    include /etc/nginx/conf.d/myserver-*.cfg;

    location / {
        root /usr/share/nginx/html;
    }
}
root@master:# cat nginx-ssl-conf.d/myserver.conf
server {
    listen 443 ssl;
    server_name www.ik8s.io;

    ssl_certificate /etc/nginx/certs/tls.crt;                              # 证书文件目录
    ssl_certificate_key /etc/nginx/certs/tls.key;

    ssl_session_timeout 5m;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;

    include /etc/nginx/conf.d/myserver-*.cfg;

    location / {
        root /usr/share/nginx/html;
    }
}

server {
    listen 80;
    server_name www.ilinux.io;
    return 301 https://$host$request_uri;
}
  • 创建nginx配置文件configmap
root@master:# kubectl create configmap nginx-sslvhosts-confs --from-file=./nginx-ssl-conf.d/
configmap/nginx-sslvhosts-confs created

root@jgswy-master:/data/learning-k8s/examples/configmaps_and_secrets# kubectl get cm
NAME                    DATA   AGE
kube-root-ca.crt        1      20d
nginx-sslvhosts-confs   3      22s
  • 创建secret 证书文件
root@master:# cat secret-nginx-certs.yaml
apiVersion: v1
data:
  tls.crt: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURsVENDQW4yZ0F3SUJBZ0lVR2Zya05FeGZiS2N5Yy9LYkpLU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3eDlwVVNWSGhrcTl6WG1OSkZVZEJLdnlvU2Nla0VIWWttbTdsMm1XT2VLWnUrSEVlbDFLdm15M05STgo5TFQ1OGk0WU9KeEdWczdUdlhKS0pCb1lyNjIwMDh6K2J3Z3BmTnJYTk00NHVPUUN6YnpaeTkwVCt4aEkvMUgrCnhwQlpSK3NSRzJOTjE4d1VCUW9wQVFLQmdFdnNHWDdiRytmUTJ3Z3Iwa2NZd0NML2ZvQXdPaGR2elZpaEltcUkKNmlxOFh1ejhUOWZibWNYbHFoTVVyZnpvNU5JZmdpUlBGL0RCSmwwUENWbXQ0RGxTVkpxamxJa0xDdnhqZmhiSQo3blBQZEI3YjlyTzQ5dEVvZHRzMWlJYWUzUXBwRys5L09pd055aXdRZ0VNVTV4Mzc4Yzk4dmJqWHVBVWVrT3c0CmNZeXRBb0dBTEtNazBwMUUrZHJtVzVuS3VMa2dyNDYzbmtlenBld2Y4eVJ2UHZQcUo4UkdobTBGblphOW1mQ0YKd1JCMDh1MkZ6YTRHSldEY0p2VG9kV3NtSmpCVHRMQ0wzaFlCSmNNQXNVNEdEbXBCZDdQMVBQQ1ZCb0JMeVJjbApyK2VTOFJzZTRwTERjOWdpQk9ZSGp4Q2hsVktSdVhESmh2V2k0azZVNGY5bStBNmp4STg9Ci0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==
kind: Secret
metadata:
  creationTimestamp: null
  name: nginx-ssl-secret
type: kubernetes.io/tls
root@master:# kubectl apply -f secret-nginx-certs.yaml
secret/nginx-ssl-secret created
root@master:# kubectl get secret
NAME               TYPE                             DATA   AGE
dockerhub-secret   kubernetes.io/dockerconfigjson   1      18h
mysql-secret       Opaque                           4      24h
nginx-ssl-secret   kubernetes.io/tls                2      6s
nginx-tls          kubernetes.io/tls                2      19h
  • 创建Pod挂载配置文件和证书文件
root@master:# cat secrets-volume-demo.yaml
# Maintainer: MageEdu <mage@magedu.com>
# URL: http://www.magedu.com
---
apiVersion: v1
kind: Pod
metadata:
  name: secrets-volume-demo
  namespace: default
spec:
  containers:
  - image: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx 
    name: ngxserver
    volumeMounts:
    - name: nginxcerts
      mountPath: /etc/nginx/certs/
      readOnly: true
    - name: nginxconfs
      mountPath: /etc/nginx/conf.d/
      readOnly: true
  volumes:
  - name: nginxcerts
    secret:
      secretName: nginx-ssl-secret
  - name: nginxconfs
    configMap:
      name: nginx-sslvhosts-confs
      optional: false
root@master:# kubectl apply -f secrets-volume-demo.yaml
pod/secrets-volume-demo created
root@master:# kubectl get pod
NAME                  READY   STATUS    RESTARTS   AGE
mysql                 1/1     Running   0          20h
nginx-tls             1/1     Running   0          18h
secrets-volume-demo   1/1     Running   0          21s
posted @ 2024-10-09 14:21  *一炁化三清*  阅读(12)  评论(0编辑  收藏  举报