Purpose / Feature
Steps
kubeadm
, kind
or minikube
for cluster administration.
References:
Control plane
as it manages everything inside cluster. E.g. Creating new nodes or creating new deployment for a service etc.cubectl
commands are execued on Master Node
.kubectl create deployment ....
command.
0 downtime
using kubectl set image deployment ....
command.
kubectl expose deployment hello-world-rest-api --type=LoadBalancer --port=8080
GCP load balancers
for all load balancers created on platform.kubernetes-poc-1
.Kubernetes API
in GCP platform (pop-up).Standard
.
gcloud container clusters get-credentials my-standard-cluster-1 --zone us-central1-c --project operating-ally-446306-t7
kubectl create deployment hello-world-rest-api --image=in28min/hello-world-rest-api:0.0.1.RELEASE
kubectl get deployment
kubectl expose deployment hello-world-rest-api --type=LoadBalancer --port=8080
kubectl get services
kubectl get services --watch
curl 35.184.204.214:8080/hello-world
https://shell.cloud.google.com/?pli=1&show=ide&environment_deployment=ide
# initalize the sdk
# Requires login to GCP account.
gcloud init
# If using gcloud local installation
# login to GCP account
gcloud auth login
# Connect to gcloud-shell
# It will generate and store a local ssh file for future logins
gcloud cloud-shell ssh
# Automatic authentication with GCP CLI tools in Cloud Shell is disabled. To enable, rerun above command with `--authorize-session` flag.
gcloud cloud-shell --authorize-session ssh
# set project using project id-operating-ally-446306-t7
# Check project info for 'project-id'
gcloud config set project operating-ally-446306-t7
# Fetch cluster auth data and configures cluster
gcloud container clusters get-credentials my-standard-cluster-1 --zone us-central1-c --project operating-ally-446306-t7
# Deploy microservices
kubectl create deployment hello-world-rest-api --image=in28min/hello-world-rest-api:0.0.1.RELEASE
# View deployed services
kubectl get deployment
# View deployment info for specific deployment
kubectl get deployment currency-exchange-service
# view the deployment config in yaml format
kubectl get deployment currency-exchange-service -o yaml
# Exponse deployed service using load blancer (stable service IP) on port 8080
kubectl expose deployment hello-world-rest-api --type=LoadBalancer --port=8080
# List running service with details (IP, port, age etc.)
kubectl get services
# View service info for specific deployment
kubectl get services currency-exchange-service
# view the deployment config in yaml format
kubectl get services currency-exchange-service -o yaml
#watch progress
kubectl get services --watch
# hit service
curl 35.184.204.214:8080/hello-world
# get list of events for the cluster
kubectl get events
# Scale the service deployement to 3 PODs
kubectl scale deployment hello-world-rest-api --replicas=3
# List all the PODs
kubectl get pod
# Mnual scaling
# Default cluster pool has only 3 nodes.
# If we need more pods then we need to scale-up our cluster
gcloud container clusters resize my-standard-cluster-1 --node-pool default-pool --num-nodes=2 --zone=us-central1-c
# Horizontal Auto scaling microservice
# It can scale only upto number of nodes present in cluster.
kubectl autoscale deployment hello-world-rest-api --max=4 --cpu-percent=70
# Details of HPA - Horizontal pod autoscaling
kubectl get hpa
# delete hpa
kubectl delete hpa hello-world-rest-api
# cluster auto scaling
gcloud container cluster update my-standard-cluster-1 --enable-autoscaling --min-nodes=1 --max-nodes=10
# Config Map
# Configuration to database or something like that
kubectl create configmap hello-world-config --from-literal=RDS_DB_NAME=todos
# list configs present
kubectl get configmap
# Show data in config map
kubectl describe configmap hello-world-config
# Store secrets / password in config map
# Secret is stores as 'Opaque' type
kubectl create secret generic hello-world-secrets-1 --from-literal=RDS_PASSWORD=dummytodos
kubectl get secret
kubectl describe secret hello-world-secrets-1
# Update/deploy cluster using yaml definition
kubectl apply -f deployment.yaml
# Create new node-pool
gcloud container node-pools create my-new-pool --cluster=my-standard-cluster-1 --zone=us-central1-c
# Delete nodes from node-pool
gcloud container node-pools delete my-new-pool --cluster=my-standard-cluster-1 --zone=us-central1-c
# list node pools details (name, disk-size, machine-type, node-version)
gcloud container node-pools list --zone=us-central1-c --cluster=my-standard-cluster-1
# get detailed status of pods - Name, status, containers, restart, IP, age etc.
kubectl get pods -o wide
# Update / Set a new image for running deployement / microservice
# This will automatically deploy the new image with '0 downtime'.
kubectl set image deployment hello-world-rest-api hello-world-rest-api=in28min/hello-world-rest-api:0.0.2.RELEASE
# list services replicasets & pods
kubectl get services
kubectl get replicasets
kubectl get pods
# delete pod
kubectl delete pod hello-world-rest-api-58dc9d7fcc-8pv7r
# Set desired cound of Pods in the replica-set
kubectl scale deployment hello-world-rest-api --replicas=1
# List projects
gcloud projects list
# delete ervice
kubectl delete service hello-world-rest-api
# delete deployment
kubectl delete deployment hello-world-rest-api
# Delete all services related to a service - pods, service replicasets & deployment.apps
kubectl delete all -l app=currency-exchange-service
# delete cluster
gcloud container clusters delete my-standard-cluster-1 --zone us-central1-c
# check logs of a POD
kubectl logs currency-exchange-service-5bf8dd7984-p7tmm
# follow / tail logs of a POD
kubectl logs -f currency-exchange-service-5bf8dd7984-p7tmm
Section 1
for instructions.gke-gcloud-auth-plugin
required to use kubectl
with gcloud
.
gcloud components install gke-gcloud-auth-plugin
gcloud init
to initialize the sdk.gcloud auth login
.Note: Install the SDK where user level access are granted. Don't use root user / sudo.
e2-currency-exchange-service-kubernetes &
e3-currency-conversion-service-openfeign-kubernetes
Purpose / Feature
gcloud CLI
and kubectl
commands.Steps
kubernetes
provide inbuild support for these funcationalities.
@FeignClient(name = "E2-CURRENCY-EXCHANGE-SERVICE-KUBERNETES", url = "${CURRENCY_EXCHANGE_SERVICE_HOST:http://localhost}:8000")
gcloud auth login
gcloud container clusters get-credentials my-standard-cluster-1 --zone us-central1-c --project operating-ally-446306-t7
kubectl create deployment currency-exchange-service --image=srvivek/e2-currency-exchange-service-kubernetes:0.0.11-SNAPSHOT
kubectl create deployment currency-conversion-service --image=srvivek/e3-currency-conversion-service-openfeign-kubernetes:0.0.11-SNAPSHOT
kubectl set env deployment/currency-conversion-service CURRENCY_EXCHANGE_SERVICE_HOST=currency-exchange-service
kubectl set env pods --all --list
kubectl expose deployment currency-exchange-service --type=LoadBalancer --port=8000
kubectl expose deployment currency-conversion-service --type=LoadBalancer --port=8100
kubectl get deployment currency-exchange-service -o yaml >> deployment.yaml
kubectl get services currency-exchange-service -o yaml >> services.yaml
kubectl diff -f deployment.yaml
kubectl apply -f deployment.yaml
watch -n 0.1 curl http://34.44.230.142:8100/currency-conversion-feign/from/UsD/to/iNr/quantity/100
deployment.yaml
code in below section.Code / Config changes
import org.springframework.cloud.openfeign.FeignClient;
@FeignClient(name = "E2-CURRENCY-EXCHANGE-SERVICE-KUBERNETES", url = "${CURRENCY_EXCHANGE_SERVICE_HOST:http://localhost}:8000")
public interface CurrencyExchangeProxy {
/**
* Method as defined in the host service.
* @param from
* @param to
* @return
*/
@GetMapping("/jpa/currency-exchange/from/{from}/to/{to}")
public CurrencyConversion retrieveExchangeRateFromDatabase(@PathVariable String from, @PathVariable String to);
}
spring.application.name=e3-currency-conversion-service-ofeign-kubernetes
server.port=8100
# Start: CHANGE-KUBERNETES
management.endpoint.health.probes.enabled=true
management.health.livenessstate.enabled=true
management.health.readinessstate.enabled=true
# End: CHANGE-KUBERNETES
# Start: Spring Load Balancer
# Enable load balancer.
spring.cloud.loadbalancer.enabled=true
# Enables LoadBalancer retries.
spring.cloud.loadbalancer.retry.enabled=true
# End: Spring Load Balancer
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
labels:
app: currency-exchange-service
name: currency-exchange-service
namespace: default
spec:
replicas: 2 # 2 instances should be available
selector:
matchLabels:
app: currency-exchange-service
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
app: currency-exchange-service
spec:
containers:
- image: srvivek/e2-currency-exchange-service-kubernetes:0.0.11-SNAPSHOT
imagePullPolicy: IfNotPresent
name: e2-currency-exchange-service-kubernetes
restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
annotations:
cloud.google.com/neg: '{"ingress":true}'
labels:
app: currency-exchange-service
name: currency-exchange-service
namespace: default
spec:
allocateLoadBalancerNodePorts: true
ports:
- port: 8000
protocol: TCP
targetPort: 8000
selector:
app: currency-exchange-service
sessionAffinity: None
type: LoadBalancer
Notes:
<DEPLOYMENT_SERVICE_NAME>_SERVICE_HOST
App links:
References:
API Library
.cloud logging API
Stackdriver API
Manage
andwatch -n 0.1 curl http://34.42.22.155:8100/currency-conversion-feign/from/UsD/to/iNr/quantity/100
Features section
and click on:
View logs
- To check logs.
cluster
, clear this.Kubernetes containers
in filter and select desired pod
.
View GKE Dashboard
- For Cloud monitoring.resource.type="k8s_container" textPayload:"a0f973a78782bd1ec580c444fda324c1"
Note: Ensure following dependencies are present in POM.
implementation platform('io.micrometer:micrometer-tracing-bom:latest.release')
implementation 'io.micrometer:micrometer-tracing'
References:
DNS subdomain name
.kubectl create configmap currency-conversion-service-openfeign --from-literal=CURRENCY_EXCHANGE_URI=http://currency-exchange-service
kubectl get configmap currency-conversion-service-openfeign
kubectl get configmap currency-conversion-service-openfeign -o yaml
deloyment.yaml
file.
envFrom
declaration in containers
to read data from configmap
.env
declaration from containers
.kubectl diff -f deployment-0-3-with-configmap.yaml
kubectl apply -f deployment-0-3-with-configmap.yaml
Note: Must veriy the difference before applying the update yaml file. I also helps to validate yaml by throwing error, if there's any syntactical mistakes.
About / Introduction
Readiness probe
is not succesfull. no traffic is sent.actuator/health/readiness
actuator/health/liveness
Steps
kubectl rollout history deployment currency-conversion-service-openfeign
kubectl rollout undo deployment currency-conversion-service-openfeign --to-revision=2
deployment.yaml
# ... other config
template:
metadata:
labels:
app: currency-exchange-service
spec:
containers:
- image: srvivek/e2-currency-exchange-service-kubernetes:0.0.11-SNAPSHOT
imagePullPolicy: IfNotPresent
name: e2-currency-exchange-service-kubernetes
readinessProbe:
httpGet:
port: 8000
path: /actuator/health/readiness
livenessProbe:
httpGet:
port: 8000
path: /actuator/health/liveness
restartPolicy: Always
# ... other config
Note: Readiness and Liveness probes help to increase high availability of our application.
About / Introduction
Steps
kubectl autoscale deployment currency-conversion-service-openfeign --min=1 --max=3 --cpu-percent=5
kubectl get hpa
kubectl get hpa -o yaml
kubectl top pods
kubectl top nodes
References: