{"id":20590243,"url":"https://github.com/eitancj/k8s-bluegreen","last_synced_at":"2026-04-28T12:38:47.345Z","repository":{"id":195077705,"uuid":"692019444","full_name":"eitancj/k8s-bluegreen","owner":"eitancj","description":"Simple Blue-Green Deployment on Kubernetes","archived":false,"fork":false,"pushed_at":"2023-09-19T20:21:36.000Z","size":12,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-07T14:44:22.053Z","etag":null,"topics":["bash-script","bluegreen","bluegreen-deployment","docker-desktop","docker-desktop-for-mac","docker-images","dockerfile","kubernetes","kubernetes-deployment","nginx","nginx-docker","shell-script"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eitancj.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-09-15T11:39:17.000Z","updated_at":"2023-09-19T19:38:40.000Z","dependencies_parsed_at":"2023-10-11T07:54:59.390Z","dependency_job_id":null,"html_url":"https://github.com/eitancj/k8s-bluegreen","commit_stats":{"total_commits":7,"total_committers":1,"mean_commits":7.0,"dds":0.0,"last_synced_commit":"551dd738e894576ae01e9df0df9697fde90e084d"},"previous_names":["eitancj/k8s-bluegreen"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eitancj/k8s-bluegreen","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eitancj%2Fk8s-bluegreen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eitancj%2Fk8s-bluegreen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eitancj%2Fk8s-bluegreen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eitancj%2Fk8s-bluegreen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eitancj","download_url":"https://codeload.github.com/eitancj/k8s-bluegreen/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eitancj%2Fk8s-bluegreen/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32381691,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-28T11:25:28.583Z","status":"ssl_error","status_checked_at":"2026-04-28T11:25:05.435Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bash-script","bluegreen","bluegreen-deployment","docker-desktop","docker-desktop-for-mac","docker-images","dockerfile","kubernetes","kubernetes-deployment","nginx","nginx-docker","shell-script"],"created_at":"2024-11-16T07:34:45.919Z","updated_at":"2026-04-28T12:38:47.307Z","avatar_url":"https://github.com/eitancj.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Blue-Green Deployment on Kubernetes\n\\\n![](https://github.com/eitancj/preview_images/blob/main/bluegreen.png?raw=true)\n\n\\\nPractice blue-green deployment of a simple web app on your local machine using Docker Desktop.\n\n### Tested Tech Stack\n- nginx 1.25.2 on Alpine 3.18-slim\n- kubernetes v1.25.2\n- docker desktop 4.15.0\n- macOS X\n\n### Prerequisites\n- Docker Desktop with Kubernetes enabled\n- Mac\\Linux machine\n\n\n### Run Programme\n1a. Clone Repo.\n\n1b. Create a new namespace as an isolated sandbox for this session.\n```sh\nkubectl create namespace blue-green\n```\n\\\n2. Review the blue deployment 'blu-dep.yml'. Note how the image points to app version 1.0.\\\n   This is the stable version of our app - the BLUE version.\n\n\\\n3. Build the app images. It should output two new images, tagged 1.0 and 2.0. \n```sh\nchmod u+x ./build_bg_images.sh\n./build_bg_images.sh\n```\n\n\\\n4. Deploy the blue pods.\n```sh\nkubectl apply -f blu-dep.yml -n blue-green\n```\n\n\\\n5. Review the Service config 'blugrn-svc.yml'; note how it points to the blue pods only.\\\n   Deploy it.\n```sh\nkubectl apply -f blugrn-svc.yml -n blue-green\n```\n\n\\\n6. The working \"blue\" version of the app is up and running.\\\n   Test it.\n```sh\n# Test it from your browser\nhttp://localhost:32123\n\n# OR don't even leave the terminal\nwatch curl http://localhost:32123\n```\n\u003e The page title should display – \"blue - 1.0\".  \n\n\u003e Hard-refreshing the page (or waiting 2 secs in *watch*) should alternate between two container hostnames, both of which should be blue at this point.\n\n\\\n7. Review the green deployment 'grn-dep.yml'.\\\n   It should be identical to the blue deployment, same image, only green in the labels.\n\n\\\n8. Deploy the green stand-by.\n```sh\nkubectl apply -f grn-dep.yml -n blue-green\n```\n\n\\\n9. You now have a running identical stand-by.\\\n   Yet web requests should still only be directed to the blue world.\\\n   Hard-refresh/curl your localhost at 32123 to make sure of that.\n```sh\nhttp://localhost:32123\n# OR\nwatch curl http://localhost:32123\n```\n\n\\\n10. Still skeptical of the green world?\\\n    Bypass the service selector and verify that the green pods are running app version 1.0.\\\n    Run this:\n```sh\nchmod u+x ./expose_green.sh\n. ./expose_green.sh\n```\n\u003e You should see a green 1.0 pod displayed in your browser.  \n\n\\\nNow delete the temporary service we've just created, and let's move on.\n```sh\nkubectl delete svc $GREENPOD -n blue-green\n```\n\n\\\n11.  So we have a service that points to our blue deployment, and a green deployment in stand-by.\\\n    Let's upgrade the green pods to version 2.0 of our app by changing a single line in the deployment yaml.\n```sh\ngrep 'image:' grn-dep.yml | awk -F '#' '{print $1 \" \u003c--before\"}' | xargs\n\nsed -i'.orig' 's/1.0/2.0/' grn-dep.yml \u0026\u0026 rm grn-dep.yml.orig\n# get rid of the '.orig' parts if your version of sed doesn't require it\n\ngrep 'image:' grn-dep.yml | awk -F '#' '{print $1 \" \u003c--after\"}' | xargs\n```\n\u003e You can of course do this manually by editing the yaml file.\n\n\\\n12. Since no requests are being directed to the green world, we should be able to deploy the new app there without affecting our stable blue world (given a robust \u0026 agile infrastructure).\n```sh\nkubectl apply -f grn-dep.yml -n blue-green\n```\n\n\\\n13. Test the new version of the app before we direct users there.\\\n    In this practice case, we'll settle for browser testing like we did earlier.\n```sh\n. ./expose_green.sh\n```\n\u003e You should now see a green *2.0* pod displayed in your browser.  \n\n\u003e Yet if you navigate back to http://localhost:32123, you'll see that users our still only being directed to the blue 1.0 app.\n\n\\\nDelete the temporary service we've just created.\n```sh\nkubectl delete svc $GREENPOD -n blue-green \u0026\u0026 unset GREENPOD \u0026\u0026 unset GREENPORT\n```\n\n\\\n14. Update the Service yaml from blue to green.\n```sh\ngrep 'env:' blugrn-svc.yml | awk -F '#' '{print $1 \" \u003c--before\"}' | xargs\n\nsed -i'.orig' 's/blue/green/' blugrn-svc.yml \u0026\u0026 rm blugrn-svc.yml.orig\n# get rid of the '.orig' parts if your version of sed doesn't require it\n\ngrep 'env:' blugrn-svc.yml | awk -F '#' '{print $1 \" \u003c--after\"}' | xargs\n```\n\n\\\n15. OK, everything's set – time to do the switch.\\\n   Henceforth, all new web requests will be directed to the new version of the app – to the green world.\n```sh\nkubectl apply -f blugrn-svc.yml -n blue-green\n```\n\n\\\n16. Test your main app at 32123. Should be green and 2.0.\n```sh\nhttp://localhost:32123\n# OR\nwatch curl http://localhost:32123\n```\n\n\\\n17.  Alright! All new connections are now using the latest version of our app.\\\n    But WAIT! there's a problem. Something's not working right for some of the users. SHIT.\\\n    But why? we tested the new version and deployed it to an identical environment!\\\n    Well, that's a question to be delt with later – first get the app up and running again.\\\n    \\\n    Let's implement one of the best features of blue-green deployments – swift rollbacks.\n```sh\n# change 'green' to 'blue' in the service's yaml file\ngrep 'env:' blugrn-svc.yml | awk -F '#' '{print $1 \" \u003c--before\"}' | xargs\n\nsed -i'.orig' 's/green/blue/' blugrn-svc.yml \u0026\u0026 rm blugrn-svc.yml.orig\n# # get rid of the '.orig' parts if your version of sed doesn't require it\n\ngrep 'env:' blugrn-svc.yml | awk -F '#' '{print $1 \" \u003c--after\"}' | xargs\n\n\n# apply changes — revert back to the blue world with the old version of the app\nkubectl apply -f blugrn-svc.yml -n blue-green\n```\n\n\\\n18. Test the app again.\\\n   Should have now gone back to blue and 1.0.\n```sh\nhttp://localhost:32123\n# OR\nwatch curl http://localhost:32123\n```\n\u003e Good. All users report that the app is now functioning properly.  \n\n\u003e We'll need to investigate why this happened, and perhaps implement more strenuous CI/CD pipeline testing before attempting the update again.\n\n\\\n19. That's it! Let's clean up some, get your machine back to the way it was.\n\n\n### Cleanup\n1. Delete the blue-green namespace and all its resources along with it.\n```sh\nkubectl delete ns blue-green\n```\n2. Remove the docker images we've created at the beginning of the session.\n```sh\ndocker rmi bgdeploy:1.0 bgdeploy:2.0\n```\n3. Delete cloned repo if you wish.\n\n4. Thanks for your time.\n\n\n### Notes\n1. After a successful blue-green deployment is achieved, you may tear down the blue environment (ideally with an IaC tool).\\\n   When the time comes to do another update, you may mount a blue stand-by, and repeat the same process only from green to blue, ending up with a blue 3.0 version, for example.\\\n   And so on and so forth.\n\n2. If you have access to a load-balanced cloud k8s cluster, you could try and change the service type in the yaml file from NodePort to LoadBalancer, and then also make the corresponding changes to your cloud's lb when making the switch.\\\n   I haven't tried it myself, but I see no reason for it not to work.\n   \n3. Ideally this entire procedure would be done using a comprehensive CI/CD pipeline – a continuous (automatic) blue-green deployment, so to speak.\n\n4. Blue-green deployments entail pros and cons when compared with other deployment methods.\\\n   I recommend reading up on it, as this example doesn't cover theory nor complex scenarios.\n\n5. In real-world scenarios, blue-green deployments may require further steps to be taken with each update, such as adjusting the configuration of persistent storage, dns, load balancers, databases, etc.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feitancj%2Fk8s-bluegreen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feitancj%2Fk8s-bluegreen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feitancj%2Fk8s-bluegreen/lists"}