Kubernetes #
TOC #
Prepration #
First lets configure our local environment. The following are the dependencies and packages that need to be installed.
- Docker
- Enable k8s in docker
- kubectl
- Minikube
- Helm (package manager for k8s, optional at first)
start minkube
minkube start
Container #
Kubernetes relies on a technology called OS-level virtualization (a.k.a. container technology. Docker is the deafult but can be changed. lets start with simple web server in go and a dockerfile.
package main
import (
"fmt"
"io"
"net/http"
"os"
)
func hello(w http.ResponseWriter, req *http.Request) {
host, _ := os.Hostname()
io.WriteString(w, fmt.Sprintf("[v3] Hello, Kubernetes!, From host: %s", host))
}
func main() {
http.HandleFunc("/hello", hello)
http.ListenAndServe(":8000", nil)
}
# Dockerfile
FROM golang:1.16-buster AS builder
RUN mkdir /src
ADD . /src
WORKDIR /src
RUN go env -w GO111MODULE=auto
RUN go build -o main .
FROM gcr.io/distroless/base-debian10
WORKDIR /
COPY --from=builder /src/main /main
EXPOSE 8000
ENTRYPOINT ["/main"]
now buld the dockerfile and run it. curl it and should return some string.
docker build . -t chaddiman/gok8:v1
docker run -p 3000:3000 --name gok8 -d chaddiman/gok8:v1
you can push it to your dockerhub.
Pod #
Now we create pods. Think of them as small balls that are inside a box that is node. We can run any number of pods. Within each pods, multiple containers can operate independently. Lets write a file that is used to create pod. Lets write it for previous webserver.
# gok8pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: gok8-pod
spec:
containers:
- name: gok8
image: chaddiman/gok8:v1 # write the image name from dockerhub or if the
image is available locally only then load image
with cmd `minikube image load <image-name>`
Now try these commands
kubectl apply -f gok8pod.yaml // create pod
kubectl get pods // list pods
kubectl port-forward gok8pod-pod 8000:8000
kubectl exec -it gok8pod-pod /bin/bash
Finally, curl or access through the browser at http://localhost:8000
Deployment #
In a production environment, we basically do not manage pods directly. We create deployment resource to manage pods. Create deployment.yaml file to manage pods.
apiVersion: apps/v1
kind: Deployment
metadata:
name: gok8-deployment
labels:
app: gok8
spec:
replicas: 3
selector:
matchLabels:
app: gok8
template:
metadata:
labels:
app: gok8
spec:
containers:
- name: gok8
image: chaddiman/gok8:v1
ports:
- containerPort: 8000
now run the commands.
kubectl apply -f deployment.yaml
kubectl get deployments
kubectl get pods // 3 pods should be listed
kubectl port-forward <pod-name> 8000:8000
# open another terminal
curl http://localhost:8000
We are running 3 pods, but here we mannually port-forwarded a single pod. K8s should manage it right?. For that we need to make services.
Services #
Service provides a stable endpoint for pods. It also does load balancing and round-robin is the default method.
# service.yaml
apiVersion: v1
kind: Service
metadata:
name: gok8-service
spec:
type: ClusterIP
selector:
app: gok8
ports:
- port: 8000
targetPort: 8000
now run the commands.
kubectl apply -f service.yaml
kubectl get endpoints
kubectl get pods
kubectl get services
now we can port-forward the service or we can try the command minikube service <service-name>
now try curl, and the host name should be different each time.