yamlfiles are generated on the root repository folder. InitDocker Destopand enableKuberneteson settings.kubectl apply -f discovery-server.yaml,api-gateway.yaml,auth-server.yaml,store-server.yamlto create deployments and services.NodePortparam is configured toDiscovery-serverandApi-Gatewayso users can access to test Eureka Dashboard and API endpoints.Discovery-serverlocalhost:31000Api-Gatewaylocalhost:32000
Kubernetes is a tool that helps to manage and deploy containerized applications (such as Docker containers) across a cluster of computers or virtualized environments. It takes care of things like load balancing, scaling, and failover.
In Kubernetes, users define desired state of their containerized applications by using a configuration file, and Kubernetes will automatically ensure that the applications are running as expected.
Main components of Kubernetes:
- Pod: smallest unit, usually has one container into it (contains an app). Each POD has an internal IP address. When a POD is replaced, new IP is assigned.
- Service: because IP of the POD may change, service offers an static IP address and PODs can attach to it. If associated POD goes down, Service IP doesn't change.
- Ingress: component that forwards typically http traffic from outside the cluster to inside.
- ConfigMap: contains configurations of the application like db url, discovery...
- Secret: to store credentials, keys...
- Volumes: to store persistent data on the cluster, attaches fisical store to POD, locally or cloud. Usually, data persistency is placed outside clusters.
- StatefulSet: sincronize read, write from PODs to database.
Kubernetes manages 2 types of nodes:
Master node: node that manages cluster by using these services:kube-schedulerexecs programmed tasks.kube-control-managerruns the clusterkube-apiservercommunicates master with workers.
Worker node: contains the APP.kubelet: service that communicates withkube-apiserver, eachkubelethas aPOD.
Abstraction layers of Kubernetes: Deployment => controls => ReplicaSet => controls => POD
Type of scalling in Kubernetes:
- Horizontal: by adding
PODson-demmand. - Vertical: by adding more clusters.
- Docker Desktop is needed. Go to settings, and enable Kubernetes.
- A Kubernetes
yamlmanifests files are needed, they describe sources to be created like volumes, pods and services. The easiest way to generate a manifest is by using adocker-compose.ymlfile that is already used to deploy all containers by once in this repo.- Kompose is highly used to make this. Easiest way by creating a docker image:
docker build -t kompose https://github.com/kubernetes/kompose.git#main - To generate the manifests, go where
docker-compose.ymlfile is placed, and run on Windows:docker run --rm -it -v ${PWD}:/opt kompose sh -c "cd /opt && kompose convert" - In this case, some config attributes should change, so it is preferred to make custom
yamlconfig files, as the located on root folder of this repo.
- Kompose is highly used to make this. Easiest way by creating a docker image:
- To create and run the Kubernetes resources specifying
yamlfiles:❗ It is important to not have spaces or additional commas between file specification.kubectl apply -f api-gateway.yaml,auth-server.yaml,discovery-server.yaml,store-server.yaml - To stop cluster and delete associated containers, on
Docker Desktop, go tosettings -> kubernetes -> reset kubernetes cluster
-
Parts of config files:
- Metadata: typically name of the component.
- Spec: configuration to apply to the component.
- Status: managed by Kubernetes.
-
Deployment example of
discovery-server: Configuration can be changed while running the cluster and update the application by applying the deployment config.apiVersion: apps/v1 kind: Deployment metadata: name: discovery-server labels: app: discovery-server spec: replicas: 2 selector: matchLabels: app: discovery-server #match with specified template (labels:app:...) template: metadata: labels: app: discovery-server spec: containers: - name: discovery-server image: guill392/discovery-service:latest #image on docker hub or local ports: - containerPort: 8761 #port exposed but container, NOT outside cluster network -
Service example, in this one, port 31000 of cluster is beeing exposed using NodePort.
apiVersion: v1 kind: Service metadata: name: discovery-server-service spec: type: NodePort #Nodeport exposes port to outside of the cluster network selector: app: discovery-server #refering deployment name specified on deployment kind config (labels:app:...) ports: - protocol: TCP port: 8761 #port by the service to connect other services or pods targetPort: 8761 #port forwarded to the POD, must match containerPort on deployment nodePort: 31000 #port exposed outside, kubernetes forces >30000
- Forward a service running on Kubernetes:
- Get POD name
kubectl get pods - Forward port of POD
kubectl port-forward podName port:port
- Get POD name
| Command | Usage |
|---|---|
kubectl apply -f yamlFiles |
Create resources from list of yaml files |
kubectl describe svc api-gateway |
Look for IP of a service |
kubectl get nodes/pods/services/deployments |
Get status of nodes, pods, services or deployments running |
kubectl create/edit/delete deployment deplName |
CRUD of deployments |