使用二进制文件的方式安装 Kubernetes v1.17.0 集群(二)

一、实现方式介绍

1. 控制平面的所有组件都采用二进制方式部署,交由systemd统一管理;

控制平面包括:etcd、kube-apiserver、kube-controller-manager和kube-scheduler。

2. 工作节点上的所有组件都采用二进制方式部署,交由systemd统一管理;

工作节点上的组件包括:docker、kubelet和kube-proxy。

3. 网络插件依然采用Addon方式部署,交由kubernetes统一管理。

网络插件包括:calico-node及其相关的组件和coredns。

二、实验环境版本信息

1. 操作系统的版本信息

CentOS Linux release 7.6.1810 (Core)

2. 各组件的版本信息

etcd v3.4.3
kube-apiserver v1.17.0
kube-controller-manager v1.17.0
kube-scheduler v1.17.0
kubectl v1.17.0

docker 18.09.9
kubelet v1.17.0
calico v3.11.1

三、部署架构

1. Kubernetes Master(Control Plane)

192.168.112.128 master -> etcd kube-apiserver kube-controller-manager kube-scheduler

2. Kubernetes Node

192.168.112.129 node01 -> docker kubelet kube-proxy calico-node
192.168.112.130 node02 -> docker kubelet kube-proxy calico-node

四、准备二进制文件与Docker镜像

1. 下载相关的二进制文件压缩包

https://github.com/etcd-io/etcd/releases/download/v3.4.3/etcd-v3.4.3-linux-amd64.tar.gz
https://dl.k8s.io/v1.17.0/kubernetes-server-linux-amd64.tar.gz

2. 拉取相关的Docker镜像

calico网络组件的镜像:
calico/node:v3.11.1
calico/pod2daemon-flexvol:v3.11.1
calico/cni:v3.11.1
calico/kube-controllers:v3.11.1

kube-dns的镜像:
registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.5

infra容器的镜像:
registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64:3.1

五、部署过程记录

1. 准备基础环境(Master和Node上都执行)

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
# 更新系统
yum update -y

# 设置正确的时区和时间
yum install -y ntpdate
timedatectl set-timezone Asia/Shanghai
ntpdate cn.ntp.org.cn

# 关闭防火墙
systemctl disable firewalld.service
systemctl stop firewalld.service

# 关闭swap分区
swapoff -a
sed -i 's#/dev/mapper/cl-swap#\# /dev/mapper/cl-swap#' /etc/fstab

# 关闭selinux
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

# 设置各个节点的主机名
## 192.168.112.128
hostnamectl set-hostname master

## 192.168.112.129
hostnamectl set-hostname node01

## 192.168.112.130
hostnamectl set-hostname node02

# 配置主机名和IP的映射
cat <<EOF >> /etc/hosts

# For Kubernetes Cluster
192.168.112.128 master
192.168.112.129 node01
192.168.112.130 node02
EOF

# 修改内核参数
cat <<EOF > /etc/sysctl.d/kubernetes.conf
net.bridge.bridge-nf-call-iptables = 1

net.ipv4.ip_forward = 1

net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_tw_resue = 1
EOF

sysctl --system

# 修改ulimit限制
cat <<EOF > /etc/security/limits.d/kubernetes.conf
* hard nofile 65535
* soft nofile 65535
EOF

2. 安装Docker环境(在所有Node上都执行)

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
# 配置yum源,然后安装Docker
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum install -y docker-ce-18.09.9-3.el7 docker-ce-cli-18.09.9-3.el7

# 启动Docker,并将其设置为开机启动
systemctl daemon-reload
systemctl enable docker.service
systemctl start docker.service

# 确认Docker启动是否正常
systemctl status docker.service
docker info
docker version

# 方法一:检查iptables的forward链的默认策略
iptables -nL
。。。
Chain FORWARD (policy ACCEPT)
。。。

# 方法二:检查iptables的forward链的默认策略
iptables-save -t filter
。。。
# Generated by iptables-save v1.4.21 on Thu Oct 3 12:28:24 2019
*filter
:INPUT ACCEPT [2117:366255]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [2188:436727]
。。。

# 设置docker daemon的cgroup driver为systemd
cat <<EOF > /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver":"json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
EOF

systemctl daemon-reload
systemctl restart docker.service
systemctl status docker.service

# 验证docker daemon的cgroup driver是否为systemd
docker info
。。。
Cgroup Driver: systemd
。。。

3. 复制所有二进制文件到操作系统/usr/bin/目录下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 192.168.112.128 (Master上执行)
tar -zxvf etcd-v3.4.3-linux-amd64.tar.gz
tar -zxvf kubernetes-server-linux-amd64.tar.gz

cp etcd-v3.4.3-linux-amd64/etcd /usr/bin/
cp etcd-v3.4.3-linux-amd64/etcdctl /usr/bin/
cp kubernetes/server/bin/kube-apiserver /usr/bin/
cp kubernetes/server/bin/kube-controller-manager /usr/bin/
cp kubernetes/server/bin/kube-scheduler /usr/bin/
cp kubernetes/server/bin/kubectl /usr/bin/


# 192.168.112.129 和 192.168.112.130 (Node上执行)
tar -zxvf kubernetes-server-linux-amd64.tar.gz

cp kubernetes/server/bin/kubelet /usr/bin/
cp kubernetes/server/bin/kube-proxy /usr/bin/

4. 在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
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
164
165
# 创建证书和配置文件的存放目录
mkdir -p /etc/kubernetes/pki/etcd/

# 生成etcd的相关证书
cd /etc/kubernetes/pki/etcd/
openssl genrsa -out ca.key 2048
openssl req -x509 -new -nodes -key ca.key -subj "/CN=etcd-ca" -days 5000 -out ca.crt

cat <<EOF > server_ssl.cnf
[req]
req_extensions = v3_req
distinguished_name = req_distinguished_name

[req_distinguished_name]
[v3_req]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation,digitalSignature,keyEncipherment
subjectAltName = @alt_names
[alt_names]
DNS.1 = master
DNS.2 = localhost
IP.1 = 192.168.112.128
IP.2 = 127.0.0.1
EOF
openssl genrsa -out server.key 2048
openssl req -new -key server.key -subj "/CN=master" -config server_ssl.cnf -out server.csr
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -days 5000 -extensions v3_req -extfile server_ssl.cnf -out server.crt

cat <<EOF > peer_ssl.cnf
[req]
req_extensions = v3_req
distinguished_name = req_distinguished_name

[req_distinguished_name]
[v3_req]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation,digitalSignature,keyEncipherment
subjectAltName = @alt_names
[alt_names]
DNS.1 = master
DNS.2 = localhost
IP.1 = 192.168.112.128
IP.2 = 127.0.0.1
EOF
openssl genrsa -out peer.key 2048
openssl req -new -key peer.key -subj "/CN=master" -config peer_ssl.cnf -out peer.csr
openssl x509 -req -in peer.csr -CA ca.crt -CAkey ca.key -CAcreateserial -days 5000 -extensions v3_req -extfile peer_ssl.cnf -out peer.crt

openssl genrsa -out healthcheck-client.key 2048
openssl req -new -key healthcheck-client.key -subj "/O=system:masters/CN=kube-etcd-healthcheck-client" -out healthcheck-client.csr
openssl x509 -req -in healthcheck-client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out healthcheck-client.crt -days 5000

cd /etc/kubernetes/pki/
openssl genrsa -out apiserver-etcd-client.key 2048
openssl req -new -key apiserver-etcd-client.key -subj "/O=system:masters/CN=kube-apiserver-etcd-client" -out apiserver-etcd-client.csr
openssl x509 -req -in apiserver-etcd-client.csr -CA /etc/kubernetes/pki/etcd/ca.crt -CAkey /etc/kubernetes/pki/etcd/ca.key -CAcreateserial -out apiserver-etcd-client.crt -days 5000


# 生成kubernetes组件的相关证书
# 生成rsa的公钥和私钥
openssl genrsa -out sa.key 2048
openssl rsa -in sa.key -pubout -out sa.pub

# 生成根证书
openssl genrsa -out ca.key 2048
openssl req -x509 -new -nodes -key ca.key -subj "/CN=kubernetes" -days 5000 -out ca.crt

openssl genrsa -out front-proxy-ca.key 2048
openssl req -x509 -new -nodes -key front-proxy-ca.key -subj "/CN=front-proxy-ca" -days 5000 -out front-proxy-ca.crt

# 为kube-apiserver生成相关的证书和配置文件
cat <<EOF > master_ssl.cnf
[req]
req_extensions = v3_req
distinguished_name = req_distinguished_name

[req_distinguished_name]
[v3_req]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation,digitalSignature,keyEncipherment
subjectAltName = @alt_names
[alt_names]
DNS.1 = master
DNS.2 = kubernetes
DNS.3 = kubernetes.default
DNS.4 = kubernetes.default.svc
DNS.5 = kubernetes.default.svc.cluster.local
IP.1 = 10.96.0.1
IP.2 = 192.168.112.128
EOF

openssl genrsa -out apiserver.key 2048
openssl req -new -key apiserver.key -subj "/CN=kube-apiserver" -config master_ssl.cnf -out apiserver.csr
openssl x509 -req -in apiserver.csr -CA ca.crt -CAkey ca.key -CAcreateserial -days 5000 -extensions v3_req -extfile master_ssl.cnf -out apiserver.crt

openssl genrsa -out front-proxy-client.key 2048
openssl req -new -key front-proxy-client.key -subj "/CN=front-proxy-client" -out front-proxy-client.csr
openssl x509 -req -in front-proxy-client.csr -CA front-proxy-ca.crt -CAkey front-proxy-ca.key -CAcreateserial -out front-proxy-client.crt -days 5000

openssl genrsa -out apiserver-kubelet-client.key 2048
openssl req -new -key apiserver-kubelet-client.key -subj "/O=system:masters/CN=kube-apiserver-kubelet-client" -out apiserver-kubelet-client.csr
openssl x509 -req -in apiserver-kubelet-client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out apiserver-kubelet-client.crt -days 5000

# 为kube-controller-manager生成相关的证书和配置文件
openssl genrsa -out controller-manager.key 2048
openssl req -new -key controller-manager.key -subj "/CN=system:kube-controller-manager" -out controller-manager.csr
openssl x509 -req -in controller-manager.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out controller-manager.crt -days 5000

export KUBECONFIG=/etc/kubernetes/controller-manager.conf
kubectl config set-cluster kubernetes --server=https://192.168.112.128:6443 --certificate-authority=/etc/kubernetes/pki/ca.crt --embed-certs=true
kubectl config set-credentials system:kube-controller-manager --client-certificate=/etc/kubernetes/pki/controller-manager.crt --client-key=/etc/kubernetes/pki/controller-manager.key --embed-certs=true
kubectl config set-context system:kube-controller-manager@kubernetes --cluster=kubernetes --user=system:kube-controller-manager
kubectl config use-context system:kube-controller-manager@kubernetes
unset KUBECONFIG

# 为kube-scheduler生成相关的证书和配置文件
openssl genrsa -out scheduler.key 2048
openssl req -new -key scheduler.key -subj "/CN=system:kube-scheduler" -out scheduler.csr
openssl x509 -req -in scheduler.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out scheduler.crt -days 5000

export KUBECONFIG=/etc/kubernetes/scheduler.conf
kubectl config set-cluster kubernetes --server=https://192.168.112.128:6443 --certificate-authority=/etc/kubernetes/pki/ca.crt --embed-certs=true
kubectl config set-credentials system:kube-scheduler --client-certificate=/etc/kubernetes/pki/scheduler.crt --client-key=/etc/kubernetes/pki/scheduler.key --embed-certs=true
kubectl config set-context system:kube-scheduler@kubernetes --cluster=kubernetes --user=system:kube-scheduler
kubectl config use-context system:kube-scheduler@kubernetes
unset KUBECONFIG

# 为kubectl生成相关的证书和配置文件
openssl genrsa -out kubectl.key 2048
openssl req -new -key kubectl.key -subj "/O=system:masters/CN=kubernetes-admin" -out kubectl.csr
openssl x509 -req -in kubectl.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out kubectl.crt -days 5000

export KUBECONFIG=/etc/kubernetes/admin.conf
kubectl config set-cluster kubernetes --server=https://192.168.112.128:6443 --certificate-authority=/etc/kubernetes/pki/ca.crt --embed-certs=true
kubectl config set-credentials kubernetes-admin --client-certificate=/etc/kubernetes/pki/kubectl.crt --client-key=/etc/kubernetes/pki/kubectl.key --embed-certs=true
kubectl config set-context kubernetes-admin@kubernetes --cluster=kubernetes --user=kubernetes-admin
kubectl config use-context kubernetes-admin@kubernetes
unset KUBECONFIG

# 注意:因为kubelet使用了tls bootstrap的方式,所以下面这部分内容没有必要了,需把其注释掉
# 为kubelet生成相关的证书和配置文件
# openssl genrsa -out kubelet.key 2048
# openssl req -new -key kubelet.key -subj "/O=system:nodes/CN=system:node:node01" -out kubelet.csr
# openssl x509 -req -in kubelet.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out kubelet.crt -days 5000
#
# export KUBECONFIG=/etc/kubernetes/kubelet.conf
# kubectl config set-cluster kubernetes --server=https://192.168.112.128:6443 --certificate-authority=/etc/kubernetes/pki/ca.crt # --embed-certs=true
# kubectl config set-credentials system:node:node01 --client-certificate=/etc/kubernetes/pki/kubelet.crt --client-key=/etc/kubernetes/pki/# kubelet.key --embed-certs=true
# kubectl config set-context system:node:node01@kubernetes --cluster=kubernetes --user=system:node:node01
# kubectl config use-context system:node:node01@kubernetes
# unset KUBECONFIG

# 为kube-proxy生成相关的证书和配置文件
# kubernetes内置的为kube-proxy而生的clusterrole,可以使用kubectl get clusterrole system:node-proxier -o yaml进行查看
# kubernetes内置的为kube-proxy而生的clusterrolebinding,绑定到了用户system:kube-proxy,可以使用kubectl get clusterrolebinding system:node-proxier -o yaml进行查看
openssl genrsa -out proxy.key 2048
openssl req -new -key proxy.key -subj "/CN=system:kube-proxy" -out proxy.csr
openssl x509 -req -in proxy.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out proxy.crt -days 5000

export KUBECONFIG=/etc/kubernetes/proxy.conf
kubectl config set-cluster kubernetes --server=https://192.168.112.128:6443 --certificate-authority=/etc/kubernetes/pki/ca.crt --embed-certs=true
kubectl config set-credentials system:kube-proxy --client-certificate=/etc/kubernetes/pki/proxy.crt --client-key=/etc/kubernetes/pki/proxy.key --embed-certs=true
kubectl config set-context system:kube-proxy@kubernetes --cluster=kubernetes --user=system:kube-proxy
kubectl config use-context system:kube-proxy@kubernetes
unset KUBECONFIG

5. 配置和启动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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# 配置和启动etcd服务
mkdir -p /etc/etcd/
mkdir -p /var/lib/etcd/

cat <<EOF > /usr/lib/systemd/system/etcd.service
[Unit]
Description=Etcd Server
After=network.target

[Service]
Type=simple
WorkingDirectory=/var/lib/etcd/
EnvironmentFile=-/etc/etcd/etcd.env
ExecStart=/usr/bin/etcd \$ETCD_ARGS

[Install]
WantedBy=multi-user.target
EOF

cat <<EOF > /etc/etcd/etcd.env
ETCD_ARGS="--advertise-client-urls=https://192.168.112.128:2379 --cert-file=/etc/kubernetes/pki/etcd/server.crt --client-cert-auth=true --data-dir=/var/lib/etcd --initial-advertise-peer-urls=https://192.168.112.128:2380 --initial-cluster=master=https://192.168.112.128:2380 --key-file=/etc/kubernetes/pki/etcd/server.key --listen-client-urls=https://127.0.0.1:2379,https://192.168.112.128:2379 --listen-metrics-urls=http://127.0.0.1:2381 --listen-peer-urls=https://192.168.112.128:2380 --name=master --peer-cert-file=/etc/kubernetes/pki/etcd/peer.crt --peer-client-cert-auth=true --peer-key-file=/etc/kubernetes/pki/etcd/peer.key --peer-trusted-ca-file=/etc/kubernetes/pki/etcd/ca.crt --snapshot-count=10000 --trusted-ca-file=/etc/kubernetes/pki/etcd/ca.crt"
EOF

systemctl daemon-reload
systemctl enable etcd.service
systemctl start etcd.service
systemctl status etcd.service

# 配置和启动kube-apiserver服务
cat <<EOF > /usr/lib/systemd/system/kube-apiserver.service
[Unit]
Description=Kubernetes API Server
Documentation=https://github.com/kubernetes/kubernetes
After=etcd.service
Wants=etcd.service

[Service]
EnvironmentFile=-/etc/kubernetes/kube-apiserver.env
ExecStart=/usr/bin/kube-apiserver \$KUBE_API_ARGS
Restart=on-failure
Type=notify
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

cat <<EOF > /etc/kubernetes/kube-apiserver.env
KUBE_API_ARGS=" --advertise-address=192.168.112.128 --allow-privileged=true --authorization-mode=Node,RBAC --client-ca-file=/etc/kubernetes/pki/ca.crt --enable-admission-plugins=NodeRestriction --enable-bootstrap-token-auth=true --etcd-cafile=/etc/kubernetes/pki/etcd/ca.crt --etcd-certfile=/etc/kubernetes/pki/apiserver-etcd-client.crt --etcd-keyfile=/etc/kubernetes/pki/apiserver-etcd-client.key --etcd-servers=https://127.0.0.1:2379 --insecure-port=0 --kubelet-client-certificate=/etc/kubernetes/pki/apiserver-kubelet-client.crt --kubelet-client-key=/etc/kubernetes/pki/apiserver-kubelet-client.key --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname --proxy-client-cert-file=/etc/kubernetes/pki/front-proxy-client.crt --proxy-client-key-file=/etc/kubernetes/pki/front-proxy-client.key --requestheader-allowed-names=front-proxy-client --requestheader-client-ca-file=/etc/kubernetes/pki/front-proxy-ca.crt --requestheader-extra-headers-prefix=X-Remote-Extra- --requestheader-group-headers=X-Remote-Group --requestheader-username-headers=X-Remote-User --secure-port=6443 --service-account-key-file=/etc/kubernetes/pki/sa.pub --service-cluster-ip-range=10.96.0.0/16 --tls-cert-file=/etc/kubernetes/pki/apiserver.crt --tls-private-key-file=/etc/kubernetes/pki/apiserver.key"
EOF

systemctl daemon-reload
systemctl enable kube-apiserver.service
systemctl start kube-apiserver.service
systemctl status kube-apiserver.service

export KUBECONFIG=/etc/kubernetes/admin.conf

# 为各个系统组件涉及的用户绑定cluster-admin角色
# export KUBECONFIG=/etc/kubernetes/admin.conf
# kubectl create clusterrolebinding kube-apiserver --clusterrole=cluster-admin --user=kube-apiserver
# kubectl create clusterrolebinding front-proxy-client --clusterrole=cluster-admin --user=front-proxy-client
# kubectl create clusterrolebinding system:kube-controller-manager --clusterrole=cluster-admin --user=system:kube-controller-manager
# kubectl create clusterrolebinding system:kube-scheduler --clusterrole=cluster-admin --user=system:kube-scheduler
# kubectl create clusterrolebinding system:kubelet --clusterrole=cluster-admin --user=system:kubelet
# kubectl create clusterrolebinding system:kube-proxy --clusterrole=cluster-admin --user=system:kube-proxy

# 创建Bootstrap Token的相关配置 注意:expiration必须要在当前日期以后,否则会出现token创建后,kubernetes就会自动删除
cat <<EOF > /etc/kubernetes/bootstrap-token-abcdef.yaml
apiVersion: v1
kind: Secret
metadata:
name: bootstrap-token-abcdef
namespace: kube-system
type: bootstrap.kubernetes.io/token
stringData:
auth-extra-groups: system:bootstrappers:default-node-token
expiration: 2020-01-31T00:00:00+08:00
token-id: abcdef
token-secret: 0123456789abcdef
usage-bootstrap-authentication: "true"
usage-bootstrap-signing: "true"
EOF
kubectl create -f /etc/kubernetes/bootstrap-token-abcdef.yaml

cat <<EOF > /etc/kubernetes/create-csrs-for-bootstrapping.yaml
# enable bootstrapping nodes to create CSR
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: create-csrs-for-bootstrapping
subjects:
- kind: Group
name: system:bootstrappers
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: system:node-bootstrapper
apiGroup: rbac.authorization.k8s.io
EOF
kubectl create -f /etc/kubernetes/create-csrs-for-bootstrapping.yaml

cat <<EOF > /etc/kubernetes/auto-approve-csrs-for-group.yaml
# Approve all CSRs for the group "system:bootstrappers"
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: auto-approve-csrs-for-group
subjects:
- kind: Group
name: system:bootstrappers
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: system:certificates.k8s.io:certificatesigningrequests:nodeclient
apiGroup: rbac.authorization.k8s.io
EOF
kubectl create -f /etc/kubernetes/auto-approve-csrs-for-group.yaml

cat <<EOF > /etc/kubernetes/auto-approve-renewals-for-nodes.yaml
# Approve renewal CSRs for the group "system:nodes"
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: auto-approve-renewals-for-nodes
subjects:
- kind: Group
name: system:nodes
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: system:certificates.k8s.io:certificatesigningrequests:selfnodeclient
apiGroup: rbac.authorization.k8s.io
EOF
kubectl create -f /etc/kubernetes/auto-approve-renewals-for-nodes.yaml

export KUBECONFIG=/etc/kubernetes/bootstrap-kubelet.conf
kubectl config set-cluster kubernetes --server=https://192.168.112.128:6443 --certificate-authority=/etc/kubernetes/pki/ca.crt --embed-certs=true
kubectl config set-credentials system:bootstrap:abcdef --token=abcdef.0123456789abcdef
kubectl config set-context system:bootstrap:abcdef@kubernetes --cluster=kubernetes --user=system:bootstrap:abcdef
kubectl config use-context system:bootstrap:abcdef@kubernetes
unset KUBECONFIG

# 配置和启动kube-controller-manager服务
cat <<EOF > /usr/lib/systemd/system/kube-controller-manager.service
[Unit]
Description=Kubernetes Controller Manager
Documentation=https://github.com/kubernetes/kubernetes
After=kube-apiserver.service
Requires=kube-apiserver.service

[Service]
EnvironmentFile=-/etc/kubernetes/kube-controller-manager.env
ExecStart=/usr/bin/kube-controller-manager \$KUBE_CONTROLLER_MANAGER_ARGS
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

cat <<EOF > /etc/kubernetes/kube-controller-manager.env
KUBE_CONTROLLER_MANAGER_ARGS="--allocate-node-cidrs=true --authentication-kubeconfig=/etc/kubernetes/controller-manager.conf --authorization-kubeconfig=/etc/kubernetes/controller-manager.conf --bind-address=127.0.0.1 --client-ca-file=/etc/kubernetes/pki/ca.crt --cluster-cidr=10.211.0.0/16 --cluster-signing-cert-file=/etc/kubernetes/pki/ca.crt --cluster-signing-key-file=/etc/kubernetes/pki/ca.key --controllers=*,bootstrapsigner,tokencleaner --kubeconfig=/etc/kubernetes/controller-manager.conf --leader-elect=true --node-cidr-mask-size=24 --requestheader-client-ca-file=/etc/kubernetes/pki/front-proxy-ca.crt --root-ca-file=/etc/kubernetes/pki/ca.crt --service-account-private-key-file=/etc/kubernetes/pki/sa.key --service-cluster-ip-range=10.96.0.0/16 --use-service-account-credentials=true"
EOF

systemctl daemon-reload
systemctl enable kube-controller-manager.service
systemctl start kube-controller-manager.service
systemctl status kube-controller-manager.service

# 配置和启动kube-scheduler服务
cat <<EOF > /usr/lib/systemd/system/kube-scheduler.service
[Unit]
Description=Kubernetes Scheduler
Documentation=https://github.com/kubernetes/kubernetes
After=kube-apiserver.service
Requires=kube-apiserver.service

[Service]
EnvironmentFile=-/etc/kubernetes/kube-scheduler.env
ExecStart=/usr/bin/kube-scheduler \$KUBE_SCHEDULER_ARGS
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

cat <<EOF > /etc/kubernetes/kube-scheduler.env
KUBE_SCHEDULER_ARGS=" --authentication-kubeconfig=/etc/kubernetes/scheduler.conf --authorization-kubeconfig=/etc/kubernetes/scheduler.conf --bind-address=127.0.0.1 --kubeconfig=/etc/kubernetes/scheduler.conf --leader-elect=true"
EOF

systemctl daemon-reload
systemctl enable kube-scheduler.service
systemctl start kube-scheduler.service
systemctl status kube-scheduler.service

# 如果master节点需要具备node节点的功能,那么请参考5中的步骤,先在master上完成kubelet和kube-proxy的安装后,再给master节点打上下面的标签和污点
kubectl label node master node-role.kubernetes.io/master=
kubectl taint node master node-role.kubernetes.io/master=:NoSchedule

5. 配置和启动Node上的所有组件

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
# 创建配置目录和工作目录
mkdir -p /etc/kubernetes/manifests
mkdir -p /etc/kubernetes/pki/
mkdir -p /var/lib/kubelet/
mkdir -p /var/lib/kube-proxy/

# 传输相关配置文件到当前节点上
scp root@192.168.112.128:/etc/kubernetes/bootstrap-kubelet.conf /etc/kubernetes/
scp root@192.168.112.128:/etc/kubernetes/pki/ca.crt /etc/kubernetes/pki/
# scp root@192.168.112.128:/etc/kubernetes/kubelet.conf /etc/kubernetes/ # 使用了tls bootstrap的方式,故这步没有必要,需要注释掉
scp root@192.168.112.128:/etc/kubernetes/proxy.conf /etc/kubernetes/

# 创建kubelet的配置文件
cat <<EOF > /var/lib/kubelet/config.yaml
apiVersion: kubelet.config.k8s.io/v1beta1
authentication:
anonymous:
enabled: true
webhook:
cacheTTL: 0s
enabled: true
x509:
clientCAFile: /etc/kubernetes/pki/ca.crt
authorization:
mode: Webhook
webhook:
cacheAuthorizedTTL: 0s
cacheUnauthorizedTTL: 0s
clusterDNS:
- 10.96.0.10
clusterDomain: cluster.local
cpuManagerReconcilePeriod: 0s
evictionPressureTransitionPeriod: 0s
fileCheckFrequency: 0s
healthzBindAddress: 127.0.0.1
healthzPort: 10248
httpCheckFrequency: 0s
imageMinimumGCAge: 0s
kind: KubeletConfiguration
nodeStatusReportFrequency: 0s
nodeStatusUpdateFrequency: 0s
rotateCertificates: true
runtimeRequestTimeout: 0s
staticPodPath: /etc/kubernetes/manifests
streamingConnectionIdleTimeout: 0s
syncFrequency: 0s
volumeStatsAggPeriod: 0s
EOF

# 配置和启动kubelet服务
cat <<EOF > /usr/lib/systemd/system/kubelet.service
[Unit]
Description=Kubernetes Kubelet Server
Documentation=https://github.com/kubernetes/kubernetes
After=docker.service
Requires=docker.service

[Service]
WorkingDirectory=/var/lib/kubelet
EnvironmentFile=-/etc/kubernetes/kubelet.env
ExecStart=/usr/bin/kubelet \$KUBELET_ARGS
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

cat <<EOF > /etc/kubernetes/kubelet.env
KUBELET_ARGS="--bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf --config=/var/lib/kubelet/config.yaml --cgroup-driver=systemd --network-plugin=cni --pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64:3.1"
EOF

systemctl daemon-reload
systemctl enable kubelet.service
systemctl start kubelet.service
systemctl status kubelet.service


# 创建kube-proxy的配置文件
cat <<EOF > /var/lib/kube-proxy/config.conf
apiVersion: kubeproxy.config.k8s.io/v1alpha1
bindAddress: 0.0.0.0
clientConnection:
acceptContentTypes: ""
burst: 0
contentType: ""
kubeconfig: /etc/kubernetes/proxy.conf
qps: 0
clusterCIDR: 10.211.0.0/16
configSyncPeriod: 0s
conntrack:
maxPerCore: null
min: null
tcpCloseWaitTimeout: null
tcpEstablishedTimeout: null
enableProfiling: false
healthzBindAddress: ""
hostnameOverride: ""
iptables:
masqueradeAll: false
masqueradeBit: null
minSyncPeriod: 0s
syncPeriod: 0s
ipvs:
excludeCIDRs: null
minSyncPeriod: 0s
scheduler: ""
strictARP: false
syncPeriod: 0s
kind: KubeProxyConfiguration
metricsBindAddress: ""
mode: ""
nodePortAddresses: null
oomScoreAdj: null
portRange: ""
udpIdleTimeout: 0s
winkernel:
enableDSR: false
networkName: ""
sourceVip: ""
EOF

# 配置和启动kube-proxy服务
cat <<EOF > /usr/lib/systemd/system/kube-proxy.service
[Unit]
Description=Kubernetes Proxy Server
Documentation=https://github.com/kubernetes/kubernetes
After=network.target
Requires=network.service

[Service]
EnvironmentFile=-/etc/kubernetes/kube-proxy.env
ExecStart=/usr/bin/kube-proxy \$KUBE_PROXY_ARGS
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

cat <<EOF > /etc/kubernetes/kube-proxy.env
KUBE_PROXY_ARGS="--config=/var/lib/kube-proxy/config.conf --hostname-override=node01"
EOF

yum install -y conntrack

systemctl daemon-reload
systemctl enable kube-proxy.service
systemctl start kube-proxy.service
systemctl status kube-proxy.service

6. 配置和安装网络插件

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
mkdir -p calico/

cat <<EOF > calico/calico.yaml
---
# Source: calico/templates/calico-config.yaml
# This ConfigMap is used to configure a self-hosted Calico installation.
kind: ConfigMap
apiVersion: v1
metadata:
name: calico-config
namespace: kube-system
data:
# Typha is disabled.
typha_service_name: "none"
# Configure the backend to use.
calico_backend: "bird"

# Configure the MTU to use
veth_mtu: "1440"

# The CNI network configuration to install on each node. The special
# values in this config will be automatically populated.
cni_network_config: |-
{
"name": "k8s-pod-network",
"cniVersion": "0.3.1",
"plugins": [
{
"type": "calico",
"log_level": "info",
"datastore_type": "kubernetes",
"nodename": "__KUBERNETES_NODE_NAME__",
"mtu": __CNI_MTU__,
"ipam": {
"type": "calico-ipam"
},
"policy": {
"type": "k8s"
},
"kubernetes": {
"kubeconfig": "__KUBECONFIG_FILEPATH__"
}
},
{
"type": "portmap",
"snat": true,
"capabilities": {"portMappings": true}
}
]
}

---
# Source: calico/templates/kdd-crds.yaml
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: felixconfigurations.crd.projectcalico.org
spec:
scope: Cluster
group: crd.projectcalico.org
version: v1
names:
kind: FelixConfiguration
plural: felixconfigurations
singular: felixconfiguration
---

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: ipamblocks.crd.projectcalico.org
spec:
scope: Cluster
group: crd.projectcalico.org
version: v1
names:
kind: IPAMBlock
plural: ipamblocks
singular: ipamblock

---

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: blockaffinities.crd.projectcalico.org
spec:
scope: Cluster
group: crd.projectcalico.org
version: v1
names:
kind: BlockAffinity
plural: blockaffinities
singular: blockaffinity

---

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: ipamhandles.crd.projectcalico.org
spec:
scope: Cluster
group: crd.projectcalico.org
version: v1
names:
kind: IPAMHandle
plural: ipamhandles
singular: ipamhandle

---

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: ipamconfigs.crd.projectcalico.org
spec:
scope: Cluster
group: crd.projectcalico.org
version: v1
names:
kind: IPAMConfig
plural: ipamconfigs
singular: ipamconfig

---

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: bgppeers.crd.projectcalico.org
spec:
scope: Cluster
group: crd.projectcalico.org
version: v1
names:
kind: BGPPeer
plural: bgppeers
singular: bgppeer

---

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: bgpconfigurations.crd.projectcalico.org
spec:
scope: Cluster
group: crd.projectcalico.org
version: v1
names:
kind: BGPConfiguration
plural: bgpconfigurations
singular: bgpconfiguration

---

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: ippools.crd.projectcalico.org
spec:
scope: Cluster
group: crd.projectcalico.org
version: v1
names:
kind: IPPool
plural: ippools
singular: ippool

---

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: hostendpoints.crd.projectcalico.org
spec:
scope: Cluster
group: crd.projectcalico.org
version: v1
names:
kind: HostEndpoint
plural: hostendpoints
singular: hostendpoint

---

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: clusterinformations.crd.projectcalico.org
spec:
scope: Cluster
group: crd.projectcalico.org
version: v1
names:
kind: ClusterInformation
plural: clusterinformations
singular: clusterinformation

---

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: globalnetworkpolicies.crd.projectcalico.org
spec:
scope: Cluster
group: crd.projectcalico.org
version: v1
names:
kind: GlobalNetworkPolicy
plural: globalnetworkpolicies
singular: globalnetworkpolicy

---

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: globalnetworksets.crd.projectcalico.org
spec:
scope: Cluster
group: crd.projectcalico.org
version: v1
names:
kind: GlobalNetworkSet
plural: globalnetworksets
singular: globalnetworkset

---

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: networkpolicies.crd.projectcalico.org
spec:
scope: Namespaced
group: crd.projectcalico.org
version: v1
names:
kind: NetworkPolicy
plural: networkpolicies
singular: networkpolicy

---

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: networksets.crd.projectcalico.org
spec:
scope: Namespaced
group: crd.projectcalico.org
version: v1
names:
kind: NetworkSet
plural: networksets
singular: networkset
---
# Source: calico/templates/rbac.yaml

# Include a clusterrole for the kube-controllers component,
# and bind it to the calico-kube-controllers serviceaccount.
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: calico-kube-controllers
rules:
# Nodes are watched to monitor for deletions.
- apiGroups: [""]
resources:
- nodes
verbs:
- watch
- list
- get
# Pods are queried to check for existence.
- apiGroups: [""]
resources:
- pods
verbs:
- get
# IPAM resources are manipulated when nodes are deleted.
- apiGroups: ["crd.projectcalico.org"]
resources:
- ippools
verbs:
- list
- apiGroups: ["crd.projectcalico.org"]
resources:
- blockaffinities
- ipamblocks
- ipamhandles
verbs:
- get
- list
- create
- update
- delete
# Needs access to update clusterinformations.
- apiGroups: ["crd.projectcalico.org"]
resources:
- clusterinformations
verbs:
- get
- create
- update
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: calico-kube-controllers
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: calico-kube-controllers
subjects:
- kind: ServiceAccount
name: calico-kube-controllers
namespace: kube-system
---
# Include a clusterrole for the calico-node DaemonSet,
# and bind it to the calico-node serviceaccount.
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: calico-node
rules:
# The CNI plugin needs to get pods, nodes, and namespaces.
- apiGroups: [""]
resources:
- pods
- nodes
- namespaces
verbs:
- get
- apiGroups: [""]
resources:
- endpoints
- services
verbs:
# Used to discover service IPs for advertisement.
- watch
- list
# Used to discover Typhas.
- get
- apiGroups: [""]
resources:
- nodes/status
verbs:
# Needed for clearing NodeNetworkUnavailable flag.
- patch
# Calico stores some configuration information in node annotations.
- update
# Watch for changes to Kubernetes NetworkPolicies.
- apiGroups: ["networking.k8s.io"]
resources:
- networkpolicies
verbs:
- watch
- list
# Used by Calico for policy information.
- apiGroups: [""]
resources:
- pods
- namespaces
- serviceaccounts
verbs:
- list
- watch
# The CNI plugin patches pods/status.
- apiGroups: [""]
resources:
- pods/status
verbs:
- patch
# Calico monitors various CRDs for config.
- apiGroups: ["crd.projectcalico.org"]
resources:
- globalfelixconfigs
- felixconfigurations
- bgppeers
- globalbgpconfigs
- bgpconfigurations
- ippools
- ipamblocks
- globalnetworkpolicies
- globalnetworksets
- networkpolicies
- networksets
- clusterinformations
- hostendpoints
- blockaffinities
verbs:
- get
- list
- watch
# Calico must create and update some CRDs on startup.
- apiGroups: ["crd.projectcalico.org"]
resources:
- ippools
- felixconfigurations
- clusterinformations
verbs:
- create
- update
# Calico stores some configuration information on the node.
- apiGroups: [""]
resources:
- nodes
verbs:
- get
- list
- watch
# These permissions are only requried for upgrade from v2.6, and can
# be removed after upgrade or on fresh installations.
- apiGroups: ["crd.projectcalico.org"]
resources:
- bgpconfigurations
- bgppeers
verbs:
- create
- update
# These permissions are required for Calico CNI to perform IPAM allocations.
- apiGroups: ["crd.projectcalico.org"]
resources:
- blockaffinities
- ipamblocks
- ipamhandles
verbs:
- get
- list
- create
- update
- delete
- apiGroups: ["crd.projectcalico.org"]
resources:
- ipamconfigs
verbs:
- get
# Block affinities must also be watchable by confd for route aggregation.
- apiGroups: ["crd.projectcalico.org"]
resources:
- blockaffinities
verbs:
- watch
# The Calico IPAM migration needs to get daemonsets. These permissions can be
# removed if not upgrading from an installation using host-local IPAM.
- apiGroups: ["apps"]
resources:
- daemonsets
verbs:
- get
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: calico-node
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: calico-node
subjects:
- kind: ServiceAccount
name: calico-node
namespace: kube-system

---
# Source: calico/templates/calico-node.yaml
# This manifest installs the calico-node container, as well
# as the CNI plugins and network config on
# each master and worker node in a Kubernetes cluster.
kind: DaemonSet
apiVersion: apps/v1
metadata:
name: calico-node
namespace: kube-system
labels:
k8s-app: calico-node
spec:
selector:
matchLabels:
k8s-app: calico-node
updateStrategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
template:
metadata:
labels:
k8s-app: calico-node
annotations:
# This, along with the CriticalAddonsOnly toleration below,
# marks the pod as a critical add-on, ensuring it gets
# priority scheduling and that its resources are reserved
# if it ever gets evicted.
scheduler.alpha.kubernetes.io/critical-pod: ''
spec:
nodeSelector:
beta.kubernetes.io/os: linux
hostNetwork: true
tolerations:
# Make sure calico-node gets scheduled on all nodes.
- effect: NoSchedule
operator: Exists
# Mark the pod as a critical add-on for rescheduling.
- key: CriticalAddonsOnly
operator: Exists
- effect: NoExecute
operator: Exists
serviceAccountName: calico-node
# Minimize downtime during a rolling upgrade or deletion; tell Kubernetes to do a "force
# deletion": https://kubernetes.io/docs/concepts/workloads/pods/pod/#termination-of-pods.
terminationGracePeriodSeconds: 0
priorityClassName: system-node-critical
initContainers:
# This container performs upgrade from host-local IPAM to calico-ipam.
# It can be deleted if this is a fresh installation, or if you have already
# upgraded to use calico-ipam.
- name: upgrade-ipam
image: calico/cni:v3.11.1
command: ["/opt/cni/bin/calico-ipam", "-upgrade"]
env:
- name: KUBERNETES_NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: CALICO_NETWORKING_BACKEND
valueFrom:
configMapKeyRef:
name: calico-config
key: calico_backend
volumeMounts:
- mountPath: /var/lib/cni/networks
name: host-local-net-dir
- mountPath: /host/opt/cni/bin
name: cni-bin-dir
securityContext:
privileged: true
# This container installs the CNI binaries
# and CNI network config file on each node.
- name: install-cni
image: calico/cni:v3.11.1
command: ["/install-cni.sh"]
env:
# Name of the CNI config file to create.
- name: CNI_CONF_NAME
value: "10-calico.conflist"
# The CNI network config to install on each node.
- name: CNI_NETWORK_CONFIG
valueFrom:
configMapKeyRef:
name: calico-config
key: cni_network_config
# Set the hostname based on the k8s node name.
- name: KUBERNETES_NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
# CNI MTU Config variable
- name: CNI_MTU
valueFrom:
configMapKeyRef:
name: calico-config
key: veth_mtu
# Prevents the container from sleeping forever.
- name: SLEEP
value: "false"
volumeMounts:
- mountPath: /host/opt/cni/bin
name: cni-bin-dir
- mountPath: /host/etc/cni/net.d
name: cni-net-dir
securityContext:
privileged: true
# Adds a Flex Volume Driver that creates a per-pod Unix Domain Socket to allow Dikastes
# to communicate with Felix over the Policy Sync API.
- name: flexvol-driver
image: calico/pod2daemon-flexvol:v3.11.1
volumeMounts:
- name: flexvol-driver-host
mountPath: /host/driver
securityContext:
privileged: true
containers:
# Runs calico-node container on each Kubernetes node. This
# container programs network policy and routes on each
# host.
- name: calico-node
image: calico/node:v3.11.1
env:
# Use Kubernetes API as the backing datastore.
- name: DATASTORE_TYPE
value: "kubernetes"
# Wait for the datastore.
- name: WAIT_FOR_DATASTORE
value: "true"
# Set based on the k8s node name.
- name: NODENAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
# Choose the backend to use.
- name: CALICO_NETWORKING_BACKEND
valueFrom:
configMapKeyRef:
name: calico-config
key: calico_backend
# Cluster type to identify the deployment type
- name: CLUSTER_TYPE
value: "k8s,bgp"
# Auto-detect the BGP IP address.
- name: IP
value: "autodetect"
# Enable IPIP
- name: CALICO_IPV4POOL_IPIP
value: "CrossSubnet"
# Set MTU for tunnel device used if ipip is enabled
- name: FELIX_IPINIPMTU
valueFrom:
configMapKeyRef:
name: calico-config
key: veth_mtu
# The default IPv4 pool to create on startup if none exists. Pod IPs will be
# chosen from this range. Changing this value after installation will have
# no effect. This should fall within \`--cluster-cidr\`.
- name: CALICO_IPV4POOL_CIDR
value: "10.211.0.0/16"
# Disable file logging so \`kubectl logs\` works.
- name: CALICO_DISABLE_FILE_LOGGING
value: "true"
# Set Felix endpoint to host default action to ACCEPT.
- name: FELIX_DEFAULTENDPOINTTOHOSTACTION
value: "ACCEPT"
# Disable IPv6 on Kubernetes.
- name: FELIX_IPV6SUPPORT
value: "false"
# Set Felix logging to "info"
- name: FELIX_LOGSEVERITYSCREEN
value: "info"
- name: FELIX_HEALTHENABLED
value: "true"
securityContext:
privileged: true
resources:
requests:
cpu: 250m
livenessProbe:
exec:
command:
- /bin/calico-node
- -felix-live
- -bird-live
periodSeconds: 10
initialDelaySeconds: 10
failureThreshold: 6
readinessProbe:
exec:
command:
- /bin/calico-node
- -felix-ready
- -bird-ready
periodSeconds: 10
volumeMounts:
- mountPath: /lib/modules
name: lib-modules
readOnly: true
- mountPath: /run/xtables.lock
name: xtables-lock
readOnly: false
- mountPath: /var/run/calico
name: var-run-calico
readOnly: false
- mountPath: /var/lib/calico
name: var-lib-calico
readOnly: false
- name: policysync
mountPath: /var/run/nodeagent
volumes:
# Used by calico-node.
- name: lib-modules
hostPath:
path: /lib/modules
- name: var-run-calico
hostPath:
path: /var/run/calico
- name: var-lib-calico
hostPath:
path: /var/lib/calico
- name: xtables-lock
hostPath:
path: /run/xtables.lock
type: FileOrCreate
# Used to install CNI.
- name: cni-bin-dir
hostPath:
path: /opt/cni/bin
- name: cni-net-dir
hostPath:
path: /etc/cni/net.d
# Mount in the directory for host-local IPAM allocations. This is
# used when upgrading from host-local to calico-ipam, and can be removed
# if not using the upgrade-ipam init container.
- name: host-local-net-dir
hostPath:
path: /var/lib/cni/networks
# Used to create per-pod Unix Domain Sockets
- name: policysync
hostPath:
type: DirectoryOrCreate
path: /var/run/nodeagent
# Used to install Flex Volume Driver
- name: flexvol-driver-host
hostPath:
type: DirectoryOrCreate
path: /usr/libexec/kubernetes/kubelet-plugins/volume/exec/nodeagent~uds
---

apiVersion: v1
kind: ServiceAccount
metadata:
name: calico-node
namespace: kube-system

---
# Source: calico/templates/calico-kube-controllers.yaml

# See https://github.com/projectcalico/kube-controllers
apiVersion: apps/v1
kind: Deployment
metadata:
name: calico-kube-controllers
namespace: kube-system
labels:
k8s-app: calico-kube-controllers
spec:
# The controllers can only have a single active instance.
replicas: 1
selector:
matchLabels:
k8s-app: calico-kube-controllers
strategy:
type: Recreate
template:
metadata:
name: calico-kube-controllers
namespace: kube-system
labels:
k8s-app: calico-kube-controllers
annotations:
scheduler.alpha.kubernetes.io/critical-pod: ''
spec:
nodeSelector:
beta.kubernetes.io/os: linux
tolerations:
# Mark the pod as a critical add-on for rescheduling.
- key: CriticalAddonsOnly
operator: Exists
- key: node-role.kubernetes.io/master
effect: NoSchedule
serviceAccountName: calico-kube-controllers
priorityClassName: system-cluster-critical
containers:
- name: calico-kube-controllers
image: calico/kube-controllers:v3.11.1
env:
# Choose which controllers to run.
- name: ENABLED_CONTROLLERS
value: node
- name: DATASTORE_TYPE
value: kubernetes
readinessProbe:
exec:
command:
- /usr/bin/check-status
- -r

---

apiVersion: v1
kind: ServiceAccount
metadata:
name: calico-kube-controllers
namespace: kube-system
---
# Source: calico/templates/calico-etcd-secrets.yaml

---
# Source: calico/templates/calico-typha.yaml

---
# Source: calico/templates/configure-canal.yaml

EOF

kubectl create -f calico/

7. 配置和安装DNS插件

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
mkdir -p kube-dns/

cat <<EOF > kube-dns/configmap.yaml
apiVersion: v1
data:
Corefile: |
.:53 {
errors
health {
lameduck 5s
}
ready
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
ttl 30
}
prometheus :9153
forward . /etc/resolv.conf
cache 30
loop
reload
loadbalance
}
kind: ConfigMap
metadata:
name: coredns
namespace: kube-system
EOF

cat <<EOF > kube-dns/serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: coredns
namespace: kube-system
EOF

cat <<EOF > kube-dns/clusterrole.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: system:coredns
rules:
- apiGroups:
- ""
resources:
- endpoints
- services
- pods
- namespaces
verbs:
- list
- watch
- apiGroups:
- ""
resources:
- nodes
verbs:
- get
EOF

cat <<EOF > kube-dns/clusterrolebinding.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: system:coredns
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:coredns
subjects:
- kind: ServiceAccount
name: coredns
namespace: kube-system
EOF

cat <<EOF > kube-dns/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
labels:
k8s-app: kube-dns
name: coredns
namespace: kube-system
spec:
replicas: 2
revisionHistoryLimit: 10
selector:
matchLabels:
k8s-app: kube-dns
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 1
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
k8s-app: kube-dns
spec:
containers:
- args:
- -conf
- /etc/coredns/Corefile
image: registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.5
imagePullPolicy: IfNotPresent
livenessProbe:
failureThreshold: 5
httpGet:
path: /health
port: 8080
scheme: HTTP
initialDelaySeconds: 60
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 5
name: coredns
ports:
- containerPort: 53
name: dns
protocol: UDP
- containerPort: 53
name: dns-tcp
protocol: TCP
- containerPort: 9153
name: metrics
protocol: TCP
readinessProbe:
failureThreshold: 3
httpGet:
path: /ready
port: 8181
scheme: HTTP
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 1
resources:
limits:
memory: 170Mi
requests:
cpu: 100m
memory: 70Mi
securityContext:
allowPrivilegeEscalation: false
capabilities:
add:
- NET_BIND_SERVICE
drop:
- all
readOnlyRootFilesystem: true
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /etc/coredns
name: config-volume
readOnly: true
dnsPolicy: Default
nodeSelector:
beta.kubernetes.io/os: linux
priorityClassName: system-cluster-critical
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: coredns
serviceAccountName: coredns
terminationGracePeriodSeconds: 30
tolerations:
- key: CriticalAddonsOnly
operator: Exists
- effect: NoSchedule
key: node-role.kubernetes.io/master
volumes:
- configMap:
defaultMode: 420
items:
- key: Corefile
path: Corefile
name: coredns
name: config-volume
EOF

cat <<EOF > kube-dns/service.yaml
apiVersion: v1
kind: Service
metadata:
annotations:
prometheus.io/port: "9153"
prometheus.io/scrape: "true"
labels:
k8s-app: kube-dns
kubernetes.io/cluster-service: "true"
kubernetes.io/name: KubeDNS
name: kube-dns
namespace: kube-system
spec:
clusterIP: 10.96.0.10
ports:
- name: dns
port: 53
protocol: UDP
targetPort: 53
- name: dns-tcp
port: 53
protocol: TCP
targetPort: 53
- name: metrics
port: 9153
protocol: TCP
targetPort: 9153
selector:
k8s-app: kube-dns
sessionAffinity: None
type: ClusterIP
EOF

kubectl create -f kube-dns/

7. 验证Service的访问

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
mkdir -p nginx/

cat <<EOF > nginx/01-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
EOF

cat <<EOF > nginx/02-service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
selector:
app: nginx
ports:
- nodePort: 31073
protocol: TCP
port: 80
targetPort: 80
type: NodePort
EOF

kubectl create -f nginx/

# 有响应视为正常,否则视为不正常
curl -XGET http://192.168.112.129:31073
curl -XGET http://192.168.112.130:31073

8. 验证Pod的网络和DNS配置

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
# 在node01节点和node02节点上分别操作
mkdir -p network/
cd network/

cat <<EOF > Dockerfile
FROM alpine:3.8

MAINTAINER wangxin_0611@126.com

RUN apk add --no-cache ca-certificates bind-tools iputils iproute2 net-tools tcpdump
EOF

docker build -t wangx/alpine:3.8-network .

# 在master节点上操作
mkdir -p network/

cat <<EOF > network/network.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: network
namespace: default
spec:
selector:
matchLabels:
app: network
template:
metadata:
labels:
app: network
spec:
containers:
- name: network
image: wangx/alpine:3.8-network
imagePullPolicy: IfNotPresent
command:
- sleep
- "3600"
restartPolicy: Always
EOF

kubectl create -f network/

[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
network-bl7jx 1/1 Running 0 6m 10.211.1.4 node02
network-m2vp6 1/1 Running 0 6m 10.211.0.4 node01

# 在node01上的pod中验证
[root@master ~]# kubectl exec -it network-m2vp6 /bin/sh
/ # cat /etc/resolv.conf
nameserver 10.96.0.10
search default.svc.cluster.local svc.cluster.local cluster.local
options ndots:5
/ # ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: tunl0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1
link/ipip 0.0.0.0 brd 0.0.0.0
4: eth0@if11: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 72:01:49:fa:fb:f3 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 10.211.0.4/32 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::7001:49ff:fefa:fbf3/64 scope link
valid_lft forever preferred_lft forever
/ # ping -c 4 10.211.1.4
PING 10.211.1.4 (10.211.1.4) 56(84) bytes of data.
64 bytes from 10.211.1.4: icmp_seq=1 ttl=62 time=0.314 ms
64 bytes from 10.211.1.4: icmp_seq=2 ttl=62 time=0.490 ms
64 bytes from 10.211.1.4: icmp_seq=3 ttl=62 time=0.415 ms
64 bytes from 10.211.1.4: icmp_seq=4 ttl=62 time=0.491 ms

--- 10.211.1.4 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3000ms
rtt min/avg/max/mdev = 0.314/0.427/0.491/0.075 ms
/ # nslookup kubernetes.default
Server: 10.96.0.10
Address: 10.96.0.10#53

Name: kubernetes.default.svc.cluster.local
Address: 10.96.0.1

/ # nslookup kubernetes
Server: 10.96.0.10
Address: 10.96.0.10#53

Name: kubernetes.default.svc.cluster.local
Address: 10.96.0.1

/ # exit

# 在node02上的pod中验证
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
network-bl7jx 1/1 Running 0 9m 10.211.1.4 node02
network-m2vp6 1/1 Running 0 9m 10.211.0.4 node01
[root@master ~]# kubectl exec -it network-bl7jx /bin/sh
/ # cat /etc/resolv.conf
nameserver 10.96.0.10
search default.svc.cluster.local svc.cluster.local cluster.local
options ndots:5
/ # ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: tunl0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1
link/ipip 0.0.0.0 brd 0.0.0.0
4: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 5a:a6:51:22:9d:2b brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 10.211.1.4/32 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::58a6:51ff:fe22:9d2b/64 scope link
valid_lft forever preferred_lft forever
/ # ping -c 4 10.211.0.4
PING 10.211.0.4 (10.211.0.4) 56(84) bytes of data.
64 bytes from 10.211.0.4: icmp_seq=1 ttl=62 time=0.450 ms
64 bytes from 10.211.0.4: icmp_seq=2 ttl=62 time=0.685 ms
64 bytes from 10.211.0.4: icmp_seq=3 ttl=62 time=0.726 ms
64 bytes from 10.211.0.4: icmp_seq=4 ttl=62 time=0.707 ms

--- 10.211.0.4 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 0.450/0.642/0.726/0.111 ms
/ # nslookup kubernetes.default
Server: 10.96.0.10
Address: 10.96.0.10#53

Name: kubernetes.default.svc.cluster.local
Address: 10.96.0.1

/ # nslookup kubernetes
Server: 10.96.0.10
Address: 10.96.0.10#53

Name: kubernetes.default.svc.cluster.local
Address: 10.96.0.1

/ # exit

六、参考资料

https://mritd.me/2018/01/07/kubernetes-tls-bootstrapping-note/
https://mritd.me/2018/08/28/kubernetes-tls-bootstrapping-with-bootstrap-token/
https://jimmysong.io/kubernetes-handbook/practice/kubectl-installation.html
https://kubernetes.io/docs/reference/access-authn-authz/rbac/#default-roles-and-role-bindings
https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/#noderestriction
https://kubernetes.io/docs/reference/access-authn-authz/node/
https://kubernetes.io/docs/reference/command-line-tools-reference/kubelet-tls-bootstrapping/
https://kubernetes.io/docs/reference/access-authn-authz/bootstrap-tokens/
https://www.cnblogs.com/shenlinken/p/9968274.html

Kubernetes Harbor 的安装、配置和使用

一、实验环境版本信息

1. 操作系统的版本信息

CentOS Linux release 7.6.1810 (Core)

2. 各组件的版本信息

kubernetes cluster v1.17.0,推荐使用kubeadm v1.17.0 进行试验

etcd v3.4.3
kube-apiserver v1.17.0
kube-controller-manager v1.17.0
kube-scheduler v1.17.0
kubectl v1.17.0
helm v3.1.0

docker 18.09.9
kubelet v1.17.0
calico v3.11.1

kubernetes harbor,使用容器化的方式部署

kubernetes harbor v1.10.1

二、在 Node 节点上准备Docker镜像

1
2
3
4
5
6
7
8
9
10
11
12
docker pull goharbor/clair-adapter-photon:v1.0.1-v1.10.1
docker pull goharbor/clair-photon:v2.1.1-v1.10.1
docker pull goharbor/notary-server-photon:v0.6.1-v1.10.1
docker pull goharbor/harbor-core:v1.10.1
docker pull goharbor/harbor-portal:v1.10.1
docker pull goharbor/chartmuseum-photon:v0.9.0-v1.10.1
docker pull goharbor/redis-photon:v1.10.1
docker pull goharbor/notary-signer-photon:v0.6.1-v1.10.1
docker pull goharbor/harbor-registryctl:v1.10.1
docker pull goharbor/registry-photon:v2.7.1-patch-2819-2553-v1.10.1
docker pull goharbor/harbor-jobservice:v1.10.1
docker pull goharbor/harbor-db:v1.10.1

三、安装和配置 Kubernetes Harbor

1. 安装 Helm 3 包管理工具

1
2
3
4
5
6
7
# curl -o helm-v3.1.0-linux-amd64.tar.gz https://get.helm.sh/helm-v3.1.0-linux-amd64.tar.gz
# tar -zxvf helm-v3.1.0-linux-amd64.tar.gz
# cd linux-amd64/
# cp helm /usr/local/bin/

# helm version
version.BuildInfo{Version:"v3.1.0", GitCommit:"b29d20baf09943e134c2fa5e1e1cab3bf93315fa", GitTreeState:"clean", GoVersion:"go1.13.7"}

2. 在 Kubernetes 集群中加入动态存储供应的支持,并配置相关的 StorageClass 对象

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
## 本文以安装了NFS Client Provisioner为例,下面是我环境中的相关资源对象展示,请依据实际环境进行配置。我环境中的 StorageClass 对象叫 managed-nfs-storage 
# kubectl get pod -n storage -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nfs-client-provisioner-6c45d64447-hrjhq 1/1 Running 2 21h 10.211.196.155 node01 <none> <none>

# kubectl get storageclass managed-nfs-storage -o yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
creationTimestamp: "2020-03-03T12:10:27Z"
name: managed-nfs-storage
resourceVersion: "27152"
selfLink: /apis/storage.k8s.io/v1/storageclasses/managed-nfs-storage
uid: 05c23ff9-5f9a-4a6b-89dd-ed99013cc344
mountOptions:
- vers=4
parameters:
archiveOnDelete: "false"
provisioner: fuseim.pri/ifs
reclaimPolicy: Delete
volumeBindingMode: Immediate

# kubectl get storageclass -o wide
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
managed-nfs-storage fuseim.pri/ifs Delete Immediate false 21h

3. 使用 Helm 3 安装 Kubernetes Harbor

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
# git clone https://github.com/goharbor/harbor-helm.git
# cd harbor-helm/
# git checkout -b v1.3.1.tag v1.3.1
# git diff values.yaml
diff --git a/values.yaml b/values.yaml
index b201d98..fdc5593 100644
--- a/values.yaml
+++ b/values.yaml
@@ -26,8 +26,8 @@ expose:
commonName: ""
ingress:
hosts:
- core: core.harbor.domain
- notary: notary.harbor.domain
+ core: core.harbor.singhwang.com
+ notary: notary.harbor.singhwang.com
# set to the type of ingress controller if it has specific requirements.
# leave as `default` for most ingress controllers.
# set to `gce` if using the GCE ingress controller
@@ -98,7 +98,7 @@ expose:
# the IP address of k8s node
#
# If Harbor is deployed behind the proxy, set it as the URL of proxy
-externalURL: https://core.harbor.domain
+externalURL: https://core.harbor.singhwang.com

# The persistence is enabled by default and a default StorageClass
# is needed in the k8s cluster to provision volumes dynamicly.
@@ -120,19 +120,19 @@ persistence:
# Specify the "storageClass" used to provision the volume. Or the default
# StorageClass will be used(the default).
# Set it to "-" to disable dynamic provisioning
- storageClass: ""
+ storageClass: "managed-nfs-storage"
subPath: ""
accessMode: ReadWriteOnce
size: 5Gi
chartmuseum:
existingClaim: ""
- storageClass: ""
+ storageClass: "managed-nfs-storage"
subPath: ""
accessMode: ReadWriteOnce
size: 5Gi
jobservice:
existingClaim: ""
- storageClass: ""
+ storageClass: "managed-nfs-storage"
subPath: ""
accessMode: ReadWriteOnce
size: 1Gi
@@ -140,7 +140,7 @@ persistence:
# be ignored
database:
existingClaim: ""
- storageClass: ""
+ storageClass: "managed-nfs-storage"
subPath: ""
accessMode: ReadWriteOnce
size: 1Gi
@@ -148,7 +148,7 @@ persistence:
# be ignored
redis:
existingClaim: ""
- storageClass: ""
+ storageClass: "managed-nfs-storage"
subPath: ""
accessMode: ReadWriteOnce
size: 1Gi
@@ -250,7 +250,7 @@ updateStrategy:
logLevel: info

# The initial password of Harbor admin. Change it from portal after launching Harbor
-harborAdminPassword: "Harbor12345"
+harborAdminPassword: "190708"
# The secret key used for encryption. Must be a string of 16 chars.
secretKey: "not-a-secure-key"
## 按照上述git对比出来的变化进行修改,storageClass 字段的请依据自己的环境进行配置,比如我环境里的叫 managed-nfs-storage

# helm install harbor harbor-helm --namespace registry
# helm list -n registry
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
harbor registry 1 2020-03-04 11:05:18.297145404 +0800 CST deployed harbor-1.3.1 1.10.1

# kubectl get pod -n registry -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
harbor-harbor-chartmuseum-6cc998b954-b65jc 1/1 Running 2 6h7m 10.211.140.95 node02 <none> <none>
harbor-harbor-clair-654dcfd8bf-tk98w 2/2 Running 10 6h7m 10.211.196.153 node01 <none> <none>
harbor-harbor-core-85b965488d-tp7jv 1/1 Running 4 6h7m 10.211.196.156 node01 <none> <none>
harbor-harbor-database-0 1/1 Running 2 6h7m 10.211.140.94 node02 <none> <none>
harbor-harbor-jobservice-5f489d87b9-sl57q 1/1 Running 2 6h7m 10.211.140.89 node02 <none> <none>
harbor-harbor-notary-server-795ccb7bb6-ngfml 1/1 Running 7 6h7m 10.211.196.152 node01 <none> <none>
harbor-harbor-notary-signer-7595696bf9-kvkk7 1/1 Running 8 6h7m 10.211.140.87 node02 <none> <none>
harbor-harbor-portal-5cbc6d5897-gm2rc 1/1 Running 2 6h7m 10.211.196.154 node01 <none> <none>
harbor-harbor-redis-0 1/1 Running 2 6h7m 10.211.140.93 node02 <none> <none>
harbor-harbor-registry-75c4f4cc9b-8h72h 2/2 Running 4 6h7m 10.211.140.88 node02 <none> <none>

四、使用说明

  1. 获取 ingress 资源对象中的 HOSTS 和 ADDRESS 在访问端做好 hosts 映射,条件允许的话,也可以配置为网络中的 DNS 记录

    1
    2
    3
    # kubectl get ingress -n registry -o wide
    NAME HOSTS ADDRESS PORTS AGE
    harbor-harbor-ingress core.harbor.singhwang.com,notary.harbor.singhwang.com 192.168.112.129,192.168.112.130 80, 443 7h7m
  2. 访问 Kubernetes Harbor 服务并登陆 https://core.harbor.singhwang.com
    login_01
    login_02

  3. 在 Kubernetes Harbor 中创建项目
    project_01
    project_02

  4. 需要使用镜像仓库的Node节点上完成证书的配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    ## 创建证书目录
    # mkdir -p /etc/docker/certs.d/core.harbor.singhwang.com/

    ## 获取证书内容
    # kubectl get secrets harbor-harbor-ingress -n registry -o jsonpath="{.data.ca\.crt}" | base64 --decode

    ## 根据证书内容生成证书文件
    cat <<EOF > /etc/docker/certs.d/core.harbor.singhwang.com/ca.crt
    。。。这里替换为证书内容。。。
    EOF
  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
    # docker login core.harbor.singhwang.com
    Username: admin
    Password:
    WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
    Configure a credential helper to remove this warning. See
    https://docs.docker.com/engine/reference/commandline/login/#credentials-store

    Login Succeeded

    # docker tag nginx:1.7.9 core.harbor.singhwang.com/demo/nginx:1.7.9

    # docker push core.harbor.singhwang.com/demo/nginx:1.7.9
    The push refers to repository [core.harbor.singhwang.com/demo/nginx]
    5f70bf18a086: Pushed
    4b26ab29a475: Pushed
    ccb1d68e3fb7: Pushed
    e387107e2065: Pushed
    63bf84221cce: Pushed
    e02dce553481: Pushed
    dea2e4984e29: Pushed
    1.7.9: digest: sha256:b1f5935eb2e9e2ae89c0b3e2e148c19068d91ca502e857052f14db230443e4c2 size: 3012

    # docker logout core.harbor.singhwang.com
    Removing login credentials for core.harbor.singhwang.com
  6. 在 Kubernetes Harbor 中查看推送上去的镜像和推送日志
    image_01
    log_01

五、参考资料

https://github.com/goharbor/harbor-helm/tree/v1.3.1
https://www.cnblogs.com/longgor/p/11203820.html

Kubernetes Dashboard 的安装、配置和使用

一、实验环境版本信息

1. 操作系统的版本信息

CentOS Linux release 7.6.1810 (Core)

2. 各组件的版本信息

kubernetes cluster v1.17.0,推荐使用kubeadm v1.17.0 进行试验

etcd v3.4.3
kube-apiserver v1.17.0
kube-controller-manager v1.17.0
kube-scheduler v1.17.0
kubectl v1.17.0

docker 18.09.9
kubelet v1.17.0
calico v3.11.1

kubernetes dashborad,使用容器化的方式部署

kubernetes dashboard v2.0.0-rc5

二、准备Docker镜像与Kubernetes YAML部署文件

1. 准备相关的 Docker 镜像

1
2
3
docker pull kubernetesui/dashboard:v2.0.0-rc5
docker pull kubernetesui/metrics-scraper:v1.0.3
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server-amd64:v0.3.6

2. 准备相关的 Kubernetes YAML 部署文件

1
2
3
# git clone https://github.com/kubernetes/dashboard.git
# cd dashboard/
# git checkout -b v2.0.0-rc5.tag v2.0.0-rc5

三、在 Kubernetes Cluster 上安装 Kubernetes Dashboard

1
2
## 接上一步,在 dashboard/ 目录下操作,直接使用 kubectl 创建资源即可
# kubectl create -f aio/deploy/recommended.yaml

四、安装 Kubernetes 监控组件 Metric Server

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# git clone https://github.com/kubernetes-sigs/metrics-server.git
# cd metrics-server/
# git checkout -b v0.3.6.tag v0.3.6
# git branch
master
* v0.3.6.tag

# git diff deploy/1.8+/metrics-server-deployment.yaml
diff --git a/deploy/1.8+/metrics-server-deployment.yaml b/deploy/1.8+/metrics-server-deployment.yaml
index 2393e75..86f4219 100644
--- a/deploy/1.8+/metrics-server-deployment.yaml
+++ b/deploy/1.8+/metrics-server-deployment.yaml
@@ -29,8 +29,12 @@ spec:
emptyDir: {}
containers:
- name: metrics-server
- image: k8s.gcr.io/metrics-server-amd64:v0.3.6
- imagePullPolicy: Always
+ image: registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server-amd64:v0.3.6
+ imagePullPolicy: IfNotPresent
+ command:
+ - /metrics-server
+ - --kubelet-preferred-address-types=InternalIP
+ - --kubelet-insecure-tls
volumeMounts:
- name: tmp-dir
mountPath: /tmp
## 按照上述git对比出来的变化进行修改

# kubectl create -f deploy/1.8+/

# kubectl get pod -n kube-system -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
。。。
metrics-server-c6774ddf4-nnktl 1/1 Running 0 30h 10.211.196.130 node01 <none> <none>
。。。

## 验证metric server的可用性
# kubectl get --raw "/apis/metrics.k8s.io/v1beta1/nodes" | jq .
{
"kind": "NodeMetricsList",
"apiVersion": "metrics.k8s.io/v1beta1",
"metadata": {
"selfLink": "/apis/metrics.k8s.io/v1beta1/nodes"
},
"items": [
{
"metadata": {
"name": "node02",
"selfLink": "/apis/metrics.k8s.io/v1beta1/nodes/node02",
"creationTimestamp": "2020-02-22T05:32:50Z"
},
"timestamp": "2020-02-22T05:32:13Z",
"window": "30s",
"usage": {
"cpu": "88057337n",
"memory": "822040Ki"
}
},
{
"metadata": {
"name": "master",
"selfLink": "/apis/metrics.k8s.io/v1beta1/nodes/master",
"creationTimestamp": "2020-02-22T05:32:50Z"
},
"timestamp": "2020-02-22T05:32:21Z",
"window": "30s",
"usage": {
"cpu": "184970966n",
"memory": "1045388Ki"
}
},
{
"metadata": {
"name": "node01",
"selfLink": "/apis/metrics.k8s.io/v1beta1/nodes/node01",
"creationTimestamp": "2020-02-22T05:32:50Z"
},
"timestamp": "2020-02-22T05:32:18Z",
"window": "30s",
"usage": {
"cpu": "92128619n",
"memory": "833480Ki"
}
}
]
}


# kubectl get --raw "/apis/metrics.k8s.io/v1beta1/pods" | jq .
{
"kind": "PodMetricsList",
"apiVersion": "metrics.k8s.io/v1beta1",
"metadata": {
"selfLink": "/apis/metrics.k8s.io/v1beta1/pods"
},
"items": [
{
"metadata": {
"name": "calico-node-vp2mk",
"namespace": "kube-system",
"selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/kube-system/pods/calico-node-vp2mk",
"creationTimestamp": "2020-02-22T05:33:20Z"
},
"timestamp": "2020-02-22T05:32:13Z",
"window": "30s",
"containers": [
{
"name": "calico-node",
"usage": {
"cpu": "23828482n",
"memory": "29852Ki"
}
}
]
},
{
"metadata": {
"name": "coredns-7f9c544f75-nbtt9",
"namespace": "kube-system",
"selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/kube-system/pods/coredns-7f9c544f75-nbtt9",
"creationTimestamp": "2020-02-22T05:33:20Z"
},
"timestamp": "2020-02-22T05:32:12Z",
"window": "30s",
"containers": [
{
"name": "coredns",
"usage": {
"cpu": "2739715n",
"memory": "8372Ki"
}
}
]
},
{
"metadata": {
"name": "kube-controller-manager-master",
"namespace": "kube-system",
"selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/kube-system/pods/kube-controller-manager-master",
"creationTimestamp": "2020-02-22T05:33:20Z"
},
"timestamp": "2020-02-22T05:32:22Z",
"window": "30s",
"containers": [
{
"name": "kube-controller-manager",
"usage": {
"cpu": "11899093n",
"memory": "61688Ki"
}
}
]
},
{
"metadata": {
"name": "etcd-master",
"namespace": "kube-system",
"selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/kube-system/pods/etcd-master",
"creationTimestamp": "2020-02-22T05:33:20Z"
},
"timestamp": "2020-02-22T05:32:15Z",
"window": "30s",
"containers": [
{
"name": "etcd",
"usage": {
"cpu": "15906661n",
"memory": "46120Ki"
}
}
]
},
{
"metadata": {
"name": "coredns-7f9c544f75-bk25k",
"namespace": "kube-system",
"selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/kube-system/pods/coredns-7f9c544f75-bk25k",
"creationTimestamp": "2020-02-22T05:33:20Z"
},
"timestamp": "2020-02-22T05:32:07Z",
"window": "30s",
"containers": [
{
"name": "coredns",
"usage": {
"cpu": "2311367n",
"memory": "11300Ki"
}
}
]
},
{
"metadata": {
"name": "dashboard-metrics-scraper-7b8b58dc8b-nnktl",
"namespace": "kubernetes-dashboard",
"selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/kubernetes-dashboard/pods/dashboard-metrics-scraper-7b8b58dc8b-nnktl",
"creationTimestamp": "2020-02-22T05:33:20Z"
},
"timestamp": "2020-02-22T05:32:12Z",
"window": "30s",
"containers": [
{
"name": "dashboard-metrics-scraper",
"usage": {
"cpu": "487926n",
"memory": "3932Ki"
}
}
]
},
{
"metadata": {
"name": "kube-proxy-v6vtg",
"namespace": "kube-system",
"selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/kube-system/pods/kube-proxy-v6vtg",
"creationTimestamp": "2020-02-22T05:33:20Z"
},
"timestamp": "2020-02-22T05:32:11Z",
"window": "30s",
"containers": [
{
"name": "kube-proxy",
"usage": {
"cpu": "414352n",
"memory": "23932Ki"
}
}
]
},
{
"metadata": {
"name": "network-pz6st",
"namespace": "default",
"selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/default/pods/network-pz6st",
"creationTimestamp": "2020-02-22T05:33:20Z"
},
"timestamp": "2020-02-22T05:32:18Z",
"window": "30s",
"containers": [
{
"name": "network",
"usage": {
"cpu": "0",
"memory": "44Ki"
}
}
]
},
{
"metadata": {
"name": "kube-apiserver-master",
"namespace": "kube-system",
"selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/kube-system/pods/kube-apiserver-master",
"creationTimestamp": "2020-02-22T05:33:20Z"
},
"timestamp": "2020-02-22T05:32:17Z",
"window": "30s",
"containers": [
{
"name": "kube-apiserver",
"usage": {
"cpu": "35264598n",
"memory": "300208Ki"
}
}
]
},
{
"metadata": {
"name": "calico-kube-controllers-648f4868b8-844cd",
"namespace": "kube-system",
"selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/kube-system/pods/calico-kube-controllers-648f4868b8-844cd",
"creationTimestamp": "2020-02-22T05:33:20Z"
},
"timestamp": "2020-02-22T05:32:19Z",
"window": "30s",
"containers": [
{
"name": "calico-kube-controllers",
"usage": {
"cpu": "1185857n",
"memory": "8716Ki"
}
}
]
},
{
"metadata": {
"name": "metrics-server-c6774ddf4-f6lg2",
"namespace": "kube-system",
"selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/kube-system/pods/metrics-server-c6774ddf4-f6lg2",
"creationTimestamp": "2020-02-22T05:33:20Z"
},
"timestamp": "2020-02-22T05:32:17Z",
"window": "30s",
"containers": [
{
"name": "metrics-server",
"usage": {
"cpu": "1059092n",
"memory": "11748Ki"
}
}
]
},
{
"metadata": {
"name": "kube-scheduler-master",
"namespace": "kube-system",
"selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/kube-system/pods/kube-scheduler-master",
"creationTimestamp": "2020-02-22T05:33:20Z"
},
"timestamp": "2020-02-22T05:32:20Z",
"window": "30s",
"containers": [
{
"name": "kube-scheduler",
"usage": {
"cpu": "2369907n",
"memory": "24184Ki"
}
}
]
},
{
"metadata": {
"name": "calico-node-2qjtg",
"namespace": "kube-system",
"selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/kube-system/pods/calico-node-2qjtg",
"creationTimestamp": "2020-02-22T05:33:20Z"
},
"timestamp": "2020-02-22T05:32:21Z",
"window": "30s",
"containers": [
{
"name": "calico-node",
"usage": {
"cpu": "25966443n",
"memory": "25984Ki"
}
}
]
},
{
"metadata": {
"name": "kube-proxy-pwh6h",
"namespace": "kube-system",
"selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/kube-system/pods/kube-proxy-pwh6h",
"creationTimestamp": "2020-02-22T05:33:20Z"
},
"timestamp": "2020-02-22T05:32:15Z",
"window": "30s",
"containers": [
{
"name": "kube-proxy",
"usage": {
"cpu": "216986n",
"memory": "25156Ki"
}
}
]
},
{
"metadata": {
"name": "calico-node-gq9r9",
"namespace": "kube-system",
"selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/kube-system/pods/calico-node-gq9r9",
"creationTimestamp": "2020-02-22T05:33:20Z"
},
"timestamp": "2020-02-22T05:32:04Z",
"window": "30s",
"containers": [
{
"name": "calico-node",
"usage": {
"cpu": "25302709n",
"memory": "26760Ki"
}
}
]
},
{
"metadata": {
"name": "network-s5tjd",
"namespace": "default",
"selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/default/pods/network-s5tjd",
"creationTimestamp": "2020-02-22T05:33:20Z"
},
"timestamp": "2020-02-22T05:32:10Z",
"window": "30s",
"containers": [
{
"name": "network",
"usage": {
"cpu": "0",
"memory": "48Ki"
}
}
]
},
{
"metadata": {
"name": "kubernetes-dashboard-866f987876-5npr9",
"namespace": "kubernetes-dashboard",
"selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/kubernetes-dashboard/pods/kubernetes-dashboard-866f987876-5npr9",
"creationTimestamp": "2020-02-22T05:33:20Z"
},
"timestamp": "2020-02-22T05:32:18Z",
"window": "30s",
"containers": [
{
"name": "kubernetes-dashboard",
"usage": {
"cpu": "281233n",
"memory": "9512Ki"
}
}
]
},
{
"metadata": {
"name": "kube-proxy-knt4j",
"namespace": "kube-system",
"selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/kube-system/pods/kube-proxy-knt4j",
"creationTimestamp": "2020-02-22T05:33:20Z"
},
"timestamp": "2020-02-22T05:32:05Z",
"window": "30s",
"containers": [
{
"name": "kube-proxy",
"usage": {
"cpu": "485574n",
"memory": "15496Ki"
}
}
]
}
]
}

五、配置以https的方式访问 Kubernetes Dashboard

  1. 决定了 Kubernetes Dashboard 以 https 的形式对外提供服务的关键参数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    ## 以 https 对外提供服务时,Kubernetes Dashboard 默认是启用登录模式的
    ## 特别注意:该参数启用后,Kubernetes Dashboard 会监听 8443 端口对外提供 https 服务,并且不会监听 9090 端口提供 http 服务
    --auto-generate-certificates

    ## 设置 https 监听端口,默认值为 8443
    --port

    ## 设置 https 监听地址,默认值为 0.0.0.0
    --bind-address
  2. 决定了 Kubernetes Dashboard 能够启动成功的关键参数

    1
    2
    ## 证书相关的secret对象放在哪个namespace下,通常情况下与 Kubernetes Dashboard 的 pod 所在的 namespace 相同,默认值为 kube-system
    --namespace
  3. 如何访问 Kubernetes Dashboard 的登录页

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    ## 修改 service 类型为 NodePort 类型
    # kubectl edit service kubernetes-dashboard -n kubernetes-dashboard
    。。。。。。
    spec:
    clusterIP: 10.96.56.103
    externalTrafficPolicy: Cluster
    ports:
    - nodePort: 32027
    port: 443
    protocol: TCP
    targetPort: 8443
    selector:
    k8s-app: kubernetes-dashboard
    sessionAffinity: None
    type: NodePort
    。。。。。。

    ## 通过 https://<node-ip>:<node-port> 的形式访问 Kubernetes Dashboard 的登录页,例如 https://192.168.112.129:32027/

login_01

六、使用说明

  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
    mkdir -p access/
    cat <<EOF > access/01-serviceaccount.yaml
    apiVersion: v1
    kind: ServiceAccount
    metadata:
    name: admin-user
    namespace: kubernetes-dashboard
    EOF

    cat <<EOF > access/02-clusterrolebinding.yaml
    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRoleBinding
    metadata:
    name: admin-user
    roleRef:
    apiGroup: rbac.authorization.k8s.io
    kind: ClusterRole
    name: cluster-admin
    subjects:
    - kind: ServiceAccount
    name: admin-user
    namespace: kubernetes-dashboard
    EOF

    kubectl create -f access/
  2. 获取用户的 Token, 并在登录页面上输入, 然后登录 Kubernetes Dashboard

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    ## 获取上一步授权的用户 Token,用于登录 Kubernetes Dashboard 
    # kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')
    Name: admin-user-token-bhp84
    Namespace: kubernetes-dashboard
    Labels: <none>
    Annotations: kubernetes.io/service-account.name: admin-user
    kubernetes.io/service-account.uid: 4c67c8da-0694-4de9-b978-eff7a1075bea

    Type: kubernetes.io/service-account-token

    Data
    ====
    ca.crt: 1025 bytes
    namespace: 20 bytes
    token: eyJhbGciOiJSUzI1NiIsImtpZCI6IkpWcTFvc0Rza0xYZVVhVnlkRkhUX2VDM1RBR1hUNXpKVkdna3kyRTAyVlEifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyLXRva2VuLWJocDg0Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiI0YzY3YzhkYS0wNjk0LTRkZTktYjk3OC1lZmY3YTEwNzViZWEiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZXJuZXRlcy1kYXNoYm9hcmQ6YWRtaW4tdXNlciJ9.aF2iWV--nNTdcrOTSQiQLEWi9QQnonjqk4EZNQSxcgqjZjUIaK1ezXBDrm9_bT5M9ddsMXKuE7E4PuTqk2IMTC_8m9DlinRrHHERAneI5OVO8aoAGqRo-pMyatEF7n9YfNoZMR0pLCWgrwrm1ttADHWtsTYsjrj4uT42Gt_h7J4i47VxF5g9qtqv8Jt_yoQNemje_XhWoGK4p9F_jPt3H8OrQ7CKYx1SwTGfw8t7P_mt9XY9AsWuUO4r4AixnZhWOLBtxa0QibM-mK7X4iREN3Ib8nmezVGkdiVt1epd_zmWAAWMHxEgh1D48wSro2Gb0e2p5AQQV2FAHJrfra4qUA

    ## 登录页面上输入 Token 后,点击对应按钮即可实现登录

login_02
login_03

七、参考资料

1. Kubernetes Dashboard 的官方资料

https://github.com/kubernetes/dashboard/blob/v2.0.0-rc5/src/app/backend/dashboard.go
https://github.com/kubernetes/dashboard/blob/v2.0.0-rc5/docs/user/accessing-dashboard/1.7.x-and-above.md
https://github.com/kubernetes/dashboard/blob/v2.0.0-rc5/docs/user/access-control/creating-sample-user.md
https://github.com/kubernetes/dashboard/blob/v2.0.0-rc5/docs/user/integrations.md
https://github.com/kubernetes/dashboard/blob/v2.0.0-rc5/docs/user/certificate-management.md

2. Kubernetes Metric Server

https://www.cnblogs.com/ding2016/p/10786252.html
https://github.com/singhwang/k8s-prom-hpa

3. 关于 Chrome 无法访问 Kubernetes Dashboard 的问题解决

http://team.jiunile.com/blog/2018/12/k8s-dashboard-chrome-err.html
https://superuser.com/questions/27268/how-do-i-disable-the-warning-chrome-gives-if-a-security-certificate-is-not-trust
https://www.jianshu.com/p/a8cc2c04ee7c
https://blog.gxxsite.com/wei-mac-osxde-cheng-xu-tian-jia-yong-jiu-qi-dong-can-shu/

使用二进制文件的方式安装 Kubernetes v1.11.0 集群(一)

一、实现方式介绍

1. 控制平面的所有组件都采用二进制方式部署,交由systemd统一管理;

控制平面包括:etcd、kube-apiserver、kube-controller-manager和kube-scheduler。

2. 网络插件依然采用Addon方式部署,交由kubernetes统一管理。

网络插件包括:calico-node及其相关的组件。

二、实验环境版本信息

1. 操作系统的版本信息

CentOS Linux release 7.6.1810 (Core)

2. 各组件的版本信息

etcd v3.2.18
kube-apiserver v1.11.0
kube-controller-manager v1.11.0
kube-scheduler v1.11.0
kubectl v1.11.0

docker 17.03.1-ce
kubelet v1.11.0
calico v3.1.3

三、部署架构

1. Kubernetes Master(Control Plane)

192.168.112.128 master -> etcd kube-apiserver kube-controller-manager kube-scheduler

2. Kubernetes Node

192.168.112.129 node01 -> docker kubelet kube-proxy calico-node
192.168.112.130 node02 -> docker kubelet kube-proxy calico-node

四、准备二进制文件与Docker镜像

1. 下载相关的二进制文件压缩包

https://github.com/etcd-io/etcd/releases/download/v3.2.18/etcd-v3.2.18-linux-amd64.tar.gz
https://dl.k8s.io/v1.11.0/kubernetes-server-linux-amd64.tar.gz

2. 拉取相关的Docker镜像

calico网络组件的镜像:
calico/cni:v3.1.3
calico/node:v3.1.3
calico/typha:v0.7.4

kube-dns的镜像:
registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.1.3

infra容器的镜像:
registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64:3.1

五、部署过程记录

1. 准备基础环境(Master和Node上都执行)

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
# 更新系统
yum update -y

# 设置正确的时区和时间
yum install -y ntpdate
timedatectl set-timezone Asia/Shanghai
ntpdate cn.ntp.org.cn

# 关闭防火墙
systemctl disable firewalld.service
systemctl stop firewalld.service

# 关闭swap分区
swapoff -a
sed -i 's#/dev/mapper/cl-swap#\# /dev/mapper/cl-swap#' /etc/fstab

# 关闭selinux
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

# 设置各个节点的主机名
## 192.168.112.128
hostnamectl set-hostname master

## 192.168.112.129
hostnamectl set-hostname node01

## 192.168.112.130
hostnamectl set-hostname node02

# 配置主机名和IP的映射
cat <<EOF >> /etc/hosts

# For Kubernetes Cluster
192.168.112.128 master
192.168.112.129 node01
192.168.112.130 node02
EOF

# 修改内核参数
cat <<EOF > /etc/sysctl.d/kubernetes.conf
net.bridge.bridge-nf-call-iptables = 1

net.ipv4.ip_forward = 1

net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_tw_resue = 1
EOF

sysctl --system

# 修改ulimit限制
cat <<EOF > /etc/security/limits.d/kubernetes.conf
* hard nofile 65535
* soft nofile 65535
EOF

2. 安装Docker环境(在所有Node上都执行)

1
2
3
4
5
6
7
8
9
10
11
12
13
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast

yum install -y https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.1.ce-1.el7.centos.noarch.rpm

yum install -y docker-ce-17.03.1.ce-1.el7.centos

systemctl enable docker.service
systemctl start docker.service
systemctl status docker.service

docker version

3. 复制所有二进制文件到操作系统/usr/bin/目录下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 192.168.112.128 (Master上执行)
tar -zxvf etcd-v3.2.18-linux-amd64.tar.gz
tar -zxvf kubernetes-server-linux-amd64.tar.gz

cp etcd-v3.2.18-linux-amd64/etcd /usr/bin/
cp etcd-v3.2.18-linux-amd64/etcdctl /usr/bin/
cp kubernetes/server/bin/kube-apiserver /usr/bin/
cp kubernetes/server/bin/kube-controller-manager /usr/bin/
cp kubernetes/server/bin/kube-scheduler /usr/bin/
cp kubernetes/server/bin/kubectl /usr/bin/


# 192.168.112.129 和 192.168.112.130 (Node上执行)
tar -zxvf kubernetes-server-linux-amd64.tar.gz

cp kubernetes/server/bin/kubelet /usr/bin/
cp kubernetes/server/bin/kube-proxy /usr/bin/

4. 在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
92
93
94
95
96
97
98
99
# 创建证书和配置文件的存放目录
mkdir -p /etc/kubernetes/pki/

# 生成rsa的公钥和私钥
cd /etc/kubernetes/
openssl genrsa -out sa.key 2048
openssl rsa -in sa.key -pubout -out sa.pub

# 进入证书目录
cd /etc/kubernetes/pki/

# 生成根证书
openssl genrsa -out ca.key 2048
openssl req -x509 -new -nodes -key ca.key -subj "/CN=master" -days 5000 -out ca.crt

# 为kube-apiserver生成相关的证书和配置文件
cat <<EOF > master_ssl.cnf
[req]
req_extensions = v3_req
distinguished_name = req_distinguished_name

[req_distinguished_name]
[v3_req]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation,digitalSignature,keyEncipherment
subjectAltName = @alt_names
[alt_names]
DNS.1 = kubernetes
DNS.2 = kubernetes.default
DNS.3 = kubernetes.default.svc
DNS.4 = kubernetes.default.svc.cluster.local
DNS.5 = master
IP.1 = 10.96.0.1
IP.2 = 192.168.112.128
EOF

openssl genrsa -out apiserver.key 2048
openssl req -new -key apiserver.key -subj "/CN=master" -config master_ssl.cnf -out apiserver.csr
openssl x509 -req -in apiserver.csr -CA ca.crt -CAkey ca.key -CAcreateserial -days 5000 -extensions v3_req -extfile master_ssl.cnf -out apiserver.crt

# 为kube-controller-manager生成相关的证书和配置文件
openssl genrsa -out controller-manager.key 2048
openssl req -new -key controller-manager.key -subj "/CN=master" -out controller-manager.csr
openssl x509 -req -in controller-manager.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out controller-manager.crt -days 5000

export KUBECONFIG=/etc/kubernetes/controller-manager.conf
kubectl config set-cluster kubernetes --server=https://192.168.112.128:6443 --certificate-authority=/etc/kubernetes/pki/ca.crt --embed-certs=true
kubectl config set-credentials system:kube-controller-manager --client-certificate=/etc/kubernetes/pki/controller-manager.crt --client-key=/etc/kubernetes/pki/controller-manager.key --embed-certs=true
kubectl config set-context system:kube-controller-manager@kubernetes --cluster=kubernetes --user=system:kube-controller-manager
kubectl config use-context system:kube-controller-manager@kubernetes
unset KUBECONFIG

# 为kube-scheduler生成相关的证书和配置文件
openssl genrsa -out scheduler.key 2048
openssl req -new -key scheduler.key -subj "/CN=master" -out scheduler.csr
openssl x509 -req -in scheduler.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out scheduler.crt -days 5000

export KUBECONFIG=/etc/kubernetes/scheduler.conf
kubectl config set-cluster kubernetes --server=https://192.168.112.128:6443 --certificate-authority=/etc/kubernetes/pki/ca.crt --embed-certs=true
kubectl config set-credentials system:kube-scheduler --client-certificate=/etc/kubernetes/pki/scheduler.crt --client-key=/etc/kubernetes/pki/scheduler.key --embed-certs=true
kubectl config set-context system:kube-scheduler@kubernetes --cluster=kubernetes --user=system:kube-scheduler
kubectl config use-context system:kube-scheduler@kubernetes
unset KUBECONFIG

# 为kubectl生成相关的证书和配置文件
openssl genrsa -out kubectl.key 2048
openssl req -new -key kubectl.key -subj "/CN=master" -out kubectl.csr
openssl x509 -req -in kubectl.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out kubectl.crt -days 5000

export KUBECONFIG=/etc/kubernetes/admin.conf
kubectl config set-cluster kubernetes --server=https://192.168.112.128:6443 --certificate-authority=/etc/kubernetes/pki/ca.crt --embed-certs=true
kubectl config set-credentials system:admin --client-certificate=/etc/kubernetes/pki/kubectl.crt --client-key=/etc/kubernetes/pki/kubectl.key --embed-certs=true
kubectl config set-context system:admin@kubernetes --cluster=kubernetes --user=system:admin
kubectl config use-context system:admin@kubernetes
unset KUBECONFIG

# 为kubelet生成相关的证书和配置文件
openssl genrsa -out kubelet.key 2048
openssl req -new -key kubelet.key -subj "/CN=node" -out kubelet.csr
openssl x509 -req -in kubelet.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out kubelet.crt -days 5000

export KUBECONFIG=/etc/kubernetes/kubelet.conf
kubectl config set-cluster kubernetes --server=https://192.168.112.128:6443 --certificate-authority=/etc/kubernetes/pki/ca.crt --embed-certs=true
kubectl config set-credentials system:kubelet --client-certificate=/etc/kubernetes/pki/kubelet.crt --client-key=/etc/kubernetes/pki/kubelet.key --embed-certs=true
kubectl config set-context system:kubelet@kubernetes --cluster=kubernetes --user=system:kubelet
kubectl config use-context system:kubelet@kubernetes
unset KUBECONFIG

# 为kube-proxy生成相关的证书和配置文件
openssl genrsa -out proxy.key 2048
openssl req -new -key proxy.key -subj "/CN=node" -out proxy.csr
openssl x509 -req -in proxy.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out proxy.crt -days 5000

export KUBECONFIG=/etc/kubernetes/proxy.conf
kubectl config set-cluster kubernetes --server=https://192.168.112.128:6443 --certificate-authority=/etc/kubernetes/pki/ca.crt --embed-certs=true
kubectl config set-credentials system:proxy --client-certificate=/etc/kubernetes/pki/proxy.crt --client-key=/etc/kubernetes/pki/proxy.key --embed-certs=true
kubectl config set-context system:proxy@kubernetes --cluster=kubernetes --user=system:proxy
kubectl config use-context system:proxy@kubernetes
unset KUBECONFIG

5. 配置和启动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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 配置和启动etcd服务
mkdir -p /var/lib/etcd/

cat <<EOF > /usr/lib/systemd/system/etcd.service
[Unit]
Description=Etcd Server
After=network.target

[Service]
Type=simple
WorkingDirectory=/var/lib/etcd/
EnvironmentFile=-/etc/etcd/etcd.conf
ExecStart=/usr/bin/etcd

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable etcd.service
systemctl start etcd.service
systemctl status etcd.service

# 配置和启动kube-apiserver服务
cat <<EOF > /usr/lib/systemd/system/kube-apiserver.service
[Unit]
Description=Kubernetes API Server
Documentation=https://github.com/kubernetes/kubernetes
After=etcd.service
Wants=etcd.service

[Service]
EnvironmentFile=-/etc/kubernetes/kube-apiserver
ExecStart=/usr/bin/kube-apiserver \$KUBE_API_ARGS
Restart=on-failure
Type=notify
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

cat <<EOF > /etc/kubernetes/kube-apiserver
KUBE_API_ARGS="--storage-backend=etcd3 --etcd-servers=http://127.0.0.1:2379 --allow-privileged=true --client-ca-file=/etc/kubernetes/pki/ca.crt --tls-cert-file=/etc/kubernetes/pki/apiserver.crt --tls-private-key-file=/etc/kubernetes/pki/apiserver.key --service-account-key-file=/etc/kubernetes/sa.pub --advertise-address=192.168.112.128 --secure-port=6443 --insecure-bind-address=0.0.0.0 --insecure-port=8080 --service-cluster-ip-range=10.96.0.0/16 --service-node-port-range=30000-32767 --authorization-mode=Node,RBAC --logtostderr=false --log-dir=/var/log/kubernetes --v=2 --enable-admission-plugins=ResourceQuota,LimitRanger"
EOF

systemctl daemon-reload
systemctl enable kube-apiserver.service
systemctl start kube-apiserver.service
systemctl status kube-apiserver.service

# 为第4步中涉及的用户master和node绑定cluster-admin角色
kubectl create clusterrolebinding system:component:master --clusterrole=cluster-admin --user=master
kubectl create clusterrolebinding system:component:node --clusterrole=cluster-admin --user=node

# 配置和启动kube-controller-manager服务
cat <<EOF > /usr/lib/systemd/system/kube-controller-manager.service
[Unit]
Description=Kubernetes Controller Manager
Documentation=https://github.com/kubernetes/kubernetes
After=kube-apiserver.service
Requires=kube-apiserver.service

[Service]
EnvironmentFile=-/etc/kubernetes/kube-controller-manager
ExecStart=/usr/bin/kube-controller-manager \$KUBE_CONTROLLER_MANAGER_ARGS
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

cat <<EOF > /etc/kubernetes/kube-controller-manager
KUBE_CONTROLLER_MANAGER_ARGS="--service-account-private-key-file=/etc/kubernetes/sa.key --leader-elect=true --cluster-signing-cert-file=/etc/kubernetes/pki/ca.crt --cluster-signing-key-file=/etc/kubernetes/pki/ca.key --root-ca-file=/etc/kubernetes/pki/ca.crt --kubeconfig=/etc/kubernetes/controller-manager.conf --allocate-node-cidrs=true --cluster-cidr=10.211.0.0/16 --logtostderr=false --log-dir=/var/log/kubernetes --v=2"
EOF

systemctl daemon-reload
systemctl enable kube-controller-manager.service
systemctl start kube-controller-manager.service
systemctl status kube-controller-manager.service

# 配置和启动kube-scheduler服务
cat <<EOF > /usr/lib/systemd/system/kube-scheduler.service
[Unit]
Description=Kubernetes Scheduler
Documentation=https://github.com/kubernetes/kubernetes
After=kube-apiserver.service
Requires=kube-apiserver.service

[Service]
EnvironmentFile=-/etc/kubernetes/kube-scheduler
ExecStart=/usr/bin/kube-scheduler \$KUBE_SCHEDULER_ARGS
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

cat <<EOF > /etc/kubernetes/kube-scheduler
KUBE_SCHEDULER_ARGS="--kubeconfig=/etc/kubernetes/scheduler.conf --leader-elect=true --logtostderr=false --log-dir=/var/log/kubernetes --v=2"
EOF

systemctl daemon-reload
systemctl enable kube-scheduler.service
systemctl start kube-scheduler.service
systemctl status kube-scheduler.service

5. 配置和启动Node上的所有组件

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
# 创建配置目录和工作目录
mkdir -p /etc/kubernetes
mkdir -p /var/lib/kubelet

# 传输相关配置文件到当前节点上
scp root@192.168.112.128:/etc/kubernetes/kubelet.conf /etc/kubernetes/
scp root@192.168.112.128:/etc/kubernetes/proxy.conf /etc/kubernetes/

# 配置和启动kubelet服务
cat <<EOF > /usr/lib/systemd/system/kubelet.service
[Unit]
Description=Kubernetes Kubelet Server
Documentation=https://github.com/kubernetes/kubernetes
After=docker.service
Requires=docker.service

[Service]
WorkingDirectory=/var/lib/kubelet
EnvironmentFile=-/etc/kubernetes/kubelet
ExecStart=/usr/bin/kubelet \$KUBELET_ARGS
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

cat <<EOF > /etc/kubernetes/kubelet
KUBELET_ARGS="--kubeconfig=/etc/kubernetes/kubelet.conf --cluster-dns=10.96.0.10 --cluster-domain=cluster.local --logtostderr=false --log-dir=/var/log/kubernetes --v=2 --cgroup-driver=cgroupfs --cni-bin-dir=/opt/cni/bin --cni-conf-dir=/etc/cni/net.d --network-plugin=cni --pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64:3.1"
EOF

systemctl daemon-reload
systemctl enable kubelet.service
systemctl start kubelet.service
systemctl status kubelet.service

# 配置和启动kube-proxy服务
mkdir -p /var/lib/kube-proxy/
cat <<EOF > /var/lib/kube-proxy/config.conf
apiVersion: kubeproxy.config.k8s.io/v1alpha1
bindAddress: 0.0.0.0
clientConnection:
acceptContentTypes: ""
burst: 10
contentType: application/vnd.kubernetes.protobuf
kubeconfig: /etc/kubernetes/proxy.conf
qps: 5
clusterCIDR: 10.211.0.0/16
configSyncPeriod: 15m0s
conntrack:
max: null
maxPerCore: 32768
min: 131072
tcpCloseWaitTimeout: 1h0m0s
tcpEstablishedTimeout: 24h0m0s
enableProfiling: false
healthzBindAddress: 0.0.0.0:10256
hostnameOverride: ""
iptables:
masqueradeAll: false
masqueradeBit: 14
minSyncPeriod: 0s
syncPeriod: 30s
ipvs:
excludeCIDRs: null
minSyncPeriod: 0s
scheduler: ""
syncPeriod: 30s
kind: KubeProxyConfiguration
metricsBindAddress: 127.0.0.1:10249
mode: iptables
nodePortAddresses: null
oomScoreAdj: -999
portRange: ""
resourceContainer: /kube-proxy
udpIdleTimeout: 250ms
EOF

cat <<EOF > /usr/lib/systemd/system/kube-proxy.service
[Unit]
Description=Kubernetes Proxy Server
Documentation=https://github.com/kubernetes/kubernetes
After=network.target
Requires=network.service

[Service]
EnvironmentFile=-/etc/kubernetes/kube-proxy
ExecStart=/usr/bin/kube-proxy \$KUBE_PROXY_ARGS
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

cat <<EOF > /etc/kubernetes/kube-proxy
KUBE_PROXY_ARGS="--config=/var/lib/kube-proxy/config.conf --logtostderr=false --log-dir=/var/log/kubernetes --v=2"
EOF

systemctl daemon-reload
systemctl enable kube-proxy.service
systemctl start kube-proxy.service
systemctl status kube-proxy.service

6. 配置和安装网络插件

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
mkdir -p calico

cat <<EOF > calico/rbac-kdd.yaml
# Calico Version v3.1.3
# https://docs.projectcalico.org/v3.1/releases#v3.1.3
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: calico-node
rules:
- apiGroups: [""]
resources:
- namespaces
verbs:
- get
- list
- watch
- apiGroups: [""]
resources:
- pods/status
verbs:
- update
- apiGroups: [""]
resources:
- pods
verbs:
- get
- list
- watch
- patch
- apiGroups: [""]
resources:
- services
verbs:
- get
- apiGroups: [""]
resources:
- endpoints
verbs:
- get
- apiGroups: [""]
resources:
- nodes
verbs:
- get
- list
- update
- watch
- apiGroups: ["extensions"]
resources:
- networkpolicies
verbs:
- get
- list
- watch
- apiGroups: ["networking.k8s.io"]
resources:
- networkpolicies
verbs:
- watch
- list
- apiGroups: ["crd.projectcalico.org"]
resources:
- globalfelixconfigs
- felixconfigurations
- bgppeers
- globalbgpconfigs
- bgpconfigurations
- ippools
- globalnetworkpolicies
- globalnetworksets
- networkpolicies
- clusterinformations
- hostendpoints
verbs:
- create
- get
- list
- update
- watch

---

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: calico-node
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: calico-node
subjects:
- kind: ServiceAccount
name: calico-node
namespace: kube-system
EOF

cat <<EOF > calico/calico.yaml
# Calico Version v3.1.3
# https://docs.projectcalico.org/v3.1/releases#v3.1.3
# This manifest includes the following component versions:
# calico/node:v3.1.3
# calico/cni:v3.1.3

# This ConfigMap is used to configure a self-hosted Calico installation.
kind: ConfigMap
apiVersion: v1
metadata:
name: calico-config
namespace: kube-system
data:
# To enable Typha, set this to "calico-typha" *and* set a non-zero value for Typha replicas
# below. We recommend using Typha if you have more than 50 nodes. Above 100 nodes it is
# essential.
typha_service_name: "none"

# The CNI network configuration to install on each node.
cni_network_config: |-
{
"name": "k8s-pod-network",
"cniVersion": "0.3.0",
"plugins": [
{
"type": "calico",
"log_level": "info",
"datastore_type": "kubernetes",
"nodename": "__KUBERNETES_NODE_NAME__",
"mtu": 1500,
"ipam": {
"type": "host-local",
"subnet": "usePodCidr"
},
"policy": {
"type": "k8s"
},
"kubernetes": {
"kubeconfig": "__KUBECONFIG_FILEPATH__"
}
},
{
"type": "portmap",
"snat": true,
"capabilities": {"portMappings": true}
}
]
}

---

# This manifest creates a Service, which will be backed by Calico's Typha daemon.
# Typha sits in between Felix and the API server, reducing Calico's load on the API server.

apiVersion: v1
kind: Service
metadata:
name: calico-typha
namespace: kube-system
labels:
k8s-app: calico-typha
spec:
ports:
- port: 5473
protocol: TCP
targetPort: calico-typha
name: calico-typha
selector:
k8s-app: calico-typha

---

# This manifest creates a Deployment of Typha to back the above service.

apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: calico-typha
namespace: kube-system
labels:
k8s-app: calico-typha
spec:
# Number of Typha replicas. To enable Typha, set this to a non-zero value *and* set the
# typha_service_name variable in the calico-config ConfigMap above.
#
# We recommend using Typha if you have more than 50 nodes. Above 100 nodes it is essential
# (when using the Kubernetes datastore). Use one replica for every 100-200 nodes. In
# production, we recommend running at least 3 replicas to reduce the impact of rolling upgrade.
replicas: 0
revisionHistoryLimit: 2
template:
metadata:
labels:
k8s-app: calico-typha
annotations:
# This, along with the CriticalAddonsOnly toleration below, marks the pod as a critical
# add-on, ensuring it gets priority scheduling and that its resources are reserved
# if it ever gets evicted.
scheduler.alpha.kubernetes.io/critical-pod: ''
spec:
hostNetwork: true
tolerations:
# Mark the pod as a critical add-on for rescheduling.
- key: CriticalAddonsOnly
operator: Exists
# Since Calico can't network a pod until Typha is up, we need to run Typha itself
# as a host-networked pod.
serviceAccountName: calico-node
containers:
- image: calico/typha:v0.7.4
name: calico-typha
ports:
- containerPort: 5473
name: calico-typha
protocol: TCP
env:
# Enable "info" logging by default. Can be set to "debug" to increase verbosity.
- name: TYPHA_LOGSEVERITYSCREEN
value: "info"
# Disable logging to file and syslog since those don't make sense in Kubernetes.
- name: TYPHA_LOGFILEPATH
value: "none"
- name: TYPHA_LOGSEVERITYSYS
value: "none"
# Monitor the Kubernetes API to find the number of running instances and rebalance
# connections.
- name: TYPHA_CONNECTIONREBALANCINGMODE
value: "kubernetes"
- name: TYPHA_DATASTORETYPE
value: "kubernetes"
- name: TYPHA_HEALTHENABLED
value: "true"
# Uncomment these lines to enable prometheus metrics. Since Typha is host-networked,
# this opens a port on the host, which may need to be secured.
#- name: TYPHA_PROMETHEUSMETRICSENABLED
# value: "true"
#- name: TYPHA_PROMETHEUSMETRICSPORT
# value: "9093"
livenessProbe:
httpGet:
path: /liveness
port: 9098
periodSeconds: 30
initialDelaySeconds: 30
readinessProbe:
httpGet:
path: /readiness
port: 9098
periodSeconds: 10
volumeMounts:
- name: tz-config
mountPath: /etc/localtime
readOnly: true
volumes:
- name: tz-config
hostPath:
path: /etc/localtime
---

# This manifest installs the calico/node container, as well
# as the Calico CNI plugins and network config on
# each master and worker node in a Kubernetes cluster.
kind: DaemonSet
apiVersion: extensions/v1beta1
metadata:
name: calico-node
namespace: kube-system
labels:
k8s-app: calico-node
spec:
selector:
matchLabels:
k8s-app: calico-node
updateStrategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
template:
metadata:
labels:
k8s-app: calico-node
annotations:
# This, along with the CriticalAddonsOnly toleration below,
# marks the pod as a critical add-on, ensuring it gets
# priority scheduling and that its resources are reserved
# if it ever gets evicted.
scheduler.alpha.kubernetes.io/critical-pod: ''
spec:
hostNetwork: true
tolerations:
# Make sure calico/node gets scheduled on all nodes.
- effect: NoSchedule
operator: Exists
# Mark the pod as a critical add-on for rescheduling.
- key: CriticalAddonsOnly
operator: Exists
- effect: NoExecute
operator: Exists
serviceAccountName: calico-node
# Minimize downtime during a rolling upgrade or deletion; tell Kubernetes to do a "force
# deletion": https://kubernetes.io/docs/concepts/workloads/pods/pod/#termination-of-pods.
terminationGracePeriodSeconds: 0
containers:
# Runs calico/node container on each Kubernetes node. This
# container programs network policy and routes on each
# host.
- name: calico-node
image: calico/node:v3.1.3
env:
# Use Kubernetes API as the backing datastore.
- name: DATASTORE_TYPE
value: "kubernetes"
# Enable felix info logging.
- name: FELIX_LOGSEVERITYSCREEN
value: "info"
# Cluster type to identify the deployment type
- name: CLUSTER_TYPE
value: "k8s,bgp"
# Disable file logging so \`kubectl logs\` works.
- name: CALICO_DISABLE_FILE_LOGGING
value: "true"
# Set Felix endpoint to host default action to ACCEPT.
- name: FELIX_DEFAULTENDPOINTTOHOSTACTION
value: "ACCEPT"
# Disable IPV6 on Kubernetes.
- name: FELIX_IPV6SUPPORT
value: "false"
# Set MTU for tunnel device used if ipip is enabled
- name: FELIX_IPINIPMTU
value: "1440"
# Wait for the datastore.
- name: WAIT_FOR_DATASTORE
value: "true"
# The default IPv4 pool to create on startup if none exists. Pod IPs will be
# chosen from this range. Changing this value after installation will have
# no effect. This should fall within \`--cluster-cidr\`.
- name: CALICO_IPV4POOL_CIDR
value: "10.211.0.0/16"
# Enable IPIP
- name: CALICO_IPV4POOL_IPIP
value: "CrossSubnet"
# Enable IP-in-IP within Felix.
- name: FELIX_IPINIPENABLED
value: "true"
# Typha support: controlled by the ConfigMap.
- name: FELIX_TYPHAK8SSERVICENAME
valueFrom:
configMapKeyRef:
name: calico-config
key: typha_service_name
# Set based on the k8s node name.
- name: NODENAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
# Auto-detect the BGP IP address.
- name: IP
value: "autodetect"
- name: FELIX_HEALTHENABLED
value: "true"
securityContext:
privileged: true
resources:
requests:
cpu: 250m
livenessProbe:
httpGet:
path: /liveness
port: 9099
periodSeconds: 10
initialDelaySeconds: 10
failureThreshold: 6
readinessProbe:
httpGet:
path: /readiness
port: 9099
periodSeconds: 10
volumeMounts:
- mountPath: /lib/modules
name: lib-modules
readOnly: true
- mountPath: /var/run/calico
name: var-run-calico
readOnly: false
- mountPath: /var/lib/calico
name: var-lib-calico
readOnly: false
- name: tz-config
mountPath: /etc/localtime
readOnly: true
# This container installs the Calico CNI binaries
# and CNI network config file on each node.
- name: install-cni
image: calico/cni:v3.1.3
command: ["/install-cni.sh"]
env:
# Name of the CNI config file to create.
- name: CNI_CONF_NAME
value: "10-calico.conflist"
# The CNI network config to install on each node.
- name: CNI_NETWORK_CONFIG
valueFrom:
configMapKeyRef:
name: calico-config
key: cni_network_config
# Set the hostname based on the k8s node name.
- name: KUBERNETES_NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
volumeMounts:
- mountPath: /host/opt/cni/bin
name: cni-bin-dir
- mountPath: /host/etc/cni/net.d
name: cni-net-dir
- name: tz-config
mountPath: /etc/localtime
readOnly: true
volumes:
# Used by calico/node.
- name: lib-modules
hostPath:
path: /lib/modules
- name: var-run-calico
hostPath:
path: /var/run/calico
- name: var-lib-calico
hostPath:
path: /var/lib/calico
# Used to install CNI.
- name: cni-bin-dir
hostPath:
path: /opt/cni/bin
- name: cni-net-dir
hostPath:
path: /etc/cni/net.d
- name: tz-config
hostPath:
path: /etc/localtime

# Create all the CustomResourceDefinitions needed for
# Calico policy and networking mode.
---

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: felixconfigurations.crd.projectcalico.org
spec:
scope: Cluster
group: crd.projectcalico.org
version: v1
names:
kind: FelixConfiguration
plural: felixconfigurations
singular: felixconfiguration

---

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: bgppeers.crd.projectcalico.org
spec:
scope: Cluster
group: crd.projectcalico.org
version: v1
names:
kind: BGPPeer
plural: bgppeers
singular: bgppeer

---

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: bgpconfigurations.crd.projectcalico.org
spec:
scope: Cluster
group: crd.projectcalico.org
version: v1
names:
kind: BGPConfiguration
plural: bgpconfigurations
singular: bgpconfiguration

---

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: ippools.crd.projectcalico.org
spec:
scope: Cluster
group: crd.projectcalico.org
version: v1
names:
kind: IPPool
plural: ippools
singular: ippool

---

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: hostendpoints.crd.projectcalico.org
spec:
scope: Cluster
group: crd.projectcalico.org
version: v1
names:
kind: HostEndpoint
plural: hostendpoints
singular: hostendpoint

---

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: clusterinformations.crd.projectcalico.org
spec:
scope: Cluster
group: crd.projectcalico.org
version: v1
names:
kind: ClusterInformation
plural: clusterinformations
singular: clusterinformation

---

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: globalnetworkpolicies.crd.projectcalico.org
spec:
scope: Cluster
group: crd.projectcalico.org
version: v1
names:
kind: GlobalNetworkPolicy
plural: globalnetworkpolicies
singular: globalnetworkpolicy

---

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: globalnetworksets.crd.projectcalico.org
spec:
scope: Cluster
group: crd.projectcalico.org
version: v1
names:
kind: GlobalNetworkSet
plural: globalnetworksets
singular: globalnetworkset

---

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: networkpolicies.crd.projectcalico.org
spec:
scope: Namespaced
group: crd.projectcalico.org
version: v1
names:
kind: NetworkPolicy
plural: networkpolicies
singular: networkpolicy

---

apiVersion: v1
kind: ServiceAccount
metadata:
name: calico-node
namespace: kube-system
EOF

kubectl create -f calico/

7. 配置和安装DNS插件

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
mkdir -p kube-dns/

cat <<EOF > kube-dns/deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
labels:
k8s-app: kube-dns
name: coredns
namespace: kube-system
spec:
replicas: 2
revisionHistoryLimit: 10
selector:
matchLabels:
k8s-app: kube-dns
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 1
type: RollingUpdate
template:
metadata:
labels:
k8s-app: kube-dns
spec:
containers:
- args:
- -conf
- /etc/coredns/Corefile
image: registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.1.3
imagePullPolicy: IfNotPresent
livenessProbe:
failureThreshold: 5
httpGet:
path: /health
port: 8080
scheme: HTTP
initialDelaySeconds: 60
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 5
name: coredns
ports:
- containerPort: 53
name: dns
protocol: UDP
- containerPort: 53
name: dns-tcp
protocol: TCP
- containerPort: 9153
name: metrics
protocol: TCP
resources:
limits:
memory: 170Mi
requests:
cpu: 100m
memory: 70Mi
securityContext:
allowPrivilegeEscalation: false
capabilities:
add:
- NET_BIND_SERVICE
drop:
- all
readOnlyRootFilesystem: true
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /etc/coredns
name: config-volume
readOnly: true
- mountPath: /etc/localtime
name: tz-config
readOnly: true
dnsPolicy: Default
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: coredns
serviceAccountName: coredns
terminationGracePeriodSeconds: 30
tolerations:
- key: CriticalAddonsOnly
operator: Exists
- effect: NoSchedule
key: node-role.kubernetes.io/master
volumes:
- configMap:
defaultMode: 420
items:
- key: Corefile
path: Corefile
name: coredns
name: config-volume
- hostPath:
path: /etc/localtime
type: ""
name: tz-config
EOF

cat <<EOF > kube-dns/service.yaml
apiVersion: v1
kind: Service
metadata:
annotations:
prometheus.io/scrape: "true"
labels:
k8s-app: kube-dns
kubernetes.io/cluster-service: "true"
kubernetes.io/name: KubeDNS
name: kube-dns
namespace: kube-system
spec:
clusterIP: 10.96.0.10
ports:
- name: dns
port: 53
protocol: UDP
targetPort: 53
- name: dns-tcp
port: 53
protocol: TCP
targetPort: 53
selector:
k8s-app: kube-dns
sessionAffinity: None
type: ClusterIP
EOF

cat <<EOF > kube-dns/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: coredns
namespace: kube-system
data:
Corefile: |
.:53 {
errors
health
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
upstream
fallthrough in-addr.arpa ip6.arpa
}
prometheus :9153
proxy . /etc/resolv.conf
cache 30
reload
}
EOF

cat <<EOF > kube-dns/serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: coredns
namespace: kube-system
EOF

cat <<EOF > kube-dns/clusterrole.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: system:coredns
rules:
- apiGroups:
- ""
resources:
- endpoints
- services
- pods
- namespaces
verbs:
- list
- watch
EOF

cat <<EOF > kube-dns/clusterrolebinding.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: system:coredns
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:coredns
subjects:
- kind: ServiceAccount
name: coredns
namespace: kube-system
EOF

kubectl create -f kube-dns/

7. 验证Service的访问

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
mkdir -p nginx/

cat <<EOF > nginx/01-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
EOF

cat <<EOF > nginx/02-service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
selector:
app: nginx
ports:
- nodePort: 31073
protocol: TCP
port: 80
targetPort: 80
type: NodePort
EOF

# 有响应视为正常,否则视为不正常
curl -XGET http://192.168.112.129:31073
curl -XGET http://192.168.112.130:31073

8. 验证Pod的网络和DNS配置

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
# 在node01节点和node02节点上分别操作
mkdir -p network/
cd network/

cat <<EOF > Dockerfile
FROM alpine:3.8

MAINTAINER wangxin_0611@126.com

RUN apk add --no-cache ca-certificates bind-tools iputils iproute2 net-tools tcpdump
EOF

docker build -t alpine:3.8-network .

# 在master节点上操作
mkdir -p network/
cd network/

cat <<EOF > network.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: network
namespace: default
spec:
selector:
matchLabels:
app: network
template:
metadata:
labels:
app: network
spec:
containers:
- name: network
image: alpine:3.8-network
imagePullPolicy: IfNotPresent
command:
- sleep
- "3600"
restartPolicy: Always
EOF

kubectl create -f network/

[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
network-bl7jx 1/1 Running 0 6m 10.211.1.4 node02
network-m2vp6 1/1 Running 0 6m 10.211.0.4 node01

# 在node01上的pod中验证
[root@master ~]# kubectl exec -it network-m2vp6 /bin/sh
/ # cat /etc/resolv.conf
nameserver 10.96.0.10
search default.svc.cluster.local svc.cluster.local cluster.local
options ndots:5
/ # ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: tunl0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1
link/ipip 0.0.0.0 brd 0.0.0.0
4: eth0@if11: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 72:01:49:fa:fb:f3 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 10.211.0.4/32 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::7001:49ff:fefa:fbf3/64 scope link
valid_lft forever preferred_lft forever
/ # ping -c 4 10.211.1.4
PING 10.211.1.4 (10.211.1.4) 56(84) bytes of data.
64 bytes from 10.211.1.4: icmp_seq=1 ttl=62 time=0.314 ms
64 bytes from 10.211.1.4: icmp_seq=2 ttl=62 time=0.490 ms
64 bytes from 10.211.1.4: icmp_seq=3 ttl=62 time=0.415 ms
64 bytes from 10.211.1.4: icmp_seq=4 ttl=62 time=0.491 ms

--- 10.211.1.4 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3000ms
rtt min/avg/max/mdev = 0.314/0.427/0.491/0.075 ms
/ # nslookup kubernetes.default
Server: 10.96.0.10
Address: 10.96.0.10#53

Name: kubernetes.default.svc.cluster.local
Address: 10.96.0.1

/ # nslookup kubernetes
Server: 10.96.0.10
Address: 10.96.0.10#53

Name: kubernetes.default.svc.cluster.local
Address: 10.96.0.1

/ # exit

# 在node02上的pod中验证
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
network-bl7jx 1/1 Running 0 9m 10.211.1.4 node02
network-m2vp6 1/1 Running 0 9m 10.211.0.4 node01
[root@master ~]# kubectl exec -it network-bl7jx /bin/sh
/ # cat /etc/resolv.conf
nameserver 10.96.0.10
search default.svc.cluster.local svc.cluster.local cluster.local
options ndots:5
/ # ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: tunl0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1
link/ipip 0.0.0.0 brd 0.0.0.0
4: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 5a:a6:51:22:9d:2b brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 10.211.1.4/32 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::58a6:51ff:fe22:9d2b/64 scope link
valid_lft forever preferred_lft forever
/ # ping -c 4 10.211.0.4
PING 10.211.0.4 (10.211.0.4) 56(84) bytes of data.
64 bytes from 10.211.0.4: icmp_seq=1 ttl=62 time=0.450 ms
64 bytes from 10.211.0.4: icmp_seq=2 ttl=62 time=0.685 ms
64 bytes from 10.211.0.4: icmp_seq=3 ttl=62 time=0.726 ms
64 bytes from 10.211.0.4: icmp_seq=4 ttl=62 time=0.707 ms

--- 10.211.0.4 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 0.450/0.642/0.726/0.111 ms
/ # nslookup kubernetes.default
Server: 10.96.0.10
Address: 10.96.0.10#53

Name: kubernetes.default.svc.cluster.local
Address: 10.96.0.1

/ # nslookup kubernetes
Server: 10.96.0.10
Address: 10.96.0.10#53

Name: kubernetes.default.svc.cluster.local
Address: 10.96.0.1

/ # exit

Kubernetes集群对接Ceph集群:搭建可对接Ceph实验环境的Kubernetes实验环境

一、实验环境说明

1. 环境主旨说明

本文旨在帮助读者搭建一个可对接Ceph实验环境的Kubernetes实验环境。关于基本Kubernetes环境的搭建,这里不做讲解,读者请参考网络上的资料,或者本博客的另外一篇文章《使用kubeadm的方式安装Kubernetes集群(一)》,链接地址详见“参考资料”。

2. 环境要点说明

升级所有节点的内核为主线版本,包括master节点和所有node节点。
所有的节点都安装ceph-common组件和python-cephfs组件,包括master节点和所有node节点。

二、实验环境版本信息

1. 操作系统的版本信息

CentOS Linux release 7.7.1908 (Core)

2. 核心组件的版本信息

Ceph Luminous 版本 的 ceph-common 和 python-cephfs
Kubernetes v1.16.0

三、实验步骤

1. 升级所有节点(所有的master和node节点)的内核为主线版本(当前主线版本为 5.3.6-1.el7.elrepo.x86_64)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
yum install -y yum-plugin-fastestmirror
cat /etc/redhat-release
cat /etc/os-release
uname -snr

rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -Uvh https://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
yum repolist

yum --enablerepo=elrepo-kernel install -y kernel-ml
yum repolist all

awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg
grub2-set-default 0
grub2-mkconfig -o /boot/grub2/grub.cfg
reboot

uname -snr

2. 所有节点(所有的master和node节点)安装 ceph luminous 版本 的 ceph-common 和 python-cephfs

1
2
3
4
5
6
7
8
9
10
11
12
13
cat <<EOF >> /etc/yum.repos.d/ceph.repo
[ceph-noarch]
name=Ceph noarch packages
baseurl=http://mirrors.163.com/ceph/rpm-luminous/el7/noarch
enabled=1
gpgcheck=1
priority=1
type=rpm-md
gpgkey=http://mirrors.163.com/ceph/keys/release.asc
EOF

yum makecache fast
yum install -y ceph-common python-cephfs

3. 为 Kubernetes 集群安装 ceph rbd 和 ceph fs 的 对应的 provisioner 服务

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
mkdir -p external-storage/ceph/common/
mkdir -p external-storage/ceph/rbd/
mkdir -p external-storage/ceph/fs/

cat <<EOF >> external-storage/ceph/common/01-namespaces.yaml
apiVersion: v1
kind: Namespace
metadata:
name: storage
EOF

# For ceph rbd
cat <<EOF >> external-storage/ceph/rbd/01-serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: cephrbd-provisioner
namespace: storage
EOF

cat <<EOF >> external-storage/ceph/rbd/02-clusterrole.yaml
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: cephrbd-provisioner
rules:
- apiGroups: [""]
resources: ["endpoints"]
verbs: ["get", "list", "watch", "create", "update", "patch"]
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get", "watch", "list", "create", "update", "delete"]
- apiGroups: [""]
resources: ["persistentvolumes"]
verbs: ["get", "list", "watch", "create", "delete"]
- apiGroups: [""]
resources: ["persistentvolumeclaims"]
verbs: ["get", "list", "watch", "update"]
- apiGroups: ["storage.k8s.io"]
resources: ["storageclasses"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["events"]
verbs: ["create", "update", "patch"]
- apiGroups: [""]
resources: ["services"]
resourceNames: ["kube-dns","coredns"]
verbs: ["list", "get"]
EOF

cat <<EOF >> external-storage/ceph/rbd/03-clusterrolebinding.yaml
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: cephrbd-provisioner
subjects:
- kind: ServiceAccount
name: cephrbd-provisioner
namespace: storage
roleRef:
kind: ClusterRole
name: cephrbd-provisioner
apiGroup: rbac.authorization.k8s.io
EOF

cat <<EOF >> external-storage/ceph/rbd/04-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: cephrbd-provisioner
namespace: storage
spec:
replicas: 1
selector:
matchLabels:
app: cephrbd-provisioner
strategy:
type: Recreate
template:
metadata:
labels:
app: cephrbd-provisioner
spec:
containers:
- name: cephrbd-provisioner
image: wangx/rbd-provisioner:luminous
imagePullPolicy: IfNotPresent
env:
- name: PROVISIONER_NAME
value: ceph.com/rbd
serviceAccount: cephrbd-provisioner
EOF


# For ceph fs
cat <<EOF >> external-storage/ceph/fs/01-serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: cephfs-provisioner
namespace: storage
EOF

cat <<EOF >> external-storage/ceph/fs/02-clusterrole.yaml
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: cephfs-provisioner
rules:
- apiGroups: [""]
resources: ["endpoints"]
verbs: ["get", "list", "watch", "create", "update", "patch"]
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get", "watch", "list", "create", "update", "delete"]
- apiGroups: [""]
resources: ["persistentvolumes"]
verbs: ["get", "list", "watch", "create", "delete"]
- apiGroups: [""]
resources: ["persistentvolumeclaims"]
verbs: ["get", "list", "watch", "update"]
- apiGroups: ["storage.k8s.io"]
resources: ["storageclasses"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["events"]
verbs: ["create", "update", "patch"]
EOF

cat <<EOF >> external-storage/ceph/fs/03-clusterrolebinding.yaml
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: cephfs-provisioner
subjects:
- kind: ServiceAccount
name: cephfs-provisioner
namespace: storage
roleRef:
kind: ClusterRole
name: cephfs-provisioner
apiGroup: rbac.authorization.k8s.io
EOF

cat <<EOF >> external-storage/ceph/fs/04-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: cephfs-provisioner
namespace: storage
spec:
replicas: 1
selector:
matchLabels:
app: cephfs-provisioner
strategy:
type: Recreate
template:
metadata:
labels:
app: cephfs-provisioner
spec:
containers:
- name: cephfs-provisioner
image: wangx/cephfs-provisioner:luminous
imagePullPolicy: IfNotPresent
env:
- name: PROVISIONER_NAME
value: ceph.com/cephfs
command:
- "/usr/local/bin/cephfs-provisioner"
args:
- "-id=cephfs-provisioner-1"
serviceAccount: cephfs-provisioner
EOF

# crete ceph provisioner's namespace
kubectl create -f external-storage/ceph/common/

# create ceph rbd provisioner
kubectl create -f external-storage/ceph/rbd/

# create ceph fs provisioner
kubectl create -f external-storage/ceph/fs/

4. 为 Kubernetes 集群的 ceph rbd 和 ceph fs 的 provisioner 服务创建 StroageClass 对象

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
# For ceph rbd 
## 注意:这里需要更换两个Secret的key的值为你的环境的。
mkdir -p external-storage/ceph/rbd/storageclass/
cat <<EOF >> external-storage/ceph/rbd/storageclass/01-secrets.yaml
apiVersion: v1
kind: Secret
metadata:
name: cephrbd-admin-secret
namespace: storage
type: "kubernetes.io/cephrbd"
data:
# ceph auth get-key client.admin | base64
key: QVFDTmZxRmRDRmtnT3hBQURwY29VdjltbGJqRmIxMTJ2dzlLdEE9PQ==
---
apiVersion: v1
kind: Secret
metadata:
name: cephrbd-user-secret
namespace: storage
type: "kubernetes.io/cephrbd"
data:
# ceph auth add client.kube mon 'allow r' osd 'allow rwx pool=kube'
# ceph auth get-key client.kube | base64
key: QVFBZ3Q2RmRibnBOTXhBQXkwQkJrdmQxQW5adHlWN0syZWIvSEE9PQ==
EOF

cat <<EOF >> external-storage/ceph/rbd/storageclass/02-storageclass.yaml
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: cephrbd
provisioner: ceph.com/rbd
parameters:
monitors: ceph-mon.storage.svc.cluster.local:6789 # 这里需要使用Kubernetes内部的DNS配置ceph monitor的地址
pool: kube
adminId: admin
adminSecretNamespace: storage
adminSecretName: cephrbd-admin-secret
userId: kube
userSecretNamespace: storage
userSecretName: cephrbd-user-secret
imageFormat: "2"
imageFeatures: layering
---
kind: Service
apiVersion: v1
metadata:
name: ceph-mon
namespace: storage
spec:
type: ExternalName
externalName: 192.168.112.131.xip.io # ceph monitor的地址
EOF

kubectl create -f external-storage/ceph/rbd/storageclass/

# For ceph fs
## 注意:这里需要更换Secret的key的值为你的环境的。
mkdir -p external-storage/ceph/fs/storageclass/
cat <<EOF >> external-storage/ceph/fs/storageclass/01-secrets.yaml
apiVersion: v1
kind: Secret
metadata:
name: cephfs-admin-secret
namespace: storage
type: "kubernetes.io/cephfs"
data:
# ceph auth get-key client.admin | base64
key: QVFDTmZxRmRDRmtnT3hBQURwY29VdjltbGJqRmIxMTJ2dzlLdEE9PQ==
EOF

cat <<EOF >> external-storage/ceph/fs/storageclass/02-storageclass.yaml
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: cephfs
provisioner: ceph.com/cephfs
parameters:
monitors: 192.168.112.131:6789
adminId: admin
adminSecretName: cephfs-admin-secret
adminSecretNamespace: storage
claimRoot: /volumes/kubernetes
EOF

kubectl create -f external-storage/ceph/fs/storageclass/

5. 验证 Kubernetes 集群的 ceph rbd 和 ceph fs 的 provisioner 服务配合StroageClass 对象实现的动态存储供应功能

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# 创建pvc和pod用于验证ceph rbd
mkdir -p external-storage/ceph/rbd/example/
cat <<EOF >> external-storage/ceph/rbd/example/01-claim.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: claim1
spec:
accessModes:
- ReadWriteOnce
storageClassName: cephrbd
resources:
requests:
storage: 1Gi
EOF

cat <<EOF >> external-storage/ceph/rbd/example/02-pod.yaml
kind: Pod
apiVersion: v1
metadata:
name: test-pod-1
spec:
containers:
- name: test-pod-1
image: nginx:1.7.9
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
volumeMounts:
- name: pvc
mountPath: "/data"
volumes:
- name: pvc
persistentVolumeClaim:
claimName: claim1
EOF

kubectl create -f external-storage/ceph/rbd/example/

# 创建pvc和pod用于验证ceph fs
mkdir -p external-storage/ceph/fs/example/
cat <<EOF >> external-storage/ceph/fs/example/01-claim.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: claim2
spec:
storageClassName: cephfs
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi
EOF

cat <<EOF >> external-storage/ceph/fs/example/02-pod.yaml
kind: Pod
apiVersion: v1
metadata:
name: test-pod-2
spec:
containers:
- name: test-pod-2
image: nginx:1.7.9
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
volumeMounts:
- name: pvc
mountPath: "/data"
volumes:
- name: pvc
persistentVolumeClaim:
claimName: claim2
EOF

kubectl create -f external-storage/ceph/fs/example/

# 验证步骤如下所示:
[root@master ~]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
claim1 Bound pvc-8bcd7a8a-9c72-4c24-a5ce-dda3f72b459c 1Gi RWO cephrbd 5m22s
claim2 Bound pvc-6aeaf23b-c1c0-4654-8fa9-50656b5b7247 1Gi RWX cephfs 3m43s

[root@master ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-6aeaf23b-c1c0-4654-8fa9-50656b5b7247 1Gi RWX Delete Bound default/claim2 cephfs 3m55s
pvc-8bcd7a8a-9c72-4c24-a5ce-dda3f72b459c 1Gi RWO Delete Bound default/claim1 cephrbd 5m36s

[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
test-pod-1 1/1 Running 0 6m2s 10.211.196.133 node01 <none> <none>
test-pod-2 1/1 Running 0 4m23s 10.211.140.69 node02 <none> <none>

## 进入pod下的container中
[root@master ~]# kubectl exec -it test-pod-1 /bin/bash
root@test-pod-1:/# df -h
Filesystem Size Used Avail Use% Mounted on
overlay 17G 3.6G 14G 21% /
tmpfs 64M 0 64M 0% /dev
tmpfs 982M 0 982M 0% /sys/fs/cgroup
/dev/rbd0 976M 2.6M 958M 1% /data
/dev/mapper/cl-root 17G 3.6G 14G 21% /dev/termination-log
/dev/mapper/cl-root 17G 3.6G 14G 21% /etc/resolv.conf
/dev/mapper/cl-root 17G 3.6G 14G 21% /etc/hostname
/dev/mapper/cl-root 17G 3.6G 14G 21% /etc/hosts
shm 64M 0 64M 0% /dev/shm
/dev/mapper/cl-root 17G 3.6G 14G 21% /var/cache/nginx
tmpfs 982M 12K 982M 1% /run/secrets/kubernetes.io/serviceaccount
tmpfs 982M 0 982M 0% /proc/acpi
tmpfs 64M 0 64M 0% /proc/kcore
tmpfs 64M 0 64M 0% /proc/keys
tmpfs 64M 0 64M 0% /proc/timer_list
tmpfs 64M 0 64M 0% /proc/sched_debug
tmpfs 982M 0 982M 0% /proc/scsi
tmpfs 982M 0 982M 0% /sys/firmware
root@test-pod-1:/# cd /data/
root@test-pod-1:/data# ls -la
total 20
drwxr-xr-x 3 root root 4096 Oct 14 05:43 .
drwxr-xr-x 1 root root 41 Oct 14 05:39 ..
drwx------ 2 root root 16384 Oct 14 05:39 lost+found
root@test-pod-1:/data# echo 'hello ceph rbd.' > readme.md
root@test-pod-1:/data# ls -la
total 24
drwxr-xr-x 3 root root 4096 Oct 14 05:43 .
drwxr-xr-x 1 root root 41 Oct 14 05:39 ..
drwx------ 2 root root 16384 Oct 14 05:39 lost+found
-rw-r--r-- 1 root root 16 Oct 14 05:43 readme.md
root@test-pod-1:/data# cat readme.md
hello ceph rbd.
root@test-pod-1:/data# exit
exit

[root@master ~]# kubectl exec -it test-pod-2 /bin/bash
root@test-pod-2:/# df -h
Filesystem Size Used Avail Use% Mounted on
overlay 17G 3.6G 14G 21% /
tmpfs 64M 0 64M 0% /dev
tmpfs 982M 0 982M 0% /sys/fs/cgroup
192.168.112.131:6789:/volumes/kubernetes/kubernetes/kubernetes-dynamic-pvc-41646ced-ee45-11e9-bfd9-eec9a057c13d 18G 0 18G 0% /data
/dev/mapper/cl-root 17G 3.6G 14G 21% /dev/termination-log
/dev/mapper/cl-root 17G 3.6G 14G 21% /etc/resolv.conf
/dev/mapper/cl-root 17G 3.6G 14G 21% /etc/hostname
/dev/mapper/cl-root 17G 3.6G 14G 21% /etc/hosts
shm 64M 0 64M 0% /dev/shm
/dev/mapper/cl-root 17G 3.6G 14G 21% /var/cache/nginx
tmpfs 982M 12K 982M 1% /run/secrets/kubernetes.io/serviceaccount
tmpfs 982M 0 982M 0% /proc/acpi
tmpfs 64M 0 64M 0% /proc/kcore
tmpfs 64M 0 64M 0% /proc/keys
tmpfs 64M 0 64M 0% /proc/timer_list
tmpfs 64M 0 64M 0% /proc/sched_debug
tmpfs 982M 0 982M 0% /proc/scsi
tmpfs 982M 0 982M 0% /sys/firmware
root@test-pod-2:/# cd /data/
root@test-pod-2:/data# ls -la
total 0
drwxr-xr-x 2 root root 0 Oct 14 05:41 .
drwxr-xr-x 1 root root 29 Oct 14 05:41 ..
root@test-pod-2:/data# echo 'hello ceph fs.' > readme.md
root@test-pod-2:/data# ls -la
total 1
drwxr-xr-x 2 root root 1 Oct 14 05:44 .
drwxr-xr-x 1 root root 29 Oct 14 05:41 ..
-rw-r--r-- 1 root root 15 Oct 14 05:44 readme.md
root@test-pod-2:/data# cat readme.md
hello ceph fs.
root@test-pod-2:/data# exit
exit

# 在test-pod-1的宿主机上
[root@node01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 969M 0 969M 0% /dev
tmpfs 982M 0 982M 0% /dev/shm
tmpfs 982M 23M 959M 3% /run
tmpfs 982M 0 982M 0% /sys/fs/cgroup
/dev/mapper/cl-root 17G 3.6G 14G 21% /
/dev/sda1 1014M 242M 773M 24% /boot
tmpfs 982M 12K 982M 1% /var/lib/kubelet/pods/444da5da-6b0b-4eb9-961c-e67620e2790d/volumes/kubernetes.io~secret/calico-node-token-j29td
tmpfs 982M 12K 982M 1% /var/lib/kubelet/pods/5c6b4dc9-9789-431f-abda-69b791e00852/volumes/kubernetes.io~secret/kube-proxy-token-2xvqz
tmpfs 982M 12K 982M 1% /var/lib/kubelet/pods/117bf0a0-1de4-4233-8f86-58e3b7e222d9/volumes/kubernetes.io~secret/default-token-lvg9j
overlay 17G 3.6G 14G 21% /var/lib/docker/overlay2/c0137c8945f708c9a9af29c435006164f1880e62e12d0944558d76fe826cf79e/merged
overlay 17G 3.6G 14G 21% /var/lib/docker/overlay2/a173aac7b6bd051d9309d31dc98bffc8d8b4601738ad8670981c65e9f206f4d7/merged
shm 64M 0 64M 0% /var/lib/docker/containers/af5a902877155cc3f0d80506e4af572364e1b02b7527ebc272e36cc3537fdaf0/mounts/shm
shm 64M 0 64M 0% /var/lib/docker/containers/779e0a0a9e8e46ba53b976b9e0e0acb133778159c001d314d9fb4a689ce4dd51/mounts/shm
overlay 17G 3.6G 14G 21% /var/lib/docker/overlay2/e0f0e09989f3e152c17f2ccf7e15df86a0844f59277804d409126e7508e892f2/merged
overlay 17G 3.6G 14G 21% /var/lib/docker/overlay2/6cf53e19aa7a1cabfcc992b492f0c2d7b3a5b7a41a999e3e17c6e85acd432385/merged
tmpfs 197M 0 197M 0% /run/user/0
overlay 17G 3.6G 14G 21% /var/lib/docker/overlay2/ea9758ae1391c0b51be400d2c716398a6e6a75042c2d266b0fd5fffce49f1856/merged
shm 64M 0 64M 0% /var/lib/docker/containers/78374c2866c9e59956b46e6cf7745743e5366273eae4a28f584167895eff28c3/mounts/shm
overlay 17G 3.6G 14G 21% /var/lib/docker/overlay2/f75360891104dd17f6035b84fc53e2cee9aa49cf4cb99abbc07ea4e53f3e68fa/merged
tmpfs 982M 12K 982M 1% /var/lib/kubelet/pods/bc337b72-b78d-4f22-9074-0ea1e3c0854b/volumes/kubernetes.io~secret/cephfs-provisioner-token-gnvmf
overlay 17G 3.6G 14G 21% /var/lib/docker/overlay2/5f2bb0260a595ebf2685f606c8fa57ae312b36d9ed0d2ac1c0226f108dcb4f8a/merged
shm 64M 0 64M 0% /var/lib/docker/containers/2be9a9eab8bf6181da1b00bd6745053236b5b72b4a625244f58664d6cef4056d/mounts/shm
overlay 17G 3.6G 14G 21% /var/lib/docker/overlay2/19c0add3e797d302865131a736187e08b5773ff504cf24baa5ca6dc9e1e2f0bd/merged
tmpfs 982M 12K 982M 1% /var/lib/kubelet/pods/94567637-87d1-44bf-b40c-4cf8d7249455/volumes/kubernetes.io~secret/default-token-lvg9j
/dev/rbd0 976M 2.6M 958M 1% /var/lib/kubelet/plugins/kubernetes.io/rbd/mounts/kube-image-kubernetes-dynamic-pvc-06ed8402-ee45-11e9-81e1-626518d2252b
overlay 17G 3.6G 14G 21% /var/lib/docker/overlay2/0f9b3b248dd6fd5db871c9a8340c3b221099ca48e5e44496b0e8ff87d385053e/merged
shm 64M 0 64M 0% /var/lib/docker/containers/7fa70409cd56a31427c89a61db4d102261610678a06a550eecfd1c44092c0d02/mounts/shm
overlay 17G 3.6G 14G 21% /var/lib/docker/overlay2/723eda3646d4eabdb62faa89566f0c6d84a14f7f882acee46af96630d96480e4/merged

# 在test-pod-2的宿主机上
[root@node02 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 969M 0 969M 0% /dev
tmpfs 982M 0 982M 0% /dev/shm
tmpfs 982M 22M 960M 3% /run
tmpfs 982M 0 982M 0% /sys/fs/cgroup
/dev/mapper/cl-root 17G 3.6G 14G 21% /
/dev/sda1 1014M 242M 773M 24% /boot
tmpfs 982M 12K 982M 1% /var/lib/kubelet/pods/cde8bcd0-da54-40f8-90e1-a8c53daaca8a/volumes/kubernetes.io~secret/default-token-lvg9j
tmpfs 982M 12K 982M 1% /var/lib/kubelet/pods/27785efc-053d-41c1-b081-d61056715dce/volumes/kubernetes.io~secret/kube-proxy-token-2xvqz
tmpfs 982M 12K 982M 1% /var/lib/kubelet/pods/6af3ba89-fd1d-4ca6-9f1f-d1a9d24aab68/volumes/kubernetes.io~secret/calico-node-token-j29td
overlay 17G 3.6G 14G 21% /var/lib/docker/overlay2/fe5b85b1611ec8ce24761d70dec417d8102f71c13dc3ba0005387999d7f90538/merged
overlay 17G 3.6G 14G 21% /var/lib/docker/overlay2/a2e0efa3dd1bc8d4725acd7ea3123cf2331cabfbd3f58185de44f81dc61f17b2/merged
shm 64M 0 64M 0% /var/lib/docker/containers/a9a00476e189f794aaa8d448f3fd492ed95d7f285ebe398f2561c3369f98f6fc/mounts/shm
shm 64M 0 64M 0% /var/lib/docker/containers/97c5b22f15b951a1d8c88deae0a34163d729cedc2fd21aef2108e657d3dbd2ed/mounts/shm
overlay 17G 3.6G 14G 21% /var/lib/docker/overlay2/d69d00d1bb9d51e3b2a622edfe213b16069886f6a3507b7d22f8cf35aadb67fb/merged
overlay 17G 3.6G 14G 21% /var/lib/docker/overlay2/bc97fe5450c049b42c16ed70907ebf759fd16d8aa0f1f1b597828c400a95fec1/merged
tmpfs 197M 0 197M 0% /run/user/0
overlay 17G 3.6G 14G 21% /var/lib/docker/overlay2/be31ca99791b3a61b34750063296b3f61e0e33f6ca13bd89d09305e29282d93f/merged
shm 64M 0 64M 0% /var/lib/docker/containers/75bf7cd29b86a191287b60b505ca9147aee2590026227b75c108e527c8a42050/mounts/shm
overlay 17G 3.6G 14G 21% /var/lib/docker/overlay2/d424bbdc32abf0a7860ea8cc57138a578d3ac379a2a5a62d467ebdd503f85f12/merged
tmpfs 982M 12K 982M 1% /var/lib/kubelet/pods/63e075b8-1b82-4ead-924b-e8233217c597/volumes/kubernetes.io~secret/cephrbd-provisioner-token-xx8qj
overlay 17G 3.6G 14G 21% /var/lib/docker/overlay2/032279366ff9f083d5f54e8653789f77d30962fb74d9c796912462336ba089a8/merged
shm 64M 0 64M 0% /var/lib/docker/containers/8e3f8bcbbb05781e69759fe678fdba68fa6ec0514972b3a399936c90d754ea1d/mounts/shm
overlay 17G 3.6G 14G 21% /var/lib/docker/overlay2/ca226a39a87d3319f974bdbca242ff6621cafe356a24e9080d65b013f26b30ef/merged
tmpfs 982M 12K 982M 1% /var/lib/kubelet/pods/094d7470-5712-4523-8eb8-9994dbfb2cfe/volumes/kubernetes.io~secret/default-token-lvg9j
192.168.112.131:6789:/volumes/kubernetes/kubernetes/kubernetes-dynamic-pvc-41646ced-ee45-11e9-bfd9-eec9a057c13d 18G 0 18G 0% /var/lib/kubelet/pods/094d7470-5712-4523-8eb8-9994dbfb2cfe/volumes/kubernetes.io~cephfs/pvc-6aeaf23b-c1c0-4654-8fa9-50656b5b7247
overlay 17G 3.6G 14G 21% /var/lib/docker/overlay2/7615fa1f189797ca2b111fa6122ea3a1d8c2f2c009d052a3e06c6a9e9d0b9ba3/merged
shm 64M 0 64M 0% /var/lib/docker/containers/0a76bdb1043188470fa7a2aa3df3ce9640b658064cb41c872025b90ed46f6bcd/mounts/shm
overlay 17G 3.6G 14G 21% /var/lib/docker/overlay2/49ef6810dedcd675d541f06df84336744a7cbb84a876daf96ffab7bbcfa304f7/merged

四、参考资料

https://github.com/kubernetes-incubator/external-storage/tree/v5.2.0/ceph
https://www.howtoforge.com/tutorial/how-to-upgrade-kernel-in-centos-7-server/
https://singhwang.github.io/2019/10/03/kubeadm_kubernetes_cluster_000/

Kubernetes集群对接Ceph集群:搭建可供Kubernetes对接的Ceph实验环境

一、实验环境说明

1. 环境主旨说明

本文旨在使用ceph-deploy工具搭建一个可供Kubernetes集群对接使用的单点Ceph集群。更多有关多点Ceph集群搭建和使用的资料详见本文“参考资料”部分的链接。

2. 环境形态说明

多点部署环境形态:
ceph_deploy_architecture
其中admin-node、node1、node2和node3各代表一台Linux服务器,共有admin-node和node两种角色。

本文使用ceph-deploy工具搭建的单点Ceph集群,共使用了一台Linux服务器,同时扮演了admin-node和node两种角色。

二、实验环境版本信息

1. 操作系统的版本信息

CentOS Linux release 7.7.1908 (Core)

2. 核心组件的版本信息

Ceph Luminous 版本

三、搭建步骤

1. 准备基础的Linux服务器环境

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
# 更新系统
yum update -y

# 设置正确的时区和时间
yum install -y ntpdate
timedatectl set-timezone Asia/Shanghai
ntpdate cn.ntp.org.cn

# 关闭防火墙
systemctl disable firewalld.service
systemctl stop firewalld.service

# 关闭swap分区
swapoff -a
sed -i 's#/dev/mapper/cl-swap#\# /dev/mapper/cl-swap#' /etc/fstab

# 关闭selinux
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

# 设置节点的主机名
## 192.168.112.131
hostnamectl set-hostname node1

# 配置主机名和IP的映射
cat <<EOF >> /etc/hosts

# For Ceph Cluster
192.168.112.131 node1
EOF

2. 安装ceph-deploy的基础环境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
yum install -y epel-release
yum makecache fast
yum install -y python-pip

cat <<EOM > /etc/yum.repos.d/ceph.repo
[ceph-noarch]
name=Ceph noarch packages
baseurl=http://mirrors.163.com/ceph/rpm-luminous/el7/noarch
enabled=1
gpgcheck=1
type=rpm-md
gpgkey=http://mirrors.163.com/ceph/keys/release.asc
EOM

yum makecache fast
yum install -y ceph-deploy

ceph-deploy --version

3. 配置从admin-node到node1的ssh免用户和免密登录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 创建ceph-deploy工具需要使用的node用户
useradd -d /home/node -m node
passwd node

echo "node ALL = (root) NOPASSWD:ALL" | tee /etc/sudoers.d/node
chmod 0440 /etc/sudoers.d/node

# 配置ssh免用户和免密登录
su node
cd ~

ssh-keygen

ssh-copy-id node@node1

cat <<EOM > ~/.ssh/config
Host node1
Hostname node1
User node
EOM

chmod 0600 ~/.ssh/config

3. 配置ceph-deploy工具使用国内源安装指定的ceph版本

1
2
3
4
5
6
7
8
cat <<EOF >> ~/.bash_profile

export CEPH_DEPLOY_REPO_URL=http://mirrors.163.com/ceph/rpm-luminous/el7
export CEPH_DEPLOY_GPG_URL=http://mirrors.163.com/ceph/keys/release.asc
EOF

source ~/.bash_profile
sudo yum install -y yum-plugin-priorities

4. 使用ceph-deploy工具安装ceph集群

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
mkdir my-cluster
cd my-cluster

ceph-deploy new node1
ceph-deploy install node1
ceph-deploy mon create-initial

ceph-deploy admin node1
ceph-deploy mgr create node1

ceph-deploy osd create --data /dev/sdb node1

sudo ceph health
sudo ceph -s

ceph-deploy mds create node1

# 安装rgw后,将集群状态调整为健康的
ceph-deploy rgw create node1

sudo ceph osd pool ls
sudo ceph osd pool set .rgw.root size 1
sudo ceph osd pool ls
sudo ceph osd pool set default.rgw.control size 1
sudo ceph osd pool ls
sudo ceph osd pool set default.rgw.meta size 1
sudo ceph osd pool ls
sudo ceph osd pool set default.rgw.log size 1
sudo ceph osd pool ls

sudo ceph -s

echo "hello rgw." > testfile.txt
sudo ceph osd pool create mytest 8 8
sudo ceph osd pool set mytest size 1
sudo rados put test-object-1 testfile.txt --pool=mytest
sudo rados -p mytest ls
sudo ceph osd map mytest test-object-1
sudo rados rm test-object-1 --pool=mytest
sudo ceph osd pool rm mytest

5. 配置ceph集群环境使其符合kubernetes环境的要求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# For ceph rbd
ceph osd pool create kube 8 8
ceph osd pool set kube size 1

## For kubernetes ceph-admin-secret
ceph auth get-key client.admin | base64

## For kubernetes ceph-user-secret
ceph auth add client.kube mon 'allow r' osd 'allow rwx pool=kube'
ceph auth get-key client.kube | base64

# For ceph fs
ceph osd pool create cephfs_data 8 8
ceph osd pool set cephfs_data size 1
ceph osd pool create cephfs_metadata 8 8
ceph osd pool set cephfs_metadata size 1
ceph fs new fs cephfs_metadata cephfs_data
ceph osd pool application enable kube fs

四、参考资料

1. 官方文档资料(可以带领用户快速掌握从搭建到基本使用)

https://docs.ceph.com/docs/luminous/start/
https://docs.ceph.com/docs/luminous/start/quick-ceph-deploy/
https://docs.ceph.com/docs/master/start/quick-ceph-deploy/
https://docs.ceph.com/docs/luminous/start/quick-rbd/
https://docs.ceph.com/docs/luminous/start/quick-cephfs/
https://docs.ceph.com/docs/luminous/start/quick-rgw/

2. 网络上经典的错误排查资料

https://my.oschina.net/xiaozhublog/blog/664560
https://blog.51cto.com/wangzhijian/2159701

Docker针对iptables的filter表的FORWARD链的默认设置

一、环境版本信息

Docker Community 18.09.9

  • Client Version: 18.09.9
  • Server Docker Engine Version: 18.09.9

二、问题现象

1. 在一台Linux服务器上,首先安装并启动了docker,接着设置了如下内核参数:

1
2
3
4
cat <<EOF > /etc/sysctl.d/docker.conf
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF

这时发现了个有趣的现象,docker容器之间网络不通了。甚至当你使用了linux原生的两个network namespace通过veth pair设备连接在同一网桥上,然后为两个linux原生的两个network namespace中的设备配置同一网段的ip地址后,这两个linux原生的两个network namespace的网络也是不通的。

2. 在kubernetes环境中,发现当把service改为nodePort形式对外暴露时,无法通过nodeIP+nodePort的方式访问Pod内的服务。很有意思,这个问题有人装kubernetes就会遇到,有人就不会遇到。

三、排查过程

1. 如果开着 net.bridge.bridge-nf-call-iptables 这个参数,用tcmdump抓包,只能看到arp有请求和回复,icmp就只有请求没有回复,关掉就一切正常了;

注意:net.bridge.bridge-nf-call-iptables = 1 表示开启,net.bridge.bridge-nf-call-iptables = 0 表示关闭。

2. 查资料确定 net.bridge.bridge-nf-call-iptables 这个内核参数的含义,发现其含义用比较直白的话概括是这样的:网桥做流量转发时是否先转到iptables进行过滤;

关闭 net.bridge.bridge-nf-call-iptables 这个内核参数就意味着网桥把流量流量直接转发,不经过iptables,反之则先要经过iptables。

3. 既然是在iptables中出了问题,那么就查看iptables中filter表中的FORWARD链的Policy设置,发现其为DROP;

4. 这个不是我们要的设置,只能想办法手动覆盖了;

于是想到了改docker的systemd脚本docker.service,加入ExecStartPost=/usr/sbin/iptables -P FORWARD ACCEPT。

5. 目前只是在外围临时解决了,但是并未探知到问题的本质,到底官方为什么要这么改?

一头雾水,于是继续通过github上的CHANGELOG查找线索,发现了如下线索:

1
2
3
4
5
6
## 1.13.0 (2017-01-18)
。。。
### Networking
。。。
* Change the default `FORWARD` policy to `DROP` [#28257](https://github.com/docker/docker/pull/28257)
。。。

通过阅读该CHANGELOG相关的PR和issues可以看到这个修改的来龙去脉。

6. 现在只知道默认会把 FORWARD 设置为 DROP,开始查找如何才能避免docker daemon做这个配置;

7. 阅读了与实验环境版本一致的docker源码,终于找到了问题的原因:net.ipv4.ip_forward 只要在docker daemon 启动之前手动把这个参数设置为1,docker daemon 默认就认为有人用iptables的filter表的forward链了,就不会更改forward链的默认策略了。

源码详见 “五、根本原因” 部分的截图。

四、解决方法

1. 外围的解决方法

我们自己可以在外围这么修改systemd的service脚本

1
2
3
4
5
6
vi /lib/systemd/system/docker.service
。。。
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
ExecStartPost=/usr/sbin/iptables -P FORWARD ACCEPT
。。。

其实主要是加最后那句 ExecStartPost=/usr/sbin/iptables -P FORWARD ACCEPT

2. 推荐的解决方法

先设置如下两个关键的内核参数并让其生效:

1
2
3
4
5
6
cat <<EOF > /etc/sysctl.d/docker.conf
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF

sysctl --system

之后再安装docker,然后启动即可。这里的关键是,安装或者首次启动docker前,linux操作系统需要设置内核参数 net.ipv4.ip_forward = 1 才可以。

五、根本原因(可以从对应版本的docker源码中找到线索)

docker_ipv4_forward
通过源码可以看出来,当内核参数 net.ipv4.ip_forward 的值不为1时,docker daemon会默认把iptables的filter表的FORWARD链默认设置为DROP。

六、参考资料

这个是为什么要这么改?因为从纯docker角度看,是个漏洞,详见链接。不过这个漏洞对我们来说影响意义不大。

https://github.com/moby/moby/issues/14041

这个是问题的PR描述:

https://github.com/moby/moby/pull/28257

Systemd的相关配置:

https://www.centosdoc.com/system/201.html

搭建综合性网络质量检测工具-Smartping的体验环境

一、环境的相关信息

1. 版本信息

Smartping v0.8.0

2. 服务器信息

192.168.112.130
192.168.112.131
192.168.112.132

二、实验过程记录

1. 准备各个节点的配置文件

192.168.112.130 的配置文件内容如下:

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
{
"Ver": "0.8.0",
"Port": 8899,
"Name": "server03",
"Addr": "192.168.112.130",
"Mode": {
"Endpoint": "",
"LastSuccTime": "",
"Status": "true",
"Type": "local"
},
"Base": {
"Archive": 10,
"Refresh": 1,
"Timeout": 5
},
"Topology": {
"Tline": "1",
"Tsound": "/alert.mp3.4",
"Tsymbolsize": "70"
},
"Alert": {
"EmailHost": "",
"RevcEmailList": "",
"SendEmailAccount": "",
"SendEmailPassword": ""
},
"Network": {
"192.168.112.130": {
"Name": "server03",
"Addr": "192.168.112.130",
"Smartping": true,
"Ping": [
"192.168.112.131",
"192.168.112.132"
],
"Topology": [
{
"Addr": "192.168.112.131",
"Name": "server04",
"Thdavgdelay": "200",
"Thdchecksec": "900",
"Thdloss": "10",
"Thdoccnum": "3"
},
{
"Addr": "192.168.112.132",
"Name": "server05",
"Thdavgdelay": "200",
"Thdchecksec": "900",
"Thdloss": "30",
"Thdoccnum": "3"
}
]
},
"192.168.112.131": {
"Name": "server04",
"Addr": "192.168.112.131",
"Smartping": true,
"Ping": [
"192.168.112.130",
"192.168.112.132"
],
"Topology": [
{
"Addr": "192.168.112.130",
"Name": "server03",
"Thdavgdelay": "200",
"Thdchecksec": "900",
"Thdloss": "10",
"Thdoccnum": "3"
},
{
"Addr": "192.168.112.132",
"Name": "server05",
"Thdavgdelay": "200",
"Thdchecksec": "900",
"Thdloss": "30",
"Thdoccnum": "3"
}
]
},
"192.168.112.132": {
"Name": "server05",
"Addr": "192.168.112.132",
"Smartping": true,
"Ping": [
"192.168.112.130",
"192.168.112.131"
],
"Topology": [
{
"Addr": "192.168.112.130",
"Name": "server03",
"Thdavgdelay": "200",
"Thdchecksec": "900",
"Thdloss": "10",
"Thdoccnum": "3"
},
{
"Addr": "192.168.112.131",
"Name": "server04",
"Thdavgdelay": "200",
"Thdchecksec": "900",
"Thdloss": "30",
"Thdoccnum": "3"
}
]
}
},
"Chinamap": {
"上海": {
"cmcc": [
"117.184.42.114"
],
"ctcc": [
"180.163.15.160"
],
"cucc": [
"223.167.104.117"
]
},
"北京": {
"cmcc": [
"111.13.217.125"
],
"ctcc": [
"120.92.180.135"
],
"cucc": [
"111.207.189.5"
]
},
"浙江": {
"cmcc": [
"183.246.69.139"
],
"ctcc": [
"115.236.169.86"
],
"cucc": [
"60.12.214.156"
]
}
},
"Toollimit": 0,
"Authiplist": "",
"Password": "smartping"
}

192.168.112.131 的配置文件内容如下:

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
{
"Ver": "0.8.0",
"Port": 8899,
"Name": "server04",
"Addr": "192.168.112.131",
"Mode": {
"Endpoint": "",
"LastSuccTime": "",
"Status": "true",
"Type": "local"
},
"Base": {
"Archive": 10,
"Refresh": 1,
"Timeout": 5
},
"Topology": {
"Tline": "1",
"Tsound": "/alert.mp3.4",
"Tsymbolsize": "70"
},
"Alert": {
"EmailHost": "",
"RevcEmailList": "",
"SendEmailAccount": "",
"SendEmailPassword": ""
},
"Network": {
"192.168.112.130": {
"Name": "server03",
"Addr": "192.168.112.130",
"Smartping": true,
"Ping": [
"192.168.112.131",
"192.168.112.132"
],
"Topology": [
{
"Addr": "192.168.112.131",
"Name": "server04",
"Thdavgdelay": "200",
"Thdchecksec": "900",
"Thdloss": "10",
"Thdoccnum": "3"
},
{
"Addr": "192.168.112.132",
"Name": "server05",
"Thdavgdelay": "200",
"Thdchecksec": "900",
"Thdloss": "30",
"Thdoccnum": "3"
}
]
},
"192.168.112.131": {
"Name": "server04",
"Addr": "192.168.112.131",
"Smartping": true,
"Ping": [
"192.168.112.130",
"192.168.112.132"
],
"Topology": [
{
"Addr": "192.168.112.130",
"Name": "server03",
"Thdavgdelay": "200",
"Thdchecksec": "900",
"Thdloss": "10",
"Thdoccnum": "3"
},
{
"Addr": "192.168.112.132",
"Name": "server05",
"Thdavgdelay": "200",
"Thdchecksec": "900",
"Thdloss": "30",
"Thdoccnum": "3"
}
]
},
"192.168.112.132": {
"Name": "server05",
"Addr": "192.168.112.132",
"Smartping": true,
"Ping": [
"192.168.112.130",
"192.168.112.131"
],
"Topology": [
{
"Addr": "192.168.112.130",
"Name": "server03",
"Thdavgdelay": "200",
"Thdchecksec": "900",
"Thdloss": "10",
"Thdoccnum": "3"
},
{
"Addr": "192.168.112.131",
"Name": "server04",
"Thdavgdelay": "200",
"Thdchecksec": "900",
"Thdloss": "30",
"Thdoccnum": "3"
}
]
}
},
"Chinamap": {
"上海": {
"cmcc": [
"117.184.42.114"
],
"ctcc": [
"180.163.15.160"
],
"cucc": [
"223.167.104.117"
]
},
"北京": {
"cmcc": [
"111.13.217.125"
],
"ctcc": [
"120.92.180.135"
],
"cucc": [
"111.207.189.5"
]
},
"浙江": {
"cmcc": [
"183.246.69.139"
],
"ctcc": [
"115.236.169.86"
],
"cucc": [
"60.12.214.156"
]
}
},
"Toollimit": 0,
"Authiplist": "",
"Password": "smartping"
}

192.168.112.132 的配置文件内容如下:

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
{
"Ver": "0.8.0",
"Port": 8899,
"Name": "server05",
"Addr": "192.168.112.132",
"Mode": {
"Endpoint": "",
"LastSuccTime": "",
"Status": "true",
"Type": "local"
},
"Base": {
"Archive": 10,
"Refresh": 1,
"Timeout": 5
},
"Topology": {
"Tline": "1",
"Tsound": "/alert.mp3.4",
"Tsymbolsize": "70"
},
"Alert": {
"EmailHost": "",
"RevcEmailList": "",
"SendEmailAccount": "",
"SendEmailPassword": ""
},
"Network": {
"192.168.112.130": {
"Name": "server03",
"Addr": "192.168.112.130",
"Smartping": true,
"Ping": [
"192.168.112.131",
"192.168.112.132"
],
"Topology": [
{
"Addr": "192.168.112.131",
"Name": "server04",
"Thdavgdelay": "200",
"Thdchecksec": "900",
"Thdloss": "10",
"Thdoccnum": "3"
},
{
"Addr": "192.168.112.132",
"Name": "server05",
"Thdavgdelay": "200",
"Thdchecksec": "900",
"Thdloss": "30",
"Thdoccnum": "3"
}
]
},
"192.168.112.131": {
"Name": "server04",
"Addr": "192.168.112.131",
"Smartping": true,
"Ping": [
"192.168.112.130",
"192.168.112.132"
],
"Topology": [
{
"Addr": "192.168.112.130",
"Name": "server03",
"Thdavgdelay": "200",
"Thdchecksec": "900",
"Thdloss": "10",
"Thdoccnum": "3"
},
{
"Addr": "192.168.112.132",
"Name": "server05",
"Thdavgdelay": "200",
"Thdchecksec": "900",
"Thdloss": "30",
"Thdoccnum": "3"
}
]
},
"192.168.112.132": {
"Name": "server05",
"Addr": "192.168.112.132",
"Smartping": true,
"Ping": [
"192.168.112.130",
"192.168.112.131"
],
"Topology": [
{
"Addr": "192.168.112.130",
"Name": "server03",
"Thdavgdelay": "200",
"Thdchecksec": "900",
"Thdloss": "10",
"Thdoccnum": "3"
},
{
"Addr": "192.168.112.131",
"Name": "server04",
"Thdavgdelay": "200",
"Thdchecksec": "900",
"Thdloss": "30",
"Thdoccnum": "3"
}
]
}
},
"Chinamap": {
"上海": {
"cmcc": [
"117.184.42.114"
],
"ctcc": [
"180.163.15.160"
],
"cucc": [
"223.167.104.117"
]
},
"北京": {
"cmcc": [
"111.13.217.125"
],
"ctcc": [
"120.92.180.135"
],
"cucc": [
"111.207.189.5"
]
},
"浙江": {
"cmcc": [
"183.246.69.139"
],
"ctcc": [
"115.236.169.86"
],
"cucc": [
"60.12.214.156"
]
}
},
"Toollimit": 0,
"Authiplist": "",
"Password": "smartping"
}

2. 克隆Smartping的源码编译成二进制文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
git clone https://github.com/smartping/smartping.git
cd smartping/
export GOPROXY=https://goproxy.io
go mod init github.com/smartping/smartping/src
GOOS=linux GOARCH=amd64 go build -v -a -o smartping github.com/smartping/smartping/src
go mod vendor

ls -la
total 14564
drwxr-xr-x. 7 root root 135 Sep 17 12:22 .
drwxr-xr-x. 9 root root 189 Sep 17 12:02 ..
drwxr-xr-x. 2 root root 89 Sep 17 11:59 funcs
drwxr-xr-x. 2 root root 40 Sep 17 11:59 g
-rw-r--r--. 1 root root 462 Sep 17 12:19 go.mod
-rw-r--r--. 1 root root 1870 Sep 17 12:19 go.sum
drwxr-xr-x. 2 root root 51 Sep 17 12:02 http
drwxr-xr-x. 2 root root 35 Sep 17 11:59 nettools
-rwxr-xr-x. 1 root root 14897840 Sep 17 12:21 smartping
-rw-r--r--. 1 root root 765 Sep 17 11:59 smartping.go
drwxr-xr-x. 4 root root 61 Sep 17 12:22 vendor

3. 运行Smartping的二进制文件,这里以tmux为例,实际使用请选用Systemd或者Supervisord:

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
mkdir -p /var/lib/smartping/bin
mkdir -p /var/lib/smartping/conf

cp martping /var/lib/smartping/bin/
cat <<EOF > /var/lib/smartping/conf/config-base.json
...
分别替换为上面三个配置文件的内容
...
EOF

cat <<EOF > /var/lib/smartping/conf/seelog.xml
<seelog type="asynctimer" asyncinterval="5000000" minlevel="info" maxlevel="error">
<outputs formatid="main">
<console/>

<filter levels="info">
<rollingfile type="size" filename="./logs/info.log" maxsize="10240000" maxrolls="5"/>
</filter>

<filter levels="debug">
<rollingfile type="size" filename="./logs/debug.log" maxsize="10240000" maxrolls="5"/>
</filter>

<filter levels="error">
<rollingfile type="size" filename="./logs/error.log" maxsize="10240000" maxrolls="5"/>
</filter>

</outputs>
<formats>
<format id="main" format="%Date/%Time [%Level] %File %Msg%n"/>
</formats>
</seelog>
EOF

tmux new -s smartping
/var/lib/smartping/bin/smartping

三、参考资料

https://github.com/smartping/smartping.git

容器故障排除之重要的Linux命令

如何查看磁盘是否坏了?

1
查看能否向磁盘上写入文件 -> 查看磁盘是否满了-> 查看是否是小文件把inode是否耗光了

如何查看进程占用了哪些端口?

1
netstat -ntlup 或者 lsof -i:1988

如何查看进程打开了哪些文件?

1
lsof -p pid

如何查看inode使用情况?

1
df -i

如何查询文件的inode是什么?

1
stat filename

ulimit 代表进程能打开的文件数