kind 一主两从 k8s 学习
一、 环境描述
Ubuntu 20.04.6 LTS
16c 32g
x86_64
角色 | 主机名 | node ip |
---|---|---|
Master | kind-control-plane | 10.244.0.1 |
Worker | kind-work | 10.244.1.1 |
Worker | kind-work-2 | 10.244.2.1 |
二、 docker 部署
略
三、 kind 部署
curl -Lo ./kind https://kind.sigs.k8s.io/dl/latest/kind-linux-amd64
chmod +x kind
kind /usr/local/bin/kind
四、 kubectl 安装
snap install kubectl --classic
snap install kubelet --classic
五、 创建配置文件(一主两从配置)
vim kind-config.yaml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
5.1 创建k8s集群
kind create cluster --name demo-cluster --config kind-cluster.yaml
5.2 验证集群状态
root@wang:~# kubectl get nodes -o wide |
5.3 部署一个示例
创建 Deployment
kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1
修改 Deployment
kubectl edit deploy kubernetes-bootcamp
replicas: 3
5.4 访问刚才部署的 pods
- 新开一个终端执行:
kubectl proxy
- 在新的终端中访问:
curl http://localhost:8001/api/v1/namespaces/default/pods/kubernetes-bootcamp-f95c5b745-dd9cl:8080/proxy/
5.5 创建一个 Service
Kubernetes 的 Service 是一个抽象层,它所定义的是 Pod 的一个逻辑集合, 并为这些 Pod 支持外部流量公开、负载平衡和服务发现。
ClusterIP
: (默认)在集群的内部 IP 上公开 Service。 这种类型使得 Service 只能从集群内访问NodePort
: 使用 NAT 在集群中每个选定 Node 的相同端口上公开 Service 。 使用NodeIP:NodePort
从集群外部访问 Service 这是 ClusterIP 的超集 (换句话说就是 cluster 的基础上扩展了一个端口, 本质仍是 cluster )LoadBalancer
:在当前云中创建一个外部负载均衡器(如果支持的话), 并为 Service 分配一个固定的外部 IP。这是 NodePort 的超集。ExternalNam
: 将 Service 映射到 externalName 字段的内容(例如 foo.bar.example.com), 通过返回带有该名称的 CNAME 记录实现。不设置任何类型的代理。 这种类型需要 kube-dns 的 v1.7 或更高版本,或者 CoreDNS 的 v0.8 或更高版本。
5.5.1 ClusterIP
- 创建 ClusterIP 类型 service
kubectl expose deployment/kubernetes-bootcamp --type="Cluster" --port 8080
kubectl expose deployment kubernetes-bootcamp --type ClusterIP --port 8080 --name kubernetes-bootcamp-cluster
5.5.2 NodePort
创建 NodePort 类型 service
kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080
kubectl expose deployment kubernetes-bootcamp --type NodePort --port 8080 --name kubernetes-bootcamp-nodeport
固定端口号,可以加参数 –node-port=端口号访问方式:
curl nodeip:nodeport
,curl clusterip:port
5.5.3 LoadBalancer
创建 LoadBalancer:
kubectl expose deployment/kubernetes-bootcamp --type="LoadBalancer" --port 8080
kubectl expose deployment kubernetes-bootcamp --type LoadBalancer --port 8080 --name kubernetes-bootcamp-loadbalancer
扩容 Deploy 副本:
kubectl scale deployment kubernetes-bootcamp --replicas 5
修改镜像:
kubectl set image deployments kubernetes-bootcamp kubernetes-bootcamp=docker.io/jocatalin/kubernetes-bootcamp:v2
kind 从 docker 中导入镜像:
kind load docker-image jocatalin/kubernetes-bootcamp:v2 --name kind
访问:
docker exec kind-worker curl localhost:32628