Skip to content

搭载blog之安装k3s(一)

要求:单点安装搭载k3s,并用k3s安装nginx

1.安装k3s

介绍:轻量级的 Kubernetes 发行版,内核机制还是和 k8s 一样,安装简单,占用资源少,只需要512M内存就可以运行

16697104591828

  • 补全命令

    $ ku

如果失败了,需要安装bash-completion项目

$ apt-get update && apt-get install -y bash-completion

再次执行source <(kubectl completion bash) 尝试成功

# kubectl + tab键,如果没用可以多按几次tab
root@JamesWuService:~# kubectl 
alpha          auth           cordon         diff           get            patch          run            version
annotate       autoscale      cp             drain          help           plugin         scale          wait
api-resources  certificate    create         edit           kustomize      port-forward   set            
api-versions   cluster-info   debug          exec           label          proxy          taint          
apply          completion     delete         explain        logs           replace        top            
attach         config         describe       expose         options        rollout        uncordon     
  • 设置别名 查看/usr/local/bin 命令

16697756756765

可以让永久生效,比如将

1.打开文件
$ vi .bashrc
2.编辑添加
alias kaf='kubectl apply -f'
alias kgn='kubectl get namespace'
alias kgp='kubectl get pods -o wide'
alias kgs='kubectl get -o wide svc'

3.保存
$ source .bashrc
2.安装nginx

安装nginx,并通过外网ip和域名访问

  • 内网部署nginx 创建namespace,deployment和service

    #创建namesspace apiVersion: v1 kind: Namespace
    metadata: name: ns-test
    labels: name: label-test


    #创建pod/deployment apiVersion: apps/v1 kind: Deployment metadata: namespace: ns-test name: nginx-deployment spec: selector: matchLabels: app: nginx replicas: 2 template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:alpine ports: - containerPort: 80


    #创建service apiVersion: v1 kind: Service metadata: namespace: ns-test name: nginx-service spec: selector: app: nginx ports:

    • protocol: TCP port: 80 targetPort: 80

查看部署情况

$ kgp -n ns-test
NAME                               READY   STATUS    RESTARTS   AGE   IP           NODE             NOMINATED NODE   READINESS GATES
nginx-deployment-965685897-7zb2s   1/1     Running   0          51m   10.42.0.9    jameswuservice   <none>           <none>
nginx-deployment-965685897-cm4sd   1/1     Running   0          51m   10.42.0.10   jameswuservice   <none>           <none>

curl 10.42.0.10 或者 curl 10.42.0.9 根据ip 可访问 curl ip 可看到hello nginx html

  • 设置node端口,外网访问 修改service

    apiVersion: v1 kind: Service metadata: namespace: ns-test name: nginx-service spec: selector: app: nginx type: NodePort ports:

    • protocol: TCP port: 80 targetPort: 80 nodePort: 32000

执行更新

$ kaf nginx-yaml.yaml

通过外网ip访问 http://:32000

  • 配置域名访问 创建nginx-ingress.yaml

    创建 nginx-ingress.yaml apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: my-nginx namespace: ns-test spec: rules:

    • host: "jamesyt.com" http: paths:
      • pathType: Prefix path: "/" backend: service: name: nginx-service #和上面的service 名字必须对应 port: number: 80
    • host: "www.jamesyt.com" http: paths:
      • pathType: Prefix path: "/" backend: service: name: nginx-service #和上面的service 名字必须对应 port: number: 80

访问域名:http://jamesyt.com/

16697884293096

3.卸载k3s

在卸载,namespace 出现Terminating 状态

原因是有service报错
kubectl get apiservice
删除掉false的service
kubectl delete apiservice xxx
或者进入namespace 进行编写
kubectl edit ns xxx

卸载k3s

$ /usr/local/bin/k3s-uninstall.sh
4.使用阿里安装k3s

解决方法 编辑/etc/profile

vim /etc/profile 追加内容:

export KUBECONFIG=/etc/rancher/k3s/k3s.yaml 刷新配置

source /etc/profile

  • 常用的别名 直接编写

    vim ~/.bash_aliases

alias k='kubectl'
alias kg='kubectl get'
alias kgp='kubectl get pods'
alias kaf='kubectl apply -f'
alias kd='kubectl delete'
alias kdf='kubectl delete -f'
alias kdes='kubectl describe'