附录A 常见问题
- k8s service name的命名规范:
'[a-z]([-a-z0-9]*[a-z0-9])?' && len <= 63
service name遵循DNS标签的命名规范(RFC 1123):
- contain at most 63 characters
- contain only lowercase alphanumeric characters or ‘-’
- start with an alphanumeric character
- end with an alphanumeric character
-
如何配置kubectl命令提示:
echo “source <(kubectl completion bash)” » ~/.bashrc
-
获取kubeadm的–discovery-token-ca-cert-hash值。
1
2
3
4
5
6
7
|
# 默认token的有效期为24小时,24小时后,需要重新生成
$ kubeadm token list
$ kubeadm token create
$ openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | awk '{print $NF}'
$ kubeadm join --token susbs6.jdmyyvzj22ginli2 --discovery-token-ca-cert-hash sha256:fc488fb29210f8fbc4fcad92afdd4535386a95d6eab25b6355a217c245d51579 192.168.1.144:6443
|
- 查询某个命名空间下所有的资源
1
2
|
kubectl api-resources --verbs=list --namespaced -o name \
| xargs -n 1 kubectl get --show-kind --ignore-not-found -l <label>=<value> -n <namespace>
|
- 创建一个ImagePullSecret,以支持私有的Docker仓库
1
2
3
|
kubectl create secret docker-registry myregistrykey --docker-server=DUMMY_SERVER \
--docker-username=DUMMY_USERNAME --docker-password=DUMMY_DOCKER_PASSWORD \
--docker-email=DUMMY_DOCKER_EMAIL
|
将镜像拉取添加到服务账号(pod的默认服务账号是default)
1
|
kubectl patch serviceaccount default -p '{"imagePullSecrets": [{"name": "myregistrykey"}]}'
|
- requests和limits
在调度的时候requests比较重要,在运行时limits比较重要
- limits会向cgroup设置值,但是requests并不会
- limits告诉Linux内核什么时候你的进程可以为了清理空间而被杀死
- requests帮助Kubernetes调度找到合适的节点运行Pod
a) 假设你没有配置内存requests来运行Pod,而配置了一个较高的limits。正如我们所知道的Kubernetes默认会把requests的值指向limits,如果没有合适的资源的节点的话,Pod可能会调度失败,即使它实际需要的资源并没有那么多。
b) 另一方面,如果你运行了一个配置了较低requests值的Pod,你其实是在鼓励内核oom-kill掉它。为什么?假设你的Pod通常使用100MiB内存,你却只为它配置了50MiB内存requests。如果你有一个拥有75MiB内存空间的节点,那么这个Pod会被调度到这个节点。当Pod内存消耗扩大到100MiB时,会让这个节点压力变大,这个时候内核可能会选择杀掉你的进程。
- 如果没有设置requests和limits会怎样?
a) 没有任何资源限制,有多少用多少,这样的pod,在节点资源不足的情况下是最先被驱逐的。
b) 可以给namespace设置requests和limits
- k8s为何需要conntrack
https://git.netfilter.org/conntrack-tools/
https://www.netfilter.org/projects/conntrack-tools/downloads.html
- 解决谷歌浏览器无法访问k8s Dashboard的问题
在谷歌浏览器启动文件中加入启动参数--test-type --ignore-certificate-errors
mac chrome中如何操作:
1
2
3
4
5
6
7
8
|
$ cd cd "/Applications/Google Chrome.app/Contents/MacOS/"
$ sudo mv "Google Chrome" Google.real
$ cat > "Google Chrome" <<-EOF
#!/bin/bash
cd "/Applications/Google Chrome.app/Contents/MacOS"
"/Applications/Google Chrome.app/Contents/MacOS/Google.real" --args --test-type --ignore-certificate-errors
EOF
$ sudo chmod u+x "Google Chrome"
|
-
k8s,为何只有 service和node,包含EXTERNAL-IP字段呢?
-
如果replicas的值为1就必然存在单点故障,如果大于1但所有副本都调度到同一个节点了,那还是有单点故障。可以利用Pod反亲和(anti-affinity)来实现这个。
强反亲和:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: k8s-app
operator: In
values:
- kube-dns
topologyKey: kubernetes.io/hostname
* labelSelector.matchExpressions: 写该服务对应pod中labels的key与value,因为pod反亲和是通过判断pod label来实现的。
* topologyKey: 指定反亲和的拓扑域,即节点label的key。这里用 kubernetes.io/hostname 表示避免pod调度到同一节点。
* requiredDuringSchedulingIgnoredDuringExecution: 调度时必须满足该反亲和的条件,如果没有节点满足条件就不调度到任何节点(Pending)。
弱反亲和:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: k8s-app
operator: In
values:
- kube-dns
topologyKey: kubernetes.io/hostname
* weight: 表示此匹配条件的权重。匹配条件由podAffinityTerm属性来描述。
* labelSelector.matchExpressions: 写该服务对应pod中labels的key与value,因为pod反亲和是通过判断pod label来实现的。
* topologyKey: 指定反亲和的拓扑域,即节点label的key。这里用 kubernetes.io/hostname 表示避免pod调度到同一节点。
* requiredDuringSchedulingIgnoredDuringExecution: 调度时必须满足该反亲和的条件,如果没有节点满足条件就不调度到任何节点(Pending)。
-
只允许调度到master节点下,可以使用节点亲和来实现这个:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: node-role.kubernetes.io/master
operator: Exists
- 给pod配置健康检查也是提高服务可用性的一种手段
- 配置ReadinessProbe(就绪检查)可以避免将流量转发给还没启动完全或出现异常的Pod;
- 配置LivenessProbe(存活检查)可以让存在bug导致死锁或hang住的应用重启来恢复。但是,如果配置不好,可能引起其他问题。
- 不要轻易使用LivenessProbe,除非你了解后果并且明白为什么你需要它。存活探针失败将导致容器重启,这可能会使与负载相关的错误影响加剧,例如给其他容器带来更大流量压力,最终导致更多容器失效,形成恶性循环。
- LivenessProbe 的 failureThreshold必须比 ReadinessProbe 的设置得更大,这是为了避免“误杀”健康的 Pod(虽然就绪检测失败了,但是可能是太忙了,在后台疯狂的处理中,处理完了就会就绪)。
- 探测逻辑里不要有外部依赖,避免抖动导致级联故障
- 业务程序应尽量暴露HTTP探测接口来做健康检查,避免使用TCP探测,因为程序hang死时,TCP探测仍然能通过。(TCP的SYN包探测端口是否存活是在内核态完成的,应用层感知不到)
- 探测的默认行为:(interval: 10s, timeout: 1s, successThreshold: 1, failureThreshold: 3)
- a Pod will become not-ready after ~30s (3 failing health checks)
附录B 绿色版安装k8s
使用kubeadm部署Kubernetes集群时,可以不安装crictl。crictl对于kubeadm部署来说是可选的,不是必需组件
sudo modprobe br_netfilter
sudo modprobe overlay
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
overlay
EOF
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sudo sysctl --system
kubeadm config print init-defaults --component-configs KubeletConfiguration > kubeadm-init.yaml
init-node.sh文件
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
|
#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail
OS_RELEASE="$(. /etc/os-release && echo "$ID")"
set_file_content() {
local file=$1
local pattern=$2
local content=$3
awk -v pattern="$pattern" -v content="$content" '
BEGIN { found = 0 }
{
if ($0 ~ pattern) {
found = 1
gsub(pattern, content)
}
print
}
END {
if (!found) {
print content
}
}' "$file" > "$file.tmp" && \mv -f "$file.tmp" "$file"
}
command_exists() {
command -v "$@" > /dev/null 2>&1
}
disable_firewalld() {
if [ "ubuntu" == ${OS_RELEASE} ]; then
ufw disable || true
else
systemctl disable firewalld || true
systemctl stop firewalld || true
fi
}
clear_node() {
if ! command_exists ifconfig; then
if [ "ubuntu" == ${OS_RELEASE} ]; then
apt install -y net-tools
else
yum install -y net-tools
fi
fi
rm -rf /var/lib/cni/
rm -rf /etc/cni/
ifconfig cni0 down || true
ifconfig flannel.1 down || true
ifconfig docker0 down || true
ip link delete cni0 || true
ip link delete flannel.1 || true
ip link delete docker0 || true
}
swap_off() {
swapoff -a && awk -i inplace '/swap/ && !/^#/ { $0 = "#" $0 } 1' /etc/fstab || true
}
disable_selinux() {
awk -i inplace '{gsub(/SELINUX=enforcing/, "SELINUX=disabled")} 1' /etc/sysconfig/selinux /etc/selinux/config && setenforce 0 || true
}
enable_kubelet() {
systemctl enable kubelet || true
}
set_sysctl() {
set_file_content /etc/sysctl.conf "^net.ipv4.ip_forward.*" "net.ipv4.ip_forward = 1"
set_file_content /etc/sysctl.conf "^net.bridge.bridge-nf-call-iptables.*" "net.bridge.bridge-nf-call-iptables = 1"
cat <<-EOF >/etc/sysctl.d/k8s.conf
kernel.sem = 250 32000 32 1024
net.core.netdev_max_backlog = 20000
net.core.rmem_default = 262144
net.core.rmem_max = 16777216
net.core.somaxconn = 2048
net.core.wmem_default = 262144
net.core.wmem_max = 16777216
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_max_orphans = 131072
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_mem = 786432 2097152 3145728
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_forward = 1
net.netfilter.nf_conntrack_max = 524288
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
fs.inotify.max_user_watches = 1048576
fs.may_detach_mounts = 1
vm.dirty_background_ratio = 5
vm.dirty_ratio = 10
vm.swappiness = 0
vm.max_map_count = 262144
EOF
sysctl --system
}
load_kernel() {
if ! [ -d /etc/sysconfig/modules/ ]; then
mkdir -p /etc/sysconfig/modules
fi
cat <<-EOF >/etc/sysconfig/modules/ipvs.modules
modprobe -- iptable_nat
modprobe -- ip_vs
modprobe -- ip_vs_sh
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- nf_conntrack_ipv4
EOF
if modinfo br_netfilter > /dev/null; then
echo "modprobe -- br_netfilter" >> /etc/sysconfig/modules/ipvs.modules
fi
chmod 755 /etc/sysconfig/modules/ipvs.modules &&
source /etc/sysconfig/modules/ipvs.modules &&
lsmod | grep -e ip_vs -e nf_conntrack_ipv4
}
main() {
disable_firewalld
clear_node
swap_off
disable_selinux
enable_kubelet
set_sysctl
load_kernel
}
main
|
本文发表于 0001-01-01,最后修改于 0001-01-01。
本站永久域名「 jiavvc.top
」,也可搜索「 极客油画 」找到我。
上一篇 «
下一篇 »
推荐阅读
位图
发表于2021-07-17,
全文21字,
阅读约1分钟
Untitled
发表于0001-01-01,
全文4884字,
阅读约17分钟
Untitled
发表于0001-01-01,
全文397字,
阅读约2分钟