ÔõÑùʹÓÃk8sÓëgitlabÀ´ÊµÏÖ×Ô¶¯»¯°²ÅÅ
Ëæ×Å»¥ÁªÍøÐÐÒµµÄÈÕÒæÉú³¤£¬Èí¼þ¿ª·¢±äµÃÔ½À´Ô½ÖØ´ó¡£ÔõÑù¿ìËÙ¡¢¸ßЧ¡¢¿É¿¿µØÍê³ÉÈí¼þ°²ÅųÉΪÁË¿ª·¢ÕßÃÇÐèÒª½â¾öµÄÖ÷ÒªÎÊÌâ¡£¶ø×Ô¶¯»¯°²ÅÅÕýÊÇÕâÑùÒ»ÖÖ½â¾ö¼Æ»®£¬¹ØÓÚ¼ÓËÙÈí¼þÉÏÏßËÙÂÊ¡¢Ìá¸ß°²ÅÅÖÊÁ¿ÓÐ×Ų»¿ÉÌæ»»µÄ×÷Óᣱ¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃk8sÓëgitlabÀ´ÊµÏÖ×Ô¶¯»¯°²ÅÅ¡£
Ò»¡¢Ê²Ã´ÊÇk8s£¿
k8s£¨Kubernetes£©ÊÇGoogleÔÚ2014Ä꿪ԴµÄÈÝÆ÷±àÅÅƽ̨¡£Ëü¿ÉÒÔ½«ÈÝÆ÷±àÅź͵÷Àí×Ô¶¯»¯£¬×ÊÖú¿ª·¢Õß¿ìËÙ¡¢¸ßЧ¡¢¿É¿¿µØ°²ÅźÍÖÎÀíÈÝÆ÷»¯Ó¦ÓóÌÐò¡£KubernetesÖ§³Ö¶àÖÖÈÝÆ÷ÔËÐÐʱ£¬°üÀ¨DockerºÍRocketµÈ¡£Ëü»¹ÌṩÁË×Ô¶¯»¯°²ÅÅ¡¢×ÔÎÒÐÞ¸´¡¢ÉìËõµÈ¹¦Ð§¡£
¶þ¡¢Ê²Ã´ÊÇGitlab£¿
GitlabÊÇÒ»¸ö»ùÓÚgitµÄ´úÂëÍйܺÍÐ×÷ƽ̨£¬ÌṩÁË´úÂë¿ÍÕ»¡¢´úÂëÉóºË¡¢CI/CD×Ô¶¯»¯°²Åŵȿª·¢ÕßÐèÒªµÄÖÖÖÖ¹¤¾ß¡£Gitlab»¹ÌṩÁË¿ÉÊÓ»¯µÄ½çÃ棬ʹµÃÍŶÓÐ×÷Ô½·¢±ã½Ý¡£
Èý¡¢ÎªÊ²Ã´Òª½«k8sÓëGitlabÍŽáʹÓã¿
ÔڹŰåµÄÈí¼þ¿ª·¢ÖУ¬¿ª·¢Õßͨ³£ÐèÒªÊÖ¶¯Íê³ÉÈí¼þ°²ÅÅ£¬ÕâÖÖ·½·¨ÈÝÒ×·ºÆðÊÕÖ§¹ýʧ£¬²¢ÇÒ°²ÅÅËÙÂÊÂýÇÒЧÂʵÍÏ¡£½«k8sÓëGitlabÍŽáʹÓú󣬿ÉÒÔʵÏÖ×Ô¶¯»¯°²ÅźÍÒ»Á¬½»¸¶£¬´ó´óÌá¸ß°²ÅÅËÙÂʺÍЧÂÊ¡£
ËÄ¡¢ÔõÑùʵÏÖ×Ô¶¯»¯°²ÅÅ£¿
1¡¢½¨Éèk8s¼¯Èº
Ê×ÏÈÐèÒªÔÚһ̨ЧÀÍÆ÷ÉϽ¨Éèk8s¼¯Èº¡£¿ÉÒÔʹÓÃminikube¡¢kubeadmµÈ¹¤¾ßÀ´¿ìËÙ½¨ÉèÒ»¸ök8s¼¯Èº¡£½¨ÉèºÃ¼¯Èººó£¬ÐèÒªÔÚ¼¯ÈºÄÚ½¨ÉèÒ»¸önamespace£¬ÓÃÀ´°²ÅÅ×Ô¼ºµÄÓ¦Óá£
2¡¢½¨ÉèGitlabÏîÄ¿
ÔÚGitlabÉϽ¨ÉèÒ»¸öеÄÏîÄ¿¡£ÔÚÏîÄ¿Öн¨ÉèGitlab CI/CDÉèÖÃÎļþ¡£Õâ¸öÎļþ¸æËßGitlabÔõÑù¾ÙÐÐCI/CD×Ô¶¯»¯°²ÅÅ¡£Ê¾ÀýÉèÖÃÎļþÈçÏ£º
image: docker:latest services: - docker:dind stages: - build - deploy build: stage: build script: - docker build -t registry.gitlab.com/<group>/<project>:$CI_COMMIT_SHA . - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN registry.gitlab.com - docker push registry.gitlab.com/<group>/<project>:$CI_COMMIT_SHA tags: - docker deploy: stage: deploy script: - kubectl set image deployments/<deployment_name> <container_name>=registry.gitlab.com/<group>/<project>:$CI_COMMIT_SHA -n <namespace> tags: - k8s</namespace></project></group></container_name></deployment_name></project></group></project></group>
µÇ¼ºó¸´ÖÆ
3¡¢°²ÅÅÓ¦ÓÃ
ÔÚk8s¼¯ÈºÖа²ÅÅ×Ô¼ºµÄÓ¦Ó᣿ÉÒÔʹÓà kubectl create deploymentÀ´½¨ÉèÒ»¸ödeployment¹¤¾ß¡£ÆäÖУ¬ÐèÒªÖ¸¶¨¾µÏñºÍÈÝÆ÷Ãû¡£Ê¾ÀýÈçÏ£º
kubectl create deployment <deployment_name> --image=registry.gitlab.com/<group>/<project>:$CI_COMMIT_SHA -n <namespace></namespace></project></group></deployment_name>
µÇ¼ºó¸´ÖÆ
4¡¢½¨ÉèService
½¨ÉèService¹¤¾ß£¬½«deployment̻¶¸øÍⲿÓû§»á¼û¡£Ê¾ÀýÈçÏ£º
kubectl expose deployment <deployment_name> --type=LoadBalancer --port=80 --target-port=8080 -n <namespace></namespace></deployment_name>
µÇ¼ºó¸´ÖÆ
ÕâÀïÎÒÃÇʹÓõÄÊÇÒ»ÖÖ»ùÓÚLoadBalancerÀàÐ͵ÄService£¬Õ⽫ÔÚ¼¯ÈºÍ⽨ÉèÒ»¸ö¹«¹²IPµØµã£¬¹©Óû§»á¼ûÓ¦Óá£
5¡¢Íê³É×Ô¶¯»¯°²ÅÅ
Íê³ÉÉÏÊö°ì·¨ºó£¬Ã¿µ±ÓÐдúÂë±»Ìá½»µ½Gitlab¿ÍÕ»ÖÐʱ£¬Gitlab¾Í»áƾ֤CI/CDÉèÖÃÎļþ×Ô¶¯¹¹½¨¾µÏñ²¢°²Åŵ½k8s¼¯ÈºÖС£Óû§¿ÉÒÔͨ¹ýService̻¶µÄ¹«¹²IPµØµãÀ´»á¼ûÓ¦Óá£
Îå¡¢×ܽá
ͨ¹ý½«k8sÓëGitlabÍŽáʹÓ㬿ÉÒÔʵÏÖ×Ô¶¯»¯°²ÅźÍÒ»Á¬½»¸¶¡£Õâ²»µ«¿ÉÒÔÌá¸ß°²ÅÅËÙÂʺÍЧÂÊ£¬»¹¿ÉÒÔ×èÖ¹ÒòÈËΪ¹ýʧ¶øµ¼Öµİ²ÅÅÍÉ»¯ÎÊÌâ¡£ÆóÒµ¿ÉÒÔÒÀ¾Ý×Ô¼ºµÄÐèÇóÀ´Ñ¡ÔñʹÓúÎÖÖ×Ô¶¯»¯°²Åżƻ®£¬ÒÔÌá¸ßÈí¼þ¿ª·¢Ð§ÂʺÍÖÊÁ¿¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃk8sÓëgitlabÀ´ÊµÏÖ×Ô¶¯»¯°²ÅŵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡