NAME STATUS ROLES AGE VERSION k3s-master Ready master 36m v1.17.0+k3s.1
如果系统反馈以下错误信息:
1 2
WARN[2020-01-24T02:49:04.982837381Z] Unable to read /etc/rancher/k3s/k3s.yaml, please start server with --write-kubeconfig-mode to modify kube config permissions error: error loading config file "/etc/rancher/k3s/k3s.yaml": open /etc/rancher/k3s/k3s.yaml: permission denied
说明权限不够,可以采用以下两种方式解决:
使用 sudo 运行命令
1
sudo kubectl get pods
改变 k3s.yaml 的文件属性,使普通用户访问该文件
1
sudo chmod -R 777 /etc/rancher/k3s/k3s.yaml
发布一个简单的应用
在集群安装、配置领域, 发布一个 nginx 服务,几乎是最常见的做法,就像在编程领域编写一个 hello world 应用一样。
下面,我们向刚才构建的k3s集群发布一个 nginx 服务,执行:
1
kubectl create deployment nginx --image=nginx
系统显示:
1
deployment.apps/nginx created
执行 get pod, 查看是否建立了相应的 pod,
1
kubectl get pods
系统显示
1 2
NAME READY STATUS RESTARTS AGE nginx-86c57db685-rrh7w 1/1 Running 0 61s
为了让外部可以访问nginx,我们构建一个k3s中的 nodeport 服务,执行:
1
kubectl create service nodeport nginx --tcp=80:80
系统显示
1
service/nginx created
执行 get svc 命令,可以查看建立好的 k3s 服务:
1
kubectl get svc
系统显示:
1 2 3
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 45m nginx NodePort 10.43.150.48 <none> 80:30424/TCP 45s
<!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> 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 <ahref="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <ahref="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p> </body> </html>
这正是 nginx 的默认输出。 说明集群安装正常,应用发布也正常。
也可以再执行以下命令,获取更多的集群的信息:
1
kucectl get all
系统显示:
1 2 3 4 5 6 7 8 9 10 11 12
NAME READY STATUS RESTARTS AGE pod/nginx-86c57db685-rrh7w 1/1 Running 0 4m57s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 47m service/nginx NodePort 10.43.150.48 <none> 80:30424/TCP 2m18s
NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/nginx 1/1 1 1 4m57s
NAME DESIRED CURRENT READY AGE replicaset.apps/nginx-86c57db685 1 1 1 4m57s