实验环境说明
实验架构图
lab1: etcd master haproxy keepalived 11.11.11.111
lab2: etcd master haproxy keepalived 11.11.11.112
lab3: etcd master haproxy keepalived 11.11.11.113
lab4: node 11.11.11.114
lab5: node 11.11.11.115
lab6: node 11.11.11.116
vip(loadblancer ip): 11.11.11.110
实验使用的Vagrantfile
ENV[“LC_ALL”] = “en_US.UTF-8”
Vagrant.configure(“2”) do |config|
(1…6).each do |i|
config.vm.define “lab#{i}” do |node|
node.vm.box = “centos-7.4-docker-17”
node.ssh.insert_key = false
node.vm.hostname = “lab#{i}”
node.vm.network “private_network”, ip: “11.11.11.11#{i}”
node.vm.provision “shell”,
inline: “echo hello from node #{i}”
node.vm.provider “virtualbox” do |v|
v.cpus = 2
v.customize [“modifyvm”, :id, “–name”, “lab#{i}”, “–memory”, “2048”]
end
end
end
end
安装配置docker
v1.11.0版本推荐使用docker v17.03,
v1.11,v1.12,v1.13, 也可以使用,再高版本的docker可能无法正常使用。
测试发现17.09无法正常使用,不能使用资源限制(内存CPU)
如下操作在所有节点操作
安装docker
apt-get install -y docker-ce=18.06.1ce3-0~ubuntu
启动docker
systemctl enable docker && systemctl restart docker
安装 kubeadm, kubelet 和 kubectl
如下操作在所有节点操作
使用阿里镜像安装
cat < /etc/yum.repos.d/kubernetes.repo
[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
apt-get install -y kubelet=1.11.0-00 kubectl=1.11.0-00 kubeadm=1.11.0-00
配置系统相关参数
sed -i ‘s/SELINUX=permissive/SELINUX=disabled/’ /etc/sysconfig/selinux
setenforce 0
swapoff -a
iptables -P FORWARD ACCEPT
cat < /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
vm.swappiness=0
EOF
sysctl --system
modprobe ip_vs
modprobe ip_vs_rr
modprobe ip_vs_wrr
modprobe ip_vs_sh
modprobe nf_conntrack_ipv4
lsmod | grep ip_vs
配置hosts解析
如下操作在所有节点操作
cat >>/etc/hosts<
11.11.11.112 lab2
11.11.11.113 lab3
11.11.11.114 lab4
11.11.11.115 lab5
11.11.11.116 lab6
EOF
配置haproxy代理和keepalived
如下操作在节点lab1,lab2,lab3操作
docker pull haproxy:1.7.8-alpine
mkdir /etc/haproxy
cat >/etc/haproxy/haproxy.cfg<
log 127.0.0.1 local0 err
maxconn 50000
uid 99
gid 99
#daemon
nbproc 1
pidfile haproxy.pid
defaults
mode http
log 127.0.0.1 local0 err
maxconn 50000
retries 3
timeout connect 5s
timeout client 30s
timeout server 30s
timeout check 2s
listen admin_stats
mode http
bind 0.0.0.0:1080
log 127.0.0.1 local0 err
stats refresh 30s
stats uri /haproxy-status
stats realm Haproxy\ Statistics
stats auth will:will
stats hide-version
stats admin if TRUE
frontend k8s-https
bind 0.0.0.0:8443
mode tcp
#maxconn 50000
default_backend k8s-https
backend k8s-https
mode tcp
balance roundrobin
server lab1 11.11.11.111:6443 weight 1 maxconn 1000 check inter 2000 rise 2 fall 3
server lab2 11.11.11.112:6443 weight 1 maxconn 1000 check inter 2000 rise 2 fall 3
server lab3 11.11.11.113:6443 weight 1 maxconn 1000 check inter 2000 rise 2 fall 3
EOF
docker run -d --name my-haproxy
-v /etc/haproxy:/usr/local/etc/haproxy:ro
-p 8443:8443
-p 1080:1080
–restart always
haproxy:1.7.8-alpine
docker logs my-haproxy
http://11.11.11.111:1080/haproxy-status
http://11.11.11.112:1080/haproxy-status
docker pull osixia/keepalived:1.4.4
lsmod | grep ip_vs
modprobe ip_vs
docker run --net=host --cap-add=NET_ADMIN
-e KEEPALIVED_INTERFACE=eth1
-e KEEPALIVED_VIRTUAL_IPS="#PYTHON2BASH:[‘11.11.11.110’]"
-e KEEPALIVED_UNICAST_PEERS="#PYTHON2BASH:[‘11.11.11.111’,‘11.11.11.112’,‘11.11.11.113’]"
-e KEEPALIVED_PASSWORD=hello
–name k8s-keepalived
–restart always
-d osixia/keepalived:1.4.4
docker logs k8s-keepalived
ping -c4 11.11.11.110
docker rm -f k8s-keepalived
ip a del 11.11.11.110/32 dev eth1
配置启动kubelet
如下操作在所有节点操作
DOCKER_CGROUPS=$(docker info | grep ‘Cgroup’ | cut -d’ ’ -f3)
echo D O C K E R C G R O U P S c a t > / e t c / s y s c o n f i g / k u b e l e t < < E O F K U B E L E T E X T R A A R G S = " − − c g r o u p − d r i v e r = DOCKER_CGROUPS cat >/etc/sysconfig/kubelet<<EOF KUBELET_EXTRA_ARGS="--cgroup-driver= DOCKERCGROUPScat>/etc/sysconfig/kubelet<<EOFKUBELETEXTRAARGS="−−cgroup−driver=DOCKER_CGROUPS --pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64:3.1"
EOF
systemctl daemon-reload
systemctl enable kubelet && systemctl restart kubelet
配置master
配置第一个master节点
如下操作在lab1节点操作
CP0_IP=“11.11.11.111”
CP0_HOSTNAME=“lab1”
cat >kubeadm-master.config<
kind: MasterConfiguration
kubernetesVersion: v1.11.0
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
apiServerCertSANs:
api:
advertiseAddress: $CP0_IP
controlPlaneEndpoint: 11.11.11.110:8443
etcd:
local:
extraArgs:
listen-client-urls: “https://127.0.0.1:2379,https:// C P 0 I P : 2379 " a d v e r t i s e − c l i e n t − u r l s : " h t t p s : / / CP0_IP:2379" advertise-client-urls: "https:// CP0IP:2379"advertise−client−urls:"https://CP0_IP:2379”
listen-peer-urls: “https:// C P 0 I P : 2380 " i n i t i a l − a d v e r t i s e − p e e r − u r l s : " h t t p s : / / CP0_IP:2380" initial-advertise-peer-urls: "https:// CP0IP:2380"initial−advertise−peer−urls:"https://CP0_IP:2380”
initial-cluster: “ C P 0 H O S T N A M E = h t t p s : / / CP0_HOSTNAME=https:// CP0HOSTNAME=https://CP0_IP:2380”
serverCertSANs:
- $CP0_HOSTNAME
- $CP0_IP
peerCertSANs:
- $CP0_HOSTNAME
- $CP0_IP
controllerManagerExtraArgs:
node-monitor-grace-period: 10s
pod-eviction-timeout: 10s
networking:
podSubnet: 10.244.0.0/16
kubeProxy:
config:
# mode: ipvs
mode: iptables
EOF
kubeadm config images pull --config kubeadm-master.config
kubeadm init --config kubeadm-master.config
cd /etc/kubernetes && tar cvzf k8s-key.tgz admin.conf pki/ca.* pki/sa.* pki/front-proxy-ca.* pki/etcd/ca.*
scp k8s-key.tgz lab2:~/
scp k8s-key.tgz lab3:~/
ssh lab2 ‘tar xf k8s-key.tgz -C /etc/kubernetes/’
ssh lab3 ‘tar xf k8s-key.tgz -C /etc/kubernetes/’
配置第二个master节点
如下操作在lab2节点操作
CP0_IP=“11.11.11.111”
CP0_HOSTNAME=“lab1”
CP1_IP=“11.11.11.112”
CP1_HOSTNAME=“lab2”
cat >kubeadm-master.config<
kind: MasterConfiguration
kubernetesVersion: v1.11.0
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
apiServerCertSANs:
api:
advertiseAddress: $CP1_IP
controlPlaneEndpoint: 11.11.11.110:8443
etcd:
local:
extraArgs:
listen-client-urls: “https://127.0.0.1:2379,https:// C P 1 I P : 2379 " a d v e r t i s e − c l i e n t − u r l s : " h t t p s : / / CP1_IP:2379" advertise-client-urls: "https:// CP1IP:2379"advertise−client−urls:"https://CP1_IP:2379”
listen-peer-urls: “https:// C P 1 I P : 2380 " i n i t i a l − a d v e r t i s e − p e e r − u r l s : " h t t p s : / / CP1_IP:2380" initial-advertise-peer-urls: "https:// CP1IP:2380"initial−advertise−peer−urls:"https://CP1_IP:2380”
initial-cluster: “ C P 0 H O S T N A M E = h t t p s : / / CP0_HOSTNAME=https:// CP0HOSTNAME=https://CP0_IP:2380, C P 1 H O S T N A M E = h t t p s : / / CP1_HOSTNAME=https:// CP1HOSTNAME=https://CP1_IP:2380”
initial-cluster-state: existing
serverCertSANs:
- $CP1_HOSTNAME
- $CP1_IP
peerCertSANs:
- $CP1_HOSTNAME
- $CP1_IP
controllerManagerExtraArgs:
node-monitor-grace-period: 10s
pod-eviction-timeout: 10s
networking:
podSubnet: 10.244.0.0/16
kubeProxy:
config:
# mode: ipvs
mode: iptables
EOF
kubeadm alpha phase certs all --config kubeadm-master.config
kubeadm alpha phase kubelet config write-to-disk --config kubeadm-master.config
kubeadm alpha phase kubelet write-env-file --config kubeadm-master.config
kubeadm alpha phase kubeconfig kubelet --config kubeadm-master.config
systemctl restart kubelet
CP0_IP=“11.11.11.111”
CP0_HOSTNAME=“lab1”
CP1_IP=“11.11.11.112”
CP1_HOSTNAME=“lab2”
KUBECONFIG=/etc/kubernetes/admin.conf kubectl exec -n kube-system etcd- C P 0 H O S T N A M E − − e t c d c t l − − c a − f i l e / e t c / k u b e r n e t e s / p k i / e t c d / c a . c r t − − c e r t − f i l e / e t c / k u b e r n e t e s / p k i / e t c d / p e e r . c r t − − k e y − f i l e / e t c / k u b e r n e t e s / p k i / e t c d / p e e r . k e y − − e n d p o i n t s = h t t p s : / / {CP0_HOSTNAME} -- etcdctl --ca-file /etc/kubernetes/pki/etcd/ca.crt --cert-file /etc/kubernetes/pki/etcd/peer.crt --key-file /etc/kubernetes/pki/etcd/peer.key --endpoints=https:// CP0HOSTNAME−−etcdctl−−ca−file/etc/kubernetes/pki/etcd/ca.crt−−cert−file/etc/kubernetes/pki/etcd/peer.crt−−key−file/etc/kubernetes/pki/etcd/peer.key−−endpoints=https://{CP0_IP}:2379 member add C P 1 H O S T N A M E h t t p s : / / {CP1_HOSTNAME} https:// CP1HOSTNAMEhttps://{CP1_IP}:2380
kubeadm alpha phase etcd local --config kubeadm-master.config
kubeadm config images pull --config kubeadm-master.config
kubeadm alpha phase kubeconfig all --config kubeadm-master.config
kubeadm alpha phase controlplane all --config kubeadm-master.config
kubeadm alpha phase mark-master --config kubeadm-master.config
配置第三个master节点
如下操作在lab3节点操作
CP0_IP=“11.11.11.111”
CP0_HOSTNAME=“lab1”
CP1_IP=“11.11.11.112”
CP1_HOSTNAME=“lab2”
CP2_IP=“11.11.11.113”
CP2_HOSTNAME=“lab3”
cat >kubeadm-master.config<
kind: MasterConfiguration
kubernetesVersion: v1.11.0
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
apiServerCertSANs:
api:
advertiseAddress: $CP2_IP
controlPlaneEndpoint: 11.11.11.110:8443
etcd:
local:
extraArgs:
listen-client-urls: “https://127.0.0.1:2379,https:// C P 2 I P : 2379 " a d v e r t i s e − c l i e n t − u r l s : " h t t p s : / / CP2_IP:2379" advertise-client-urls: "https:// CP2IP:2379"advertise−client−urls:"https://CP2_IP:2379”
listen-peer-urls: “https:// C P 2 I P : 2380 " i n i t i a l − a d v e r t i s e − p e e r − u r l s : " h t t p s : / / CP2_IP:2380" initial-advertise-peer-urls: "https:// CP2IP:2380"initial−advertise−peer−urls:"https://CP2_IP:2380”
initial-cluster: “ C P 0 H O S T N A M E = h t t p s : / / CP0_HOSTNAME=https:// CP0HOSTNAME=https://CP0_IP:2380, C P 1 H O S T N A M E = h t t p s : / / CP1_HOSTNAME=https:// CP1HOSTNAME=https://CP1_IP:2380, C P 2 H O S T N A M E = h t t p s : / / CP2_HOSTNAME=https:// CP2HOSTNAME=https://CP2_IP:2380”
initial-cluster-state: existing
serverCertSANs:
- $CP2_HOSTNAME
- $CP2_IP
peerCertSANs:
- $CP2_HOSTNAME
- $CP2_IP
controllerManagerExtraArgs:
node-monitor-grace-period: 10s
pod-eviction-timeout: 10s
networking:
podSubnet: 10.244.0.0/16
kubeProxy:
config:
# mode: ipvs
mode: iptables
EOF
kubeadm alpha phase certs all --config kubeadm-master.config
kubeadm alpha phase kubelet config write-to-disk --config kubeadm-master.config
kubeadm alpha phase kubelet write-env-file --config kubeadm-master.config
kubeadm alpha phase kubeconfig kubelet --config kubeadm-master.config
systemctl restart kubelet
CP0_IP=“11.11.11.111”
CP0_HOSTNAME=“lab1”
CP2_IP=“11.11.11.113”
CP2_HOSTNAME=“lab3”
KUBECONFIG=/etc/kubernetes/admin.conf kubectl exec -n kube-system etcd- C P 0 H O S T N A M E − − e t c d c t l − − c a − f i l e / e t c / k u b e r n e t e s / p k i / e t c d / c a . c r t − − c e r t − f i l e / e t c / k u b e r n e t e s / p k i / e t c d / p e e r . c r t − − k e y − f i l e / e t c / k u b e r n e t e s / p k i / e t c d / p e e r . k e y − − e n d p o i n t s = h t t p s : / / {CP0_HOSTNAME} -- etcdctl --ca-file /etc/kubernetes/pki/etcd/ca.crt --cert-file /etc/kubernetes/pki/etcd/peer.crt --key-file /etc/kubernetes/pki/etcd/peer.key --endpoints=https:// CP0HOSTNAME−−etcdctl−−ca−file/etc/kubernetes/pki/etcd/ca.crt−−cert−file/etc/kubernetes/pki/etcd/peer.crt−−key−file/etc/kubernetes/pki/etcd/peer.key−−endpoints=https://{CP0_IP}:2379 member add C P 2 H O S T N A M E h t t p s : / / {CP2_HOSTNAME} https:// CP2HOSTNAMEhttps://{CP2_IP}:2380
kubeadm alpha phase etcd local --config kubeadm-master.config
kubeadm config images pull --config kubeadm-master.config
kubeadm alpha phase kubeconfig all --config kubeadm-master.config
kubeadm alpha phase controlplane all --config kubeadm-master.config
kubeadm alpha phase mark-master --config kubeadm-master.config
配置使用kubectl
如下操作在任意master节点操作
rm -rf $HOME/.kube
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown ( i d − u ) : (id -u): (id−u):(id -g) $HOME/.kube/config
kubectl get nodes
kubectl taint nodes --all node-role.kubernetes.io/master-
配置使用网络插件
如下操作在任意master节点操作
mkdir flannel && cd flannel
wget https://raw.githubusercontent.com/coreos/flannel/v0.10.0/Documentation/kube-flannel.yml
net-conf.json: |
{
“Network”: “10.244.0.0/16”,
“Backend”: {
“Type”: “vxlan”
}
}
image: registry.cn-shanghai.aliyuncs.com/gcr-k8s/flannel:v0.10.0-amd64
containers:
- name: kube-flannel
image: registry.cn-shanghai.aliyuncs.com/gcr-k8s/flannel:v0.10.0-amd64
command:
- /opt/bin/flanneld
args:
- --ip-masq
- --kube-subnet-mgr
- --iface=eth1
kubectl apply -f kube-flannel.yml
kubectl get pods --namespace kube-system
kubectl get svc --namespace kube-system
配置node节点加入集群
如下操作在所有node节点操作
kubeadm join 11.11.11.110:8443 --token yzb7v7.dy40mhlljt1d48i9 --discovery-token-ca-cert-hash sha256:61ec309e6f942305006e6622dcadedcc64420e361231eff23cb535a183c0e77a
基础测试
测试容器间的通信和DNS
配置好网络之后,kubeadm会自动部署coredns
如下测试可以在配置kubectl的节点上操作
启动
kubectl run nginx --replicas=2 --image=nginx:alpine --port=80
kubectl expose deployment nginx --type=NodePort --name=example-service-nodeport
kubectl expose deployment nginx --name=example-service
查看状态
kubectl get deploy
kubectl get pods
kubectl get svc
kubectl describe svc example-service
DNS解析
kubectl run curl --image=radial/busyboxplus:curl -i --tty
nslookup kubernetes
nslookup example-service
curl example-service
访问测试
curl “10.96.59.56:80”
http://11.11.11.112:32223/
http://11.11.11.113:32223/
清理删除
kubectl delete svc example-service example-service-nodeport
kubectl delete deploy nginx curl
高可用测试
关闭任一master节点测试集群是能否正常执行上一步的基础测试,查看相关信息,不能同时关闭两个节点,因为3个节点组成的etcd集群,最多只能有一个当机。
kubectl get pod --all-namespaces -o wide
kubectl get pod --all-namespaces -o wide | grep lab1
kubectl get pod --all-namespaces -o wide | grep lab2
kubectl get pod --all-namespaces -o wide | grep lab3
kubectl get nodes -o wide
kubectl get deploy
kubectl get pods
kubectl get svc
CURL_POD=$(kubectl get pods | grep curl | grep Running | cut -d ’ ’ -f1)
kubectl exec -ti $CURL_POD – sh --tty
nslookup kubernetes
nslookup example-service
curl example-service
#部署Dashboard
git clone https://github.com/bakuppus/kubernetes-dashboard.git
kubectl apply -f kubernetes-dashboard.yaml
小技巧
忘记初始master节点时的node节点加入集群命令怎么办
kubeadm token create --print-join-command
token=$(kubeadm token generate)
kubeadm token create $token --print-join-command --ttl=0