第四阶段
时 间:2023年8月10日
参加人:全班人员
内 容:
Kubernetes集群yum安装部署
目录
yum方式部署K8S集群
一、环境搭建
1、准备环境
2、关闭防火墙
3、修改master主机的计算机名设置host文件
二、安装部署
(一)安装master节点
(二)安装k8s-master上的node
(三)安装配置k8s-node1节点
(四)安装k8s-node2节点
(五)为所有node节点配置flannel网络
(六)配置docker开启加载防火墙规则允许转发数据
三、k8s常用资源管理
1、创建一个pod
2、pod管理
3、pod操作
yum方式部署K8S集群
一、环境搭建
1、准备环境
计算机说明,建议系统版本7.4或者7.6
主机名 | IP地址 | 角色 | 硬件 |
k8s-master | 192.168.100.131 | Master,node | Etcd、apiserver、controlor-manager、scheduler、kube-proxy、docker、registry |
K8s-node1 | 192.168.100.132 | Node | Kubletel、kube-proxy、docker |
K8s-node2 | 192.168.100.133 | Node | Kubletel、kube-proxy、docker |
2、关闭防火墙
[root@huyang1 ~]# systemctl stop firewalld
[root@huyang1 ~]# iptables -F
[root@huyang1 ~]# setenforce 0
3、修改master主机的计算机名设置host文件
[root@centos01 ~]# hostnamectl k8s-master
[root@centos01 ~]# bash
[root@k8s-master ~]# vim /etc/hosts
[root@k8s-master ~]#
scp /etc/hosts k8s-node01:/etc/hosts
[root@k8s-master ~]#
scp /etc/hosts k8s-node02:/etc/hosts
[root@centos02 ~]# hostnamectl k8s-node1
[root@centos02 ~]# bash
[root@centos03 ~]# hostnamectl k8s-node2
[root@centos03 ~]# bash
二、安装部署
(一)安装master节点
1)安装etcd配置etcd
[root@k8s-master ~]# yum -y install etcd
[root@k8s-master ~]# cp /etc/etcd/etcd.conf /etc/etcd/etcd.conf.bak
[root@k8s-master ~]# vim /etc/etcd/etcd.conf
[root@k8s-master ~]# systemctl start etcd
[root@k8s-master ~]# systemctl enable etcd
2)安装k8s-master节点
[root@k8s-master ~]#yum -y install
kubernetes-master.x86_64
3)配置apiserver
[root@k8s-master ~]# ls /etc/kubernetes/
[root@k8s-master ~]# vim /etc/kubernetes/apiserver
配置如下:
4)配置controller和scheduler
[root@k8s-master ~]# vim /etc/kubernetes/config
配置如下:
5)启动k8s服务
[root@k8s-master ~]# systemctl start kube-apiserver.service
kube-controller-manager.service
kube-scheduler.service
[root@k8s-master ~]# systemctl enable kube-apiserver.service
kube-controller-manager.service
kube-scheduler.service
6)检查节点安装都是健康的
[root@k8s-master ~]# kubectl get componentstatus
(二)安装k8s-master上的node
1)安装node
[root@k8s-master ~]# yum install kubernetes node.x86_64
2)配置kubelet
[root@k8s-master ~]# vim /etc/kubernetes/kubelet
配置如下:
3)启动kubelet启动自动启动docker服务
[root@k8s-master ~]# systemctl start kubelet
[root@k8s-master ~]# systemctl enable kubelet
4)启动kubelet-proxy
[root@k8s-master ~]# systemctl start kube-proxy
[root@k8s-master ~]# systemctl enable kube-proxy
5)检查node节点
[root@k8s-master ~]# kubectl get nodes
(三)安装配置k8s-node1节点
1)安装node
[root@k8s-node1 ~]# yum install kubernetes node.x86_64
2)node1连接k8s-master
[root@k8s-node01 ~]# ls /etc/kubernetes/
[root@k8s-node1 ~]# vim /etc/kubernetes/config
3)配置kubelet
[root@k8s-node1 ~]# vim /etc/kubernetes/kubelet
4)启动服务
[root@k8s-node1 ~]# systemctl start kubelet
[root@k8s-node1 ~]# systemctl start kube-proxy
[root@k8s-node1 ~]# systemctl enable kubelet
[root@k8s-node1 ~]# systemctl enable kube-proxy
5)在master节点检测node节点状态
[root@k8s-master ~]# kubectl get nodes
(四)安装k8s-node2节点
1)安装node
[root@k8s-node2 ~]# yum install kubernetes node.x86_64
2)node1连接k8s-master
[root@k8s-node2 ~]# vim /etc/kubernetes/config
配置如下:
3)配置kubelet
[root@k8s-node2 ~]# vim /etc/kubernetes/kubelet
4)启动服务
[root@k8s-node2 ~]# systemctl start kubelet
[root@k8s-node2 ~]# systemctl start kube-proxy
[root@k8s-node2 ~]# systemctl enable kubelet
[root@k8s-node2 ~]# systemctl enable kube-proxy
5)在master节点检测node节点状态
[root@k8s-master ~]# kubectl get nodes
(五)为所有node节点配置flannel网络
1)在k8s-master节点安装flannel
[root@k8s-master ~]# yum install flannel -y
[root@k8s-master ~]# ls /etc/sysconfig/
[root@k8s-master ~]# vim /etc/sysconfig/flanneld
[root@k8s-master ~]# etcdctl set /atomic.io/network/config '{ "Network": "172.16.0.0/16" }' //配置网络
[root@k8s-master ~]# systemctl start flanneld
[root@k8s-master ~]# systemctl enable flanneld
[root@k8s-master ~]# ifconfig //查看多一个网络
[root@k8s-master ~]# systemctl restart docker //重新启动docker服务和flannel网络保持一致
[root@k8s-master ~]# systemctl enable docker
[root@k8s-master ~]# ifconfig
Ps备注:自动安装了docker容器,在这版本下,k8s和docker还是融合在一起使用的
2)配置node1节点flannel网络
[root@k8s-node1 ~]# yum install flannel -y
[root@k8s-node01 ~]# ls /etc/sysconfig/
[root@k8s-node1 ~]# vim /etc/sysconfig/flanneld
配置如下:
[root@k8s-node1 ~]# systemctl start flanneld
[root@k8s-node1 ~]# systemctl enable flanneld
[root@k8s-node1 ~]# systemctl restart docker
[root@k8s-node1 ~]# systemctl enable docker
[root@k8s-node01 ~]# ifconfig
3)安装node2节点flannel网络
[root@k8s-node2 ~]# yum install flannel -y
[root@k8s-node02 ~]# ls /etc/sysconfig/
[root@k8s-node2 ~]# vim /etc/sysconfig/flanneld
配置如下:
[root@k8s-node2 ~]# systemctl start flanneld
[root@k8s-node2 ~]# systemctl enable flanneld
[root@k8s-node2 ~]# systemctl restart docker
[root@k8s-node2 ~]# systemctl enable docker
[root@k8s-node02 ~]# ifconfig
4)测试docker容器跨宿主机通信
[root@k8s-node1 ~]# iptables -P FORWARD ACCEPT
//允许转发数据
[root@k8s-node2 ~]# iptables -P FORWARD ACCEPT
[root@k8s-master ~]# iptables -P FORWARD ACCEPT
[root@k8s-master ~]# docker pull busybox
//下载镜像,而后进入docker run -it busybox
[root@k8s-noded01 ~]# docker run -it busybox
[root@k8s-noded02 ~]# docker run -it busybox
/ # ping 172.16.31.2 //测试和其他docker宿主机之间通信
(六)配置docker开启加载防火墙规则允许转发数据
1)配置k8s-master节点
[root@k8s-master ~]# vim /usr/lib/systemd/system/docker.service
[root@k8s-master ~]# systemctl daemon-reload
[root@k8s-master ~]# systemctl restart docker
2)配置k8s-node1节点
[root@k8s-node1 ~]# vim /usr/lib/systemd/system/docker.service
[root@k8s-node1 ~]# systemctl daemon-reload
[root@k8s-node1 ~]# systemctl restart docker
3)配置k8s-node2节点
[root@k8s-node2 ~]# vim /usr/lib/systemd/system/docker.service
[root@k8s-node2 ~]# systemctl daemon-reload
[root@k8s-node2 ~]# systemctl restart docker
三、k8s常用资源管理
1、创建一个pod
1)创建yuml文件
[root@k8s-master ~]# mkdir k8s
[root@k8s-master ~]# vim ./k8s/nginx.yaml
配置如下:
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: web
spec:
containers:
- name: nginx01
image: nginx:1.16
ports:
- containerPort: 80
- name: busybox
image: docker.io/busybox:latest
command: ["sleep","3600"]
ports:
- containerPort: 80
2)创建容器
方法一. yum安装
[root@k8s-master ~]#yum install *rhsm*
方法二 (我是用这方法解决的)
执行命令:
[root@k8s-master ~]#wget http://mirror.centos.org/centos/7/os/x86_64/Packages/python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm
[root@k8s-master ~]#rpm2cpio python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm | cpio -iv --to-stdout ./etc/rhsm/ca/redhat-uep.pem | tee /etc/rhsm/ca/redhat-uep.pem
前两个命令会生成/etc/rhsm/ca/redhat-uep.pem文件.
[root@k8s-master ~]#docker pull registry.access.redhat.com/rhel7/pod-infrastructure:latest
[root@k8s-master ~]# kubectl create -f ./k8s/nginx.yaml
3)查看所有pod创建运行状态
[root@k8s-master ~]# kubectl get pod
4)查看指定pod资源
[root@k8s-master ~]# kubectl get pod nginx
5)查看pod运行的详细信息
[root@k8s-master ~]# kubectl describe pod nginx
[root@k8s-master ~]# kubectl get pod nginx -o wide //显示调度节点
6)验证运行的pod
[root@k8s-master ~]# kubectl get pod nginx -o wide
2、pod管理
1)删除pod
[root@k8s-master ~]# kubectl delete pod nginx
2)查看删除pod无法找到
[root@k8s-master ~]# kubectl get pod nginx -o wide
3)创建pod
[root@k8s-master ~]# kubectl create -f ./k8s/nginx.yaml
4)发现最先创建的pod运行在k8s-master节点上,下载镜像速度太慢没法运行
[root@k8s-master ~]# kubectl get pod nginx -o wide
5)访问pod节点中的服务
[root@k8s-master ~]# curl -I http://172.16.7.3
3、pod操作
1)在一个pod中修改配置文件运行多个业务容器,
创建资源test1
[root@k8s-master ~]# vim ./k8s/nginx.yaml
配置如下:
apiVersion: v1
kind: Pod
metadata:
name: test1
labels:
app: web
spec:
containers:
- name: nginx01
image: nginx:1.16
ports:
- containerPort: 80
- name: busybox
image: docker.io/busybox:latest
command: ["sleep","3600"]
ports:
- containerPort: 80
[root@k8s-master ~]# kubectl create -f ./k8s/nginx.yaml
2)创建资源test2
[root@k8s-master ~]# vim ./k8s/nginx.yaml
配置如下:
apiVersion: v1
kind: Pod
metadata:
name: test2
labels:
app: web
spec:
containers:
- name: nginx01
image: nginx:1.16
ports:
- containerPort: 80
- name: busybox
image: docker.io/busybox:latest
command: ["sleep","3600"]
ports:
- containerPort: 80
[root@k8s-master ~]# kubectl create -f ./k8s/nginx.yaml
3)查看资源使用情况
[root@k8s-master ~]# kubectl get pod -o wide