ubuntu安装k8s

1. 环境准备

1.1 修改网卡为固定地址

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
cat >/etc/netplan/00-installer-config.yaml<<END 
# This is the network config written by 'subiquity'
network:
ethernets:
eth0:
dhcp4: no
dhcp6: no
addresses: [192.168.102.22/24]
gateway4: 192.168.102.2
nameservers:
addresses: [114.114.114.114]
eth1:
dhcp4: true
eth2:
dhcp4: true
eth3:
dhcp4: true
eth4:
dhcp4: true
eth5:
dhcp4: true
eth6:
dhcp4: true
eth7:
dhcp4: true
version: 2
END

netplan apply

1.2 修改主机名字

1
2
3
hostnamectl set-hostname master
hostnamectl set-hostname node1
hostnamectl set-hostname node2

1.3 关闭swap

1
2
3
sed -i 's/^\/swap.img/#\/swap.img/g' /etc/fstab

reboot

1.4 配置域名解析

1
2
3
4
5
6
7
8
9
10
11
12
13
cat >/etc/hosts<<END
127.0.0.1 localhost
127.0.1.1 zy
192.168.102.11 master
192.168.102.21 node1
192.168.102.22 node2
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
END

1.5 内核参数调优

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
cat >/etc/sysctl.d/k8s.conf<<END
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
# 下面的内核参数可以解决ipvs模式下长连接空闲超时的问题
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 10
net.ipv4.tcp_keepalive_time = 600
END

sysctl -p /etc/sysctl.d/k8s.conf

#设置rc.local服务
cat >/etc/systemd/system/rc-local.service<<END
[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
[Install]
WantedBy=multi-user.target
END
#写入rc.local
cat >/etc/rc.local<<END
#!/bin/bash
for file in /usr/lib/modules/*.modules ; do
[ -x \$file ] && \$file
done
END
#启动服务
chmod 755 /etc/rc.local
cat > /usr/lib/modules/ipvs.modules <<END
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe br_netfilter
END
chmod 755 /usr/lib/modules/ipvs.modules
systemctl enable rc-local.service --now

1.6 配置时间同步

1
2
3
apt install chrony -y
systemctl enable chrony --now
chronyc sources

2. 安装

2.1 安装containerd

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
wget https://github.com/containerd/containerd/releases/download/v1.6.10/cri-containerd-1.6.10-linux-amd64.tar.gz
tar -C / -xzf cri-containerd-1.6.10-linux-amd64.tar.gz
echo $PATH
containerd -v
apt install libseccomp-dev -y
runc -v

mkdir -p /etc/containerd
mkdir /etc/containerd/certs.d/docker.io/ -p
containerd config default > /etc/containerd/config.toml
sed -i s/"SystemdCgroup = false"/"SystemdCgroup = true"/ /etc/containerd/config.toml
sed -i s/'sandbox_image = "registry.k8s.io\/pause:3.6"'/'sandbox_image = "registry.aliyuncs.com\/k8sxio\/pause:3.8"'/ /etc/containerd/config.toml
sed -i s/config_path.*/'config_path = "\/etc\/containerd\/certs.d"'/ /etc/containerd/config.toml
# docker hub镜像加速
mkdir -p /etc/containerd/certs.d/docker.io
cat > /etc/containerd/certs.d/docker.io/hosts.toml << EOF
server = "https://docker.io"
[host."https://hub.792588.xyz"]
capabilities = ["pull", "resolve"]
EOF

# registry.k8s.io镜像加速
mkdir -p /etc/containerd/certs.d/registry.k8s.io
tee /etc/containerd/certs.d/registry.k8s.io/hosts.toml << 'EOF'
server = "https://registry.k8s.io"

[host."https://k8s.792588.xyz"]
capabilities = ["pull", "resolve", "push"]
EOF

# docker.elastic.co镜像加速
mkdir -p /etc/containerd/certs.d/docker.elastic.co
tee /etc/containerd/certs.d/docker.elastic.co/hosts.toml << 'EOF'
server = "https://docker.elastic.co"

[host."https://elastic.792588.xyz"]
capabilities = ["pull", "resolve", "push"]
EOF

# gcr.io镜像加速
mkdir -p /etc/containerd/certs.d/gcr.io
tee /etc/containerd/certs.d/gcr.io/hosts.toml << 'EOF'
server = "https://gcr.io"

[host."https://gcr.792588.xyz"]
capabilities = ["pull", "resolve", "push"]
EOF

# ghcr.io镜像加速
mkdir -p /etc/containerd/certs.d/ghcr.io
tee /etc/containerd/certs.d/ghcr.io/hosts.toml << 'EOF'
server = "https://ghcr.io"

[host."https://ghcr.792588.xyz"]
capabilities = ["pull", "resolve", "push"]
EOF

# k8s.gcr.io镜像加速
mkdir -p /etc/containerd/certs.d/k8s.gcr.io
tee /etc/containerd/certs.d/k8s.gcr.io/hosts.toml << 'EOF'
server = "https://k8s.gcr.io"

[host."https://k8sgcr.792588.xyz"]
capabilities = ["pull", "resolve", "push"]
EOF

# mcr.m.daocloud.io镜像加速
mkdir -p /etc/containerd/certs.d/mcr.microsoft.com
tee /etc/containerd/certs.d/mcr.microsoft.com/hosts.toml << 'EOF'
server = "https://mcr.microsoft.com"

[host."https://mcr.792588.xyz"]
capabilities = ["pull", "resolve", "push"]
EOF

# nvcr.io镜像加速
mkdir -p /etc/containerd/certs.d/nvcr.io
tee /etc/containerd/certs.d/nvcr.io/hosts.toml << 'EOF'
server = "https://nvcr.io"

[host."https://nvcr.792588.xyz"]
capabilities = ["pull", "resolve", "push"]
EOF

# quay.io镜像加速
mkdir -p /etc/containerd/certs.d/quay.io
tee /etc/containerd/certs.d/quay.io/hosts.toml << 'EOF'
server = "https://quay.io"

[host."https://quay.792588.xyz"]
capabilities = ["pull", "resolve", "push"]
EOF
systemctl enable containerd --now

2.2 安装kubeadm

1
2
3
4
5
6
7
8
9
apt-get install -y apt-transport-https ca-certificates curl
mkdir /etc/apt/keyrings/ -p
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.27/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.27/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list

apt-get update
apt-get install -y kubelet kubeadm kubectl
apt-mark hold kubelet kubeadm kubectl
kubeadm version

2.3 master节点操作

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
systemctl enable --now kubelet
kubeadm config print init-defaults --component-configs KubeletConfiguration > kubeadm.yaml

cat >kubeadm.yaml<<END
apiVersion: kubeadm.k8s.io/v1beta3
bootstrapTokens:
- groups:
- system:bootstrappers:kubeadm:default-node-token
token: abcdef.0123456789abcdef
ttl: 24h0m0s
usages:
- signing
- authentication
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: 192.168.102.11
bindPort: 6443
nodeRegistration:
criSocket: unix:///var/run/containerd/containerd.sock
imagePullPolicy: IfNotPresent
name: node
taints: null
---
apiServer:
timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta3
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns: {}
etcd:
local:
dataDir: /var/lib/etcd
imageRepository: registry.k8s.io
kind: ClusterConfiguration
kubernetesVersion: 1.27.0
networking:
dnsDomain: cluster.local
serviceSubnet: 10.96.0.0/12
scheduler: {}
---
apiVersion: kubelet.config.k8s.io/v1beta1
authentication:
anonymous:
enabled: false
webhook:
cacheTTL: 0s
enabled: true
x509:
clientCAFile: /etc/kubernetes/pki/ca.crt
authorization:
mode: Webhook
webhook:
cacheAuthorizedTTL: 0s
cacheUnauthorizedTTL: 0s
cgroupDriver: systemd
clusterDNS:
- 10.96.0.10
clusterDomain: cluster.local
containerRuntimeEndpoint: ""
cpuManagerReconcilePeriod: 0s
evictionPressureTransitionPeriod: 0s
fileCheckFrequency: 0s
healthzBindAddress: 127.0.0.1
healthzPort: 10248
httpCheckFrequency: 0s
imageMinimumGCAge: 0s
kind: KubeletConfiguration
logging:
flushFrequency: 0
options:
json:
infoBufferSize: "0"
verbosity: 0
memorySwap: {}
nodeStatusReportFrequency: 0s
nodeStatusUpdateFrequency: 0s
resolvConf: /run/systemd/resolve/resolv.conf
rotateCertificates: true
runtimeRequestTimeout: 0s
shutdownGracePeriod: 0s
shutdownGracePeriodCriticalPods: 0s
staticPodPath: /etc/kubernetes/manifests
streamingConnectionIdleTimeout: 0s
syncFrequency: 0s
volumeStatsAggPeriod: 0s
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: ipvs
END

2.4 node节点操作

1
2
kubeadm join 192.168.102.11:6443 --token abcdef.0123456789abcdef \
--discovery-token-ca-cert-hash sha256:3f65d93dc0cb73e169a26f9524468182805b1445659523fafef98b5c1f2c8ced

如果忘记了可以在生成

1
kubeadm token create --print-join-command

2.5 安装calico

1
2
wget https://calico-v3-25.netlify.app/archive/v3.25/manifests/calico.yaml
kubectl apply -f calico.yaml

3. 测试安装是否成功

1
2
3
4
kubectl run busybox --image docker.io/library/busybox:1.28  --image-pull-policy=IfNotPresent --restart=Never --rm -it busybox -- sh

nslookup kubernetes.default.svc.cluster.local
ping www.baidu.com

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!