Skip to content

thanglq1/k8s

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Overview

Basic concepts of kubernets: Node, Pods, Deployments, Services, Storage, Configmap and Secret

Install Docker in AWS (Amazon linux)

1. sudo yum update -y (update latest soft)
2. sudo yum install -y docker
3. sudo service docker start

Install Docker Compose in AWS (Amazon linux)

1. sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
2. sudo chmod 666 /var/run/docker.sock
3. sudo chmod +x /usr/local/bin/docker-compose;

Install k8s (Amazon linux)

https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/

Get started kubernetes commands

Check kubernetes version
kubectl version
View cluster info
kubectl cluster-info
Get information about kubernetes Pods, Deployments, Service ...
kubectl get all
Create a resource (resource mean pod, deployment, service)
kubectl create [resource]
Create or modify a resource (resource mean pod, deployment, service)
kubectl apply [resource]
Delete resource
kubectl delete [resource-name]
Forward a port to allow external access
kubectl port-forward [name-of-pod] [external-port]:[internal-port]

Node

https://kubernetes.io/docs/concepts/architecture/nodes/

https://kubernetes.io/docs/tutorials/kubernetes-basics/explore/explore-intro/

A Node is a worker machine in Kubernetes and may be either a virtual or a physical machine, depending on the cluster. Each Node is managed by the control plane. A Node can have one or multiple pods

Pods

https://kubernetes.io/docs/concepts/workloads/pods/

Pods are the smallest deployable units of computing that you can create and manage in Kubernetes.

A Pod (as in a pod of whales or pea pod) is a group of one or more containers, with shared storage and network resources, and a specification for how to run the containers.

List Pods
kubectl get pods
Create Pod
kubectl create -f [pod-file-name].yml eg: kubectl create -f nginx.pod.yml
Create or modify Pod
kubectl apply -f [pod-file-name].yml eg: kubectl apply -f nginx.pod.yml
Delete Pod
kubectl delete pod [name-of-pod] eg: kubectl delete pod my-nginx
Get information of Pod
kubectl describe pod [pod-name] eg: kubectl describe pod my-nginx
Go to bash shell
kubectl exec -it [pod-name] sh eg: kubectl exec -it my-nginx sh

Deployments

https://kubernetes.io/docs/concepts/workloads/controllers/deployment/

Pod can be created and destroyed but can not re-created. So what happend if a Pod is destroyed? Deployment and replicasets make sure Pods stay running and can be use to scale Pods No need create Pod directly. We can use Deployment instead of Pod. A Deployment manages Pods

  • Pods are managed using replicasets
  • Scale replicasets will be scale Pods
  • Zero downtime
List Deployments
kubectl get deployments
List Deployments and their labels
kubectl get deployments --show-labels
Create Deployment
kubectl create -f [deployment-file-name].yml eg: kubectl create -f nginx.deployment.yml
Create or modify Deployment
kubectl apply -f [deployment-file-name].yml eg: kubectl apply -f nginx.deployment.yml
Delete Deployment
kubectl delete deployment [name-of-deployment] eg: kubectl delete deployment my-nginx
Get information of Deployment
kubectl describe deployment [deployment-name] eg: kubectl describe deployment my-nginx
Get rollout status of Deployment
kubectl rollout status deployment [deployment-name] eg: kubectl rollout status deployment my-nginx
Get history of Deployment
kubectl rollout history deployment [deployment-name] eg: kubectl rollout history deployment my-nginx
Rollback Deployment
kubectl rollout undo deployment [deployment-name] eg: kubectl rollout status deployment my-nginx
Scale the Deployment Pods to 5
kubectl scale deployment [deployment-name] --replicas=3

Services

https://kubernetes.io/docs/concepts/services-networking/service/

The Service API, part of Kubernetes, is an abstraction to help you expose groups of Pods over a network. Each Service object defines a logical set of endpoints (usually these endpoints are Pods) along with a policy about how to make those pods accessible.

Since Pods live and die. So we can not rely on a Pod IP address. Services provide a single point of entry for accesing one or more Pods

We have 4 Services type:

  • ClusterIP: Exposes the Service on a cluster-internal IP (default).
  • NodePort: Exposes the Service on each Node's IP at a static port
  • LoadBalancer: Exposes the Service externally using a cloud provider's load balancer
  • ExternalName: Maps the Service to the contents of the externalName field (e.g. foo.bar.example.com), by returning a CNAME record with its value. No proxying of any kind is set up
List Services
kubectl get services
Create Service
kubectl create -f [service-file-name].yml eg: kubectl create -f nginx.deployment.service.yml
Create or modify Service
kubectl apply -f [service-file-name].yml eg: kubectl apply -f nginx.deployment.service.yml
Delete Service
kubectl delete service [name-of-service] eg: kubectl delete -f my-nginx
Get information of Service
kubectl describe service [service-name] eg: kubectl describe service my-nginx
Go to Pod shell, install curl and test a url
kubectl exec [pod-name] –it sh

apk add curl

curl http://podIp

Configmap

Create configmap without file
kubectl create configmap [config-name] --from-literal=[key]=[value] eg: kubectl create configmap simple-web-app-config --from-literal=APP_COLOR=blue --from-literal=APP_MODE=prod
Create configmap from file
``` kubectl create configmap [config-name] --from-file=[path-file] eg: kubectl create configmap simple-web-app-config --from-file=app-config.yml ```
Inject all config map into k8s
``` envFrom: - configMapRef: name: app-config ```
Inject single key into k8s
``` env: - name: APP_COLOR valueFrom: configMapKeyRef: name: app-config key: APP_COLOR ```
Inject volume into k8s
``` volumes: - name: app-config-volume configMap: name: app-config ```

Secret

Use secret to store sensitive data such as password (password must be encoded data to security. Secret is not encrypted. Only encoded)

Encoded data in linux/macos
echo -n "mysql" | base64
result is: bXlzcWw=
Decoded data in linux/macos
echo -n "bXlzcWw=" | base64 --decode
result is: mysql
Create secret without file
kubectl create secret generic [secret-name] --from-literal=[key]=[value] eg: kubectl create secret generic app-secret --from-literal=DB_HOST=mysql --from-literal=DB_USER=root --from-literal=DB_PASSWORD=123456
Create secret from file
``` kubectl create secret generic [secret-name] --from-file=[path-file] eg: kubectl create secret generic app-secret --from-file=app-secret.yml ```
Inject all secret into k8s
``` envFrom: - secretRef: name: app-secret ```
Inject single secret key into k8s
``` env: - name: DB_PASSWORD valueFrom: secretKeyRef: name: app-secret key: DB_PASSWORD ```
Inject volume into k8s
``` volumes: - name: app-secret-volume configMap: name: app-secret ```

Releases

No releases published

Packages

No packages published