k8s实战--redis主从--guestbook
作者:互联网
第一章
实验:通过服务自动发现的redis 主从
难点:
1,服务的自动发现,即如何确定coreDNS 已生效
2,redis的主从验证
遇到的问题:
1,Can't handle RDB format version 9
解决:一般是低版本无法兼容高版本的 rdb 导致的。要求删除 dump.rdb文件,再启动 redis-server。 但是pod 中命令不足,所以自己新建镜像使用。
2,使用k8s 起的pod 和docker 起的容器,在容器内部 /etc/resolve.cnf内的ns 不同,docker 起的容器和系统保持一致,但是K8S 起的pod 使用的是CoreDNS 的ns
(即pod 内与外网隔绝,使用ip 也不行,这句可能不对,未查资料,仅现有环境测试)
1,验证k8s CoreDNS搭建正常
## 起nginx 容器和服务测试
cat tt-nginx.yml
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
selector:
app: nginx
##起具有curl 和nslookup 工具的镜像测试
cat curl-utils.yml
apiVersion: v1
kind: Pod
metadata:
name: curl-util
spec:
containers:
- name: curl-util
image: radial/busyboxplus:curl
command:
- sh
- -c
- while true ; do sleep 1 ;done
## 进入curl 工具容器测试
nslookup nginx
curl nginx:80
2,redis 简单主从
## 根据redis 镜像制作redis-slave
cat run.sh
#!/bin/bash
if [[ ${GET_HOSTS_FROM:-dns} == "env" ]]; then
redis-server --slaveof ${REDIS_MASTER_SERVICE_HOST} 6379
else
redis-server --slaveof redis-master 6379
fi
cat Dockerfile
FROM redis
COPY run.sh /
RUN chmod +x /run.sh
ENTRYPOINT ["/run.sh"]
docker build -t redis-slave-tt .
---------------------------------------------------------------
## redis-master 的RC 和service
cat redis-master-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: redis-master
labels:
name: redis-master
spec:
replicas: 1
selector:
name: redis-master
template:
metadata:
labels:
name: redis-master
spec:
containers:
- name: master
image: redis
ports:
- containerPort: 6379
cat redis-master-svc.yml
apiVersion: v1
kind: Service
metadata:
name: redis-master
labels:
name: redis-master
spec:
ports:
- port: 6379
targetPort: 6379
selector:
name: redis-master
----------------------------------------------------
## redis-slave 的RC
cat redis-slave-controller.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: redis-slave
labels:
name: redis-slave
spec:
replicas: 2
selector:
name: redis-slave
template:
metadata:
labels:
name: redis-slave
spec:
containers:
- name: slave
image: redis-slave-tt
imagePullPolicy: Never
env:
- name: GET_HOSTS_FROM
value: dns
# If your cluster config does not include a dns service, then to
# instead access an environment variable to find the master
# service's host, comment out the 'value: dns' line above, and
# uncomment the line below.
#value: env
ports:
- containerPort: 6379
##
注意:因为K8S 使用镜像是本地的,所以必须添加 imagePullPolicy: Never 属性
环境变量使用env 也可以
镜像拉取策略:
containers:
- name: uses-private-image
image: $PRIVATE_IMAGE_NAME
imagePullPolicy: Always
command: [ "echo", "SUCCESS" ]
Always 总是拉取镜像
IfNotPresent 本地有则使用本地镜像,不拉取
Never 只使用本地镜像,从不拉取,即使本地没有
如果省略imagePullPolicy 镜像tag为 :latest 策略为always ,否则 策略为 IfNotPresent
一些命令
kubectl cluster-info
kubectl -s https://172.20.7.132:6443 get componentstatuses
kubectl -s https://172.20.7.132:6443 get nodes
标签:slave,name,redis,nginx,master,k8s,guestbook,metadata 来源: https://www.cnblogs.com/g2thend/p/11622985.html