项目作者: protess

项目描述 :
# K8S Crash Course
高级语言: Shell
项目地址: git://github.com/protess/k8scc.git
创建时间: 2019-08-09T03:26:20Z
项目社区:https://github.com/protess/k8scc

开源协议:The Unlicense

下载


Kubernetes Crash Course

Kubernetes Crash Course Blog Post(KR) - no link yet

Prerequisite

Requirement: Mac model 2010+, OS 10.12+

Docker

Install docker for mac.
Follow docker guide to increase resource limit(need to set memory to 8GB+).

Go

Install Go download from [golang.org]
You can manage Go verions with [gvm]

Kubectl

  1. ### Install with Homebrew on macOS
  2. brew install kubernetes-cli
  3. ### download kubectl 1.16.0
  4. curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.16.0/bin/darwin/amd64/kubectl
  5. chmod -x ./kubectl
  6. mv ./kubectl /usr/local/bin/kubectl
  7. ### if kubectl isn't executable
  8. chmod 755 /usr/local/bin/kubectl

KIND

Refer to KIND for more details

  1. GO111MODULE="on" go get sigs.k8s.io/kind@v0.5.1
  2. export PATH="$PATH:$(go env GOPATH)/bin"

Aliases

  1. alias k='kubectl'
  2. alias chrome="/Applications/Google\\ \\Chrome.app/Contents/MacOS/Google\\ \\Chrome"

Useful Tools

kubectl command shell [auto-completion]

  1. ### for oh-my-zsh
  2. plugins=(... kubectl)
  • k8s context/namespace changer [kubectx/kubens]
  • Awesome k8s shell prompt kube ps1
  • Very cool k8s CLI manage tool [k9s]
  • Multiple pods log tool for k8s [stern]

Practice

Create k8s multi-node cluster with KIND

Will create 1 master and 2 worker nodes.

  1. kind create cluster --name kind-m --config kind-test-config.yaml

Change k8s config

Add configs from ‘.kube/kind-config-kind-m’ to ‘.kube/config’ file
or:

  1. export KUBECONFIG="$(kind get kubeconfig-path --name="kind-m")"

Note: k8s config file may contain multiple configs.

Verify k8s cluster

  1. kubectl cluster-info
  2. kubectl get nodes
  3. kubectl get pods --all-namespaces

Install metrics-server

Refer to metrics-server for more information

  1. kubectl apply -f ./metrics-server

Install Ingress-nginx

Installation Guide

  1. kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/mandatory.yaml
  2. ### create service with nodeport 32080, 32433
  3. kubectl apply -f ./ingress-nginx/

Deploy MariaDB(MySQL) for Wordpress - Stateful Sets

Deploy MariaDB:

  1. kubectl create namespace wordpress
  2. kubectl apply -f ./mysql
  3. ### Check Stateful Sets
  4. kubectl get sts -n wordpress
  5. kubectl describe sts mysql -n wordpress

Deploy Wordpress - Deployment

Deploy Wordpress:

  1. ### get you local ip
  2. IP=$(ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk '{print $2}')
  3. ### replace ingress host with your IP
  4. sed -i.bak "s/host.*/host: blog."$IP".nip.io/g" ./wordpress/ingress.yaml
  5. kubectl apply -f ./wordpress
  6. ### edit deployment
  7. kubectl edit deploy wordpress
  8. ### port forward to check if wordpress is running correctly
  9. WPOD=$(kubectl get -n wordpress pod -l app=wordpress -o jsonpath="{.items[0].metadata.name}")
  10. kubectl port-forward pod/$WPOD 8090:80 -n wordpress

Scale in/out Deployments:

  1. kubectl scale deploy wordpress --replicas=3 -n wordpress
  2. kubectl scale deploy wordpress --replicas=1 -n wordpress
  3. ### check services for wordpress
  4. kubectl get svc -n wordpress
  5. ### check ingress
  6. kubectl get ingress -n wordpress
  7. ### other commands
  8. k explain deployment --recursive
  9. k explain svc --recursive
  10. k get pods -o wide --sort-by="{.spec.nodeName}"

Expose Ingress

Intall socat to expose nodeport on local 80 port:

  1. docker run -d --name kind-proxy-80 \
  2. --publish 80:80 \
  3. --link kind-m-control-plane:target \
  4. alpine/socat \
  5. tcp-listen:80,fork,reuseaddr tcp-connect:target:32080

Test

Test with your own DNS:

  1. chrome http://blog.$IP.nip.io

Delete resources and cluster

  1. kubectl --namespace=wordpress delete --all
  2. kubectl delete --namespace wordpress
  3. kind delete cluster --name kind-m
  4. ### remove socat
  5. docker rm -f kind-proxy-80

Extra

Web UI(Dashboard)

Install [dashboard]:

  1. kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta4/aio/deploy/recommended.yaml
  2. ### Run below to fix auth
  3. k apply -f ./dashboard
  4. ### Run below and copy hash code from token:
  5. ./dashboard/getsecret.sh
  6. ### Run below from another terminal:
  7. k proxy

Click this /proxy/">link to open dashboard in your web browser.

To disable session time-out:

  1. kubectl edit deployment kubernetes-dashboard -n kube-system
  2. ### add following after arg:
  3. - args:
  4. - --token-ttl=0

Autoscaler

  1. ### metrics-server must be running
  2. kubectl apply -f ./extra

Demo - ASCIINEMA

asciicast

Debug

Refer to K8S Debug Services for details

  1. # Run alpine image to troubleshoot
  2. kubectl run -it --rm --restart=Never alpine --image=alpine sh
  3. # lookup service
  4. nslookup wordpress
  5. # check svc name/IP
  6. wget -SO- wordpress:8081
  7. wget -SO- <SVC_IP>:8081
  8. # check pod endpoint
  9. wget -SO- <POD ID:80>

Blogs and Documentations

subicura님의 쿠버네티스 시작하기 블로그

Kubernetes Documentation(KR)

K8S Deep Dive: API Server Part1, Part2, Part3

Google Container(KR)

이어형님 딥다이브

Dockerfile Best Practices

[kubectx/kubens]:
https://github.com/ahmetb/kubectx

[auto-completion]:
https://kubernetes.io/docs/tasks/tools/install-kubectl/?source=#enabling-shell-autocompletion

[dashboard]:
https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/

[KIND]:
https://kind.sigs.k8s.io/docs/user/quick-start

[golang.org]:
https://golang.org/dl/

[gvm]:
https://github.com/moovweb/gvm

[Homebrew]:
https://brew.sh/

[k9s]:
https://k9ss.io/?fbclid=IwAR0MQO9yBF5iKpJlDkuSNtrWGy72zK81I-j071lrKQsV1DLhloOMknOLd64

[stern]:
https://github.com/wercker/stern