Skip to content

Instantly share code, notes, and snippets.

@percyvega
Created December 6, 2022 04:11
Show Gist options
  • Save percyvega/265814247e043c96c394cca4f13a06b9 to your computer and use it in GitHub Desktop.
Save percyvega/265814247e043c96c394cca4f13a06b9 to your computer and use it in GitHub Desktop.

What is Kubernetes

* Kubernetes is an open-source container orchestrator
* K8s helps with:
	* Cluster Management of machines (perform health checks, bounce them, change cluster size)
	* Scheduling containers on those machines (match CPU/memory requirements with right machines).
* Defines API objects to describe your cluster’s desired state.
* Provides basic monitoring, logging, and health checking.
* Reconciles desired state and current state.

Kubernetes Resources

* Pod
	* Pods are ephemeral, so the IP address assigned to them can not be relied upon for applications to communicate.
	* Smallest deployable unit. K8s manages pods rather than containers.
	* Composed of 1 (usually) or 2 (tightly coupled) containers e.g. file web server and a side cart to refresh the cache.
	* These containers share resources (file system, storage), and the network (they communicate via localhost).
* Deployment
	* Provides declarative updates for pods.
	* You describe a desired state in a deployment object, and the K8s cluster changes the actual state to that desired state.
	* Creates multiple replicas of a pod.
	* Rolls out a new version or rolls back to an earlier version.
* Service
	* Defines a logical set of pods (with the same label) with an API that allows it to communicate with other applications.
	* Provides its pods with a simple L4 DNS load balancer.

Usual K8s workflow

1. Package Java application
2. Create/Publish Docker image
3. Create K8s resource manifest
4. Create K8s cluster
5. Deploy K8s resources in the cluster

kubectl

The kubectl command allows K8s objects to be managed kubectl version Check Kubernetes version

kubectl cluster-info
  View cluster information

kubectl get [resource]
  all
    Retrieve information about Kubernetes Pods, Deployments, Services, and more
    e.g. kubectl get all
  deployments
    e.g. kubectl get deployments
  services
    e.g. kubectl get services
  pods
    e.g. kubectl get pods

kubectl run [container-name] -image=[ image-name]
  Simple way to create a Deployment for a Pod

kubectl port-forward [pod] [ports]
  Forward a port to allow external access

kubectl expose ...
  Expose a port for a Deployment/Pod

kubectl create [resource]
  Create a resource
  e.g. kubectl create -f k8s.yml

kubectl apply [resource]
  Create or modify a resource

kubectl version
kubectl config get-contexts

Deployment and service YAML file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello
  template:
    metadata:
      labels:
        app: hello
    spec:
      containers:
        - name: hello
          image: percyvega/hello
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 8080
              name: "http"
            - containerPort: 5005
              name: "debug"
---
apiVersion: v1
kind: Service
metadata:
  name: hello
spec:
  selector:
    app: hello
  ports:
    - name: http
      protocol: TCP
      port: 8080
      targetPort: 8080
    - name: debug
      protocol: TCP
      port: 5005
      targetPort: 5005
  type: LoadBalancer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment