Skip to content

Instantly share code, notes, and snippets.

@vniche
Last active September 9, 2018 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vniche/481cba7e6c16a413a3c60d7e6e81ea00 to your computer and use it in GitHub Desktop.
Save vniche/481cba7e6c16a413a3c60d7e6e81ea00 to your computer and use it in GitHub Desktop.
Single node Kubernetes on your local machine 🚢
#!/bin/bash
# For binaries installation instructions go for https://kubernetes.io/docs/tasks/tools/install-kubeadm/
# Clean previous installations:
sudo -E kubeadm reset
rm -rf ~/.kube/*
# Comment search domains configured in your resolv.conf if you have any, as stated in:https://github.com/kubernetes/kubernetes/issues/57709
sudo sed -e 's/^search/#search/' -i /etc/resolv.conf
# Initilize a new Kubernetes, this will create a few pods, more info on https://kubernetes.io/docs/concepts/architecture/cloud-controller/#design
sudo -E kubeadm init --pod-network-cidr=10.244.0.0/16 --kubernetes-version=stable-1.10 &
# Wait for things to start
sleep 300s
# Copy admin config to your profile's kubernetes default config file and fix it permissions
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
# Install Flannel as network plugin, more info on: https://github.com/coreos/flannel
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
# Untaint node as master, so pods other than adminstrative can be scheduled
kubectl taint nodes --all node-role.kubernetes.io/master-
# Deploy Kubernetes Dashboard
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml
# Create service account and associate to cluster admin roles
cat <<EOF | kubectl create -f -
apiVersion: v1
kind: ServiceAccount
metadata:
name: local-admin
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: local-admin
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: local-admin
namespace: kube-system
EOF
# Deploy Heapster so we have more info on pods resource consumption
kubectl create -f https://raw.githubusercontent.com/kubernetes/heapster/master/deploy/kube-config/influxdb/grafana.yaml
kubectl create -f https://raw.githubusercontent.com/kubernetes/heapster/master/deploy/kube-config/influxdb/heapster.yaml
kubectl create -f https://raw.githubusercontent.com/kubernetes/heapster/master/deploy/kube-config/influxdb/influxdb.yaml
kubectl create -f https://raw.githubusercontent.com/kubernetes/heapster/master/deploy/kube-config/rbac/heapster-rbac.yaml
# Custom DNS config to enable pods to reach the internet, more info in https://kubernetes.io/docs/tasks/administer-cluster/dns-custom-nameservers/#configure-stub-domain-and-upstream-dns-servers
#cat <<EOF | kubectl create -f -
#apiVersion: v1
#kind: ConfigMap
#metadata:
# name: kube-dns
# namespace: kube-system
#data:
# upstreamNameservers: |
# ["8.8.8.8", "8.8.4.4"]
#EOF
# Replace system pods so custom DNS config applies
kubectl get pod $(kubectl get pods -n kube-system | awk '$1 != "NAME" { print $1 }') -o yaml -n kube-system | kubectl replace -f -
# Get and print token for sake of usage
echo "Dashboard will ask for a Kubeconfig or Token to login, here is a cluster-admin token to use"
kubectl -n kube-system describe secret $(kubectl -n kube-system describe sa local-admin | grep token | awk '$1 == "Tokens:" {print $2}') | awk '$1 == "token:" { print }'
@vniche
Copy link
Author

vniche commented Jun 4, 2018

And things should be up and running:

$ kubectl get pods --all-namespaces
NAMESPACE     NAME                                READY     STATUS    RESTARTS   AGE
kube-system   etcd-greyhound                      1/1       Running   0          25s
kube-system   kube-apiserver-greyhound            1/1       Running   0          48s
kube-system   kube-controller-manager-greyhound   1/1       Running   0          39s
kube-system   kube-dns-86f4d74b45-sbl2v           3/3       Running   0          1m
kube-system   kube-flannel-ds-dvxxf               1/1       Running   0          1m
kube-system   kube-proxy-xxq4f                    1/1       Running   0          1m
kube-system   kube-scheduler-greyhound            1/1       Running   0          37s

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