文章目录
- 环境信息
- 安装docker
- 编辑kubeadm初始化yaml配置文件
- 编辑flannel 网络插件yml配置文件
- 执行master节点初始化
- 执行worker节点初始化
- worker节点先pull需要的镜像
- worker节点初始化
- worker节点加入集群
- 最终状态
环境信息
- 用vmware workstation装2台centos作为节点服务器
hostname |
ip |
os |
role |
node1 |
192.168.6.128 |
CentOS Linux release 7.6.1810 (Core) |
master |
node2 |
192.168.6.129 |
CentOS Linux release 7.6.1810 (Core) |
worker |
- kubernetes: 1.15.0
- docker: 18.09.8
- 用docker最新的19版本总出问题,而且19版本不在kubernetes1.15支持列表中,所以换回18版本
安装docker
# bash docker-install.sh 18.09.8-3.el7
#! /usr/bin/env bash
# leo 20190812
# docker YUM一键安装
# 安装最新版 ./docker-install.sh
# 安装指定旧版本 ./docker-install.sh 18.09.8-3.el7
echo "安装关联工具..."
yum install -y yum-utils device-mapper-persistent-data lvm2
echo "添加阿里yum源..."
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
echo "开启旧版本仓库"
yum-config-manager --enable docker-ce-edge
echo "更新缓存..."
yum makecache fast
if [ $# -eq 1 ]
then
echo "安装docker-ce-$1"
yum -y install docker-ce-$1
else
echo "安装最新版docker-ce"
yum -y install docker-ce
fi
echo "开机启动..."
systemctl enable docker
echo "镜像加速..."
mkdir -p /etc/docker
cat >> /etc/docker/daemon.json << 'EOF'
{
"registry-mirrors": ["https://ydfo52fm.mirror.aliyuncs.com"]
}
EOF
echo "启动docker..."
systemctl start docker
echo "查看版本信息"
docker version
docker info
编辑kubeadm初始化yaml配置文件
- node1执行
- master ip:192.168.6.128
- image仓库地址:registry.cn-hangzhou.aliyuncs.com/google_containers
- 官方仓库在墙外,下载实在痛苦,换成阿里云提供的国内下载地址
- pod子网:10.244.0.0/16
- service子网:10.96.0.0/12
- kubeadm.yaml
apiVersion: kubeadm.k8s.io/v1beta2
bootstrapTokens:
- groups:
- system:bootstrappers:kubeadm:default-node-token
token: abcdef.0123456789abcdef
ttl: 24h0m0s
usages:
- signing
- authentication
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: 192.168.6.128
bindPort: 6443
nodeRegistration:
criSocket: /var/run/dockershim.sock
name: node1
taints:
- effect: PreferNoSchedule
key: node-role.kubernetes.io/master
---
apiServer:
timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta2
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns:
type: CoreDNS
etcd:
local:
dataDir: /var/lib/etcd
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
kind: ClusterConfiguration
kubernetesVersion: v1.15.0
networking:
dnsDomain: cluster.local
podSubnet: 10.244.0.0/16
serviceSubnet: 10.96.0.0/12
scheduler: {}
编辑flannel 网络插件yml配置文件
- node1执行
- 多网卡时指定 --iface=ens33
args:
- --ip-masq
- --kube-subnet-mgr
- --iface=ens33
---
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
name: psp.flannel.unprivileged
annotations:
seccomp.security.alpha.kubernetes.io/allowedProfileNames: docker/default
seccomp.security.alpha.kubernetes.io/defaultProfileName: docker/default
apparmor.security.beta.kubernetes.io/allowedProfileNames: runtime/default
apparmor.security.beta.kubernetes.io/defaultProfileName: runtime/default
spec:
privileged: false
volumes:
- configMap
- secret
- emptyDir
- hostPath
allowedHostPaths:
- pathPrefix: "/etc/cni/net.d"
- pathPrefix: "/etc/kube-flannel"
- pathPrefix: "/run/flannel"
readOnlyRootFilesystem: false
# Users and groups
runAsUser:
rule: RunAsAny
supplementalGroups:
rule: RunAsAny
fsGroup:
rule: RunAsAny
# Privilege Escalation
allowPrivilegeEscalation: false
defaultAllowPrivilegeEscalation: false
# Capabilities
allowedCapabilities: ['NET_ADMIN']
defaultAddCapabilities: []
requiredDropCapabilities: []
# Host namespaces
hostPID: false
hostIPC: false
hostNetwork: true
hostPorts:
- min: 0
max: 65535
# SELinux
seLinux:
# SELinux is unsed in CaaSP
rule: 'RunAsAny'
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: flannel
rules:
- apiGroups: ['extensions']
resources: ['podsecuritypolicies']
verbs: ['use']
resourceNames: ['psp.flannel.unprivileged']
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- apiGroups:
- ""
resources:
- nodes
verbs:
- list
- watch
- apiGroups:
- ""
resources:
- nodes/status
verbs:
- patch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: flannel
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: flannel
subjects:
- kind: ServiceAccount
name: flannel
namespace: kube-system
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: flannel
namespace: kube-system
---
kind: ConfigMap
apiVersion: v1
metadata:
name: kube-flannel-cfg
namespace: kube-system
labels:
tier: node
app: flannel
data:
cni-conf.json: |
{
"name": "cbr0",
"plugins": [
{
"type": "flannel",
"delegate": {
"hairpinMode": true,
"isDefaultGateway": true
}
},
{
"type": "portmap",
"capabilities": {
"portMappings": true
}
}
]
}
net-conf.json: |
{
"Network": "10.244.0.0/16",
"Backend": {
"Type": "vxlan"
}
}
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: kube-flannel-ds-amd64
namespace: kube-system
labels:
tier: node
app: flannel
spec:
selector:
matchLabels:
app: flannel
template:
metadata:
labels:
tier: node
app: flannel
spec:
hostNetwork: true
nodeSelector:
beta.kubernetes.io/arch: amd64
tolerations:
- operator: Exists
effect: NoSchedule
serviceAccountName: flannel
initContainers:
- name: install-cni
image: quay.io/coreos/flannel:v0.11.0-amd64
command:
- cp
args:
- -f
- /etc/kube-flannel/cni-conf.json
- /etc/cni/net.d/10-flannel.conflist
volumeMounts:
- name: cni
mountPath: /etc/cni/net.d
- name: flannel-cfg
mountPath: /etc/kube-flannel/
containers:
- name: kube-flannel
image: quay.io/coreos/flannel:v0.11.0-amd64
command:
- /opt/bin/flanneld
args:
- --ip-masq
- --kube-subnet-mgr
- --iface=ens33
resources:
requests:
cpu: "100m"
memory: "50Mi"
limits:
cpu: "100m"
memory: "50Mi"
securityContext:
privileged: false
capabilities:
add: ["NET_ADMIN"]
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
volumeMounts:
- name: run
mountPath: /run/flannel
- name: flannel-cfg
mountPath: /etc/kube-flannel/
volumes:
- name: run
hostPath:
path: /run/flannel
- name: cni
hostPath:
path: /etc/cni/net.d
- name: flannel-cfg
configMap:
name: kube-flannel-cfg
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: kube-flannel-ds-arm64
namespace: kube-system
labels:
tier: node
app: flannel
spec:
selector:
matchLabels:
app: flannel
template:
metadata:
labels:
tier: node
app: flannel
spec:
hostNetwork: true
nodeSelector:
beta.kubernetes.io/arch: arm64
tolerations:
- operator: Exists
effect: NoSchedule
serviceAccountName: flannel
initContainers:
- name: install-cni
image: quay.io/coreos/flannel:v0.11.0-arm64
command:
- cp
args:
- -f
- /etc/kube-flannel/cni-conf.json
- /etc/cni/net.d/10-flannel.conflist
volumeMounts:
- name: cni
mountPath: /etc/cni/net.d
- name: flannel-cfg
mountPath: /etc/kube-flannel/
containers:
- name: kube-flannel
image: quay.io/coreos/flannel:v0.11.0-arm64
command:
- /opt/bin/flanneld
args:
- --ip-masq
- --kube-subnet-mgr
- --iface=ens33
resources:
requests:
cpu: "100m"
memory: "50Mi"
limits:
cpu: "100m"
memory: "50Mi"
securityContext:
privileged: false
capabilities:
add: ["NET_ADMIN"]
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
volumeMounts:
- name: run
mountPath: /run/flannel
- name: flannel-cfg
mountPath: /etc/kube-flannel/
volumes:
- name: run
hostPath:
path: /run/flannel
- name: cni
hostPath:
path: /etc/cni/net.d
- name: flannel-cfg
configMap:
name: kube-flannel-cfg
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: kube-flannel-ds-arm
namespace: kube-system
labels:
tier: node
app: flannel
spec:
selector:
matchLabels:
app: flannel
template:
metadata:
labels:
tier: node
app: flannel
spec:
hostNetwork: true
nodeSelector:
beta.kubernetes.io/arch: arm
tolerations:
- operator: Exists
effect: NoSchedule
serviceAccountName: flannel
initContainers:
- name: install-cni
image: quay.io/coreos/flannel:v0.11.0-arm
command:
- cp
args:
- -f
- /etc/kube-flannel/cni-conf.json
- /etc/cni/net.d/10-flannel.conflist
volumeMounts:
- name: cni
mountPath: /etc/cni/net.d
- name: flannel-cfg
mountPath: /etc/kube-flannel/
containers:
- name: kube-flannel
image: quay.io/coreos/flannel:v0.11.0-arm
command:
- /opt/bin/flanneld
args:
- --ip-masq
- --kube-subnet-mgr
- --iface=ens33
resources:
requests:
cpu: "100m"
memory: "50Mi"
limits:
cpu: "100m"
memory: "50Mi"
securityContext:
privileged: false
capabilities:
add: ["NET_ADMIN"]
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
volumeMounts:
- name: run
mountPath: /run/flannel
- name: flannel-cfg
mountPath: /etc/kube-flannel/
volumes:
- name: run
hostPath:
path: /run/flannel
- name: cni
hostPath:
path: /etc/cni/net.d
- name: flannel-cfg
configMap:
name: kube-flannel-cfg
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: kube-flannel-ds-ppc64le
namespace: kube-system
labels:
tier: node
app: flannel
spec:
selector:
matchLabels:
app: flannel
template:
metadata:
labels:
tier: node
app: flannel
spec:
hostNetwork: true
nodeSelector:
beta.kubernetes.io/arch: ppc64le
tolerations:
- operator: Exists
effect: NoSchedule
serviceAccountName: flannel
initContainers:
- name: install-cni
image: quay.io/coreos/flannel:v0.11.0-ppc64le
command:
- cp
args:
- -f
- /etc/kube-flannel/cni-conf.json
- /etc/cni/net.d/10-flannel.conflist
volumeMounts:
- name: cni
mountPath: /etc/cni/net.d
- name: flannel-cfg
mountPath: /etc/kube-flannel/
containers:
- name: kube-flannel
image: quay.io/coreos/flannel:v0.11.0-ppc64le
command:
- /opt/bin/flanneld
args:
- --ip-masq
- --kube-subnet-mgr
- --iface=ens33
resources:
requests:
cpu: "100m"
memory: "50Mi"
limits:
cpu: "100m"
memory: "50Mi"
securityContext:
privileged: false
capabilities:
add: ["NET_ADMIN"]
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
volumeMounts:
- name: run
mountPath: /run/flannel
- name: flannel-cfg
mountPath: /etc/kube-flannel/
volumes:
- name: run
hostPath:
path: /run/flannel
- name: cni
hostPath:
path: /etc/cni/net.d
- name: flannel-cfg
configMap:
name: kube-flannel-cfg
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: kube-flannel-ds-s390x
namespace: kube-system
labels:
tier: node
app: flannel
spec:
selector:
matchLabels:
app: flannel
template:
metadata:
labels:
tier: node
app: flannel
spec:
hostNetwork: true
nodeSelector:
beta.kubernetes.io/arch: s390x
tolerations:
- operator: Exists
effect: NoSchedule
serviceAccountName: flannel
initContainers:
- name: install-cni
image: quay.io/coreos/flannel:v0.11.0-s390x
command:
- cp
args:
- -f
- /etc/kube-flannel/cni-conf.json
- /etc/cni/net.d/10-flannel.conflist
volumeMounts:
- name: cni
mountPath: /etc/cni/net.d
- name: flannel-cfg
mountPath: /etc/kube-flannel/
containers:
- name: kube-flannel
image: quay.io/coreos/flannel:v0.11.0-s390x
command:
- /opt/bin/flanneld
args:
- --ip-masq
- --kube-subnet-mgr
- --iface=ens33
resources:
requests:
cpu: "100m"
memory: "50Mi"
limits:
cpu: "100m"
memory: "50Mi"
securityContext:
privileged: false
capabilities:
add: ["NET_ADMIN"]
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
volumeMounts:
- name: run
mountPath: /run/flannel
- name: flannel-cfg
mountPath: /etc/kube-flannel/
volumes:
- name: run
hostPath:
path: /run/flannel
- name: cni
hostPath:
path: /etc/cni/net.d
- name: flannel-cfg
configMap:
name: kube-flannel-cfg
执行master节点初始化
- node1执行
- 一键执行脚本 master-node-init.sh
- 节点名称:node1
- 节点IP:192.168.6.128
- 记得关firewalld和selinux
#!/bin/bash
# leo 20190812
# master节点一键初始化
echo "设置hostname与hosts"
hostnamectl set-hostname node1
cat >> /etc/hosts << EOF
192.168.6.128 node1
192.168.6.129 node2
EOF
echo "开启ipvs"
yum -y install ipvsadm ipset bind-utils
cat > /etc/sysconfig/modules/ipvs.modules < /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
vm.swappiness=0
EOF
sysctl -p /etc/sysctl.d/k8s.conf
echo "配置iptables forward默认策略"
iptables -P FORWARD ACCEPT
echo "修改docker cgroup"
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://ydfo52fm.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
systemctl restart docker
echo "编辑kubernetes仓库"
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
echo "安装kubelet kubeadm kubectl"
yum -y install kubelet kubeadm kubectl
echo "初始化"
systemctl enable kubelet
kubeadm init --config kubeadm.yaml
#kubeadm init --kubernetes-version=1.15.0 --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.6.128 --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers
mkdir -p ~/.kube
cp -i /etc/kubernetes/admin.conf ~/.kube/config
chown $(id -u):$(id -g) ~/.kube/config
echo "安装flannel 网络插件"
kubectl apply -f kube-flannel.yml
echo "查看状态"
kubectl get nodes
kubectl get pods --all-namespaces -o wide
执行worker节点初始化
worker节点先pull需要的镜像
#!/bin/bash
images=(
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.15.0
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.15.0
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.15.0
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.15.0
quay.io/coreos/flannel:v0.11.0-amd64
registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.3.1
registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.3.10
registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1
)
for imageName in ${images[@]} ; do
docker pull $imageName
done
worker节点初始化
- node2执行
- 一键执行脚本 worker-node-init.sh
- 节点名称:node2
- 节点IP:192.168.6.129
- 记得关firewalld和selinux
#!/bin/bash
# leo 20190812
# master节点一键初始化
echo "设置hostname与hosts"
hostnamectl set-hostname node2
cat >> /etc/hosts << EOF
192.168.6.128 node1
192.168.6.129 node2
EOF
echo "开启ipvs"
yum -y install ipvsadm ipset bind-utils
cat > /etc/sysconfig/modules/ipvs.modules < /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
vm.swappiness=0
EOF
sysctl -p /etc/sysctl.d/k8s.conf
echo "配置iptables forward默认策略"
iptables -P FORWARD ACCEPT
echo "修改docker cgroup"
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://ydfo52fm.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
systemctl restart docker
echo "编辑kubernetes仓库"
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
echo "安装kubelet kubeadm kubectl"
yum -y install kubelet kubeadm kubectl
echo "初始化"
systemctl enable kubelet
worker节点加入集群
kubeadm join 192.168.6.128:6443 --token abcdef.0123456789abcdef \
--discovery-token-ca-cert-hash sha256:9daddb894949b89ad7abe8e8ec35f3f015bf770b8fe30998244d631b1ef25776
> --discovery-token-ca-cert-hash sha256:9daddb894949b89ad7abe8e8ec35f3f015bf770b8fe30998244d631b1ef25776
[preflight] Running pre-flight checks
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.15" ConfigMap in the kube-system namespace
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Activating the kubelet service
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.
Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
最终状态
[root@node1 ~]# kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
node1 Ready master 13m v1.15.2 192.168.6.128 CentOS Linux 7 (Core) 3.10.0-957.27.2.el7.x86_64 docker://18.9.8
node2 Ready 64s v1.15.2 192.168.6.129 CentOS Linux 7 (Core) 3.10.0-957.27.2.el7.x86_64 docker://18.9.8
[root@node1 ~]# kubectl get pods --all-namespaces -o wide
NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kube-system coredns-6967fb4995-ctl64 1/1 Running 0 13m 10.244.0.2 node1
kube-system coredns-6967fb4995-sccgw 1/1 Running 0 13m 10.244.0.3 node1
kube-system etcd-node1 1/1 Running 0 12m 192.168.6.128 node1
kube-system kube-apiserver-node1 1/1 Running 0 12m 192.168.6.128 node1
kube-system kube-controller-manager-node1 1/1 Running 0 12m 192.168.6.128 node1
kube-system kube-flannel-ds-amd64-9z4gg 1/1 Running 0 13m 192.168.6.128 node1
kube-system kube-flannel-ds-amd64-wqpkh 1/1 Running 0 77s 192.168.6.129 node2
kube-system kube-proxy-79lnw 1/1 Running 0 13m 192.168.6.128 node1
kube-system kube-proxy-thn6h 1/1 Running 0 77s 192.168.6.129 node2
kube-system kube-scheduler-node1 1/1 Running 0 12m 192.168.6.128 node1
[root@node1 ~]#