Skip to content

Instantly share code, notes, and snippets.

@vinzdef
Last active November 16, 2018 16:13
Show Gist options
  • Save vinzdef/035991d7a9107f7bd84a0238782752b3 to your computer and use it in GitHub Desktop.
Save vinzdef/035991d7a9107f7bd84a0238782752b3 to your computer and use it in GitHub Desktop.
Kubernetes basic setup

Kubernetes basic setup

Huge thanks to Ani for showing me this

ON EVERY MACHINE (slaves and master)

Set hostname
nano /etc/hostname # Write your machine's IP
Prepare for kube utils
apt-get update && apt-get install -y apt-transport-https curl

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -

cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF

apt-get update
Install kube utils
apt-get install -y kubelet kubeadm kubectl
apt-mark hold kubelet kubeadm kubectl
Prepare for docker
apt-get install \
	ca-certificates \
	gnupg2 \
	software-properties-common

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -

apt-key fingerprint 0EBFCD88

sudo add-apt-repository \
	"deb [arch=amd64] https://download.docker.com/linux/debian \
	$(lsb_release -cs) \
	stable"
Install docker
apt-get install docker-ce=18.06.1~ce~3-0~debian
Disable swap
swapoff -a
nano /etc/fstab # Remove swap entry

ONLY ON MASTER

Init kubeadm
IP_MASTER=<your-master-ip>
TOKEN=`kubeadm token generate`

kubeadm init  \
	--pod-network-cidr 10.244.0.0/16 \
	--token $TOKEN \
	--skip-token-print \
	--apiserver-advertise-address $IP_MASTER
As normal (non-root) user in kubeadm
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

ONLY ON SLAVES

kubeadm token create --print-join-command
Produces
kubeadm join <your-master-ip>:6443 \
    --token qv2auc.yppv796dht7q4r1w \
    --discovery-token-ca-cert-hash \
    sha256:9df81aef766ca7d93818bfcf24c00cb1a3413bd148357cce6db7853114a966bd
✨ Your cluster is up and running 🌈

ONLY ON YOUR LOCAL MACHINE (your computer)

Copy config from master
scp root@<your-master-ip>:/home/<your-kube-user>/.kube/config ~/.kube/config
List all nodes and pods
kubectl get nodes
kubectl get pods --all-namespaces
Install flannel (Network plugin)
kubectl apply -f \
    https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
List all pods (now you have flannel pods)
kubectl get pods --all-namespaces
Install dashboard app (this also creates a role for the dashboard but does not bind it)
kubectl apply -f \
    https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml
List all pods (now you have a dashboard pod)
kubectl get pods --all-namespaces
Bind kubernetes-dashboard role to cluster-admin role
kubectl create clusterrolebinding \
	kubernetes-dashboard \
	--clusterrole=cluster-admin \
	--serviceaccount=kube-system:kubernetes-dashboard
Create a namespace for an app (optional)
kubectl create namespace staging
kubectl get namespaces # List 'em
Proxy kubernetes API on your localhost (127.0.0.1)
kubectl proxy
Get dashboard token
kubectl -n kube-system describe secret \
		$(kubectl -n kube-system get secrets | \
            grep kubernetes-dashboard | \
            cut -f1 -d ' ') | \
            grep -E '^token' | \
            cut -f2 -d':' | tr -d '\t'
Dashboard URL, you can login with the token (it is local thanks to kubectl proxy)

http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/#!/login


Todo

  • StorageClass
  • Deploy a custom application
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment