官方文档
https://kubernetes.io/docs/setup/
为什么叫k8s ,因为 kubernetes 单词过长,k后面8个字母+最后的s ,简称k8s
官方网站:https://kubernetes.io/docs/home/
角色 | IP |
---|---|
k8s-1(master) | 192.168.1.252 |
k8s-2(node1) | 192.168.1.97 |
k8s-3(node2) | 192.168.1.49 |
关闭防火墙:
$ systemctl stop firewalld
$ systemctl disable firewalld
关闭selinux:
$ sed -i 's/enforcing/disable/' /etc/selinux/config # 永久
$ setenforce 0 # 临时
关闭swap:
$ swapoff -a # 临时
$ vim /etc/fstab # 永久
#/dev/mapper/centos-swap swap swap defaults 0 0
设置主机名:
$ hostnamectl set-hostname <hostname>
在master添加hosts:
$ cat >> /etc/hosts << EOF
192.168.1.252 k8s-1-master
192.168.1.97 k8s-2-node1
192.168.1.49 k8s-3-node2
EOF
将桥接的IPv4流量传递到iptables的链:
$ cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
$ sysctl --system # 生效
时间同步:
$ yum install ntpdate -y
$ ntpdate time.windows.com
$ reboot
安装docker,配置加速源
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum -y install docker-ce-18.06.1.ce-3.el7
systemctl enable docker
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF
systemctl restart docker
kubeadm 是官方社区推出的一个用于快速部署kubernetes集群的工具。
kubelet 工作在节点上,主要作用是监控容器使用状态处理
kubectl 主要作用是控制集群管理器
通过阿里云镜像安装 kubeadm,kubelet和kubectl
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
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-1.17.0 kubeadm-1.17.0 kubectl-1.17.0
systemctl enable kubelet
官方文档
https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm-init/
kubeadm init \
--apiserver-advertise-address=192.168.1.252 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.17.0 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16
参数解析
其它参数参考官方文档
看到这个就是安装成功了
kubeadm join 192.168.1.252:6443 --token u3oziq.0bzn0c2vtk54rcr6 \
--discovery-token-ca-cert-hash sha256:afbb4facdd85627a1c6fc56ca11363f3bf5f96ba1c7de820444ecd352c36783f
安装完成以后 k8s 并没有完全启动,需要安装一个网络插件才能完全启动
安装 flannel 网络插件
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
如果遇不能访问的情况,下载 github 上的镜像包,导入,修改kube-flannel.yml 配置文件本地安装
https://github.com/flannel-io/flannel/releases
重启docker,k8s组件也会跟着docker自启
在 node 机器上运行刚刚生成的 token
遇到flannel慢的可用多等一会儿,实在不行就换下载后导入镜像本地安装
创建一个pod,验证
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pod,svc
他的命名空间在上面有创建,如果想要看它的pods 需要指定此命名空间才能看到
Dashboard 默认不对外开放
修改yeam配置文件,添加两条配置
kubectl apply -f recommended.yaml
kubectl get pods -n kubernetes-dashboard
访问地址:http://NodeIP:30001
输入 thisisunsafe
创建service account并绑定默认cluster-admin管理员集群角色
将此 token 复制到浏览器
kubectl create serviceaccount dashboard-admin -n kube-system
kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')
token
eyJhbGciOiJSUzI1NiIsImtpZCI6InZWT0lQVk1XYlBWZVIzMXpIZ2tleE01SEtLbTZ2enRPNzI2aW9JZFpXdWMifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkYXNoYm9hcmQtYWRtaW4tdG9rZW4tODRtbTciLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGFzaGJvYXJkLWFkbWluIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiODRjMzhiNTUtMjI1Yi00MTU5LWI2NGUtOWM4MTcyOGFlZDdhIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmRhc2hib2FyZC1hZG1pbiJ9.b2dhdCALXIOsOjuHhOBTnYiO40XbRA_GiZ2dW9a7u5ej204slBpW2ipCy4HXvJxHy21y2I84-_elfR9DvoLsNJ0ibyCmca13Dh9PYuL_tZsVHejnBf4eVODb3q7TpsPfXE4AY3CR5cB9oBHCzPrY2VMiXKASCO3aINN6vI3L14mYwQGNba1bWti_E1fJ63TaWyLK5FUjH-yEAcAH7VBofIHdOsZrO51rmaTBTw6b0ev91QZ17eZHq9JSqCcDY35wT4f7Wr-LfEP7jKKRF5Q6W02zlh-_VZcnY1Jg-UiKkpsgOKWX6NqlpJxA1VOYlf9RbCyzY8I_TtIafoDX-Zb_2g
这里可以同一管理一些存储卷,节点,简单的监控等
error execution phase preflight: [preflight] Some fatal errors occurred:
[ERROR Mem]: the system RAM (972 MB) is less than the minimum 1700 MB
[ERROR KubeletVersion]: the kubelet version is higher than the control plane version. This is not a supported version skew and may lead to a malfunctional cluster. Kubelet version: "1.22.1" Control plane version: "1.21.0"
error execution phase preflight: [preflight] Some fatal errors occurred:
[ERROR FileContent--proc-sys-net-bridge-bridge-nf-call-iptables]: /proc/sys/net/bridge/bridge-nf-call-iptables contents are not set to 1
[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`
解决
echo '1' > /proc/sys/net/bridge/bridge-nf-call-iptables
[ERROR Swap]: running with swap on is not supported. Please disable swap
[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`
解决
$ vim /etc/fstab
#/dev/mapper/centos-swap swap swap defaults 0 0
error execution phase preflight: [preflight] Some fatal errors occurred:
[ERROR ImagePull]: failed to pull image registry.aliyuncs.com/google_containers/coredns:v1.8.4: output: Error response from daemon: manifest for registry.aliyuncs.com/google_containers/coredns:v1.8.4 not found: manifest unknown: manifest unknown
, error: exit status 1
解决
docker pull registry.aliyuncs.com/google_containers/coredns:1.8.4
docker tag registry.aliyuncs.com/google_containers/coredns:1.8.4 registry.aliyuncs.com/google_containers/coredns:v1.8.4
kubeadm reset -f
modprobe -r ipip
lsmod
rm -rf ~/.kube/
rm -rf /etc/kubernetes/
rm -rf /etc/systemd/system/kubelet.service.d
rm -rf /etc/systemd/system/kubelet.service
rm -rf /usr/bin/kube*
rm -rf /etc/cni
rm -rf /opt/cni
rm -rf /var/lib/etcd
rm -rf /var/etcd
yum clean all
yum remove kube*
kubelet cgroup driver: "systemd" is different from docker cgroup driver: "cgroupfs"
尝试修改
vim /etc/docker/daemon.json
"exec-opts": ["native.cgroupdriver=systemd"]
阿里云 开发者社区-深入浅出Kubernetes-声东
阿良教育-李振良
马哥教育
Quick Start to Kubernetes