Skip to content

Instantly share code, notes, and snippets.

@hideojoho
Last active October 2, 2020 04:17
Show Gist options
  • Save hideojoho/931f628ab5f9d369df0cfbd1b59de0b2 to your computer and use it in GitHub Desktop.
Save hideojoho/931f628ab5f9d369df0cfbd1b59de0b2 to your computer and use it in GitHub Desktop.
From Zero to k8s via Docker Hub

From Zero to k8s via Docker Hub

GitHub -> Local -> Docker Hub -> GitHub -> Docker Hub -> k8s

Environment

  • Windows 10
  • WSL2 + Docker for Windows
  • VSCode
  • GitHub account
  • Docker Hub account

GitHub

  • Make a new repository
    • e.g., sample-svc

WSL

  • Start WSL
  • Clone the github repository
  • Open the repo folder from WSL with code sample-svc command

VSCode

The editor opens the repo folder within WSL.

  • Make index.html
  • Make Dockerfile
  • Open terminal in VSCode
    • Build a container
      • docker build -t username/sample-svc
    • Test a container
$ docker run -d -it --name svc username/sample-svc
$ docker exec svc /bin/sh -c "curl -s localhost"
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Sample Service</title>
  </head>
  <body>
    <h1>Sample Service</h1>
  </body>
</html>
  • Push to Docker Hub
$ docker push username/sample-svc
  • Commit to GitHub
$ git add .
$ git commit -m "Initial commit"
$ git push origin master

DockerHub

  • Go to the image build page
  • Select GitHub from Automated Builds
  • Select Connect to GitHub from Linked Accounts
    • Give an authentication
  • Come back to the image build page again
  • Select GitHub (Connected) from Automated Builds
  • Select your account and repository (sample-svc) -> Save

VSCode

  • Change the title of index.html to Sample Microservice
  • Commit the change
$ git add .
$ git commit -m "Changed the title"
$ git push origin master

DockerHub

  • Check if the image is registered in the queue from the image build page
  • Wait until the image is successfully built

K8s

$ kubectl apply -f sample-svc.yaml
namespace/sample-svc unchanged
deployment.apps/sample-svc unchanged
service/sample-svc configured
$ kubectl get svc -A # Determine the internal port to which 80 is mapped
$ curl -s a-k8s-node:port
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Sample Microservice</title>
  </head>
  <body>
    <h1>Sample Service</h1>
  </body>
</html>

VSCode

  • Change the body text to Sample Microservice
  • Commit the change

Docker Hub

  • Wait until the new image is successfully built

k8s

  • Reload the image
$ kubectl rollout restart deployment sample-svc -n sample-svc
deployment.apps/sample-svc restarted
$ kubectl get svc -A # Same drill
$ curl -s a-k8s-node:port
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Sample Microservice</title>
  </head>
  <body>
    <h1>Sample Microservice</h1>
  </body>
</html>
  • Delete the whole thing
$ delete ns sample-svc
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Sample Service</title>
</head>
<body>
<h1>Sample Service</h1>
</body>
</html>
# Base Image
FROM nginx:latest
# Maintainer
LABEL maintainer="you <you@example.com>"
# Add index.html to DocumentRoot
ADD index.html /usr/share/nginx/html
---
apiVersion: v1
kind: Namespace
metadata:
name: sample-svc
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: sample-svc
namespace: sample-svc
spec:
selector:
matchLabels:
run: sample-svc
app: sample-svc
template:
metadata:
labels:
run: sample-svc
app: sample-svc
spec:
containers:
- name: sample-svc
image: username/sample-svc
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: sample-svc
namespace: sample-svc
labels:
run: sample-svc
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
selector:
run: sample-svc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment