使用Kubeadm 安裝k8s叢集
#!/bin/sh
systemctl stop firewalld && systemctl disable firewalld
&& sed -i s/“SELINUX=enforcing”/“SELINUX=disabled”/g /etc/selinux/config
&& sed -i s/"^SELINUXTYPE=targeted"/""/g /etc/selinux/config
&& setenforce 0
cat < /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
vm.swappiness=0
EOF
swapoff -a
sed -i ‘/swap/s/^/#/’ /etc/fstab
modprobe br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager
–add-repo
https://download.docker.com/linux/centos/docker-ce.repo
yum makecache fast
yum install -y --setopt=obsoletes=0
docker-ce-18.06.1.ce-3.el7
systemctl start docker && systemctl enable docker
cat < /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
#配置docker代理
sed -i ‘/ExecStart/i\EnvironmentFile=/etc/sysconfig/docker’ /lib/systemd/system/docker.service
cat < /etc/sysconfig/docker
HTTP_PROXY=http://127.0.0.1:38118
HTTPS_PROXY=http://127.0.0.1:38118
EOF
systemctl daemon-reload && systemctl restart docker
echo “proxy=http://127.0.0.1:38118” >>/etc/yum.conf
yum -y makecache fast
yum install -y kubelet kubeadm kubectl
systemctl enable kubelet.service
kubeadm init
–kubernetes-version=v1.12.0
–pod-network-cidr=192.168.0.0/16
–apiserver-advertise-address=172.16.207.11
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown
(id -g) $HOME/.kube/config
#Network install
kubectl apply -f
https://docs.projectcalico.org/v3.2/getting-started/kubernetes/installation/hosted/etcd.yaml
kubectl apply -f
https://docs.projectcalico.org/v3.2/getting-started/kubernetes/installation/rbac.yaml
kubectl apply -f
https://docs.projectcalico.org/v3.2/getting-started/kubernetes/installation/hosted/calico.yaml
#叢集初始化如果遇到問題,可以使用下面的命令進行清理:
kubeadm reset
ifconfig cni0 down
ip link delete cni0
ifconfig flannel.1 down
ip link delete flannel.1
rm -rf /var/lib/cni/
從叢集中移除Node
master 上執行
kubectl drain node2 --delete-local-data --force --ignore-daemonsets
kubectl delete node node2
在node2上執行:
kubeadm reset
ifconfig cni0 down
ip link delete cni0
ifconfig flannel.1 down
ip link delete flannel.1
rm -rf /var/lib/cni/
在node1上執行:
kubectl delete node node2
測試DNS
kubectl run curl --image=radial/busyboxplus:curl -it
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
If you don't see a command prompt, try pressing enter.
[ [email protected]:/ ]$
命令執行成功將直接進入到容器中
進入後執行nslookup kubernetes.default確認解析正常:
nslookup kubernetes.default
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
Name: kubernetes.default
Address 1: 10.96.0.1 kubernetes.default.svc.cluster.local