Setup
Step 1. Make sure you have kubernetes locally
Use the local version of Kubernetes that comes with Docker desktop. See the link for installation instructions. It can be quite resource heavy so you should disable it once you're done.
There are other options like kind, minikube, k3s if you prefer.
Step 2. Connect to the cluster using kubectl
Make sure you have kubectl installed. It should be installed as part of Docker under C:\Program Files\Docker\Docker\resources\bin\kubectl.exe. If you can't type kubectl in a terminal window, then you need to add that path to the system environment variable called PATH.
kubectl config get-contextsshould list all the "contexts" (the clusters) you have access to. Thedocker-desktopcluster should be therekubectl config use-context docker-desktopshould connect you to the local cluster- Make sure you can access it by running
kubectl get pods -A(list all pods in all namespaces)
Step 3. Connect to the cluster using Lens
You might also want to connect to the cluster using Lens, so that you get a better overview of what's going on. The cluster should be listed in Lens and you should be able to connect to it.
Step 4. Create a personal access token
- In order to run Flux locally, you need to create a personal access token in GitHub: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token.
$env:GITHUB_TOKEN='<your token>'$env:GITHUB_USER='<your username>'
Step 5. Install Flux CLI
You're going to need the Flux CLI, install it from here: https://fluxcd.io/flux/installation/#install-the-flux-cli (again, Chocolatey is an easy way for Windows).
Simple kube apply and kustomize
Create a namespace
kubectl get namespaceswill list the existing namespacescd manifestskubectl apply -f namespace.yamlwill tell kubernetes to create the namespace defined in the file.kubectl get namespaceswill list the new namespace
Create a Pod
We actually create a Deployment, which is responsible of keeping one or more replicas of a pod alive.
kubectl apply -f deployment.yamlkubectl get pods -n whoamilist the pods in thewhoaminamespace- If you delete a pod, the deployment will make sure a new replica is created.
kubectl delete pod -n whoami <name_of_pod>
A Service
Create a Service which we can use to access the pods in a load balanced way.
kubectl apply -f service.yamlkubectl get services -n whoamito make sure the service is created. You can see that it has aClusterIPbut noExternalIP. This means that you cannot access this service from outside the cluster.- A way of accessing the service is to do a port-forward. That will map a port on your local computer, to a port on the service. This isn't something you do other than in debugging purposes.
kubectl port-forward -n whoami svc/whoami 8080:80will let you access the service fromhttp://localhost:8080/on your local computer. - Press
ctrl+cto abort port forwarding
An Ingress Controller
In order to access a service (and indirectly a pod) from outside the cluster, we need an Ingress Controller. The one we use is Traefik.
Setup Traefik
cd traefikkubectl apply -f 00-role.yaml -f 00-account.yaml -f 01-role-binding.yaml -f 02-traefik.yaml -f 02-traefik-services.yaml- http://localhost:8080/dashboard/#/ should show you the Traefik dashboard
- http://localhost:80 will give you a 404 (from Traefik)
Setup an Ingress resource
kubectl apply -f 03-whoami.yaml -f 03-whoami-services.yaml -f 04-whoami-ingress.yaml- http://localhost:80
Kustomize
- Start by deleting the previous stuff
kubectl delete -f 00-role.yaml -f 00-account.yaml -f 01-role-binding.yaml -f 02-traefik.yaml -f 02-traefik-services.yaml -f 03-whoami.yaml -f 03-whoami-services.yaml -f 04-whoami-ingress.yaml kubectl kustomizewill use kustomize to merge the files into one.kubectl apply -k ./
Doing GitOps using Flux
Flux is a tool that helps us do "GitOps". That means that we can have a desired state in git, and it will be Flux's job to make sure that state is reconciled in the cluster.
Flux is running as a pod in the cluster, listening for changes in the git repository while also making sure the cluster matches what's in git.
- Make sure you're good to go:
flux check --pre - Install Flux in the cluster, and create a new personal reposoty flux will sync against
flux bootstrap github --owner=$env:GITHUB_USER --repository=flux-demo --branch=main --path=./clusters/my-cluster --personal - Check Flux installation
kubectl get pods -n flux-system - Clone your new repo:
git clone https://github.com/$env:GITHUB_USER/fleet-infraand open it in an editor