lab1: master 11.11.11.111
lab2: node 11.11.11.112
lab3: node 11.11.11.113
複製代碼
Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
ENV["LC_ALL"] = "en_US.UTF-8"
Vagrant.configure("2") do |config|
(1..3).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
複製代碼
v1.11.0版本推薦使用docker v17.03, v1.11,v1.12,v1.13, 也可使用,再高版本的docker可能沒法正常使用。 測試發現17.09沒法正常使用,不能使用資源限制(內存CPU)html
以下操做在全部節點操做node
# 卸載安裝指定版本docker-ce
yum remove -y docker-ce docker-ce-selinux container-selinux
yum install -y --setopt=obsoletes=0 \
docker-ce-17.03.1.ce-1.el7.centos \
docker-ce-selinux-17.03.1.ce-1.el7.centos
複製代碼
systemctl enable docker && systemctl restart docker
複製代碼
以下操做在全部節點操做linux
# 配置源
cat <<EOF > /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
# 安裝
yum install -y kubelet kubeadm kubectl ipvsadm
複製代碼
# 臨時禁用selinux
# 永久關閉 修改/etc/sysconfig/selinux文件設置
sed -i 's/SELINUX=permissive/SELINUX=disabled/' /etc/sysconfig/selinux
setenforce 0
# 臨時關閉swap
# 永久關閉 註釋/etc/fstab文件裏swap相關的行
swapoff -a
# 開啓forward
# Docker從1.13版本開始調整了默認的防火牆規則
# 禁用了iptables filter表中FOWARD鏈
# 這樣會引發Kubernetes集羣中跨Node的Pod沒法通訊
iptables -P FORWARD ACCEPT
# 配置轉發相關參數,不然可能會出錯
cat <<EOF > /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
# 加載ipvs相關內核模塊
# 若是從新開機,須要從新加載
modprobe ip_vs
modprobe ip_vs_rr
modprobe ip_vs_wrr
modprobe ip_vs_sh
modprobe nf_conntrack_ipv4
lsmod | grep ip_vs
複製代碼
以下操做在全部節點操做nginx
cat >>/etc/hosts<<EOF
11.11.11.111 lab1
11.11.11.112 lab2
11.11.11.113 lab3
EOF
複製代碼
以下操做在全部節點操做git
# 配置kubelet使用國內pause鏡像
# 配置kubelet的cgroups
# 獲取docker的cgroups
DOCKER_CGROUPS=$(docker info | grep 'Cgroup' | cut -d' ' -f3)
echo $DOCKER_CGROUPS
cat >/etc/sysconfig/kubelet<<EOF
KUBELET_EXTRA_ARGS="--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 start kubelet
複製代碼
以下操做在
master
節點操做github
# 1.11.0 版本 centos 下使用 ipvs 模式會出問題
# 參考 https://github.com/kubernetes/kubernetes/issues/65461
# 生成配置文件
cat >kubeadm-master.config<<EOF
apiVersion: kubeadm.k8s.io/v1alpha2
kind: MasterConfiguration
kubernetesVersion: v1.11.1
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
api:
advertiseAddress: 11.11.11.111
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
複製代碼
以下操做在
master
節點操做docker
rm -rf $HOME/.kube
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
# 查看node節點
kubectl get nodes
# 只有網絡插件也安裝配置完成以後,才能會顯示爲ready狀態
# 設置master容許部署應用pod,參與工做負載,如今能夠部署其餘系統組件
# 如 dashboard, heapster, efk等
kubectl taint nodes --all node-role.kubernetes.io/master-
複製代碼
以下操做在
master
節點操做shell
# 下載配置
mkdir flannel && cd flannel
wget https://raw.githubusercontent.com/coreos/flannel/v0.10.0/Documentation/kube-flannel.yml
# 修改配置
# 此處的ip配置要與上面kubeadm的pod-network一致
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
# 若是Node有多個網卡的話,參考flannel issues 39701,
# https://github.com/kubernetes/kubernetes/issues/39701
# 目前須要在kube-flannel.yml中使用--iface參數指定集羣主機內網網卡的名稱,
# 不然可能會出現dns沒法解析。容器沒法通訊的狀況,須要將kube-flannel.yml下載到本地,
# flanneld啓動參數加上--iface=<iface-name>
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
節點操做json
# 此命令爲初始化master成功後返回的結果
kubeadm join 11.11.11.111:6443 --token yl53pn.wpx4mvx6a6jfkjhw --discovery-token-ca-cert-hash sha256:17751fcda3e79da63f5d0c4a3586e97de8b8b1d017c1a6977c88136409af5240
複製代碼
配置好網絡以後,kubeadm會自動部署corednscentos
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
複製代碼
kubectl run curl --image=radial/busyboxplus:curl -i --tty
nslookup kubernetes
nslookup example-service
curl example-service
複製代碼
# 10.96.59.56 爲查看svc時獲取到的clusterip
curl "10.96.59.56:80"
# 32223 爲查看svc時獲取到的 nodeport
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節點時的node節點加入集羣命令怎麼辦
# 簡單方法
kubeadm token create --print-join-command
# 第二種方法
token=$(kubeadm token generate)
kubeadm token create $token --print-join-command --ttl=0
複製代碼