K8S最新版安装1.13+DOCKER 18.09

DOCKER安装

复制以下内容到文件中,并保存
[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

一、直接安装kubeadm

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
systemctl enable kubelet && systemctl start kubelet && systemctl start docker

二、设置操作系统

cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system

三、初始化集群服务器

要指定网络的段,还要指定API使用IP,因为这个IP是VIP,可以负载与高可用,指定SERVICE的IP。
kubeadm init --apiserver-advertise-address=10.239.33.142 --pod-network-cidr=192.168.0.0/16 --service-cidr=10.8.0.0/16

如果要使用高可用的集群,需使用以下YAML文件和命令
kubeadm init --config=init.yaml

apiVersion: kubeadm.k8s.io/v1beta1
kind: ClusterConfiguration
kubernetesVersion: stable
apiServer:
  certSANs:
  - "10.249.6.246"
networking:
  serviceSubnet: 10.1.0.0/16
  podSubnet: 192.2.0.0/16
controlPlaneEndpoint: "10.249.6.246:6443"

并其它的二个主服务器上运行,其中这个IP是一个VIP
kubeadm join 10.249.6.246:6443 --token wr7q6a.pqegqr5zj3fzw4m2 --discovery-token-ca-cert-hash sha256:324d8410d3ff2985a2b2fda4620952a4e3b6ed939cbfad930ce6125708f82941 --experimental-control-plane

四、配置系统变量

vi .bash_profile,加到最后一行
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> /root/.bash_profile 

五、创建网络

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml

记得更改配置文件里面的IP段

六、入口界面安装,生成一个证书

openssl genrsa -des3 -passout pass:x -out dashboard.pass.key 2048
openssl rsa -passin pass:x -in dashboard.pass.key -out dashboard.key
rm dashboard.pass.key
openssl req -new -key dashboard.key -out dashboard.csr
openssl x509 -req -sha256 -days 365 -in dashboard.csr -signkey dashboard.key -out dashboard.crt

七、挂载上面后成的证书到DAB里面

kubectl create secret generic kubernetes-dashboard-certs --from-file=$HOME/certs -n kube-system
或者文件方式
kubectl create secret generic kubernetes-dashboard-certs --from-file="$HOME/certs/dashboard.crt,$HOME/certs/dashboard.key" -n kube-system 

八、查看证书是否导入,并启动

kubectl get secret kubernetes-dashboard-certs -n kube-system -o yaml

发布界面入口  
kubectl apply -f kubernetes-dashboard.yml

获取TOKEN
kubectl -n kube-system describe $(kubectl -n kube-system get secret -n kube-system -o name | grep namespace) | grep token

登陆即可

九、加入节点到这个集群里

kubeadm join 10.239.33.148:6443 --token y1yoni.oug5gvwas038a57b --discovery-token-ca-cert-hash sha256:55a5160a3640874aaa556d461a648b50ce15a8b459b13fac6135d5d314725dde

10、节点清理命令

kubeadm reset -f
iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X
ipvsadm --clear
systemctl stop kubelet
systemctl stop docker
rm -rf /var/lib/cni/
rm -rf /var/lib/kubelet/*
rm -rf /etc/cni/
ifconfig cni0 down
ifconfig flannel.1 down
ifconfig docker0 down
ip link delete cni0
ip link delete flannel.1
systemctl start docker
rm -rf /etc/kubernetes/
iptables -F -t nat
iptables -F

11、国外下载到阿里云,在转到内网的DOCKER镜像中心

docker tag fadcc5d2b066 10.249.6.188/kube-proxy:v1.13.4
docker tag fc3801f0fc54 10.249.6.188/kube-apiserver:v1.13.4
docker tag 40a817357014 10.249.6.188/kube-controller-manager:v1.13.4
docker tag dd862b749309 10.249.6.188/kube-scheduler:v1.13.4
docker tag 3cab8e1b9802 10.249.6.188/etcd:3.2.24
docker tag f59dcacceff4 10.249.6.188/coredns:1.2.6
docker tag da86e6ba6ca1 10.249.6.188/pause

docker push 10.249.6.188/kube-proxy:v1.13.4
docker push 10.249.6.188/kube-apiserver:v1.13.4
docker push 10.249.6.188/kube-controller-manager:v1.13.4
docker push 10.249.6.188/kube-scheduler:v1.13.4
docker push 10.249.6.188/etcd:3.2.24
docker push 10.249.6.188/coredns:1.2.6
docker push 10.249.6.188/pause

忘记了TOKEN或者TOKEN过期的处理办法

kubeadm token create

openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'

kubeadm join --token 2ffue8.wyyavux6d8jao28c --discovery-token-ca-cert-hash sha256:ead9c84a0d2d1d01c2164fd21da6f1e5a300f6efe519c41c12d8157010f4b211 10.239.33.142:6443