GitOps with Kubernetes
/ 3 min read
kubernetes , k8s , gitops , flux , argocd , devops , cloud-native , containers , series:kubernetes:21
Understanding GitOps
GitOps is a set of practices where the entire system’s desired state is stored in Git, and automated processes ensure the actual state matches the desired state.
GitOps Tools
1. Flux CD
apiVersion: source.toolkit.fluxcd.io/v1beta2kind: GitRepositorymetadata: name: flux-system namespace: flux-systemspec: interval: 1m ref: branch: main url: https://github.com/org/repo---apiVersion: kustomize.toolkit.fluxcd.io/v1beta2kind: Kustomizationmetadata: name: apps namespace: flux-systemspec: interval: 10m path: ./apps prune: true sourceRef: kind: GitRepository name: flux-system2. Argo CD
apiVersion: argoproj.io/v1alpha1kind: Applicationmetadata: name: myapp namespace: argocdspec: project: default source: repoURL: https://github.com/org/repo.git targetRevision: HEAD path: apps/myapp destination: server: https://kubernetes.default.svc namespace: myapp syncPolicy: automated: prune: true selfHeal: trueInfrastructure as Code
1. Helm Charts
replicaCount: 3image: repository: nginx tag: "1.21"service: type: ClusterIP port: 80apiVersion: apps/v1kind: Deploymentmetadata: name: {{ .Release.Name }}spec: replicas: {{ .Values.replicaCount }} template: spec: containers: - name: {{ .Chart.Name }} image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"2. Kustomize
apiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationresources:- deployment.yaml- service.yamlpatches:- path: patch.yaml target: kind: Deployment name: myappContinuous Deployment
1. Flux Pipeline
apiVersion: source.toolkit.fluxcd.io/v1beta2kind: HelmRepositorymetadata: name: podinfo namespace: flux-systemspec: interval: 5m url: https://stefanprodan.github.io/podinfo---apiVersion: helm.toolkit.fluxcd.io/v2beta1kind: HelmReleasemetadata: name: podinfo namespace: defaultspec: interval: 5m chart: spec: chart: podinfo version: ">=4.0.0 <5.0.0" sourceRef: kind: HelmRepository name: podinfo namespace: flux-system values: replicaCount: 22. ArgoCD Pipeline
apiVersion: argoproj.io/v1alpha1kind: ApplicationSetmetadata: name: cluster-addons namespace: argocdspec: generators: - list: elements: - cluster: production url: https://kubernetes.default.svc - cluster: staging url: https://staging-cluster:6443 template: metadata: name: '{{cluster}}-addons' spec: project: default source: repoURL: https://github.com/org/cluster-addons.git targetRevision: HEAD path: '{{cluster}}' destination: server: '{{url}}' namespace: addonsMonitoring and Alerts
1. Prometheus Configuration
apiVersion: monitoring.coreos.com/v1kind: PrometheusRulemetadata: name: gitops-alertsspec: groups: - name: gitops rules: - alert: ReconciliationFailed expr: | sum(increase(flux_reconcile_error[5m])) > 0 for: 10m labels: severity: warning annotations: summary: GitOps reconciliation failed2. Alert Manager
apiVersion: notification.toolkit.fluxcd.io/v1beta1kind: Alertmetadata: name: slack-notifications namespace: flux-systemspec: providerRef: name: slack eventSeverity: info eventSources: - kind: GitRepository name: '*' - kind: Kustomization name: '*'Security Best Practices
1. RBAC Configuration
apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata: name: gitops-reconcilerrules:- apiGroups: - "*" resources: - "*" verbs: - "*"---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata: name: gitops-reconcilersubjects:- kind: ServiceAccount name: flux-reconciler namespace: flux-systemroleRef: kind: ClusterRole name: gitops-reconciler apiGroup: rbac.authorization.k8s.io2. Secret Management
apiVersion: source.toolkit.fluxcd.io/v1beta2kind: GitRepositorymetadata: name: secrets namespace: flux-systemspec: interval: 1m url: https://github.com/org/secrets secretRef: name: git-credentials---apiVersion: kustomize.toolkit.fluxcd.io/v1beta2kind: Kustomizationmetadata: name: secrets namespace: flux-systemspec: decryption: provider: sops secretRef: name: sops-gpgMulti-Cluster Management
1. Cluster Configuration
apiVersion: cluster.x-k8s.io/v1beta1kind: Clustermetadata: name: production-1 namespace: defaultspec: clusterNetwork: pods: cidrBlocks: ["192.168.0.0/16"] infrastructureRef: apiVersion: infrastructure.cluster.x-k8s.io/v1beta1 kind: AWSCluster name: production-12. Fleet Management
apiVersion: fleet.cattle.io/v1alpha1kind: GitRepometadata: name: fleet-example namespace: fleet-defaultspec: repo: https://github.com/org/fleet-examples paths: - single-cluster/nginx targets: - name: prod clusterSelector: matchLabels: env: prodTroubleshooting
Common Issues
- Repository Sync Issues
flux get sources gitflux logs --level=error- Deployment Failures
argocd app get myappargocd app logs myapp- Reconciliation Problems
kubectl describe kustomization apps -n flux-systemBest Practices
- Use Semantic Versioning
- Implement Progressive Delivery
- Maintain Environment Parity
- Regular Backup of Git Repository
- Implement Proper Access Controls
- Monitor Reconciliation Metrics
- Document Deployment Procedures
Conclusion
GitOps provides a powerful approach to managing Kubernetes clusters and applications. By following these practices and using the right tools, teams can achieve reliable, automated, and auditable deployments.
Series Navigation
- Previous: Kubernetes Troubleshooting Guide
- Next: Kubernetes Policy Management