Files
wiki.tipsy.codes/Simple Flux.md
T
2026-05-07 23:43:19 -07:00

153 lines
4.4 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
The Flux tutorial makes things very complicated. In essence, all Flux is is a tool that pulls a git repo, and does `kubectl apply -k`; there should be a much easier way to set it up.
## Install Flux
Export your GITEA_TOKEN:
```
export GITEA_TOKEN=THERE_IS_SOME_TOKEN_HERE
```
Bootstrap the repo:
```
flux bootstrap gitea \
--token-auth=true \
--owner=charles \
--repository=flux-5pi5 \
--branch=main \
--path=./ \
--personal \
--hostname=git.tipsy.codes
```
This will create the repo in Gitea for you, and define a basic structure:
```
tree
.
└── cluster
   └── flux-system
       ├── gotk-components.yaml
       ├── gotk-sync.yaml
       └── kustomization.yaml
```
From here, in the simplest way, you can just start deploying applications.
## First deployment
In the most simple form, Flux will pick up any kustomization in the path pointed to by '--path'. Since we set it to '.', adding something should be as simple as dropping it into that folder.
```
git clone ssh://git@git.tipsy.codes:2222/charles/flux-5pi5.git
cd flux-5pi5
cat <<EOF > nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
 name: nginx-deployment
 namespace: default
spec:
 selector:
   matchLabels:
     app: nginx
 replicas: 2 # tells deployment to run 2 pods matching the template
 template:
   metadata:
     labels:
       app: nginx
   spec:
     containers:
       - name: nginx
         image: nginx:1.14.2
         ports:
           - containerPort: 80
EOF
```
Then commit, push, and reconcile:
```
git add .
git commit -am 'add: nginx deployment'
[main e1e40fb] add: nginx deployment
1 file changed, 19 insertions(+)
create mode 100644 nginx.yaml
git push
** WARNING: connection is not using a post-quantum key exchange algorithm.
** This session may be vulnerable to "store now, decrypt later" attacks.
** The server may need to be upgraded. See https://openssh.com/pq.html
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 20 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 478 bytes | 478.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
remote: . Processing 1 references
remote: Processed 1 references in total
To ssh://git.tipsy.codes:2222/charles/flux-5pi5.git
  43161d8..e1e40fb  main -> main
```
If you are impatient, you can trigger a reconciliation with:
```
flux reconcile source git flux-system
```
Watch it rollout with:
```
watch flux get all -A
```
Then you should be good to go!
```
kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-647677fc66-cltgn   1/1     Running   0          6m31s
nginx-deployment-647677fc66-r6lxj   1/1     Running   0          6m31s
```
## Some simple things to make it better
### Use kustomizations
If we use kustomizations, we can track the reconciliation of sets of things.
Here is a simple example:
```
head -n 99999 nginx.yaml nginx/*
==> nginx.yaml <==
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
 name: nginx-kustomization
 namespace: flux-system
spec:
 interval: 10m
 path: "./nginx"
 prune: true
 sourceRef:
   kind: GitRepository
   name: flux-system
 targetNamespace: default
 wait: true
==> nginx/deployment.yaml <==
apiVersion: apps/v1
kind: Deployment
metadata:
 name: nginx-deployment
 namespace: default
spec:
 selector:
   matchLabels:
     app: nginx
 replicas: 2 # tells deployment to run 2 pods matching the template
 template:
   metadata:
     labels:
       app: nginx
   spec:
     containers:
       - name: nginx
         image: nginx:1.14.2
         ports:
           - containerPort: 80
==> nginx/kustomization.yaml <==
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: default
resources:
 - deployment.yaml
```
The first file (`nginx.yaml`) tells Flux to look into a specific folder, with an update interval and some other settings. The other files are the kustomization; check the Kubernetes docs for that.