-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathMakefile
More file actions
163 lines (139 loc) · 5.04 KB
/
Copy pathMakefile
File metadata and controls
163 lines (139 loc) · 5.04 KB
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
154
155
156
157
158
159
160
161
162
163
# Run `make help` to show usage
.DEFAULT_GOAL := help
# Change ports for different kubernetes services
export PORT_ETCD ?= 2379
export PORT_KUBELET ?= 10250
export PORT_FLANNEL ?= 8472
export PORT_KUBE_APISERVER ?= 6443
export PORT_CALICO ?= 5473
# HOSTNAME is the name of the physical host
export HOSTNAME ?= $(shell hostname)
# HOST_IP is the IP address of the physical host. Accessible from other hosts.
export HOST_IP ?= $(shell ip --json route get 1 | jq -r .[0].prefsrc)
# NODE_NAME is the host name of the Kubernetes node running in Rootless Docker.
# Not accessible from other hosts.
export NODE_NAME ?= u7s-$(HOSTNAME)
# NODE_SUBNET is the subnet of the Kubernetes node running in Rootless Docker.
# Not accessible from other hosts.
export NODE_SUBNET ?= $(shell $(CURDIR)/Makefile.d/node-subnet.sh)
# NODE_IP is the IP address of the Kubernetes node running in Rootless Docker.
# Not accessible from other hosts.
export NODE_IP := $(subst .0/24,.100,$(NODE_SUBNET))
export CONTAINER_ENGINE ?= $(shell $(CURDIR)/Makefile.d/detect-container-engine.sh CONTAINER_ENGINE)
export CONTAINER_ENGINE_TYPE ?= $(shell $(CURDIR)/Makefile.d/detect-container-engine.sh CONTAINER_ENGINE_TYPE)
COMPOSE ?= $(shell $(CURDIR)/Makefile.d/detect-container-engine.sh COMPOSE)
NODE_SERVICE_NAME := node
NODE_SHELL := $(COMPOSE) exec \
-e HOST_IP=$(HOST_IP) \
-e NODE_NAME=$(NODE_NAME) \
-e NODE_SUBNET=$(NODE_SUBNET) \
-e NODE_IP=$(NODE_IP) \
-e PORT_KUBE_APISERVER=$(PORT_KUBE_APISERVER) \
-e PORT_FLANNEL=$(PORT_FLANNEL) \
-e PORT_CALICO=$(PORT_CALICO) \
-e PORT_KUBELET=$(PORT_KUBELET) \
-e PORT_ETCD=$(PORT_ETCD) \
$(NODE_SERVICE_NAME)
ifeq ($(CONTAINER_ENGINE),nerdctl)
ifneq (,$(wildcard $(XDG_RUNTIME_DIR)/bypass4netnsd.sock))
export BYPASS4NETNS := true
export BYPASS4NETNS_IGNORE_SUBNETS := ["10.96.0.0/16", "10.244.0.0/16", "$(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
# Podman creates cni files in a shared location, this ensures unique names that do not clobbed one another
sed -i "s/default_network/$(HOSTNAME)/g" docker-compose.yaml
$(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
@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) /bin/bash /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) /usernetes/Makefile.d/install-flannel.sh
.PHONY: install-calico
install-calico:
# Calico daemonset changes and node-level address changes
$(NODE_SHELL) /usernetes/Makefile.d/calico/install-calico.sh