操作系统:centos7
安装虚拟机并设置静态ip
初始操作
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
|
systemctl stop firewalld
systemctl disable firewalld
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
hostnamectl set-hostname <hostname>
cat >> /etc/hosts << EOF
192.168.192.133 k8s-master
192.168.192.134 k8s-node1
192.168.192.135 k8s-node2
EOF
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
yum install ntpdate -y
ntpdate time.windows.com
|
安装基础软件
安装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 33 34 35 36 37 38 39 40
| sudo yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo grabbing file https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo Could not fetch/save url https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to file /etc/yum.repos.d/docker-ce.repo: [Errno 14] curl
vi /etc/yum.conf
[main] sslverify=0
sudo yum makecache fast sudo yum -y install docker-ce
sudo service docker start
|
添加阿里云yum源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| 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
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
|
安装kubeadm、kubelet、kubetcl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| yum install -y kubelet-1.23.6 kubeadm-1.23.6 kubectl-1.23.6
systemctl enable kubelet
"exec-opts": ["native.cgroupdriver=systemd"]
systemctl daemon-reload
systemctl restart docker
|
部署kubernetes Master
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
kubeadm init --apiserver-advertise-address=192.168.192.133 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.23.6 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16
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 nodes
|
一些问题:
1.
1 2
| error execution phase preflight: [preflight] Some fatal errors occurred: [ERROR FileAvailable--etc-kubernetes-manifests-kube-apiserver.yaml]: /etc/kubernetes/manifests/kube-apiserver.yaml already exists
|
这个问题是之前初始化失败的问题,使用kubeadm reset
重置kubeadm然后重新初始化即可。
2.docker启动失败
/etc/docker/daemon.json
如果包含格式不正确的JSON
,Docker
将无法启动
修改daemon.json
,然后重启解决了。
3.It seems like the kubelet isn’t running or healthy
在vi /etc/docker/daemon.json中加入”exec-opts”: [“native.cgroupdriver=systemd”],然后执行下面的命令
1 2 3
| sudo systemctl daemon-reload sudo systemctl restart docker sudo systemctl restart kubelet
|
加入kubernetes Node
Master初始化成功后会有下面的提示,在Node中执行下面的命令即可加入集群
1
| kubeadm join 192.168.192.133:6443 --token unswcx.ffqnm1za6ipvcw3p --discovery-token-ca-cert-hash sha256:65b1464dda5c8db4265424fcb25792e8b769039f92a19d9d8e7ce8a344e022e9
|
部署CNI网络插件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
curl https://raw.githubusercontent.com/projectcalico/calico/v3.26.1/manifests/calico.yaml -O
sed -i 's#docker.io/##g' calico.yaml
kubectl apply -f calico.yaml
kubectl get po -n kube-system
kubectl get nodes
|
测试kubernetes集群
1 2 3 4 5 6 7 8 9 10 11
|
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pod,svc
|
部署完成后可以直接通过浏览器访问
在任意节点使用kubectl
kubectl默认只能在master节点中使用,如果想要在任意节点中使用,只需完成下面两步。
将 master 节点中 /etc/kubernetes/admin.conf 拷贝到需要运行的服务器的 /etc/kubernetes 目录中
scp /etc/kubernetes/admin.conf root@k8s-node1:/etc/kubernetes
在对应的服务器上配置环境变量
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
source ~/.bash_profile
命令行基础命令的使用
常见资源类型与缩写
资源类型 |
缩写 |
pods |
po |
deployments |
deploy |
services |
svc |
namespace |
ns |
nodes |
no |
API
- alpha 未经测试的版本,不推荐使用 v1alpha1
- beta 经过测试的版本,v2beta3
- stable 稳定版 v1
废弃API说明