Kubernetes集群大致分为两类:一主多从和多主多从。
为了测试方便,本次搭建的是一主多从类型的集群
kubernetes有多种部署方式,目前主流的方式有kubeadm
、minikube
、二进制包
。
我们需要安装kubernetes的集群环境,但是又不想过于麻烦,所以选择minikube方式。
https://minikube.sigs.k8s.io/docs/start/
要使用Debian 软件包在x86-64 Linux上安装最新的 minikube稳定版本:
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_latest_amd64.deb
sudo dpkg -i minikube_latest_amd64.deb
使用root用户运行:
minikube start \
--driver=docker \
--image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers \
--image-mirror-country=cn \
--v=5 \
--force-systemd=true \
--kubernetes-version=v1.23.8 \
--nodes 3
minikube v1.28.0 on Debian 10.11 (kvm/amd64)
✨ Using the docker driver based on user configuration
✅ Using image repository registry.cn-hangzhou.aliyuncs.com/google_containers
Using Docker driver with root privileges
Starting control plane node minikube in cluster minikube
Pulling base image ...
Creating docker container (CPUs=2, Memory=3800MB) ...
> kubelet.sha256: 64 B / 64 B [-------------------------] 100.00% ? p/s 0s
> kubeadm.sha256: 64 B / 64 B [-------------------------] 100.00% ? p/s 0s
> kubectl.sha256: 64 B / 64 B [-------------------------] 100.00% ? p/s 0s
> kubeadm: 43.12 MiB / 43.12 MiB [------------] 100.00% 18.57 MiB p/s 2.5s
> kubectl: 44.44 MiB / 44.44 MiB [------------] 100.00% 18.00 MiB p/s 2.7s
> kubelet: 118.78 MiB / 118.78 MiB [----------] 100.00% 18.32 MiB p/s 6.7s
▪ Generating certificates and keys ...
▪ Booting up control plane ...
▪ Configuring RBAC rules ...
Verifying Kubernetes components...
▪ Using image registry.cn-hangzhou.aliyuncs.com/google_containers/storage-provisioner:v5
Enabled addons: storage-provisioner, default-storageclass
❗ /usr/local/bin/kubectl is version 1.26.0, which may have incompatibilities with Kubernetes 1.23.8.
▪ Want kubectl v1.23.8? Try 'minikube kubectl -- get pods -A'
Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
# 查看集群状态
$ minikube status
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured
minikube-m02
type: Worker
host: Running
kubelet: Running
minikube-m03
type: Worker
host: Running
kubelet: Running
# 查看集群Node列表
$ minikube node list
minikube 192.168.49.2
minikube-m02 192.168.49.3
minikube-m03 192.168.49.4
$ kubectl get node
NAME STATUS ROLES AGE VERSION
minikube Ready control-plane,master 54m v1.23.8
minikube-m02 Ready <none> 54m v1.23.8
minikube-m03 Ready <none> 54m v1.23.8
在Kubernetes集群中部署一个Nginx程序,测试下集群是否正常工作。
# 部署Nginx:
$ kubectl create deployment nginx --image=nginx:1.17.1 --replicas=3 -n dev
deployment.apps/nginx created
# 暴露端口:
## 方式1:ClusterIP
$ kubectl expose deployment nginx --name=svc-nginx1 --type=ClusterIP --port=80 --target-port=80 -n dev
service/svc-nginx1 exposed
# 查看service状态:
$ kubectl get svc -n dev
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
svc-nginx1 ClusterIP 10.98.118.41 <none> 80/TCP 17m
# 集群内访问
## 1.登录到minikube集群node节点
$ minikube ssh -n minikube-m03
docker@minikube-m03:~$
## 2.通过ClusterIP访问nginx
$ curl 10.98.118.41:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
# 暴露端口:
## 方式2:NodePort
$ kubectl expose deployment nginx --name=svc-nginx2 --type=NodePort --port=80 --target-port=80 -n dev
service/svc-nginx2 exposed
# 查看service状态:
$ kubectl get svc -n dev
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
svc-nginx1 ClusterIP 10.98.118.41 <none> 80/TCP 24m
svc-nginx2 NodePort 10.105.135.208 <none> 80:30897/TCP 6m16s
# 集群外访问
todo