Last active
August 9, 2019 14:48
-
-
Save chanwit/697dc08294067430824dcbff1dd62431 to your computer and use it in GitHub Desktop.
AINTEC 2019 Tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cniVersion": "0.3.1", | |
"name": "my-cni-demo", | |
"type": "my-cni-demo", | |
"podcidr": "10.42.0.0/24" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cniVersion": "0.3.1", | |
"name": "my-cni-demo", | |
"type": "my-cni-demo", | |
"podcidr": "10.42.1.0/24" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apiVersion: v1 | |
kind: Pod | |
metadata: | |
name: alpine1 | |
spec: | |
containers: | |
- name: alpine | |
image: alpine | |
command: | |
- "/bin/ash" | |
- "-c" | |
- "sleep 2000" | |
nodeSelector: | |
kubernetes.io/hostname: node1 | |
--- | |
apiVersion: v1 | |
kind: Pod | |
metadata: | |
name: nginx1 | |
spec: | |
containers: | |
- name: nginx | |
image: nginx | |
nodeSelector: | |
kubernetes.io/hostname: node1 | |
--- | |
apiVersion: v1 | |
kind: Pod | |
metadata: | |
name: nginx2 | |
spec: | |
containers: | |
- name: nginx | |
image: nginx | |
nodeSelector: | |
kubernetes.io/hostname: node2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo k3s server \ | |
--no-deploy=coredns \ | |
--no-deploy=servicelb \ | |
--no-deploy=traefik \ | |
--no-flannel \ | |
--node-name=node1 \ | |
--node-ip=10.10.10.10 \ | |
--bind-address=10.10.10.10 \ | |
--advertise-address=10.10.10.10 \ | |
--cluster-secret=aintec2019 \ | |
-o /vagrant/kubeconfig.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo k3s agent \ | |
--no-flannel \ | |
--node-name=node2 \ | |
--node-ip=10.10.10.11 \ | |
--cluster-secret=aintec2019 \ | |
--server=https://10.10.10.10:6443 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://github.com/rancher/k3s/releases/download/v0.7.0/k3s | |
https://github.com/chanwit/k3s-cni-plugin/blob/master/loopback?raw=true | |
Kubectl for Linux | |
https://storage.googleapis.com/kubernetes-release/release/v1.14.4/bin/linux/amd64/kubectl | |
for Mac | |
https://storage.googleapis.com/kubernetes-release/release/v1.14.4/bin/darwin/amd64/kubectl | |
for Windows | |
https://storage.googleapis.com/kubernetes-release/release/v1.14.4/bin/windows/amd64/kubectl.exe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
log=/var/log/cni.log | |
config=`cat /dev/stdin` | |
echo >> $log | |
echo "COMMAND: $CNI_COMMAND" >> $log | |
case $CNI_COMMAND in | |
ADD) | |
podcidr=$(echo $config | jq -r ".podcidr") | |
podcidr_gw=$(echo $podcidr | sed "s:0/24:1:g") | |
brctl addbr cni0 | |
ip link set cni0 up | |
ip addr add "${podcidr_gw}/24" dev cni0 | |
# calculate $ip | |
if [ -f /tmp/last_allocated_ip ]; then | |
n=`cat /tmp/last_allocated_ip` | |
else | |
n=1 | |
fi | |
n=$(($n+1)) | |
ip=$(echo $podcidr | sed "s:0/24:$n:g") | |
echo $n > /tmp/last_allocated_ip | |
host_ifname="veth$n" | |
ip link add $CNI_IFNAME type veth peer name $host_ifname | |
ip link set $host_ifname up | |
mkdir -p /var/run/netns/ | |
ip link set $host_ifname master cni0 | |
ln -sfT $CNI_NETNS /var/run/netns/$CNI_CONTAINERID | |
ip link set $CNI_IFNAME netns $CNI_CONTAINERID | |
ip netns exec $CNI_CONTAINERID ip link set $CNI_IFNAME up | |
ip netns exec $CNI_CONTAINERID ip addr add $ip/24 dev $CNI_IFNAME | |
ip netns exec $CNI_CONTAINERID ip route add default via $podcidr_gw | |
mac=$(ip netns exec $CNI_CONTAINERID ip link show $CNI_IFNAME | awk '/ether/ {print $2}') | |
address="${ip}/24" | |
output_template=' | |
{ | |
"cniVersion": "0.3.1", | |
"interfaces": [ | |
{ | |
"name": "%s", | |
"mac": "%s", | |
"sandbox": "%s" | |
} | |
], | |
"ips": [ | |
{ | |
"version": "4", | |
"address": "%s", | |
"gateway": "%s", | |
"interface": 0 | |
} | |
] | |
}' | |
output=$(printf "${output_template}" $CNI_IFNAME $mac $CNI_NETNS $address $podcidr_gw) | |
echo $output >> $log | |
echo "$output" | |
;; | |
DEL) | |
rm -rf /var/run/netns/$CNI_CONTAINERID | |
;; | |
GET) | |
;; | |
VERSION) | |
echo '{ | |
"cniVersion": "0.3.1", | |
"supportedVersions": [ "0.3.0", "0.3.1", "0.4.0" ] | |
}' | |
;; | |
*) | |
echo "Unknown cni command: $CNI_COMMAND" | |
exit 1 | |
;; | |
esac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- mode: ruby -*- | |
# vi: set ft=ruby : | |
Vagrant.configure("2") do |config| | |
config.vm.box = "geerlingguy/ubuntu1604" | |
config.vm.define "node1" do |box| | |
box.vm.hostname = "node1" | |
box.vm.network :private_network, ip: "10.10.10.10" | |
box.vm.provision "shell", inline: <<-SHELL | |
apt-get install -y bridge-utils jq | |
cp /vagrant/k3s /usr/local/bin/k3s | |
chmod +x /usr/local/bin/k3s | |
mkdir -p /etc/cni/net.d | |
cp /vagrant/10-my-cni-demo_node1.conf /etc/cni/net.d/10-my-cni-demo.conf | |
mkdir -p /opt/cni/bin | |
cp /vagrant/loopback /opt/cni/bin/loopback | |
cp /vagrant/my-cni-demo /opt/cni/bin/my-cni-demo | |
chmod +x /opt/cni/bin/loopback | |
chmod +x /opt/cni/bin/my-cni-demo | |
SHELL | |
end | |
config.vm.define "node2" do |box| | |
box.vm.hostname = "node2" | |
box.vm.network :private_network, ip: "10.10.10.11" | |
box.vm.provision "shell", inline: <<-SHELL | |
apt-get install -y bridge-utils jq | |
cp /vagrant/k3s /usr/local/bin/k3s | |
chmod +x /usr/local/bin/k3s | |
mkdir -p /etc/cni/net.d | |
cp /vagrant/10-my-cni-demo_node2.conf /etc/cni/net.d/10-my-cni-demo.conf | |
mkdir -p /opt/cni/bin | |
cp /vagrant/loopback /opt/cni/bin/loopback | |
cp /vagrant/my-cni-demo /opt/cni/bin/my-cni-demo | |
chmod +x /opt/cni/bin/loopback | |
chmod +x /opt/cni/bin/my-cni-demo | |
SHELL | |
end | |
config.vm.provider "virtualbox" do |vb| | |
vb.memory = "1024" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment