基础操作
1:修改主机名
master、node1、node2主机修改
hostnamectl set-hostname master && bash
hostnamectl set-hostname node1 && bash
hostnamectl set-hostname node2 && bash
2:设置hosts
master、node1、node2主机修改
echo "
192.168.3.128 master
192.168.3.129 node1
192.168.3.130 node2
" >> /etc/hosts
3:配置无密码登陆
master、node1、node2主机修改
ssh-copy-id root@master
ssh-copy-id root@node1
ssh-copy-id root@node2
4:关闭交换分区
master、node1、node2主机修改
swapoff -a
5:修改内核参数
master、node1、node2主机修改
modprobe br_netfilter
echo "modprobe br_netfilter" >> /etc/profile
cat > /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sysctl -p /etc/sysctl.d/k8s.conf
6:关闭firewalld和selinux
master、node1、node2主机修改
systemctl stop firewalld ; systemctl disable firewalld
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
7:配置阿里云repo
master、node1、node2主机修改
cd /etc/yum.repos.d/
mkdir /root/yum.repo
mv * /root/yum.repo/
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
yum clean all
yum makecache
8:配置阿里云docker源
master、node1、node2主机修改
yum -y install yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
9:配置阿里云kubernetes源
master、node1、node2主机修改
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
EOF
10:开启ipvs
master、node1、node2主机修改
[root@master ~]# vim /etc/sysconfig/modules/ipvs.modules
#!/bin/bash
ipvs_modules="ip_vs ip_vs_lc ip_vs_wlc ip_vs_rr ip_vs_wrr ip_vs_lblc ip_vs_lblcr ip_vs_dh ip_vs_sh ip_vs_nq ip_vs_sed ip_vs_ftp nf_conntrack"
for kernel_module in ${ipvs_modules}; do
/sbin/modinfo -F filename ${kernel_module} > /dev/null 2>&1
if [ 0 -eq 0 ]; then
/sbin/modprobe ${kernel_module}
fi
done
[root@master ~]# chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep ip_vs
11:安装基础软件包
master、node1、node2主机修改
yum -y install epel-release
yum install -y yum-utilsdevice-mapper-persistent-data lvm2 wgetnet-tools nfs-utils lrzsz gcc gcc-c++ make cmake libxml2-devel openssl-develcurl curl-devel unzip sudo ntp libaio-devel wget vim ncurses-devel autoconfautomake zlib-devel python-devel epel-release openssh-server socat ipvsadmconntrack ntpdate telnet ipvsadm
12:安装docker
master、node1、node2主机修改
yum install docker-ce-20.10.6 docker-ce-cli-20.10.6 containerd.io -y
systemctl start docker&& systemctl enable docker.service
13:配置docker镜像加速器
master、node1、node2主机修改
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://vvjfywn5.mirror.aliyuncs.com"],"exec-opts":["native.cgroupdriver=systemd"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
14:安装初始化k8s所需软件包
master、node1、node2主机修改
yum install -y kubelet-1.20.6 kubeadm-1.20.6 kubectl-1.20.6
systemctl enable kubelet && systemctl start kubelet
systemctl status kubelet
\#上面可以看到kubelet状态不是running状态,这个是正常的,不用管,等k8s组件起来这个kubelet就正常了。
15:kubeadm初始化k8s集群
master、node1、node2主机修改
docker pull registry.aliyuncs.com/google_containers/coredns
docker pull registry.aliyuncs.com/google_containers/pause
docker pull registry.aliyuncs.com/google_containers/kube-controller-manager:v1.20.6
docker pull registry.aliyuncs.com/google_containers/kube-scheduler:v1.20.6
docker pull calico/cni
docker pull calico/pod2daemon-flexvol
docker pull calico/kube-controllers
docker pull calico/node
docker pull registry.aliyuncs.com/google_containers/etcd:3.1.13-0
docker pull registry.aliyuncs.com/google_containers/kube-proxy:v1.20.6
docker pull registry.aliyuncs.com/google_containers/kube-apiserver:v1.20.6
16:使用kubeadm初始化k8s集群
master主机修改
kubeadm init --kubernetes-version=1.20.6 --apiserver-advertise-address=192.168.3.128 --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16 --ignore-preflight-errors=SystemVerification
出现如下界面则表示安装成功
Your Kubernetes control-plane 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
Alternatively, if you are the root user, you can run:
export KUBECONFIG=/etc/kubernetes/admin.conf
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/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.3.128:6443 --token 1w70yh.ejzxa2rckpu1p25o \
--discovery-token-ca-cert-hash sha256:aaa612678ea7d9a2a902467d13191412d9d66c8462bd57d9c20fb2a24601da1
用此命令将node节点加入集群
kubeadm join 192.168.3.128:6443 --token 1w70yh.ejzxa2rckpu1p25o \
--discovery-token-ca-cert-hash sha256:aaa612678ea7d9a2a902467d13191412d9d66c8462bd57d9c20fb2a24601da1
\#配置kubectl的配置文件config,相当于对kubectl进行授权,这样kubectl命令可以使用这个证书对k8s集群进行管理
master主机修改
[root@master ~]# mkdir -p $HOME/.kube
[root@master ~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@master ~]# sudo chown $(id -u):$(id -g) $HOME/.kube/config
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master NotReady control-plane,master 110s v1.20.6
17:扩容k8s集群
如果刚刚忘记保存加入集群的命令,可以用以下命令来查看
master主机修改
kubeadm token create --print-join-command
将node节点加入集群
node1、node2主机修改
kubeadm join 192.168.3.128:6443 --token 38jn8o.taun5799cxwq3s5j --discovery-token-ca-cert-hash sha256:aaa612678ea7d9a2a902467d13191412d9d66c8462bd57d9c20fb2a24601da16
出现如下界面则执行成功
Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
在master节点查看是否有node节点
master主机修改
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master NotReady control-plane,master 8m10s v1.20.6
node1 NotReady <none> 4m38s v1.20.6
node2 NotReady <none> 4m38s v1.20.6
18:安装网络插件calico
master主机修改
calico地址:https://docs.projectcalico.org/manifests/calico.yaml
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master NotReady control-plane,master 8m10s v1.20.6
node1 NotReady <none> 4m38s v1.20.6
node2 NotReady <none> 4m38s v1.20.6
查看calico状态,状态全部是running,就说明运行正常了
master主机修改
[root@master ~]# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
calico-kube-controllers-56c7cdffc6-ntlfs 1/1 Running 0 4m25s
calico-node-jd9ds 1/1 Running 0 4m25s
calico-node-k5n4f 1/1 Running 0 4m25s
calico-node-q2tm9 1/1 Running 0 4m25s
..........
查看node节点,Ready状态就说明是正常的
master主机修改
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready control-plane,master 16m v1.20.6
node1 Ready <none> 13m v1.20.6
node2 Ready <none> 13m v1.20.6
19:测试部署tomcat
node1、node2主机修改
node1、node2节点下载tomcat镜像
docker pull tomcat:8.5-jre8-alpine
master节点
[root@master ~]# vim tomcat.yaml
apiVersion: v1 #pod属于k8s核心组v1
kind: Pod #创建的是一个Pod资源
metadata: #元数据
name: demo-pod #pod名字
namespace: default #pod所属的名称空间
labels:
app: myapp #pod具有的标签
env: dev #pod具有的标签
spec:
containers: #定义一个容器,容器是对象列表,下面可以有多个name
- name: tomcat-pod-java #容器的名字
ports:
- containerPort: 8080
image: tomcat:8.5-jre8-alpine #容器使用的镜像
imagePullPolicy: IfNotPresent
[root@master ~]# kubectl apply -f tomcat.yaml
pod/demo-pod create
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
demo-pod 1/1 Running 0 20s
执行tomcat.yml文件
[root@master ~]# vim tomcat-service.yaml
apiVersion: v1
kind: Service
metadata:
name: tomcat
spec:
type: NodePort
ports:
- port: 8080
nodePort: 30080
selector:
app: myapp
env: dev
[root@master ~]# kubectl apply -f tomcat-service.yaml
service/tomcat created
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 24m
tomcat NodePort 10.104.85.10 <none> 8080:30080/TCP 7s
在浏览器访问node1IP:30080、node2IP:30080
有什么问题可以在下面留言!