Skip to content

Instantly share code, notes, and snippets.

@elpddev
Last active July 22, 2020 17:53
Show Gist options
  • Save elpddev/c559dc0eb6a7c724e542e3eca2bcc1d4 to your computer and use it in GitHub Desktop.
Save elpddev/c559dc0eb6a7c724e542e3eca2bcc1d4 to your computer and use it in GitHub Desktop.
#kubernetes

Kubernetes Cheetsheat

Deployments

Depoloyment Config

apiVersion: v1
kind: Pod
metadata:
  name: my-nginx
spec:
  containers:
    - name: my-nginx
      image: nginx:alpine

Creating a Deployment

Create a deployment

k create -f deployment_a.yml

Create or update a deployment with the ability to apply changes afterward.

k create -f mypod.yaml --save-config

The same as create --save-config but also for updating.

k apply -f deployment_a.yml

Exposing a Deployment

Create a new service that expose the deployment to the outside through a specificed port.

k expose deployment -f deployment_a.yml --type=NodePort --port=80 --name=nginxexpose

Deleting a Deployment

k delete -f deployment_a.yml

Services

A servcie is a server instance that has static ip in the deployment and connect and allow access to nodes from outside or other nodes.

Needed because nodes are ephemerical and has ip which is not constant.

Service Types

https://stackoverflow.com/questions/41509439/whats-the-difference-between-clusterip-nodeport-and-loadbalancer-service-types

ClusterIP

A service that gets a cluster ip that is exposed only within the cluster. It is used to talk and connect between pods in the same cluster.

NodePort

A service that is exposed to the outside of the cluster by all the cluster nodes at a specific port. Outside party can communicate with the service throguth the node ip + the port. That communication is then redirected to the service, which in turn redirect it to the relevant pods it handles.

Load Balancer

External Name

A service that handle communication to an external party. This allow elements in the cluster to not know the details of the external party and encapsulate that logic in the external name service. When the communication details of the external party changes, only the external name service needed to be updated with that information.

Utilities

kubectrl port-forward

Create temporary tunnel which allow connection between the outside and the inside of a cluster.

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#port-forward https://stackoverflow.com/questions/51468491/how-kubectl-port-forward-works

Examples

Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in the pod

kubectl port-forward pod/mypod 5000 6000

Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in a pod selected by the deployment

kubectl port-forward deployment/mydeployment 5000 6000

Creating a Service

ClusterIP Example

apiVersion: v1
kind: Service
metadata:
  name: nginx-clusterip
spec:
  type: ClusterIP
  selector:
    app: my-nginx
  ports:
  - port: 8080
    targetPort: 80

NodePort Example

apiVersion: v1
kind: Service
metadata:
  name: nginx-nodeport
spec:
  type: NodePort
  selector:
    tier: frontend
  ports:
    - port: 80
      targetPort: 80
      nodePort: 31000

LoadBalancer Example

apiVersion: v1
kind: Service
metadata:
  name: nginx-loadbalancer
spec:
  type: LoadBalancer
  selector:
    tier: frontend
  ports:
    - name: "80"
      port: 80
      targetPort: 80

Storage

Valumes

emptyDir - Volume Type

  • Tied to the pod lifecycle. Goes away when the pod goes away.
  • Used to share data between containers in the same pod.

hostPath - Volume Type

  • Tied to the node lifecycle. Goes away when the node goes away.

nsf - Volume Type

Network file system. The pod access an external shared network storage access.

configMap/secret - Volume Type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment