尝试在Rancher Desktop上运行nginx的kubernetes安装方式

我想做的事情

使用Rancher Desktop,在本地环境的kubernetes集群上部署nginx,并从本地PC访问。

环境准备

从Mac官方网站下载Rancher Desktop并运行它。

undefined
undefined

Kubernetes 切换上下文

安装了Rancher Desktop后,在下面的命令中确认上下文后,就创建了rancher-desktop的上下文。

$ kubectl config get-contexts
CURRENT   NAME                 CLUSTER           AUTHINFO          NAMESPACE
*         rancher-desktop      rancher-desktop   rancher-desktop

由于有上下文,所以在Rancher Desktop上进行上下文切换。

$ kubectl config use-context rancher-desktop
Switched to context "rancher-desktop".

#カレントコンテキストの確認
% kubectl config current-context
rancher-desktop

我試著檢查了一下名字空間,似乎是在運作的。

$ kubectl get namespace
NAME              STATUS   AGE
default           Active   9d
kube-system       Active   9d
kube-public       Active   9d
kube-node-lease   Active   9d

暫時部署一下nginx來試試看

$ kubectl run nginx --port=80 --image=nginx
pod/nginx created

#pod確認
$ kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          74s

好像要动了!!

undefined

创建服务

使用kubectl expose命令创建服务。

$ kubectl expose pod nginx --port=8888 --target-port=80 --type=NodePort
service/nginx exposed
service/nginx exposed

#確認
kubectl get service
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.43.0.1     <none>        443/TCP          9d
nginx        NodePort    10.43.69.34   <none>        8888:32244/TCP   59s
undefined

最后通过本地环境进行连接确认。
尝试使用curl进行连接确认。试着访问在上述设置中配置的本地主机的端口52595。

$curl http://localhost:52595
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

哦,事情解决了。

bannerAds