-
Notifications
You must be signed in to change notification settings - Fork 59
/
Makefile
153 lines (130 loc) · 4.75 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Run `make help` to show usage
.DEFAULT_GOAL := help
HOSTNAME ?= $(shell hostname)
# HOSTNAME is the name of the physical host
export HOSTNAME := $(HOSTNAME)
HOST_IP ?= $(shell ip --json route get 1 | jq -r .[0].prefsrc)
NODE_NAME ?= u7s-$(HOSTNAME)
NODE_SUBNET ?= $(shell $(CURDIR)/Makefile.d/node-subnet.sh)
# U7S_HOST_IP is the IP address of the physical host. Accessible from other hosts.
export U7S_HOST_IP := $(HOST_IP)
# U7S_NODE_NAME is the host name of the Kubernetes node running in Rootless Docker.
# Not accessible from other hosts.
export U7S_NODE_NAME:= $(NODE_NAME)
# U7S_NODE_NAME is the subnet of the Kubernetes node running in Rootless Docker.
# Not accessible from other hosts.
export U7S_NODE_SUBNET := $(NODE_SUBNET)
# U7S_NODE_IP is the IP address of the Kubernetes node running in Rootless Docker.
# Not accessible from other hosts.
export U7S_NODE_IP := $(subst .0/24,.100,$(U7S_NODE_SUBNET))
CONTAINER_ENGINE ?= $(shell $(CURDIR)/Makefile.d/detect-container-engine.sh CONTAINER_ENGINE)
export CONTAINER_ENGINE := $(CONTAINER_ENGINE)
CONTAINER_ENGINE_TYPE ?= $(shell $(CURDIR)/Makefile.d/detect-container-engine.sh CONTAINER_ENGINE_TYPE)
export CONTAINER_ENGINE_TYPE := $(CONTAINER_ENGINE_TYPE)
COMPOSE ?= $(shell $(CURDIR)/Makefile.d/detect-container-engine.sh COMPOSE)
NODE_SERVICE_NAME := node
NODE_SHELL := $(COMPOSE) exec \
-e U7S_HOST_IP=$(U7S_HOST_IP) \
-e U7S_NODE_NAME=$(U7S_NODE_NAME) \
-e U7S_NODE_SUBNET=$(U7S_NODE_SUBNET) \
-e U7S_NODE_IP=$(U7S_NODE_IP) \
$(NODE_SERVICE_NAME)
ifeq ($(CONTAINER_ENGINE),nerdctl)
ifneq (,$(wildcard $(XDG_RUNTIME_DIR)/bypass4netnsd.sock))
export U7S_B4NN := true
export U7S_B4NN_IGNORE_SUBNETS := ["10.96.0.0/16", "10.244.0.0/16", "$(U7S_NODE_SUBNET)"]
endif
endif
.PHONY: help
help:
@echo '# Bootstrap a cluster'
@echo 'make up'
@echo 'make kubeadm-init'
@echo 'make install-flannel'
@echo
@echo '# Enable kubectl'
@echo 'make kubeconfig'
@echo 'export KUBECONFIG=$$(pwd)/kubeconfig'
@echo 'kubectl get pods -A'
@echo
@echo '# Multi-host'
@echo 'make join-command'
@echo 'scp join-command another-host:~/usernetes'
@echo 'ssh another-host make -C ~/usernetes up kubeadm-join'
@echo 'make sync-external-ip'
@echo
@echo '# Debug'
@echo 'make logs'
@echo 'make shell'
@echo 'make kubeadm-reset'
@echo 'make down-v'
@echo 'kubectl taint nodes --all node-role.kubernetes.io/control-plane-'
.PHONY: check-preflight
check-preflight:
./Makefile.d/check-preflight.sh
.PHONY: render
render: check-preflight
$(COMPOSE) config
.PHONY: up
up: check-preflight
$(COMPOSE) up --build -d
.PHONY: down
down:
$(COMPOSE) down
.PHONY: down-v
down-v:
$(COMPOSE) down -v
.PHONY: shell
shell:
$(NODE_SHELL) bash
.PHONY: logs
logs:
$(NODE_SHELL) journalctl --follow --since="1 day ago"
.PHONY: kubeconfig
kubeconfig:
$(COMPOSE) exec -T $(NODE_SERVICE_NAME) sed -e "s/$(NODE_NAME)/127.0.0.1/g" /etc/kubernetes/admin.conf >kubeconfig
@echo "# Run the following command by yourself:"
@echo "export KUBECONFIG=$(shell pwd)/kubeconfig"
ifeq ($(shell command -v kubectl 2> /dev/null),)
@echo "# To install kubectl, run the following command too:"
@echo "make kubectl"
endif
.PHONY: kubectl
kubectl:
$(COMPOSE) exec -T --workdir=/usr/bin $(NODE_SERVICE_NAME) tar c kubectl | tar xv
@echo "# Run the following command by yourself:"
@echo "export PATH=$(shell pwd):\$$PATH"
@echo "source <(kubectl completion bash)"
.PHONY: join-command
join-command:
echo "#!/bin/bash" >join-command
echo "set -eux -o pipefail" >>join-command
echo "echo \"$(HOST_IP) $(NODE_NAME)\" >/etc/hosts.u7s" >>join-command
echo "cat /etc/hosts.u7s >>/etc/hosts" >>join-command
$(NODE_SHELL) kubeadm token create --print-join-command | tr -d '\r' >>join-command
chmod +x join-command
@echo "# Copy the 'join-command' file to another host, and run the following commands:"
@echo "# On the other host (the new worker):"
@echo "# make kubeadm-join"
@echo "# On this host (the control plane):"
@echo "# make sync-external-ip"
.PHONY: kubeadm-init
kubeadm-init:
$(NODE_SHELL) sh -euc "envsubst </usernetes/kubeadm-config.yaml >/tmp/kubeadm-config.yaml"
$(NODE_SHELL) kubeadm init --config /tmp/kubeadm-config.yaml --skip-token-print
$(MAKE) sync-external-ip
@echo "# Run 'make join-command' to print the join command"
.PHONY: sync-external-ip
sync-external-ip:
$(NODE_SHELL) /usernetes/Makefile.d/sync-external-ip.sh
.PHONY: kubeadm-join
kubeadm-join:
$(NODE_SHELL) sh -euc "envsubst </usernetes/kubeadm-config.yaml >/tmp/kubeadm-config.yaml"
$(NODE_SHELL) /usernetes/join-command
@echo "# Run 'make sync-external-ip' on the control plane"
.PHONY: kubeadm-reset
kubeadm-reset:
$(NODE_SHELL) kubeadm reset --force
.PHONY: install-flannel
install-flannel:
$(NODE_SHELL) kubectl apply -f https://github.com/flannel-io/flannel/releases/download/v0.25.5/kube-flannel.yml