1. docker配置
參考鏈接https://yq.aliyun.com/articles/110806
安裝命令
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
阿里雲鏡像加速:登錄阿里雲,選擇“容器鏡像服務”->“鏡像中心”->"鏡像加速器“
2. k3s配置
安裝:
curl -sfL https://get.k3s.io | sh -
配置kubectl授權文件
cd - && mkdir .kube
cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
3. 下載kubectl
安裝k3s過程中,如果系統中沒有kubectl,則會自動安裝一個
4. 拉取k8s.gcr.io/pause:3.1 鏡像
(只能使用VPN,k3s不支持指定鏡像的方式,只能去k8s.gcr.io拉取。第一次拉取成功後,以後將不再拉取)
5. 製作配置文件
以postgresql爲例,創建一個yaml文件,名爲postgresql.yaml,其中yourpassword
, yourusername
, yourdbname
請替換成自己需要的
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: postgresql
spec:
serviceName: postgresql
replicas: 1
selector:
matchLabels:
app: postgresql
template:
metadata:
labels:
app: postgresql
spec:
volumes:
- name: hostpath
hostPath:
path: /home/data/pgdata
containers:
- image: postgres:11-alpine
imagePullPolicy: Always
name: postgres11
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: hostpath
env:
- name: POSTGRES_PASSWORD
value: yourpassword
- name: POSTGRES_USER
value: yourusername
- name: POSTGRES_DB
value: yourdbname
---
apiVersion: v1
kind: Service
metadata:
name: postgresql
spec:
type: NodePort
ports:
- port: 5432
selector:
app: postgresql
6. 啓動應用
在postgresql.yaml目錄下,執行
[root@localhost ~]# kubectl apply -f postgresql.yaml
檢查postgres啓動情況
[root@localhost ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
postgresql-0 1/1 Running 0 14m