前言
Kubernetes是用于自动部署、扩展和管理“容器化应用程序”的开源系统。该系统由Google设计并捐赠给Cloud Native Computing Foundation来使用。 它旨在提供“跨主机集群的自动部署、扩展以及运行应用程序容器的平台”。 它支持一系列容器工具,包括Docker等。
前置条件
不少于2台的拥有公网ip的云服务器(强烈建议使用海外服务器),也可使用VMware虚拟化服务器,需要两块网卡,NAT网卡及仅主机模式网卡,服务器最低配置:CPU2核心,内存2G,硬盘20G。
NAT网卡为DHCP自动分配IP
仅主机模式网卡:
k8s-master 192.168.192.10
k8s-worker1 192.168.192.11
k8s-worker2 192.168.192.12
系统为CentOS 7.9
准备工作
以下准备工作均在所有节点上执行
关闭防火墙
1 2
| systemctl stop firewalld systemctl disable firewalld
|
关闭SElinux
1 2
| setenforce 0 sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
|
关闭swap
1 2 3
| swapoff /dev/mapper/centos-swap rm /dev/mapper/centos-swap /dev/mapper/centos-swap swap swap default 0 0
|
配置时间同步
1 2 3 4
| vim /etc/chrony.conf
systemctl restart chronyd chronyc sources -v
|
修改主机名
1 2 3 4 5 6 7 8 9
| hostnamectl set-hostname k8s-master
hostnamectl set-hostname k8s-worker1
hostnamectl set-hostname k8s-worker2 .....
hostnamectl set-hostname k8s-worker*
|
配置hosts文件
1 2 3 4 5
| vim /etc/hosts
192.168.192.10 k8s-master 192.168.192.11 k8s-worker1 192.168.192.12 k8s-worker2
|
配置免密登录
1 2 3 4
| ssh-keygen ssh-copy-id root@k8s-worker1 ssh-copy-id root@k8s-worker2
|
安装docker
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo yum makecache fast
yum -y install docker-ce
yum list docker-ce.x86_64 --showduplicates | sort -r
systemctl start docker systemctl enable docker
mkdir -p /etc/docker tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://7c4t92zk.mirror.aliyuncs.com"] } EOF
vim /etc/docker/daemon.json { "registry-mirrors": ["https://7c4t92zk.mirror.aliyuncs.com"], "exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": { "max-size": "100m" } } systemctl daemon-reload systemctl restart docker
|
允许iptables 检查桥接流量
1 2 3 4 5 6 7 8
| cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf br_netfilter EOF
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF
|
开始安装K8s
添加K8s软件源(所有节点)
1 2 3 4 5 6 7 8 9
| 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
|
安装必需组件(所有节点)
1 2 3
| yum install -y kubelet-1.23.0 kubectl-1.23.0 kubeadm-1.23.0 systemctl start kubelet systemctl enable kubelet
|
初始化K8s集群(仅master节点)
1 2 3 4 5 6
| kubeadm init --apiserver-advertise-address 192.168.192.11 --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr 172.25.0.0/16 --service-cidr 10.88.0.0/12 --v=6
mkdir -p $HOME/.kube cp -i /etc/kubernetes/admin.conf $HOME/.kube/config chown $(id -u):$(id -g) $HOME/.kube/config
|
安装网络插件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml kubectl apply -f kube-flannel.yml
wget https://cloud.18db.top/d/aliyunpan/get/k8s/kube-flannel.yml kubectl apply -f kube-flannel.yml
kubectl get nodes --- NAME STATUS ROLES AGE VERSION k8s-master Ready control-plane,master 9h v1.23.0 ---
--- NAME STATUS ROLES AGE VERSION k8s-master NotReady control-plane,master 9h v1.23.0 ---
kubectl describe node k8s-master
---- untime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:docker: network plugin is not ready: cni config uninitialized ----
docker pull quay.io/coreos/flannel:v0.9.1-amd64
mkdir -p /etc/cni/net.d/ ll /etc/cni/net.d/ mv 10-flannel.conflist 10-flannel.conf
cat <<EOF> /etc/cni/net.d/10-flannel.conf { "name": "cbr0", "cniVersion": "0.3.1", "plugins": [ { "type": "flannel", "delegate": { "hairpinMode": true, "isDefaultGateway": true } }, { "type": "portmap", "capabilities": { "portMappings": true } } ] } EOF
mkdir /usr/share/oci-umount/oci-umount.d -p mkdir /run/flannel/
cat <<EOF> /run/flannel/subnet.env FLANNEL_NETWORK=172.100.0.0/16 FLANNEL_SUBNET=172.100.1.0/24 FLANNEL_MTU=1450 FLANNEL_IPMASQ=true EOF
ip a | grep cni0
NAME STATUS ROLES AGE VERSION k8s-master Ready control-plane,master 9h v1.23.0
|
将Worker节点加入K8s集群
1 2
| kubeadm join [master节点ip] --token xxx --discovery-token-ca-cert-hash xxx
|
重置K8s集群
1 2 3 4 5 6
| kubeadm reset
rm -rf $HOME/.kube
kubeadm reset
|
使用 Deployment 运行一个无状态应用
1 2 3 4 5 6 7 8 9 10
| kubectl apply -f https://k8s.io/examples/application/deployment.yaml
kubectl describe deployment nginx-deployment
kubectl get pods -l app=nginx
kubectl describe pod <pod-name>
kubectl delete deployment nginx-deployment
|
调试Pod
https://kubernetes.io/zh-cn/docs/tasks/debug/debug-application/debug-pods/