堪稱最優(yōu)秀的Docker可視化管理工具——Portainer你真的會用嗎?
共 16277字,需瀏覽 33分鐘
·
2024-04-16 00:00
閱讀本文大概需要 10 分鐘。
來自:blog.csdn.net/shark_chili3007/article/details/123366179
前言
安裝Portainer
編寫docker-compose文件
version: "3"
services:
portainer:
image: portainer/portainer:latest
container_name: portainer
ports:
- "9000:9000"
volumes:
- /app/portainer/data:/data
- /var/run/docker.sock:/var/run/docker.sock
初始化并配置容器
docker-compose -f portainer.yml up
“ https://www.portainer.io/take-5
docker-compose -f portainer.yml up -d
實(shí)踐-基于Portainer安裝redis-sentinel部署
介紹
-
當(dāng)主節(jié)點(diǎn)更新數(shù)據(jù)之后,從節(jié)點(diǎn)數(shù)據(jù)也會進(jìn)行同步。 -
當(dāng)我們將主節(jié)點(diǎn)手動停止之后,哨兵就會選舉出新的master繼續(xù)進(jìn)行工作。
安裝步驟
主從復(fù)制部署
version: '3'
services:
# 主節(jié)點(diǎn)
master:
image: redis
# 主節(jié)點(diǎn)名稱
container_name: redis-master
# 設(shè)置redis登錄密碼、從節(jié)點(diǎn)連接主節(jié)點(diǎn)的密碼
command: redis-server --requirepass xxxx--masterauth xxxx
ports:
# 對外暴露端口號為16379
- 16379:6379
# 從節(jié)點(diǎn)
slave1:
image: redis
container_name: redis-slave-1
ports:
# 對外暴露端口號為16380
- 16380:6379
# 啟動redis 從屬于容器名為 redis-master的redis,端口號為容器端口號而不是對外映射端口號,設(shè)置連接密碼,連接主節(jié)點(diǎn)的密碼
command: redis-server --slaveof redis-master 6379 --requirepass xxxx--masterauth xxxxx
# 從節(jié)點(diǎn)2
slave2:
image: redis
container_name: redis-slave-2
ports:
- 16381:6379
command: redis-server --slaveof redis-master 6379 --requirepass xxxxx --masterauth xxxxx
docker-compose -f redis-cluster.yml up
docker exec -it容器id bash/sh命令,有了portainer之后,我們進(jìn)入容器的操作就變得非常簡單了。
創(chuàng)建redis-sentinel專用網(wǎng)絡(luò)驅(qū)動
-
host: 使用docker宿主機(jī)網(wǎng)絡(luò) -
bridge: 該網(wǎng)絡(luò)支持在同一個宿主機(jī)上的各個容器實(shí)例之間的通信。bridge網(wǎng)絡(luò)是一個獨(dú)立的網(wǎng)絡(luò)空間,在網(wǎng)絡(luò)空間內(nèi)部的各個容器實(shí)例能夠直接通信。 -
none: 是一個完全隔離的自治網(wǎng)絡(luò),甚至與Docker宿主機(jī)的網(wǎng)絡(luò)都不通,必須手工配置網(wǎng)卡后才能夠使用。加入到該網(wǎng)絡(luò)的容器實(shí)例,往往要在后續(xù)設(shè)置中加入到其他的第三方網(wǎng)絡(luò)。 -
overlay: 該類型的網(wǎng)絡(luò)適用于Docker宿主機(jī)集群中的各個獨(dú)立的容器實(shí)例之間通信。 -
macvlan: 該類型的網(wǎng)絡(luò)適用于容器實(shí)例需要與宿主機(jī)的MAC地址直接通信,無需端口映射,也無需NAT,容器實(shí)例的eth0直接與宿主機(jī)的物理網(wǎng)卡通信。容器實(shí)例可以被賦予公共IP,并從宿主機(jī)外部直接訪問。
創(chuàng)建哨兵
version: '3'
services:
sentinel1:
image: redis
# 容器名稱
container_name: redis-sentinel-1
ports:
# 端口映射
- 26379:26379
# 啟動redis哨兵
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
# 哨兵1的sentinel.conf和宿主文件位置映射
- /app/cloud/redis/sentinel/sentinel1.conf:/usr/local/etc/redis/sentinel.conf
sentinel2:
image: redis
container_name: redis-sentinel-2
ports:
- 26380:26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- /app/cloud/redis/sentinel/sentinel2.conf:/usr/local/etc/redis/sentinel.conf
sentinel3:
image: redis
container_name: redis-sentinel-3
ports:
- 26381:26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- /app/cloud/redis/sentinel/sentinel3.conf:/usr/local/etc/redis/sentinel.conf
# 重點(diǎn),將3個哨兵加入到redis-sentinel和主從節(jié)點(diǎn)建立聯(lián)系
networks:
default:
external:
name: redis-sentinel
app/cloud/redis/sentinel/文件夾下創(chuàng)建3個哨兵的配置文件sentinel.conf。
port 26379
dir /tmp
# master節(jié)點(diǎn)在bridge網(wǎng)絡(luò)中的ip值
sentinel monitor redis-master 172.20.0.2 6379 2
# master節(jié)點(diǎn)密碼
sentinel auth-pass redis-master xxxxx
sentinel down-after-milliseconds redis-master 30000
sentinel parallel-syncs redis-master 1
sentinel failover-timeout redis-master 180000
sentinel deny-scripts-reconfig yes
docker-compose -f redis-sentinel.yml up
測試可用性
Spring Boot集成Redis Sentinel
修改哨兵配置文件
port 26379
dir /tmp
# master節(jié)點(diǎn)ip
sentinel monitor redis-master 外網(wǎng)ip 16379 2
# master節(jié)點(diǎn)密碼
sentinel auth-pass redis-master 密碼
sentinel down-after-milliseconds redis-master 30000
sentinel parallel-syncs redis-master 1
sentinel failover-timeout redis-master 180000
sentinel deny-scripts-reconfig yes
Spring Boot應(yīng)用集成依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- lettuce pool 緩存連接池 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.5.0</version>
</dependency>
注冊中心配置redis參數(shù)
@Bean
@ConditionalOnMissingBean(RedisConnectionFactory.class)
public LettuceConnectionFactory redisConnectionFactory(ClientResources clientResources)
throws UnknownHostException {
....略
//創(chuàng)建連接工廠
return createLettuceConnectionFactory(clientConfig);
}
private LettuceConnectionFactory createLettuceConnectionFactory(LettuceClientConfiguration clientConfiguration) {
//有sentinel則創(chuàng)建sentinel配置工廠
if (getSentinelConfig() != null) {
return new LettuceConnectionFactory(getSentinelConfig(), clientConfiguration);
}
//有cluster配置則取cluster創(chuàng)建cluster連接工廠
if (getClusterConfiguration() != null) {
return new LettuceConnectionFactory(getClusterConfiguration(), clientConfiguration);
}
//默認(rèn)配置
return new LettuceConnectionFactory(getStandaloneConfig(), clientConfiguration);
}
private static Map<String, Object> asMap(String master, Set<String> sentinelHostAndPorts) {
........略
Map<String, Object> map = new HashMap<>();
//spring.redis.sentinel.master 決定master的值
map.put(REDIS_SENTINEL_MASTER_CONFIG_PROPERTY, master);
........略
return map;
}
spring:
redis:
# master有密碼則需要配置master認(rèn)證面膜
password: xxxxx
sentinel:
master: redis-master
# 多個節(jié)點(diǎn)用,分開,例如節(jié)點(diǎn)1ip:26379,節(jié)點(diǎn)2ip:26379
nodes: 節(jié)點(diǎn)ip:26379
password: xxxx
# lettuce連接池配置信息
lettuce:
pool:
max-idle: 10
max-active: 20
min-idle: 5
max-wait: 10000ms
編碼測試
@Autowired
private RedisTemplate redisTemplate;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class RedisStrDto {
String key;
String value;
}
@PostMapping("setKey")
public ResultData<String> setKey(@RequestBody RedisStrDto redisStrDto) {
redisTemplate.opsForValue().set(redisStrDto.getKey(),redisStrDto.getValue());
return ResultData.success("success");
}
@GetMapping("getKey/{key}")
public ResultData<Object> getKey(@PathVariable(value = "key") String key) {
return ResultData.success(redisTemplate.opsForValue().get(key));
}
推薦閱讀:
野心藏不住了!不滿CPU統(tǒng)治,英偉達(dá)決定徹底重寫軟件開發(fā)棧!黃仁勛:為什么還要用Python?命令行都不需要!GPU開發(fā)時代將至
互聯(lián)網(wǎng)初中高級大廠面試題(9個G) 內(nèi)容包含Java基礎(chǔ)、JavaWeb、MySQL性能優(yōu)化、JVM、鎖、百萬并發(fā)、消息隊(duì)列、高性能緩存、反射、Spring全家桶原理、微服務(wù)、Zookeeper......等技術(shù)棧!
?戳閱讀原文領(lǐng)??! 朕已閱
