@dictcp (https://dictcp.men)
Session at HKOSCon 2020: https://hkoscon.org/2020/topics/automating-kubernetes-gitops
Video recording: https://www.youtube.com/watch?v=xykc3AsMQ04
Slide: https://docs.google.com/presentation/d/1rA6bhCGMydP-NOmFu7fv0zbBSzGvK9GLn63H8dHLP2g
- Hands-on Preparation
- Install k3s Kubernetes on your VM
- Install Helm & fluxctl
- Prepare the git repository for GitOps
- Install fluxcd
- fluxcd playaround
- Install Helm operator (bonus)
- Helm operator playaround (bonus)
- Set up Lens (bonus)
- Continuous Delivery (bonus)
You need
- a GitHub account (to access this material and setup gitops flow)
- a working Kubernetes in the hands-on session (to s)
- multipass VM (local installation, recommended)
- DigitalOcean VM (remote installation)
- Install multipass on https://multipass.run
- support Windows 10 Pro+, Mac OS X, Linux
- Mac OS X (with homebrew installed):
brew cask install multipass
- Ubuntu (or any Linux distro with snap installed):
sudo snap install multipass --classic
- Start Command prompt / Terminal
- Execute
multipass launch -d 30G -m 2G --name fluxcd-workshop 18.04
- Depends on network condition, you need to wait for 5-20 mins.
- Check the IP of your VM via command
multipass info fluxcd-workshop
Name: fluxcd-workshop
State: Running
IPv4: 192.168.64.15
Release: Ubuntu 18.04.4 LTS
Image hash: 55646c02a8bc (Ubuntu 18.04 LTS)
Load: 1.46 5.28 3.97
Disk usage: 4.5G out of 28.9G
Memory usage: 1.5G out of 1.9G
- Execute
multipass shell fluxcd-workshop
to obtain shell in the VMs
- (If need) Signup DigitalOcean via https://m.do.co/c/3c4b5bb5da42
- create a droplet with 2GB and Ubuntu 18.04
- https://cloud.digitalocean.com/droplets/new?size=s-2vcpu-2gb®ion=sgp1&distro=ubuntu&distroImage=ubuntu-18-04-x64
- You may set up Password Authentication for simpicity
- Wait a few minutes, and you can connect to the
ssh root@<<YOUR_DROPLET_IP>>
- Connect to the shell of the VM set up
- Execute
curl -sfL https://get.k3s.io | sh -
in the VM - Depends on network condition, you need to wait for 5-10 mins.
- You should have your local Kubernetes cluster installed. To verify, execute
sudo -i k3s kubectl get nodes
, it should give you something like
NAME STATUS ROLES AGE VERSION
fluxcd-workshop Ready master 5m53s v1.18.3+k3s1`
- Make sure the cluster setup is avaiable by the following commands:
$ mkdir .kube
$ sudo cat /etc/rancher/k3s/k3s.yaml > .kube/config
Normally you can install helm and fluxctl with the following 4 commands:
sudo snap install kubectl --classic
sudo snap install helm --classic --channel=3.0/stable
sudo snap install fluxctl --classic
sudo rm /usr/local/bin/kubectl
- Helm
$ curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3
$ chmod 700 get_helm.sh
$ ./get_helm.sh
$ helm version
version.BuildInfo{Version:"v3.2.3", GitCommit:"8f832046e258e2cb800894579b1b3b50c2d83492", GitTreeState:"clean", GoVersion:"go1.13.12"}
- fluxctl
wget -O fluxctl https://github.com/fluxcd/flux/releases/download/1.19.0/fluxctl_linux_amd64
chmod +x fluxctl
sudo mv fluxctl /usr/local/bin/
- Fork https://github.com/dictcp/flux-get-started in GitHub to your own account
- Setup the chart repository for flux
helm repo add fluxcd https://charts.fluxcd.io
- Install flux on Kubernetes cluster via Helm, with following commands
$ export GHUSER="dictcp"
$ kubectl create ns flux
$ helm upgrade -i flux fluxcd/flux \
--set [email protected]:${GHUSER}/flux-get-started \
--set 'git.path=namespaces\,workloads' \
--namespace flux
- Copy the public SSH key
fluxctl identity --k8s-fwd-ns flux
-
Setup the in GitHub (eg.
https://github.com/${GHUSER}/flux-get-started/settings/keys
) Remember to enableAllow write access
-
Trigger sync
fluxctl sync --k8s-fwd-ns flux
- View the applied change (you should see a
demo
namespace
kubectl get ns
-
copy
manifests-sample/podinfo-dep.yaml
andmanifests-sample/podinfo-svc.yaml
toworkloads/
in yourflux-get-started
repos; commit change and push -
check deployment progress
watch kubectl -n demo get pod
- Install the CRD for HelmRelease
kubectl apply -f https://raw.githubusercontent.com/fluxcd/helm-operator/1.1.0/deploy/crds.yaml
- Install Helm Operator via Helm
helm upgrade -i helm-operator fluxcd/helm-operator \
--set git.ssh.secretName=flux-git-deploy \
--set helm.versions=v3 \
--namespace flux
- copy
releases/ghost.yaml
asworkloads/ghost.yaml
in yourflux-get-started
repos; commit change and push OR running command
cat <<EOF | kubectl apply -f -
---
apiVersion: helm.fluxcd.io/v1
kind: HelmRelease
metadata:
name: ghost
namespace: demo
annotations:
fluxcd.io/automated: "false"
fluxcd.io/tag.chart-image: glob:3.1.1-debian-9-*
spec:
releaseName: ghost
chart:
git: ssh://[email protected]/fluxcd/flux-get-started
ref: master
path: charts/ghost
values:
image:
repository: bitnami/ghost
tag: 3.1.1-debian-9-r0
persistence:
enabled: false
resources:
requests:
memory: 32Mi
cpu: 10m
service:
type: ClusterIP
mariadb:
master:
persistence:
enabled: false
EOF
-
check Helm install progress
watch kubectl -n demo get hr
-
Port-forward the result
kubectl -n demo port-forward --address 0.0.0.0 service/ghost 8080:80
-
Open browser and check the result
-
install Lens at https://k8slens.dev/
- Mac OS X (with homebrew installed):
brew cask install lens
- Mac OS X (with homebrew installed):
-
Add clusters with your KUBECONFIG
-
check it out
- Check into the magic of annotations
fluxcd.io/automated: "true"