LinuxЧÀÍÆ÷Çå¾²£ºÔõÑù±£»¤ÈÝÆ÷ÇéÐÎÖеÄÃô¸ÐÐÅÏ¢£¿
linuxЧÀÍÆ÷Çå¾²£ºÔõÑù±£»¤ÈÝÆ÷ÇéÐÎÖеÄÃô¸ÐÐÅÏ¢£¿
ÔÚµ±½ñ»¥ÁªÍøʱ´ú£¬Ð§ÀÍÆ÷Çå¾²ÎÊÌâ±äµÃÔ½À´Ô½Ö÷Òª¡£ÓÈÆäÊǹØÓÚʹÓÃÈÝÆ÷»¯ÊÖÒÕµÄЧÀÍÆ÷ÇéÐΣ¬±£»¤Ãô¸ÐÐÅÏ¢±äµÃ¸ü¾ßÌôÕ½ÐÔ¡£±¾ÎĽ«ÏÈÈÝһЩÔÚLinuxЧÀÍÆ÷Éϱ£»¤ÈÝÆ÷ÇéÐÎÖеÄÃô¸ÐÐÅÏ¢µÄ×î¼Ñʵ¼ù£¬²¢ÌṩһЩ´úÂëʾÀý£¬ÒÔ×ÊÖú¶ÁÕ߸üºÃµØÃ÷È·¡£
ʹÓÃÃÜÔ¿ÖÎÀíÆ÷
ÔÚÈÝÆ÷ÇéÐÎÖУ¬ÎªÁ˱£»¤Ãô¸ÐÐÅÏ¢£¬ÈçAPIÃÜÔ¿¡¢Êý¾Ý¿âÃÜÂëµÈ£¬¿ÉÒÔʹÓÃÃÜÔ¿ÖÎÀíÆ÷¡£ÃÜÔ¿ÖÎÀíÆ÷¿ÉÒÔ×ÊÖúÎÒÃÇÒÔÇå¾²µÄ·½·¨´æ´¢ºÍ»á¼ûÕâЩÃô¸ÐÐÅÏ¢¡£ÒÔÏÂÊÇÒ»¸öʹÓÃHashiCorpµÄVault×÷ΪÃÜÔ¿ÖÎÀíÆ÷µÄʾÀý´úÂ룺
# ×°ÖÃVault wget https://releases.hashicorp.com/vault/1.6.3/vault_1.6.3_linux_amd64.zip unzip vault_1.6.3_linux_amd64.zip sudo mv vault /usr/local/bin/ # Æô¶¯VaultЧÀÍÆ÷ vault server -dev # ½¨ÉèÒ»¸öVault secret vault kv put secret/myapp/api-key value=abc123 # ÔÚÈÝÆ÷ÖÐʹÓÃVault»ñÈ¡ÃÜÔ¿ vault kv get secret/myapp/api-key
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöʾÀýÖУ¬ÎÒÃÇʹÓÃVaultÔÚЧÀÍÆ÷ÉϽ¨ÉèÒ»¸öÃûΪmyappµÄÉñÃؿռ䣬²¢ÔÚÆäÖд洢ÁËÒ»¸öÃûΪapi-keyµÄÃô¸ÐÐÅÏ¢¡£ÎªÁËÔÚÈÝÆ÷ÖÐʹÓÃÕâ¸öÉñÃØÐÅÏ¢£¬ÎÒÃÇÐèҪװÖÃVault£¬²¢Ê¹ÓÃAPIÇëÇóÀ´»ñÈ¡Ëü¡£
ʹÓÃÇéÐαäÁ¿
ÔÚÈÝÆ÷ÇéÐÎÖУ¬¿ÉÒÔʹÓÃÇéÐαäÁ¿À´´æ´¢Ãô¸ÐÐÅÏ¢£¬²¢ÔÚÈÝÆ÷Æô¶¯Ê±½«Æä×¢Èëµ½ÈÝÆ÷ÖС£ÒÔÏÂÊÇÒ»¸öʹÓÃDockerµÄʾÀý´úÂ룺
# ½¨ÉèÒ»¸ö°üÀ¨Ãô¸ÐÐÅÏ¢µÄ.envÎļþ echo "API_KEY=abc123" > /path/to/myapp/.env # ÔÚDockerfileÖн«.envÎļþ¸´ÖƵ½ÈÝÆ÷ÖÐ COPY .env /app # ÔÚDockerfileÖнç˵һ¸öÇéÐαäÁ¿ ENV API_KEY $API_KEY # ÔÚÈÝÆ÷ÖÐʹÓÃÇéÐαäÁ¿ echo $API_KEY
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöʾÀýÖУ¬ÎÒÃǽ«Ãô¸ÐÐÅÏ¢´æ´¢ÔÚÒ»¸öÃûΪ.envµÄÎļþÖУ¬²¢ÔÚDockerfileÖн«Æ临ÖƵ½ÈÝÆ÷ÖС£È»ºó£¬ÎÒÃÇʹÓÃENVÖ¸ÁîÔÚÈÝÆ÷Öнç˵һ¸öÃûΪAPI_KEYµÄÇéÐαäÁ¿£¬²¢ÔÚÈÝÆ÷ÖÐʹÓøÃÇéÐαäÁ¿¡£
ÏÞÖÆÈÝÆ÷ȨÏÞ
ΪÁ˱£»¤ÈÝÆ÷ÇéÐÎÖеÄÃô¸ÐÐÅÏ¢£¬ÎÒÃÇ»¹¿ÉÒÔÏÞÖÆÈÝÆ÷µÄȨÏÞ¡£ÒÔÏÂÊÇÒ»¸öʹÓÃDockerµÄʾÀý´úÂ룺
# ÔÚDockerfileÖÐÒÔ·ÇrootÓû§ÔËÐÐÈÝÆ÷ USER myuser # ÔÚDockerfileÖÐÉèÖÃÈÝÆ÷µÄÖ´ÐÐȨÏÞ RUN chmod 500 /app/run.sh
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöʾÀýÖУ¬ÎÒÃÇʹÓÃUSERÖ¸ÁîÔÚDockerfileÖÐÉèÖÃÈÝÆ÷ÒÔ·ÇrootÓû§ÔËÐС£Õâ¿ÉÒÔ×ÊÖúïÔÌDZÔÚµÄÇ徲Σº¦¡£ÁíÍ⣬ÎÒÃÇʹÓÃRUNÖ¸ÁîÉèÖÃÈÝÆ÷ÖÐij¸ö¾ç±¾ÎļþµÄÖ´ÐÐȨÏÞ£¬ÒÔÈ·±£Ö»ÓÐÌض¨µÄÓû§¿ÉÒÔÖ´ÐиÃÎļþ¡£
×ÛÉÏËùÊö£¬Ð§ÀÍÆ÷Çå¾²¹ØÓÚÈÝÆ÷ÇéÐÎÖеÄÃô¸ÐÐÅÏ¢ÖÁ¹ØÖ÷Òª¡£Í¨¹ýʹÓÃÃÜÔ¿ÖÎÀíÆ÷¡¢ÇéÐαäÁ¿ºÍÏÞÖÆÈÝÆ÷ȨÏÞµÈ×î¼Ñʵ¼ù£¬ÎÒÃÇ¿ÉÒÔ¸üºÃµØ±£»¤ÈÝÆ÷ÇéÐÎÖеÄÃô¸ÐÐÅÏ¢¡£Ï£Íû±¾ÎÄÌṩµÄ´úÂëʾÀýÄÜ×ÊÖú¶ÁÕ߸üºÃµØÃ÷È·ºÍÓ¦ÓÃÕâЩÇå¾²²½·¥£¬´Ó¶øÈ·±£Ð§ÀÍÆ÷µÄÇå¾²ÐÔ¡£
ÒÔÉϾÍÊÇLinuxЧÀÍÆ÷Çå¾²£ºÔõÑù±£»¤ÈÝÆ÷ÇéÐÎÖеÄÃô¸ÐÐÅÏ¢£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡