本文共 4040 字,大约阅读时间需要 13 分钟。
lab1: master 11.11.11.111lab2: node 11.11.11.112lab3: 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 复制代码
cat >>/etc/hosts<
由于kubeadm默认会去拉取gcr.io上的镜像来启动master相关的组件,由于在国内无法访问gcr.io所以会导致无法成功启动。有如下几种解决办法:
cat >kubeadm-master.config<
没有经过测试的另一种初始化方法
# 使用命令行方式KUBE_REPO_PREFIX='registry.cn-hangzhou.aliyuncs.com/google_containers' kubeadm init \--kubernetes-version=v1.10.3 \--pod-network-cidr=10.244.0.0/16 \--apiserver-advertise-address=11.11.11.111复制代码
rm -rf $HOME/.kubemkdir -p $HOME/.kubesudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/configsudo 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-复制代码
配置使用flannel
# 下载配置mkdir flannel && cd flannelwget https://raw.githubusercontent.com/coreos/flannel/master/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=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-systemkubectl get svc --namespace kube-system复制代码
# 配置kubelet使用国内可用pause镜像 # 修改/etc/systemd/system/kubelet.service.d/10-kubeadm.conf # 添加如下配置sed -i '/ExecStart=$/i Environment="KUBELET_EXTRA_ARGS=--pod-infra-container-image=registry.cn-shanghai.aliyuncs.com/gcr-k8s/pause-amd64:3.0"' /etc/systemd/system/kubelet.service.d/10-kubeadm.conf# 重新载入配置systemctl daemon-reload# 设置kubelet开机启动systemctl enable kubelet复制代码
# 此命令为启动master成功后返回的结果kubeadm join --token 55a6f8.1091208463fe1252 11.11.11.111:6443 --discovery-token-ca-cert-hash sha256:790c6b38b087b167c1f52c04526d8729115192a305eb91c01c0fd8dc7facbbcd复制代码
配置好calico网络之后,kubeadm会自动部署kube-dns
kubectl run nginx --replicas=2 --image=nginx:alpine --port=80kubectl expose deployment nginx --type=NodePort --name=example-service-nodeportkubectl expose deployment nginx --name=example-service复制代码
kubectl get deploykubectl get podskubectl get svckubectl describe svc example-service复制代码
kubectl run curl --image=radial/busyboxplus:curl -i --ttynslookup kubernetesnslookup example-servicecurl example-service复制代码
# 10.96.59.56 为查看svc时获取到的clusteripcurl "10.96.59.56:80" # 32223 为查看svc时获取到的 nodeporthttp://11.11.11.112:32223/http://11.11.11.113:32223/复制代码
kubectl delete svc example-service example-service-nodeportkubectl delete deploy nginx curl复制代码
1. 如果删除kube-dns后怎么修复
# 第一种方法kubeadm upgrade apply v1.10.3 --config kubeadm-master.config# 第二种方法kubeadm config upload from-file --config kubeadm-master.configkubeadm upgrade apply v1.10.3复制代码
2. 忘记初始master节点时的node节点加入集群命令怎么办
# 简单方法kubeadm token create --print-join-command# 第二种方法token=$(kubeadm token generate)kubeadm token create $token --print-join-command --ttl=0本文转自掘金-
转载地址:http://frzdo.baihongyu.com/