Kubernetes is a powerful orchestration platform designed to manage containerized applications across a cluster of machines. It automates deployment, scaling, and operations of application containers, providing a robust infrastructure for managing applications in production. This section will cover essential Kubernetes concepts and practical steps for deploying and managing Go applications using Kubernetes.
Creating a Kubernetes Cluster
shminikube start
shgcloud container clusters create go-app-cluster --num-nodes=3
sheksctl create cluster --name go-app-cluster --region us-west-2
shaz aks create --resource-group myResourceGroup --name myAKSCluster --node-count 3 --enable-addons monitoring --generate-ssh-keys
Kubernetes Manifest Files
yamlapiVersion: apps/v1
kind: Deployment
metadata:
name: go-app
spec:
replicas: 3
selector:
matchLabels:
app: go-app
template:
metadata:
labels:
app: go-app
spec:
containers:
- name: go-app
image: go-app:latest
ports:
- containerPort: 8080
yamlapiVersion: v1
kind: Service
metadata:
name: go-app-service
spec:
type: LoadBalancer
selector:
app: go-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
Applying the Manifest Files
shkubectl apply -f deployment.yaml kubectl apply -f service.yaml
Managing Kubernetes Resources
shkubectl scale deployment go-app --replicas=5
shkubectl set image deployment/go-app go-app=go-app:v2
shkubectl logs deployment/go-app
shkubectl get services go-app-service
ConfigMaps and Secrets
yamlapiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
APP_ENV: "production"
LOG_LEVEL: "info"
shkubectl apply -f configmap.yaml
yamlapiVersion: v1
kind: Secret
metadata:
name: app-secret
type: Opaque
data:
DB_PASSWORD: bXlwYXNzd29yZA==
shkubectl apply -f secret.yaml
Persistent Storage
yamlapiVersion: v1
kind: PersistentVolume
metadata:
name: pv-volume
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data"
yamlapiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pv-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
Ingress Controllers
yamlapiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: go-app-ingress
spec:
rules:
- host: go-app.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: go-app-service
port:
number: 80
shkubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yaml
Helm for Package Management
shcurl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
shhelm create go-app
shhelm install my-go-app ./go-app
Prometheus and Grafana
shkubectl apply -f https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/master/bundle.yaml
shkubectl apply -f https://raw.githubusercontent.com/grafana/helm-charts/main/charts/grafana/templates/deployment.yaml
ELK Stack (Elasticsearch, Logstash, Kibana)
shkubectl apply -f https://raw.githubusercontent.com/elastic/cloud-on-k8s/master/config/samples/elasticsearch/elasticsearch.yaml
yamlapiVersion: v1
kind: ConfigMap
metadata:
name: logstash-config
data:
logstash.conf: |
input {
file {
path => "/var/log/*.log"
}
}
output {
elasticsearch {
hosts => ["http://elasticsearch:9200"]
}
}
shkubectl apply -f logstash-config.yaml
shkubectl apply -f https://raw.githubusercontent.com/elastic/cloud-on-k8s/master/config/samples/kibana/kibana.yaml
RBAC (Role-Based Access Control)
yamlapiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch", "list"]
yamlapiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods
namespace: default
subjects:
- kind: User
name: jane
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
Network Policies
yamlapiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-web
namespace: default
spec:
podSelector:
matchLabels:
app: web
policyTypes:
- Ingress
- Egress
ingress:
- from:
- podSelector:
matchLabels:
app: web
egress:
- to:
- podSelector:
matchLabels:
app: database
Pod Security Policies
yamlapiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
name: restricted
spec:
privileged: false
allowPrivilegeEscalation: false
runAsUser:
rule: 'MustRunAsNonRoot'
seLinux:
rule: 'RunAsAny'
supplementalGroups:
rule: 'MustRunAs'
ranges:
- min: 1
max: 65535
fsGroup:
rule: 'MustRunAs'
ranges:
- min: 1
max: 65535
volumes:
- 'configMap'
- 'secret'
By mastering these Kubernetes concepts and practices, you will be well-equipped to deploy, manage, and scale Go applications in a production environment, ensuring high availability, reliability, and performance.