Skip to content

k3s 速通

配置 ssh login

  • Windows
sh
type $env:USERPROFILE\.ssh\id_rsa.pub | ssh ubuntu@43.154.121.203 "cat >>.ssh/authorized_keys"
  • MacOS / Linux
sh
ssh-copy-id -i ~/.ssh/id_rsa.pub ubuntu@43.154.121.203

login test

sh
 ssh ubuntu@43.154.121.203

install docker

shell
apt install docker.io

docker 镜像加速

https://gist.github.com/y0ngb1n/7e8f16af3242c7815e7ca2f0833d3ea6

install docker-compose

  • MacOS / Linux
shell
sudo curl -L "https://github.com/docker/compose/releases/download/v2.32.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose

install k3s

shell
curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -

install helm

shell
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash

fix: Helm CMD Error: INSTALLATION FAILED: Kubernetes cluster unreachable: Get "http://localhost:8080/version": dial tcp 127.0.0.1:8080: connect: connection refused

临时解决

shell
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml

编辑/etc/profile

vim /etc/profile

shell
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml

source /etc/profile

添加 Helm 仓库并更新

  • 添加 Rancher Helm 仓库:
shell
helm repo add rancher-latest https://releases.rancher.com/server-charts/latest

helm repo update
  • create namespace
shell
kubectl create namespace cattle-system

kubectl describe pod rancher-55769c4668-5ldt2 -n cattle-system

  • install cert-manager 安装 cert-manager,Rancher 需要它来处理证书:
shell
kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.13.1/cert-manager.yaml

等待 cert-manager 部署完成

shell
kubectl get pods --namespace cert-manager

所有 Pod 都应处于 Running 状态。

  • install rancher
shell
helm install rancher rancher-latest/rancher \
  --namespace cattle-system \
  --set hostname=rancher.pursue.pub
  • uninstall rancher
shell
helm uninstall rancher -n cattle-system

MySQL Redis

添加仓库

shell
helm repo add bitnami https://charts.bitnami.com/bitnami

查看chart

shell
helm show chart bitnami/mysql

查看默认值

shell
helm show values bitnami/mysql

install MySQL

shell
root@VM-0-13-ubuntu:/home/ubuntu# helm install my-mysql --set-string auth.rootPassword="123456" --set primary.persistence.size=1Gi bitnami/mysql
NAME: my-mysql
LAST DEPLOYED: Mon Jan  6 15:33:58 2025
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: mysql
CHART VERSION: 12.2.1
APP VERSION: 8.4.3

Did you know there are enterprise versions of the Bitnami catalog? For enhanced secure software supply chain features, unlimited pulls from Docker, LTS support, or application customization, see Bitnami Premium or Tanzu Application Catalog. See https://www.arrow.com/globalecs/na/vendors/bitnami for more information.

** Please be patient while the chart is being deployed **

Tip:

  Watch the deployment status using the command: kubectl get pods -w --namespace default

Services:

  echo Primary: my-mysql.default.svc.cluster.local:3306

Execute the following to get the administrator credentials:

  echo Username: root
  MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default my-mysql -o jsonpath="{.data.mysql-root-password}" | base64 -d)

To connect to your database:

  1. Run a pod that you can use as a client:

      kubectl run my-mysql-client --rm --tty -i --restart='Never' --image  docker.io/bitnami/mysql:8.4.3-debian-12-r5 --namespace default --env MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD --command -- bash

  2. To connect to primary service (read/write):

      mysql -h my-mysql.default.svc.cluster.local -uroot -p"$MYSQL_ROOT_PASSWORD"

WARNING: There are "resources" sections in the chart not set. Using "resourcesPreset" is not recommended for production. For production installations, please set the following values according to your workload needs:
  - primary.resources
  - secondary.resources

test MySQL

进入 pod

shell
kubectl exec -it my-mysql-0 -- /bin/bash

test

shell
mysql -u root -p -h my-mysql.default.svc.cluster.local

Redis

shell
root@VM-0-13-ubuntu:/home/ubuntu# helm install redis bitnami/redis  --namespace default
NAME: redis
LAST DEPLOYED: Mon Jan  6 15:39:20 2025
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: redis
CHART VERSION: 20.6.1
APP VERSION: 7.4.1

Did you know there are enterprise versions of the Bitnami catalog? For enhanced secure software supply chain features, unlimited pulls from Docker, LTS support, or application customization, see Bitnami Premium or Tanzu Application Catalog. See https://www.arrow.com/globalecs/na/vendors/bitnami for more information.

** Please be patient while the chart is being deployed **

Redis® can be accessed on the following DNS names from within your cluster:

    redis-master.default.svc.cluster.local for read/write operations (port 6379)
    redis-replicas.default.svc.cluster.local for read-only operations (port 6379)

To get your password run:

    export REDIS_PASSWORD=$(kubectl get secret --namespace default redis -o jsonpath="{.data.redis-password}" | base64 -d)

To connect to your Redis® server:

1. Run a Redis® pod that you can use as a client:

   kubectl run --namespace default redis-client --restart='Never'  --env REDIS_PASSWORD=$REDIS_PASSWORD  --image docker.io/bitnami/redis:7.4.1-debian-12-r3 --command -- sleep infinity

   Use the following command to attach to the pod:

   kubectl exec --tty -i redis-client \
   --namespace default -- bash

2. Connect using the Redis® CLI:
   REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h redis-master
   REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h redis-replicas

To connect to your database from outside the cluster execute the following commands:

    kubectl port-forward --namespace default svc/redis-master 6379:6379 &
    REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h 127.0.0.1 -p 6379

WARNING: There are "resources" sections in the chart not set. Using "resourcesPreset" is not recommended for production. For production installations, please set the following values according to your workload needs:
- replica.resources
- master.resources

test redis

进入 pod

shell
kubectl exec -it redis-replicas-0 -- /bin/bash

主节点

shell
redis-replicas-0:/$ redis-cli -h redis-master.default.svc.cluster.local -p 6379
redis-master.default.svc.cluster.local:6379> auth am0yPlpJJM
OK
redis-master.default.svc.cluster.local:6379> set aa 123
OK
redis-master.default.svc.cluster.local:6379> get aa
"123"

从节点

shell
redis-replicas-0:/$ redis-cli -h redis-replicas.default.svc.cluster.local -p 6379
redis-replicas.default.svc.cluster.local:6379> auth am0yPlpJJM
OK
redis-replicas.default.svc.cluster.local:6379>  get aa
"123"
redis-replicas.default.svc.cluster.local:6379> set aaa 123
(error) READONLY You can't write against a read only replica.

文章来源于自己总结和网络转载,内容如有任何问题,请大佬斧正!联系我