常规步骤:安装vim openssh,生成密钥
sudo apt-get update
sudo apt-get install vim openssh-server
cd .ssh
ssh-keygen -t rsa -C "sirius1y@outlook.com"
cat id_rsa.pub > authorized_keys
ssh localhost
exit
rm known_hosts
安装docker
设置 Docker 的apt
存储库
官网教程:https://docs.docker.com/desktop/install/ubuntu/
# Add Docker's official GPG key:
sudo apt-get update
# 安装证书、下载工具、证书验证工具
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
# 证书对所有人都可读
sudo chmod a+r /etc/apt/keyrings/docker.gpg
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
安装 Docker 软件包
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
通过运行镜像来验证Docker Engine安装是否成功
sudo docker run hello-world
安装Kubernetes
换源
apt-get update && apt-get install -y apt-transport-https
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -
sudo vim /etc/apt/sources.list.d/kubernetes.list
###
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
###
sudo apt-get update
# 验证
apt-cache madison kubectl
apt-cache madison kubeadm
开始安装
sudo apt-get install kubelet=1.18.0-00 kubeadm=1.18.0-00 kubectl=1.18.0-00
验证安装成功
kubeadm version
在腾讯云上将当前主机创建为镜像
从镜像中恢复后,检查docker, kubeadm为1.18的版本
sudo docker run hello-world
kubeadm version
更改hostname,hosts
添加内网地址
sudo vim /etc/hostname
sudo vim /etc/hosts
重启之后,可以实现免密ssh登录
关闭swap分区虚拟内存
sudo swapoff -a
kubenetes初始化
使用kubeadm初始化master node
参考指南:https://kubernetes.io/zh-cn/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/
这里的内网地址和外网地址都可以,但是后面的--service-cidr,--pod-network
是需要的,不然会出现flannel一直处于crash状态一直在重启,并且dnscore也会一直处于creating的状态。
sudo kubeadm init --apiserver-advertise-address=172.19.16.5 --image-repository=registry.aliyuncs.com/google_containers --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubernete slave加入
sudo kubeadm join 172.19.16.6:6443 --token 836yhd.5plsxh5r8j13xcjz \
--discovery-token-ca-cert-hash sha256:8dbfb4048b7e636a20184b8e24a55a27319597001d526468ee58531d2d0521c8
进行部署
# master
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl get pods --all-namespaces
kubectl get nodes
redis deployment
mkdir redis_example
cd redis_example/
vim redis-leader-deployment.yaml
# redis-leader-deployment.yaml
# SOURCE: https://cloud.google.com/kubernetes-engine/docs/tutorials/guestbook
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-leader
labels:
app: redis
role: leader
tier: backend
spec:
replicas: 1
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
role: leader
tier: backend
spec:
containers:
- name: leader
image: "docker.io/redis:6.0.5"
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 6379
kubectl apply -f redis-leader-deployment.yaml
redis service
vim redis-leader-service.yaml
# redis-leader-service.yaml
# SOURCE: https://cloud.google.com/kubernetes-engine/docs/tutorials/guestbook
apiVersion: v1
kind: Service
metadata:
name: redis-leader
labels:
app: redis
role: leader
tier: backend
spec:
ports:
- port: 6379
targetPort: 6379
selector:
app: redis
role: leader
tier: backend
kubectl apply -f redis-leader-service.yaml
redis deployment
vim redis-follower-deployment.yaml
# SOURCE: https://cloud.google.com/kubernetes-engine/docs/tutorials/guestbook
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-follower
labels:
app: redis
role: follower
tier: backend
spec:
replicas: 2
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
role: follower
tier: backend
spec:
containers:
- name: follower
image: us-docker.pkg.dev/google-samples/containers/gke/gb-redis-follower:v2
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 6379
kubectl apply -f redis-follower-deployment.yaml
redis service
vim redis-follower-service.yaml
# SOURCE: https://cloud.google.com/kubernetes-engine/docs/tutorials/guestbook
apiVersion: v1
kind: Service
metadata:
name: redis-follower
labels:
app: redis
role: follower
tier: backend
spec:
ports:
# the port that this service should serve on
- port: 6379
selector:
app: redis
role: follower
tier: backend
kubectl apply -f redis-follower-service.yaml
前端deployment
vim frontend-deployment.yaml
# SOURCE: https://cloud.google.com/kubernetes-engine/docs/tutorials/guestbook
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend
spec:
replicas: 3
selector:
matchLabels:
app: guestbook
tier: frontend
template:
metadata:
labels:
app: guestbook
tier: frontend
spec:
containers:
- name: php-redis
image: us-docker.pkg.dev/google-samples/containers/gke/gb-frontend:v5
env:
- name: GET_HOSTS_FROM
value: "dns"
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 80
kubectl apply -f frontend-deployment.yaml
前端service
vim frontend-service.yaml
# SOURCE: https://cloud.google.com/kubernetes-engine/docs/tutorials/guestbook
apiVersion: v1
kind: Service
metadata:
name: frontend
labels:
app: guestbook
tier: frontend
spec:
# if your cluster supports it, uncomment the following to automatically create
# an external load-balanced IP for the frontend service.
# type: LoadBalancer
type: NodePort
ports:
# the port that this service should serve on
- port: 80
selector:
app: guestbook
tier: frontend
kubectl apply -f frontend-service.yaml
访问前端
在云服务器的安全组里面放开frontend的端口,在浏览器中访问可以看到页面
删除其中的一个pod
在尝试删除一个front pod之后,可以看到kubernetes系统自动为我们新建了一个frontend的pod
kubectl delete pod frontend-769fbdbdcc-jdvfb
pod扩容
kubectl scale deployment frontend --replicas=5