目录
Kubernetes简介
Kubernetes的好处:
kubernetes设计架构
Kubernetes主要由以下几个核心组件组成:
Kubernetes部署
Kubernetes集群包含有节点代理kubelet和Master组件(APIs, scheduler, etc),一切都基于分布式的存储系统。
除了核心组件,还有一些推荐的Add-ons:
Kubernetes设计理念和功能其实就是一个类似Linux的分层架构
外部:日志、监控、配置管理、CI、CD、Workflow、FaaS、OTS应用、ChatOps等 Kubernetes
内部:CRI、CNI、CVI、镜像仓库、Cloud Provider、集群自身的配置和管理等
实验前准备项:
server1:cd harbor/ ls docker-compose ps docker-compose start
server2
docker node ls docker stack rm web docker service ls
server3、4
docker swarm leave
server2
docker swarm leave --force
server3:
cd /etc/systemd/system/docker.service.d/ ls rm -f 10-machine.conf systemctl daemon-reload systemctl restart docker
真机输入命令:
iptables -t nat -I POSTROUTING -s 172.25.70.0/24 -j MASQUERADE #联网
禁用swap
server2 3 4:
swapoff -a
vim /etc/fstab
#注释掉最后一行
设定docker cgroup driver为systemd
server2:
cd /etc/docker/
ls
vim daemon.json
systemctl daemon-reload
systemctl restart docker
rm -f *.pem
systemctl stop docker
systemctl stop docker.socket
systemctl start docker.socket
systemctl start docker.service
docker info
systemctl enable docker.socket
systemctl enable docker.service
server3:
systemctl enable docker.socket
systemctl enable docker.service
server2:
scp daemon.json server3:/etc/docker/
scp daemon.json server4:/etc/docker/
server3:
systemctl daemon-reload
systemctl restart docker
systemctl stop docker
systemctl stop docker.socket
systemctl start docker.socket
systemctl start docker.service
docker info
server4
systemctl daemon-reload
systemctl restart docker
docker info
server3:
cd /etc/sysctl.d/
scp docker.conf server4:/etc/sysctl.d/
server4:
cd /etc/sysctl.d/
sysctl --system
docker info
编辑k8s安装软件仓库文件
server2
cd /etc/yum.repos.d/
vim k8s.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
yum install -y kubelet kubeadm kubectl
scp k8s.repo server3:/etc/yum.repos.d/
scp k8s.repo server4:/etc/yum.repos.d/
server3、4
yum install -y kubelet kubeadm kubectl
k8s集群设置免密操作
server2
systemctl enable --now kubelet
#设置开机自启
ssh-keygen
ssh-copy-id server3
ssh-copy-id server4
ssh server3 systemctl enable --now kubelet
ssh server4 systemctl enable --now kubelet
server3测试开机自启是否设置成功
systemctl is-enabled kubelet.service
使用 kubeadm 创建集群
server2
kubeadm config print init-defaults
//查看默认配置信息
cd
kubeadm config images list --image-repository registry.aliyuncs.com/google_containers
//列出所需镜像
kubeadm config images pull --image-repository registry.aliyuncs.com/google_containers
//拉取镜像
网页: 在私有仓库中建立新项目:k8s(一定要点击公开)
docker login reg.westos.org
docker images | grep registry
docker images | grep k8s
docker images | grep k8s | awk '{system("docker push "$1":"$2"")}'
#把镜像下载到私有仓库中(k8s)
网页:刷新可以看到刚上传的镜像
kubeadm init --pod-network-cidr=10.244.0.0/16 --image-repository
//初始化集群(路径不能改,要求)【所有组件设置开机自启】
ll /etc/kubernetes/admin.conf
export KUBECONFIG=/etc/kubernetes/admin.conf
#部署设置变量、赋值(重启就没了)
vim .bash_profile
#把赋值的命令写入这个文件
kubectl get node
#可以看到已经添加了,但是没有就绪
kubectl get pod -n kube-system
#可以看到有两个pod是pending状态,没有运行
kubectl get pod -n kube-system -o wide
#可以看到刚才没有运行的pod是准备状态,需要先部署网络组建通信
wget https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
#下载官方的网络插件【站点不稳定,需要多下载两次】
ls
vim kube-flannel.yml
#把里面的镜像在server1上拉取
拉取镜像
server1:
docker pull rancher/mirrored-flannelcni-flannel-cni-plugin:v1.0.0
docker pull rancher/mirrored-flannelcni-flannel:v0.16.1
网页新建rancher项目,一定要点击公开
改名、上传镜像到私有仓库的指定目录
server1
docker tag rancher/mirrored-flannelcni-flannel-cni-plugin:v1.0.0 reg.westos.org/rancher/mirrored-flannelcni-flannel-cni-plugin:v1.0.0
docker push reg.westos.org/rancher/mirrored-flannelcni-flannel-cni-plugin:v1.0.0
docker tag rancher/mirrored-flannelcni-flannel:v0.16.1 reg.westos.org/rancher/mirrored-flannelcni-flannel:v0.16.1
docker push reg.westos.org/rancher/mirrored-flannelcni-flannel:v0.16.1
网页在自己私有仓库可以看到刚上传的镜像
server2
配置kubectl命令补齐功能:
echo "source <(kubectl completion bash)" >> ~/.bashrc
source .bashrc
kubectl apply -f kube-flannel.yml
日志:可以看到有匿名拉取镜像
server2
kubectl get pod -n kube-system
#可以看到全部都running了
kubectl get node
#可是ready的状态
server3、4
kubeadm join 172.25.70.2:6443 --token n8zkol.9pv1fm97l1hi0zud --discovery-token-ca-cert-hash sha256:b53a6c0e7536a304d91ac47db4154eb19b1f707b1165c3a1b91c2a12d3dae1d1
#从仓库下载镜像;--token在触发集群的时候默认24小时(之后要是相加节点要重新创建token)
日志:可以看到有匿名拉取镜像
server2
kubectl get node
kubectl get pod -n kube-system
#可以看到全running
kubectl get pod -n kube-system -o wide
#-o可以看到pod运行在哪些节点上