Kubernetes⎈
简介kubernetes,简称K8s,是用8代替8个字符“ubernete”而成的缩写。
是一个开源的,用于管理云平台中多个主机上的容器化的应用,Kubernetes的目标是让部署容器化的应用简单并且高效(powerful),Kubernetes提供了应用部署,规划,更新,维护的一种机制。
kubernetes
原名:borg
伯格,编排引擎,原名
kubernetes⎈
部署过程系统信息:
Ubuntu19.04
,kubernetes=1.14.1(及组件版本)
系统内核:
Linux - 5.0.0-13
docker
版本:containerd=1.2.5-1
,docker-ce-cli_18.09.5
,docker-ce_18.09.5
关闭ufw防火墙,Ubuntu默认未启用,无需设置。
sudo ufw disable
禁用SELINUX (ubuntu19.04默认不安装)
# 临时禁用 sudo setenforce 0
# 修改配置文件 sudo vi /etc/selinux/config SELINUX=permissive
开启数据包转发
# 修改/etc/sysctl.conf,开启ipv4转发: sudo vim /etc/sysctl.conf # 命令重启生效 sudo sysctl -p
防火墙修改FORWARD链默认策略
# 临时生效 sudo iptables -P FORWARD ACCEPT
# 命令修改配置文件 /usr/sbin/iptables -P FORWARD ACCEPT
禁用
swap
# 禁掉所有的swap分区 sudo swapoff -a
永久修改
swap
# 路径如下 vim /etc/fstab # /etc/fstab: static file system information. # # Use 'blkid' to print the universally unique identifier for a # device; this may be used with UUID= as a more robust way to name devices # that works even if disks are added and removed. See fstab(5). # # <file system> <mount point> <type> <options> <dump> <pass> # / was on /dev/sda1 during installation UUID=d54d0616-6d61-4512-9a70-1f40306a4016 / ext4 errors=remount-ro 0 1 #/swapfile none swap sw 0 0 # 将最后一行注释掉
配置iptables参数,使得流经网桥的流量也经过iptables/netfilter防火墙
sudo tee /etc/sysctl.d/k8s.conf <<-'EOF' net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF sudo sysctl --system
安装
docker
# 使用国内源(清华源)进行获取 wget https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/dists/bionic/pool/stable/amd64/containerd.io_1.2.5-1_amd64.deb && \ wget https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/dists/bionic/pool/stable/amd64/docker-ce-cli_18.09.5~3-0~ubuntu-bionic_amd64.deb && \ wget https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/dists/bionic/pool/stable/amd64/docker-ce_18.09.5~3-0~ubuntu-bionic_amd64.deb
# 安装deb包 dpkg -i containerd.io_1.2.5-1_amd64.deb && \ dpkg -i docker-ce-cli_18.09.5~3-0~ubuntu-bionic_amd64.deb && \ dpkg -i docker-ce_18.09.5~3-0~ubuntu-bionic_amd64.deb
docker
使用加速器(阿里云加速器)tee /etc/docker/daemon.json <<- 'EOF' { "registry-mirrors": ["https://5xcgs6ii.mirror.aliyuncs.com"] } EOF
设置
docker
开机自启动sudo systemctl enable docker && sudo systemctl start docker
kubeadm
、kubelet
、kubectl
创建
kubernetes
的source
文件(使用阿里云或者中科大的镜像站)sudo apt-get update && sudo apt-get install -y apt-transport-https curl
sudo curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -
sudo tee /etc/apt/sources.list.d/kubernetes.list <<-'EOF' deb https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial main EOF
sudo apt-get update
使用
apt
来进行安装kubeadm
、kubelet
、kubectl
# 查看可用版本(选择最新的) apt-cache madison kubeadm
# 安装指定版本 sudo apt-get install -y kubelet=1.14.1-00 kubeadm=1.14.1-00 kubectl=1.14.1-00 sudo apt-mark hold kubelet=1.14.1-00 kubeadm=1.14.1-00 kubectl=1.14.1-00
# 设置开机自启动 sudo systemctl enable kubelet && sudo systemctl start kubelet
Kubernetes⎈
集群安装master
节点部署查看
kubernetes
需要哪些镜像kubeadm config images list --kubernetes-version=v1.14.1
使用
shell脚本
将其pull
、push
、tag
#!/bin/bash images=(kube-proxy:v1.14.1 kube-scheduler:v1.14.1 kube-controller-manager:v1.14.1 kube-apiserver:v1.14.1 etcd:3.3.10 pause:3.1 coredns:1.3.1) for imageName in ${images[@]} ; do docker pull mirrorgooglecontainers/$imageName docker tag mirrorgooglecontainers/$imageName k8s.gcr.io/$imageName docker rmi mirrorgooglecontainers/$imageName done
coredns
需要单独拉取docker pull coredns/coredns:1.3.1 docker tag coredns/coredns:1.3.1 k8s.gcr.io/coredns:1.3.1
kubeadm init
初始化集群# -apiserver-advertise-address=192.168.1.175需要换成本机IP sudo kubeadm init --apiserver-advertise-address=192.168.1.175 --pod-network-cidr=172.16.0.0/16 --service-cidr=10.233.0.0/16 --kubernetes-version=v1.14.1
部署完成
# 部署完成之后,将会有如下界面 Your Kubernetes master has initialized successfully! To start using your cluster, you need to run the following as a regular user: mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config You should now deploy a pod network to the cluster. Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at: https://kubernetes.io/docs/concepts/cluster-administration/addons/ You can now join any number of machines by running the following on each node as root: kubeadm join 192.168.1.175:6443 --token v1nj22.l30dctzysf2jynly --discovery-token-ca-cert-hash sha256:0170607e7e069ffde2f2b6b440e7982f066887e59db49e9a62ac9518924af690
# token需要留下 kubeadm join 192.168.1.175:6443 --token v1nj22.l30dctzysf2jynly --discovery-token-ca-cert-hash sha256:0170607e7e069ffde2f2b6b440e7982f066887e59db49e9a62ac9518924af690
查看
K8s
状态# 查看集群及节点状态 kubectl get node # 查看所有管理元素 kubectl get pods # 查看k8s的Pod状态 kubectl get pod --all-namespaces
master
节点init
错误检查
kubelet
使用的cgroup driver
docker info | grep -i cgroup -> Cgroup Driver: cgroupfs
sudo vim /etc/default/kubelet KUBELET_KUBEADM_EXTRA_ARGS=--cgroup-driver=<value> sudo systemctl daemon-reload sudo systemctl restart kubelet
创建
kubectl
使用的kubeconfig
文件mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl get node
出现server localhost:8080 was refused
及解决方法出现这个问题的原因是
kubectl
命令需要使用kubernetes-admin
来运行,解决方法如下,将主节点中的/etc/kubernetes/admin.conf
文件拷贝到从节点相同目录下,然后配置环境变量echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
立即生效
source ~/.bash_profile
然后再运行
kubectl get node
kubectl get node
coredns Pending状态
解决coredns
问题