🐳🚢 Docker-Kubernetes-101

Workflow License Issues

Learnings and approach for deployments using Docker and Kubernetes.

✍️ Approach

✔️ Fork sub-directory from kubernetes/examples

To fork kubernetes/examples/guestbook-go directory,

git clone https://github.com/kubernetes/examples.git
cd examples
git subtree split --prefix=guestbook-go -b main
git checkout main
# create a GitHub repo
git remote set-url origin YOUR_NEW_GIT_LINK
git fetch -pa
git push -u origin main
# clone the new repo
cd ..
git clone YOUR_NEW_GIT_LINK
cd YOUR_NEW_REPO

Since the forked repo had Apache License 2.0, we persevere the LICENSE

✔️ Prevent merging anything in main branch without review

GitHub has some granular and configurable settings to enable branch protection,

Settings:
    - [x] Require a pull request before merging
    - [x] Require approvals: 1 (testing purpose, do increase if required) 
    - [x] Dismiss stale pull request approvals when new commits are pushed
    - [x] Require review from Code Owners (may include CODEOWNERS file)
    - [ ] Do not allow bypassing the above settings (testing purpose)

✔️ Write GitHub action workflow

Requirements:
    - Create a container image
    - Push that image to DockerHub
    - Build container image only when one of the below conditions is true,
        - When PR get merged in main/master branch from any other branch
        - When commit message contains `BUILD_CONTAINER_IMAGE` string
  1. To build a container image use docker’s build-and-push GitHub action
  2. Create a token on DockerHub and add credentials to repository secrets. Refer DockerHub Docs
  3. Add conditionals to action workflow

...
on:
  push:
    branches: [main]
  pull_request:
    types:
      - closed
    branches:
      - main

jobs:
  build-publish-container-image:
    if: github.event.pull_request.merged == true || contains(github.event.head_commit.message, 'BUILD_CONTAINER_IMAGE')

✔️ Deploy container image to Kubernetes cluster

🏡 Local Kubernetes Cluster using minikube

  1. Install and run minikube, Refer Gettting started with minikube.
    1. (Optional, Fedora specific steps) Prefer virtualization over containerization, due to known issues with btrfs and systemd.

    sudo dnf install @virtualization
    sudo systemctl start libvirtd
    sudo systemctl enable libvirtd
    
    # start minikube
    minikube start --driver kvm2

🚂 Deployment

  1. Make sure that your image path is correct and pointing to your DockerHub container image in guestbook-controller.json,

 ...
 "spec":{
            "containers":[
               {
                  "name":"guestbook",
                  "image":"jayantkatia/actions-for-docker:latest",
                  ...
               }
             ]
        }
  ...
  1. Run,

kubectl apply -f redis-master-controller.json
kubectl apply -f redis-master-service.json
kubectl apply -f redis-replica-controller.json
kubectl apply -f redis-replica-service.json
kubectl apply -f guestbook-controller.json
kubectl apply -f guestbook-service.json
  1. Run minikube tunnel to allocate an external IP to LoadBalancer.

🎉 Result

Screenshot from 2022-09-29 22-12-01 Screenshot from 2022-09-29 22-18-45 Screenshot from 2022-09-29 22-22-25

✨ Contributing

Yes, please! Feel free to contribute, raise issues and recommend best practices.

A few resources to get you started:

GitHub

View Github