1.环境准备

按照官方文档所示https://minikube.sigs.k8s.io/docs/start/

a)新建centos虚拟机

b) 关闭防火墙和selinux

systemctl stop firewalld && systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/sysconfig/selinux

c)设置主机名

hostnamectl set-hostname k8s-master

2.安装docker

a)配置docker源

yum install -y wget
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo

b)安装docker环境依赖

yum install -y yum-utils device-mapper-persistent-data lvm2

c)安装docker

yum install docker-ce-18.09.9 docker-ce-cli-18.09.9 containerd.io -y 

d)验证

docker -v

e)启动docker并设置为开机自启

systemctl start docker && systemctl enable docker

f)配置镜像加速

mkdir -p /etc/docker 
tee /etc/docker/daemon.json <<-'EOF'
{
    "registry-mirrors": ["https://9cpn8tt6.mirror.aliyuncs.com"], 
    "exec-opts": ["native.cgroupdriver=systemd"]
}
EOF

g)重新启动守护进程并重启docker

systemctl daemon-reload && systemctl restart docker

3.安装minikube

curl -LO https://github.com/kubernetes/minikube/releases/download/v1.18.0/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube

4.启动minikube

minikube start --driver=docker

“The "docker" driver should not be used with root privileges.”报错

useradd minikube 创建用户

passwd minikube 设置密码

su tomx 切换用户

报错

* Suggestion: Add your user to the 'docker' group: 'sudo usermod -aG docker $USER && newgrp docker'

执行sudo usermod -aG docker $USER && newgrp docker

报错minikube is not in the sudoers file.  This incident will be reported.

root下

chmod u+w /etc/sudoers 赋予读写权限

vi /etc/sudoers

root	ALL=(ALL) 	ALL
minikube ALL=(ALL)      ALL

切换到minikube

su minikube

minikube start --driver=docker

kubectl not found. If you need it, try: 'minikube kubectl -- get pods -A'

执行minikube kubectl -- get pods -A

设置别名

alias kubectl="minikube kubectl --"

查看状态 minikube status

配置dashboard

minikube dashboard --url

开启kube-proxy端口映射,使其可以远程访问

kubectl proxy --port=30030 --address='0.0.0.0' --accept-hosts='^.*' &

5.部署应用

查看你创建的 Service

kubectl get services

对于支持负载均衡器的云服务平台而言,平台将提供一个外部 IP 来访问该服务。 在 Minikube 上,LoadBalancer 使得服务可以通过命令 minikube service 访问

minikube service hello-node

暴露端口

kubectl expose deployment hello-node --type=LoadBalancer --port=8080

查看所有服务

kubectl get svc

查看具体服务

kubectl get services hello-node

 To access a LoadBalancer deployment, use the “minikube tunnel” command

查看 Deployment   

kubectl get deployments

查看 Pod

kubectl get pods

查看集群事件

kubectl get events

查看 kubectl 配置

kubectl config view

查看创建的 Pod 和 Service

kubectl get pod,svc -n kube-system

6.清理

清理你在集群中创建的资源

kubectl delete service hello-node
kubectl delete deployment hello-node

停止 Minikube

minikube stop

删除 Minikube

minikube delete

7.External

尽管每个 Pod 都有一个唯一的 IP 地址,但是如果没有 Service ,这些 IP 不会暴露在集群外部。Service 允许您的应用程序接收流量。Service 也可以用在 ServiceSpec 标记type的方式暴露

  • ClusterIP (默认) - 在集群的内部 IP 上公开 Service 。这种类型使得 Service 只能从集群内访问。
  • NodePort - 使用 NAT 在集群中每个选定 Node 的相同端口上公开 Service 。使用<NodeIP>:<NodePort> 从集群外部访问 Service。是 ClusterIP 的超集。
  • LoadBalancer - 在当前云中创建一个外部负载均衡器(如果支持的话),并为 Service 分配一个固定的外部IP。是 NodePort 的超集。
  • ExternalName - 通过返回带有该名称的 CNAME 记录,使用任意名称(由 spec 中的externalName指定)公开 Service。不使用代理。这种类型需要kube-dns的v1.7或更高版本

kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
# 获取访问地址
minikube service --url nginx

也可以通过 kubectl proxy 拼接 url 访问,https://kubernetes.io/zh/docs/tasks/access-application-cluster/access-cluster/#manually-constructing-apiserver-proxy-urls

http://192.168.204.129:30030/api/v1/namespaces/default/services/nginx:80/proxy/

测试 curl http://192.168.49.2:32084

浏览器检查

删除部署服务pod

# 新开窗口运行
minikube tunnel --cleanup=true   

# 重新部署
kubectl delete deployment nginx
kubectl delete service nginx
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=LoadBalancer
# 查看外部地址
kubectl get svc
#通过转发访问,https://kubernetes.io/zh/docs/tasks/access-application-cluster/port-forward-access-application-cluster
kubectl port-forward pods/nginx-6799fc88d8-p8llb 8080:80 --address='0.0.0.0'

https://kubernetes.io/zh/docs/tasks/access-application-cluster/port-forward-access-application-cluster/

https://kubernetes.io/zh/docs/tasks/access-application-cluster/access-cluster/

https://minikube.sigs.k8s.io/docs/handbook/accessing/

https://www.cnblogs.com/jhxxb/p/15220729.html

https://mp.weixin.qq.com/s/clZXVLsrdJQy_GZFDPQt6Q

https://blog.csdn.net/aixiaoyang168/article/details/78331847​​​​​​​

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐